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 (
69 char *, unsigned short *
83 #define HISTFRACTION 2
84 #define HISTCOUNTER unsigned short
85 #define HASHFRACTION 1
88 #define BASEADDRESS 0x8000000 /* On Solaris 2 X86 all executables start here
98 unsigned long raw_frompc
;
99 unsigned long raw_selfpc
;
102 #define ROUNDDOWN(x,y) (((x)/(y))*(y))
103 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
107 typedef __SIZE_TYPE__
size_t;
108 typedef __PTRDIFF_TYPE__
intptr_t;
111 * froms is actually a bunch of unsigned shorts indexing tos
113 static int profiling
= 3;
114 static unsigned short *froms
;
115 static struct tostruct
*tos
= 0;
116 static long tolimit
= 0;
117 static char *s_lowpc
= 0;
118 static char *s_highpc
= 0;
119 static size_t s_textsize
= 0;
124 /* see profil(2) where this is describe (incorrectly) */
125 #define SCALE_1_TO_1 0x10000L
127 #define MSG "No space for profiling buffer(s)\n"
131 extern void *sbrk (intptr_t);
134 monstartup(char *lowpc
, char *highpc
)
141 * round lowpc and highpc to multiples of the density we're using
142 * so the rest of the scaling (here and in gprof) stays in ints.
145 ROUNDDOWN((size_t)lowpc
, HISTFRACTION
*sizeof(HISTCOUNTER
));
148 ROUNDUP((size_t)highpc
, HISTFRACTION
*sizeof(HISTCOUNTER
));
150 s_textsize
= highpc
- lowpc
;
151 monsize
= (s_textsize
/ HISTFRACTION
) + sizeof(struct phdr
);
152 buffer
= (char *) sbrk( monsize
);
153 if ( buffer
== (char *) -1 ) {
154 write( 2 , MSG
, sizeof(MSG
) );
157 froms
= (unsigned short *) sbrk( s_textsize
/ HASHFRACTION
);
158 if ( froms
== (unsigned short *) -1 ) {
159 write( 2 , MSG
, sizeof(MSG
) );
163 tolimit
= s_textsize
* ARCDENSITY
/ 100;
164 if ( tolimit
< MINARCS
) {
166 } else if ( tolimit
> 65534 ) {
169 tos
= (struct tostruct
*) sbrk( tolimit
* sizeof( struct tostruct
) );
170 if ( tos
== (struct tostruct
*) -1 ) {
171 write( 2 , MSG
, sizeof(MSG
) );
176 /* minbrk = (char *) sbrk(0);*/
180 ( (struct phdr
*) buffer
) -> lpc
= lowpc
;
181 ( (struct phdr
*) buffer
) -> hpc
= highpc
;
182 ( (struct phdr
*) buffer
) -> ncnt
= ssiz
;
183 monsize
-= sizeof(struct phdr
);
189 s_scale
= ( (float) monsize
/ o
) * SCALE_1_TO_1
;
190 #else /* avoid floating point */
192 int quot
= o
/ monsize
;
196 else if (quot
>= 0x100)
197 s_scale
= 0x10000 / quot
;
198 else if (o
>= 0x800000)
199 s_scale
= 0x1000000 / (o
/ (monsize
>> 8));
201 s_scale
= 0x1000000 / ((o
<< 8) / monsize
);
205 s_scale
= SCALE_1_TO_1
;
217 struct rawarc rawarc
;
220 fd
= creat( "gmon.out" , 0666 );
222 perror( "mcount: gmon.out" );
226 fprintf( stderr
, "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf
, ssiz
);
229 write( fd
, sbuf
, ssiz
);
230 endfrom
= s_textsize
/ (HASHFRACTION
* sizeof(*froms
));
231 for ( fromindex
= 0 ; fromindex
< endfrom
; fromindex
++ ) {
232 if ( froms
[fromindex
] == 0 ) {
235 frompc
= s_lowpc
+ (fromindex
* HASHFRACTION
* sizeof(*froms
));
236 for (toindex
=froms
[fromindex
]; toindex
!=0; toindex
=tos
[toindex
].link
) {
239 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
240 frompc
, tos
[toindex
].selfpc
, tos
[toindex
].count
);
242 rawarc
.raw_frompc
= (unsigned long) frompc
;
243 rawarc
.raw_selfpc
= (unsigned long) tos
[toindex
].selfpc
;
244 rawarc
.raw_count
= tos
[toindex
].count
;
245 write( fd
, &rawarc
, sizeof rawarc
);
252 /* See GLIBC for additional information about this technique. */
253 asm(".globl _mcount\n"
254 "\t.type\t_mcount, @function\n"
256 /* The compiler calls _mcount after the prologue, and does not
257 save any of the registers. Therefore we must preserve all
258 seven registers which may contain function arguments. */
259 "\tsubq\t$0x38,%rsp\n"
260 "\tmovq\t%rax,(%rsp)\n"
261 "\tmovq\t%rcx,0x08(%rsp)\n"
262 "\tmovq\t%rdx,0x10(%rsp)\n"
263 "\tmovq\t%rsi,0x18(%rsp)\n"
264 "\tmovq\t%rdi,0x20(%rsp)\n"
265 "\tmovq\t%r8,0x28(%rsp)\n"
266 "\tmovq\t%r9,0x30(%rsp)\n"
267 /* Get SELFPC (pushed by the call to this function) and
268 FROMPCINDEX (via the frame pointer. */
269 "\tmovq\t0x38(%rsp),%rdi\n"
270 "\tmovq\t0x8(%rbp),%rsi\n"
271 "\tcallq\tinternal_mcount\n"
272 /* Restore the saved registers. */
273 "\tmovq\t0x30(%rsp),%r9\n"
274 "\tmovq\t0x28(%rsp),%r8\n"
275 "\tmovq\t0x20(%rsp),%rdi\n"
276 "\tmovq\t0x18(%rsp),%rsi\n"
277 "\tmovq\t0x10(%rsp),%rdx\n"
278 "\tmovq\t0x08(%rsp),%rdx\n"
279 "\tmovq\t(%rsp),%rax\n"
280 "\taddq\t$0x38,%rsp\n"
284 /* Solaris 2 libraries use _mcount. */
285 asm(".globl _mcount; _mcount: jmp internal_mcount");
286 /* This is for compatibility with old versions of gcc which used mcount. */
287 asm(".globl mcount; mcount: jmp internal_mcount");
294 unsigned short *frompcindex
301 register char *selfpc
;
302 register unsigned short *frompcindex
;
304 register struct tostruct
*top
;
305 register struct tostruct
*prevtop
;
306 register long toindex
;
307 static char already_setup
;
311 * find the return address for mcount,
312 * and the return address for mcount's caller.
315 /* selfpc = pc pushed by mcount call.
316 This identifies the function that was just entered. */
317 selfpc
= (void *) __builtin_return_address (0);
318 /* frompcindex = pc in preceding frame.
319 This identifies the caller of the function just entered. */
320 frompcindex
= (void *) __builtin_return_address (1);
327 monstartup(0, etext
);
329 monstartup((char*)0x08040000, etext
);
332 on_exit(_mcleanup
, 0);
338 * check that we are profiling
339 * and that we aren't recursively invoked.
346 * check that frompcindex is a reasonable pc value.
347 * for example: signal catchers get called from the stack,
348 * not from text space. too bad.
350 frompcindex
= (unsigned short *)((long)frompcindex
- (long)s_lowpc
);
351 if ((unsigned long)frompcindex
> s_textsize
) {
355 &froms
[((long)frompcindex
) / (HASHFRACTION
* sizeof(*froms
))];
356 toindex
= *frompcindex
;
359 * first time traversing this arc
361 toindex
= ++tos
[0].link
;
362 if (toindex
>= tolimit
) {
365 *frompcindex
= toindex
;
367 top
->selfpc
= selfpc
;
373 if (top
->selfpc
== selfpc
) {
375 * arc at front of chain; usual case.
381 * have to go looking down chain for it.
382 * top points to what we are looking at,
383 * prevtop points to previous top.
384 * we know it is not at the head of the chain.
386 for (; /* goto done */; ) {
387 if (top
->link
== 0) {
389 * top is end of the chain and none of the chain
390 * had top->selfpc == selfpc.
391 * so we allocate a new tostruct
392 * and link it to the head of the chain.
394 toindex
= ++tos
[0].link
;
395 if (toindex
>= tolimit
) {
399 top
->selfpc
= selfpc
;
401 top
->link
= *frompcindex
;
402 *frompcindex
= toindex
;
406 * otherwise, check the next arc on the chain.
409 top
= &tos
[top
->link
];
410 if (top
->selfpc
== selfpc
) {
413 * increment its count
414 * move it to the head of the chain.
417 toindex
= prevtop
->link
;
418 prevtop
->link
= top
->link
;
419 top
->link
= *frompcindex
;
420 *frompcindex
= toindex
;
427 /* and fall through */
429 return; /* normal return restores saved registers */
432 profiling
++; /* halt further profiling */
433 # define TOLIMIT "mcount: tos overflow\n"
434 write(2, TOLIMIT
, sizeof(TOLIMIT
));
440 * profiling is what mcount checks to see if
441 * all the data structures are ready.
449 profil((unsigned short *)(sbuf
+ sizeof(struct phdr
)),
450 ssiz
- sizeof(struct phdr
),
451 (size_t)s_lowpc
, s_scale
);
456 profil((unsigned short *)0, 0, 0, 0);