]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/sparc/gmon-sol2.c
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
31 /* Mangled into a form that works on SPARC Solaris 2 by Mark Eichin
32 * for Cygnus Support, July 1992.
37 #include "coretypes.h"
41 #include "sparc/gmon.h"
48 #define HISTFRACTION 2
49 #define HISTCOUNTER unsigned short
50 #define HASHFRACTION 1
59 unsigned long raw_frompc
;
60 unsigned long raw_selfpc
;
63 #define ROUNDDOWN(x,y) (((x)/(y))*(y))
64 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
68 /* extern mcount() asm ("mcount"); */
69 /*extern*/ char *minbrk
/* asm ("minbrk") */;
72 * froms is actually a bunch of unsigned shorts indexing tos
74 static int profiling
= 3;
75 static unsigned short *froms
;
76 static struct tostruct
*tos
= 0;
77 static long tolimit
= 0;
78 static char *s_lowpc
= 0;
79 static char *s_highpc
= 0;
80 static unsigned long s_textsize
= 0;
85 /* see profil(2) where this is describe (incorrectly) */
86 #define SCALE_1_TO_1 0x10000L
88 #define MSG "No space for profiling buffer(s)\n"
90 static void moncontrol
PARAMS ((int));
91 extern void monstartup
PARAMS ((char *, char *));
92 extern void _mcleanup
PARAMS ((void));
94 void monstartup(lowpc
, highpc
)
103 * round lowpc and highpc to multiples of the density we're using
104 * so the rest of the scaling (here and in gprof) stays in ints.
107 ROUNDDOWN((unsigned long)lowpc
, HISTFRACTION
*sizeof(HISTCOUNTER
));
110 ROUNDUP((unsigned long)highpc
, HISTFRACTION
*sizeof(HISTCOUNTER
));
112 s_textsize
= highpc
- lowpc
;
113 monsize
= (s_textsize
/ HISTFRACTION
) + sizeof(struct phdr
);
114 buffer
= sbrk( monsize
);
115 if ( buffer
== (char *) -1 ) {
116 write( 2 , MSG
, sizeof(MSG
) );
119 froms
= (unsigned short *) sbrk( s_textsize
/ HASHFRACTION
);
120 if ( froms
== (unsigned short *) -1 ) {
121 write( 2 , MSG
, sizeof(MSG
) );
125 tolimit
= s_textsize
* ARCDENSITY
/ 100;
126 if ( tolimit
< MINARCS
) {
128 } else if ( tolimit
> 65534 ) {
131 tos
= (struct tostruct
*) sbrk( tolimit
* sizeof( struct tostruct
) );
132 if ( tos
== (struct tostruct
*) -1 ) {
133 write( 2 , MSG
, sizeof(MSG
) );
142 ( (struct phdr
*) buffer
) -> lpc
= lowpc
;
143 ( (struct phdr
*) buffer
) -> hpc
= highpc
;
144 ( (struct phdr
*) buffer
) -> ncnt
= ssiz
;
145 monsize
-= sizeof(struct phdr
);
151 s_scale
= ( (float) monsize
/ o
) * SCALE_1_TO_1
;
152 #else /* avoid floating point */
154 int quot
= o
/ monsize
;
158 else if (quot
>= 0x100)
159 s_scale
= 0x10000 / quot
;
160 else if (o
>= 0x800000)
161 s_scale
= 0x1000000 / (o
/ (monsize
>> 8));
163 s_scale
= 0x1000000 / ((o
<< 8) / monsize
);
167 s_scale
= SCALE_1_TO_1
;
179 struct rawarc rawarc
;
181 const char *proffile
;
184 extern char **___Argv
;
188 if ((profdir
= getenv("PROFDIR")) != NULL
) {
189 /* If PROFDIR contains a null value, no profiling output is produced */
190 if (*profdir
== '\0') {
194 progname
=strrchr(___Argv
[0], '/');
195 if (progname
== NULL
)
200 sprintf(buf
, "%s/%ld.%s", profdir
, (long) getpid(), progname
);
203 proffile
= "gmon.out";
206 fd
= creat( proffile
, 0666 );
212 fprintf( stderr
, "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf
, ssiz
);
214 write( fd
, sbuf
, ssiz
);
215 endfrom
= s_textsize
/ (HASHFRACTION
* sizeof(*froms
));
216 for ( fromindex
= 0 ; fromindex
< endfrom
; fromindex
++ ) {
217 if ( froms
[fromindex
] == 0 ) {
220 frompc
= s_lowpc
+ (fromindex
* HASHFRACTION
* sizeof(*froms
));
221 for (toindex
=froms
[fromindex
]; toindex
!=0; toindex
=tos
[toindex
].link
) {
224 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
225 frompc
, tos
[toindex
].selfpc
, tos
[toindex
].count
);
227 rawarc
.raw_frompc
= (unsigned long) frompc
;
228 rawarc
.raw_selfpc
= (unsigned long) tos
[toindex
].selfpc
;
229 rawarc
.raw_count
= tos
[toindex
].count
;
230 write( fd
, &rawarc
, sizeof rawarc
);
237 * The SPARC stack frame is only held together by the frame pointers
238 * in the register windows. According to the SVR4 SPARC ABI
239 * Supplement, Low Level System Information/Operating System
240 * Interface/Software Trap Types, a type 3 trap will flush all of the
241 * register windows to the stack, which will make it possible to walk
242 * the frames and find the return addresses.
243 * However, it seems awfully expensive to incur a trap (system
244 * call) for every function call. It turns out that "call" simply puts
245 * the return address in %o7 expecting the "save" in the procedure to
246 * shift it into %i7; this means that before the "save" occurs, %o7
247 * contains the address of the call to mcount, and %i7 still contains
248 * the caller above that. The asm mcount here simply saves those
249 * registers in argument registers and branches to internal_mcount,
250 * simulating a call with arguments.
252 * 1) the branch to internal_mcount is hard coded; it should be
253 * possible to tell asm to use the assembler-name of a symbol.
254 * 2) in theory, the function calling mcount could have saved %i7
255 * somewhere and reused the register; in practice, I *think* this will
256 * break longjmp (and maybe the debugger) but I'm not certain. (I take
257 * some comfort in the knowledge that it will break the native mcount
259 * 3) if builtin_return_address worked, this could be portable.
260 * However, it would really have to be optimized for arguments of 0
261 * and 1 and do something like what we have here in order to avoid the
262 * trap per function call performance hit.
263 * 4) the atexit and monsetup calls prevent this from simply
264 * being a leaf routine that doesn't do a "save" (and would thus have
265 * access to %o7 and %i7 directly) but the call to write() at the end
266 * would have also prevented this.
268 * -- [eichin:19920702.1107EST]
271 static void internal_mcount
PARAMS ((char *, unsigned short *)) ATTRIBUTE_UNUSED
;
273 /* i7 == last ret, -> frompcindex */
274 /* o7 == current ret, -> selfpc */
275 /* Solaris 2 libraries use _mcount. */
276 asm(".global _mcount; _mcount: mov %i7,%o1; mov %o7,%o0;b,a internal_mcount");
277 /* This is for compatibility with old versions of gcc which used mcount. */
278 asm(".global mcount; mcount: mov %i7,%o1; mov %o7,%o0;b,a internal_mcount");
280 static void internal_mcount(selfpc
, frompcindex
)
281 register char *selfpc
;
282 register unsigned short *frompcindex
;
284 register struct tostruct
*top
;
285 register struct tostruct
*prevtop
;
286 register long toindex
;
287 static char already_setup
;
290 * find the return address for mcount,
291 * and the return address for mcount's caller.
296 extern char _start
[];
299 monstartup(_start
< _init
? _start
: _init
, etext
);
301 on_exit(_mcleanup
, 0);
307 * check that we are profiling
308 * and that we aren't recursively invoked.
315 * check that frompcindex is a reasonable pc value.
316 * for example: signal catchers get called from the stack,
317 * not from text space. too bad.
319 frompcindex
= (unsigned short *)((long)frompcindex
- (long)s_lowpc
);
320 if ((unsigned long)frompcindex
> s_textsize
) {
324 &froms
[((long)frompcindex
) / (HASHFRACTION
* sizeof(*froms
))];
325 toindex
= *frompcindex
;
328 * first time traversing this arc
330 toindex
= ++tos
[0].link
;
331 if (toindex
>= tolimit
) {
334 *frompcindex
= toindex
;
336 top
->selfpc
= selfpc
;
342 if (top
->selfpc
== selfpc
) {
344 * arc at front of chain; usual case.
350 * have to go looking down chain for it.
351 * top points to what we are looking at,
352 * prevtop points to previous top.
353 * we know it is not at the head of the chain.
355 for (; /* goto done */; ) {
356 if (top
->link
== 0) {
358 * top is end of the chain and none of the chain
359 * had top->selfpc == selfpc.
360 * so we allocate a new tostruct
361 * and link it to the head of the chain.
363 toindex
= ++tos
[0].link
;
364 if (toindex
>= tolimit
) {
368 top
->selfpc
= selfpc
;
370 top
->link
= *frompcindex
;
371 *frompcindex
= toindex
;
375 * otherwise, check the next arc on the chain.
378 top
= &tos
[top
->link
];
379 if (top
->selfpc
== selfpc
) {
382 * increment its count
383 * move it to the head of the chain.
386 toindex
= prevtop
->link
;
387 prevtop
->link
= top
->link
;
388 top
->link
= *frompcindex
;
389 *frompcindex
= toindex
;
396 /* and fall through */
398 return; /* normal return restores saved registers */
401 profiling
++; /* halt further profiling */
402 # define TOLIMIT "mcount: tos overflow\n"
403 write(2, TOLIMIT
, sizeof(TOLIMIT
));
409 * profiling is what mcount checks to see if
410 * all the data structures are ready.
412 static void moncontrol(mode
)
417 profil((unsigned short *)(sbuf
+ sizeof(struct phdr
)),
418 ssiz
- sizeof(struct phdr
),
419 (long)s_lowpc
, s_scale
);
423 profil((unsigned short *)0, 0, 0, 0);