2 * Copyright (c) 1991 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 * This is a modified gmon.c by J.W.Hawtin <oolon@ankh.org>,
33 * 14/8/96 based on the original gmon.c in GCC and the hacked version
34 * solaris 2 sparc version (config/sparc/gmon-sol.c) by Mark Eichin. To do
35 * process profiling on solaris 2.X X86
37 * It must be used in conjunction with sol2-gc1.asm, which is used to start
38 * and stop process monitoring.
42 * On Solaris 2 _mcount is called by library functions not mcount, so support
43 * has been added for both.
45 * Also the prototype for profil() is different
47 * Solaris 2 does not seem to have char *minbrk whcih allows the setting of
48 * the minimum SBRK region so this code has been removed and lets pray malloc
49 * does not mess it up.
53 * This code could easily be integrated with the original gmon.c and perhaps
58 #include <fcntl.h> /* for creat() */
64 static void moncontrol (int);
65 extern void monstartup (char *, char *);
66 extern void _mcleanup (void);
67 extern void internal_mcount (void);
77 #define HISTFRACTION 2
78 #define HISTCOUNTER unsigned short
79 #define HASHFRACTION 1
82 #define BASEADDRESS 0x8000000 /* On Solaris 2 X86 all executables start here
92 unsigned long raw_frompc;
93 unsigned long raw_selfpc;
96 #define ROUNDDOWN(x,y) (((x)/(y))*(y))
97 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
102 * froms is actually a bunch of unsigned shorts indexing tos
104 static int profiling = 3;
105 static unsigned short *froms;
106 static struct tostruct *tos = 0;
107 static long tolimit = 0;
108 static char *s_lowpc = 0;
109 static char *s_highpc = 0;
110 static unsigned long s_textsize = 0;
115 /* see profil(2) where this is describe (incorrectly) */
116 #define SCALE_1_TO_1 0x10000L
118 #define MSG "No space for profiling buffer(s)\n"
123 monstartup(char *lowpc, char *highpc)
130 * round lowpc and highpc to multiples of the density we're using
131 * so the rest of the scaling (here and in gprof) stays in ints.
134 ROUNDDOWN((unsigned long)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
137 ROUNDUP((unsigned long)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
139 s_textsize = highpc - lowpc;
140 monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
141 buffer = (char *) sbrk( monsize );
142 if ( buffer == (char *) -1 ) {
143 write( 2 , MSG , sizeof(MSG) );
146 froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION );
147 if ( froms == (unsigned short *) -1 ) {
148 write( 2 , MSG , sizeof(MSG) );
152 tolimit = s_textsize * ARCDENSITY / 100;
153 if ( tolimit < MINARCS ) {
155 } else if ( tolimit > 65534 ) {
158 tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) );
159 if ( tos == (struct tostruct *) -1 ) {
160 write( 2 , MSG , sizeof(MSG) );
165 /* minbrk = (char *) sbrk(0);*/
169 ( (struct phdr *) buffer ) -> lpc = lowpc;
170 ( (struct phdr *) buffer ) -> hpc = highpc;
171 ( (struct phdr *) buffer ) -> ncnt = ssiz;
172 monsize -= sizeof(struct phdr);
178 s_scale = ( (float) monsize / o ) * SCALE_1_TO_1;
179 #else /* avoid floating point */
181 int quot = o / monsize;
185 else if (quot >= 0x100)
186 s_scale = 0x10000 / quot;
187 else if (o >= 0x800000)
188 s_scale = 0x1000000 / (o / (monsize >> 8));
190 s_scale = 0x1000000 / ((o << 8) / monsize);
194 s_scale = SCALE_1_TO_1;
206 struct rawarc rawarc;
209 fd = creat( "gmon.out" , 0666 );
211 perror( "mcount: gmon.out" );
215 fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
218 write( fd , sbuf , ssiz );
219 endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
220 for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
221 if ( froms[fromindex] == 0 ) {
224 frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
225 for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
228 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
229 frompc , tos[toindex].selfpc , tos[toindex].count );
231 rawarc.raw_frompc = (unsigned long) frompc;
232 rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
233 rawarc.raw_count = tos[toindex].count;
234 write( fd , &rawarc , sizeof rawarc );
240 /* Solaris 2 libraries use _mcount. */
241 asm(".globl _mcount; _mcount: jmp internal_mcount");
242 /* This is for compatibility with old versions of gcc which used mcount. */
243 asm(".globl mcount; mcount: jmp internal_mcount");
246 internal_mcount (void)
248 register char *selfpc;
249 register unsigned short *frompcindex;
250 register struct tostruct *top;
251 register struct tostruct *prevtop;
252 register long toindex;
253 static char already_setup;
256 * find the return address for mcount,
257 * and the return address for mcount's caller.
260 /* selfpc = pc pushed by mcount call.
261 This identifies the function that was just entered. */
262 selfpc = (void *) __builtin_return_address (0);
263 /* frompcindex = pc in preceding frame.
264 This identifies the caller of the function just entered. */
265 frompcindex = (void *) __builtin_return_address (1);
270 /* monstartup(0, etext); */
271 monstartup((char*)0x08040000, etext);
273 on_exit(_mcleanup, 0);
279 * check that we are profiling
280 * and that we aren't recursively invoked.
287 * check that frompcindex is a reasonable pc value.
288 * for example: signal catchers get called from the stack,
289 * not from text space. too bad.
291 frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
292 if ((unsigned long)frompcindex > s_textsize) {
296 &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
297 toindex = *frompcindex;
300 * first time traversing this arc
302 toindex = ++tos[0].link;
303 if (toindex >= tolimit) {
306 *frompcindex = toindex;
308 top->selfpc = selfpc;
314 if (top->selfpc == selfpc) {
316 * arc at front of chain; usual case.
322 * have to go looking down chain for it.
323 * top points to what we are looking at,
324 * prevtop points to previous top.
325 * we know it is not at the head of the chain.
327 for (; /* goto done */; ) {
328 if (top->link == 0) {
330 * top is end of the chain and none of the chain
331 * had top->selfpc == selfpc.
332 * so we allocate a new tostruct
333 * and link it to the head of the chain.
335 toindex = ++tos[0].link;
336 if (toindex >= tolimit) {
340 top->selfpc = selfpc;
342 top->link = *frompcindex;
343 *frompcindex = toindex;
347 * otherwise, check the next arc on the chain.
350 top = &tos[top->link];
351 if (top->selfpc == selfpc) {
354 * increment its count
355 * move it to the head of the chain.
358 toindex = prevtop->link;
359 prevtop->link = top->link;
360 top->link = *frompcindex;
361 *frompcindex = toindex;
368 /* and fall through */
370 return; /* normal return restores saved registers */
373 profiling++; /* halt further profiling */
374 # define TOLIMIT "mcount: tos overflow\n"
375 write(2, TOLIMIT, sizeof(TOLIMIT));
381 * profiling is what mcount checks to see if
382 * all the data structures are ready.
390 profil((unsigned short *)(sbuf + sizeof(struct phdr)),
391 ssiz - sizeof(struct phdr),
392 (long)s_lowpc, s_scale);
397 profil((unsigned short *)0, 0, 0, 0);