]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgcc/config/gmon-sol2.c
t-slibgcc-darwin: Move to ...
[thirdparty/gcc.git] / libgcc / config / gmon-sol2.c
CommitLineData
638e2294
RS
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.
a8196924 13 * 3. [rescinded 22 July 1999]
638e2294
RS
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.
17 *
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
28 * SUCH DAMAGE.
29 */
30
ca24c5ad 31/* FIXME: Check comment. */
56149abc 32/* Mangled into a form that works on SPARC Solaris 2 by Mark Eichin
638e2294 33 * for Cygnus Support, July 1992.
ca24c5ad
RO
34 *
35 * This is a modified gmon.c by J.W.Hawtin <oolon@ankh.org>,
36 * 14/8/96 based on the original gmon.c in GCC and the hacked version
37 * solaris 2 sparc version (config/sparc/gmon-sol.c) by Mark Eichin. To do
38 * process profiling on solaris 2.X X86
39 *
40 * It must be used in conjunction with sol2-gc1.asm, which is used to start
41 * and stop process monitoring.
42 *
43 * Differences.
44 *
45 * On Solaris 2 _mcount is called by library functions not mcount, so support
46 * has been added for both.
47 *
48 * Also the prototype for profil() is different
49 *
50 * Solaris 2 does not seem to have char *minbrk whcih allows the setting of
51 * the minimum SBRK region so this code has been removed and lets pray malloc
52 * does not mess it up.
53 *
54 * Notes
55 *
56 * This code could easily be integrated with the original gmon.c and perhaps
57 * should be.
638e2294 58 */
4977bab6
ZW
59#include "tconfig.h"
60#include "tsystem.h"
c7c50494 61#include <fcntl.h> /* for creat() */
638e2294 62
ca24c5ad
RO
63#ifdef DEBUG
64#include <stdio.h>
65#endif
66
67static void moncontrol (int);
68extern void monstartup (char *, char *);
69extern void _mcleanup (void);
70
638e2294
RS
71struct phdr {
72 char *lpc;
73 char *hpc;
74 int ncnt;
75};
ca24c5ad
RO
76
77
638e2294
RS
78#define HISTFRACTION 2
79#define HISTCOUNTER unsigned short
80#define HASHFRACTION 1
81#define ARCDENSITY 2
82#define MINARCS 50
ca24c5ad
RO
83#define BASEADDRESS 0x8000000 /* On Solaris 2 X86 all executables start here
84 and not at 0 */
85
638e2294
RS
86struct tostruct {
87 char *selfpc;
88 long count;
89 unsigned short link;
90};
ca24c5ad 91
638e2294
RS
92struct rawarc {
93 unsigned long raw_frompc;
94 unsigned long raw_selfpc;
95 long raw_count;
96};
97#define ROUNDDOWN(x,y) (((x)/(y))*(y))
98#define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
99
638e2294
RS
100/* extern mcount() asm ("mcount"); */
101/*extern*/ char *minbrk /* asm ("minbrk") */;
ca24c5ad
RO
102typedef __SIZE_TYPE__ size_t;
103typedef __PTRDIFF_TYPE__ intptr_t;
104
105extern int errno;
106
107extern void *sbrk (intptr_t);
638e2294
RS
108
109 /*
110 * froms is actually a bunch of unsigned shorts indexing tos
111 */
112static int profiling = 3;
113static unsigned short *froms;
114static struct tostruct *tos = 0;
115static long tolimit = 0;
116static char *s_lowpc = 0;
117static char *s_highpc = 0;
ca24c5ad 118static size_t s_textsize = 0;
638e2294
RS
119
120static int ssiz;
121static char *sbuf;
122static int s_scale;
123 /* see profil(2) where this is describe (incorrectly) */
124#define SCALE_1_TO_1 0x10000L
125
126#define MSG "No space for profiling buffer(s)\n"
127
f7835768
KG
128static void moncontrol (int);
129extern void monstartup (char *, char *);
130extern void _mcleanup (void);
9870475c 131
f7835768 132void monstartup(char *lowpc, char *highpc)
638e2294 133{
ca24c5ad 134 size_t monsize;
638e2294 135 char *buffer;
ca24c5ad 136 register size_t o;
638e2294
RS
137
138 /*
139 * round lowpc and highpc to multiples of the density we're using
140 * so the rest of the scaling (here and in gprof) stays in ints.
141 */
142 lowpc = (char *)
ca24c5ad 143 ROUNDDOWN((size_t)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
638e2294
RS
144 s_lowpc = lowpc;
145 highpc = (char *)
ca24c5ad 146 ROUNDUP((size_t)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
638e2294
RS
147 s_highpc = highpc;
148 s_textsize = highpc - lowpc;
149 monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
ca24c5ad 150 buffer = (char *) sbrk( monsize );
638e2294
RS
151 if ( buffer == (char *) -1 ) {
152 write( 2 , MSG , sizeof(MSG) );
153 return;
154 }
155 froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION );
156 if ( froms == (unsigned short *) -1 ) {
157 write( 2 , MSG , sizeof(MSG) );
158 froms = 0;
159 return;
160 }
161 tolimit = s_textsize * ARCDENSITY / 100;
162 if ( tolimit < MINARCS ) {
163 tolimit = MINARCS;
164 } else if ( tolimit > 65534 ) {
165 tolimit = 65534;
166 }
167 tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) );
168 if ( tos == (struct tostruct *) -1 ) {
169 write( 2 , MSG , sizeof(MSG) );
170 froms = 0;
171 tos = 0;
172 return;
173 }
174 minbrk = sbrk(0);
175 tos[0].link = 0;
176 sbuf = buffer;
177 ssiz = monsize;
178 ( (struct phdr *) buffer ) -> lpc = lowpc;
179 ( (struct phdr *) buffer ) -> hpc = highpc;
180 ( (struct phdr *) buffer ) -> ncnt = ssiz;
181 monsize -= sizeof(struct phdr);
182 if ( monsize <= 0 )
183 return;
184 o = highpc - lowpc;
185 if( monsize < o )
186#ifndef hp300
187 s_scale = ( (float) monsize / o ) * SCALE_1_TO_1;
188#else /* avoid floating point */
189 {
190 int quot = o / monsize;
191
192 if (quot >= 0x10000)
193 s_scale = 1;
194 else if (quot >= 0x100)
195 s_scale = 0x10000 / quot;
196 else if (o >= 0x800000)
197 s_scale = 0x1000000 / (o / (monsize >> 8));
198 else
199 s_scale = 0x1000000 / ((o << 8) / monsize);
200 }
201#endif
202 else
203 s_scale = SCALE_1_TO_1;
204 moncontrol(1);
205}
206
6156580d 207void
f7835768 208_mcleanup(void)
638e2294
RS
209{
210 int fd;
211 int fromindex;
212 int endfrom;
213 char *frompc;
214 int toindex;
215 struct rawarc rawarc;
6156580d 216 char *profdir;
3bb5de61 217 const char *proffile;
6156580d
JW
218 char *progname;
219 char buf[PATH_MAX];
220 extern char **___Argv;
638e2294
RS
221
222 moncontrol(0);
6156580d
JW
223
224 if ((profdir = getenv("PROFDIR")) != NULL) {
225 /* If PROFDIR contains a null value, no profiling output is produced */
226 if (*profdir == '\0') {
227 return;
228 }
229
230 progname=strrchr(___Argv[0], '/');
231 if (progname == NULL)
232 progname=___Argv[0];
233 else
234 progname++;
235
fd05eb80 236 sprintf(buf, "%s/%ld.%s", profdir, (long) getpid(), progname);
6156580d
JW
237 proffile = buf;
238 } else {
239 proffile = "gmon.out";
240 }
241
242 fd = creat( proffile, 0666 );
638e2294 243 if ( fd < 0 ) {
6156580d 244 perror( proffile );
638e2294
RS
245 return;
246 }
247# ifdef DEBUG
ca24c5ad 248 fprintf( stderr , "[mcleanup] sbuf %#x ssiz %d\n" , sbuf , ssiz );
ce1cc601 249# endif /* DEBUG */
ca24c5ad 250
638e2294
RS
251 write( fd , sbuf , ssiz );
252 endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
253 for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
254 if ( froms[fromindex] == 0 ) {
255 continue;
256 }
257 frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
258 for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
259# ifdef DEBUG
260 fprintf( stderr ,
ca24c5ad 261 "[mcleanup] frompc %#x selfpc %#x count %d\n" ,
638e2294 262 frompc , tos[toindex].selfpc , tos[toindex].count );
ce1cc601 263# endif /* DEBUG */
638e2294
RS
264 rawarc.raw_frompc = (unsigned long) frompc;
265 rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
266 rawarc.raw_count = tos[toindex].count;
267 write( fd , &rawarc , sizeof rawarc );
268 }
269 }
270 close( fd );
271}
272
ca24c5ad 273#ifdef __sparc__
638e2294 274/*
56149abc 275 * The SPARC stack frame is only held together by the frame pointers
638e2294
RS
276 * in the register windows. According to the SVR4 SPARC ABI
277 * Supplement, Low Level System Information/Operating System
278 * Interface/Software Trap Types, a type 3 trap will flush all of the
279 * register windows to the stack, which will make it possible to walk
280 * the frames and find the return addresses.
281 * However, it seems awfully expensive to incur a trap (system
282 * call) for every function call. It turns out that "call" simply puts
283 * the return address in %o7 expecting the "save" in the procedure to
284 * shift it into %i7; this means that before the "save" occurs, %o7
285 * contains the address of the call to mcount, and %i7 still contains
286 * the caller above that. The asm mcount here simply saves those
287 * registers in argument registers and branches to internal_mcount,
288 * simulating a call with arguments.
289 * Kludges:
290 * 1) the branch to internal_mcount is hard coded; it should be
291 * possible to tell asm to use the assembler-name of a symbol.
292 * 2) in theory, the function calling mcount could have saved %i7
293 * somewhere and reused the register; in practice, I *think* this will
294 * break longjmp (and maybe the debugger) but I'm not certain. (I take
295 * some comfort in the knowledge that it will break the native mcount
296 * as well.)
297 * 3) if builtin_return_address worked, this could be portable.
298 * However, it would really have to be optimized for arguments of 0
299 * and 1 and do something like what we have here in order to avoid the
300 * trap per function call performance hit.
301 * 4) the atexit and monsetup calls prevent this from simply
302 * being a leaf routine that doesn't do a "save" (and would thus have
303 * access to %o7 and %i7 directly) but the call to write() at the end
304 * would have also prevented this.
305 *
306 * -- [eichin:19920702.1107EST]
307 */
308
ad6cc85a 309static void internal_mcount (char *, unsigned short *) __attribute__ ((used));
3bb5de61 310
638e2294
RS
311/* i7 == last ret, -> frompcindex */
312/* o7 == current ret, -> selfpc */
e6381520
RK
313/* Solaris 2 libraries use _mcount. */
314asm(".global _mcount; _mcount: mov %i7,%o1; mov %o7,%o0;b,a internal_mcount");
315/* This is for compatibility with old versions of gcc which used mcount. */
638e2294 316asm(".global mcount; mcount: mov %i7,%o1; mov %o7,%o0;b,a internal_mcount");
ca24c5ad
RO
317#elif defined __x86_64__
318extern void internal_mcount (char *, unsigned short *);
638e2294 319
ca24c5ad
RO
320/* See GLIBC for additional information about this technique. */
321asm(".globl _mcount\n"
322 "\t.type\t_mcount, @function\n"
323 "_mcount:\n"
324 /* The compiler calls _mcount after the prologue, and does not
325 save any of the registers. Therefore we must preserve all
326 seven registers which may contain function arguments. */
327 "\tsubq\t$0x38,%rsp\n"
328 "\tmovq\t%rax,(%rsp)\n"
329 "\tmovq\t%rcx,0x08(%rsp)\n"
330 "\tmovq\t%rdx,0x10(%rsp)\n"
331 "\tmovq\t%rsi,0x18(%rsp)\n"
332 "\tmovq\t%rdi,0x20(%rsp)\n"
333 "\tmovq\t%r8,0x28(%rsp)\n"
334 "\tmovq\t%r9,0x30(%rsp)\n"
335 /* Get SELFPC (pushed by the call to this function) and
336 FROMPCINDEX (via the frame pointer. */
337 "\tmovq\t0x38(%rsp),%rdi\n"
338 "\tmovq\t0x8(%rbp),%rsi\n"
339 "\tcall\tinternal_mcount\n"
340 /* Restore the saved registers. */
341 "\tmovq\t0x30(%rsp),%r9\n"
342 "\tmovq\t0x28(%rsp),%r8\n"
343 "\tmovq\t0x20(%rsp),%rdi\n"
344 "\tmovq\t0x18(%rsp),%rsi\n"
345 "\tmovq\t0x10(%rsp),%rdx\n"
346 "\tmovq\t0x08(%rsp),%rcx\n"
347 "\tmovq\t(%rsp),%rax\n"
348 "\taddq\t$0x38,%rsp\n"
349 "\tretq\n"
350 );
351#else
352extern void internal_mcount (void);
353
354 /* Solaris 2 libraries use _mcount. */
355asm(".globl _mcount; _mcount: jmp internal_mcount");
356 /* This is for compatibility with old versions of gcc which used mcount. */
357asm(".globl mcount; mcount: jmp internal_mcount");
358#endif
359
360#ifdef __sparc__
361static
362#endif
363void
364internal_mcount (
365#if defined __sparc__ || defined __x86_64__
366 char *selfpc,
367 unsigned short *frompcindex
368#else
369 void
370#endif
371 )
638e2294 372{
ca24c5ad
RO
373#if !defined __sparc__ && !defined __x86_64__
374 register char *selfpc;
375 register unsigned short *frompcindex;
376#endif
638e2294
RS
377 register struct tostruct *top;
378 register struct tostruct *prevtop;
379 register long toindex;
380 static char already_setup;
381
ca24c5ad 382#if !defined __sparc__ && !defined __x86_64__
638e2294
RS
383 /*
384 * find the return address for mcount,
385 * and the return address for mcount's caller.
386 */
387
ca24c5ad
RO
388 /* selfpc = pc pushed by mcount call.
389 This identifies the function that was just entered. */
390 selfpc = (void *) __builtin_return_address (0);
391 /* frompcindex = pc in preceding frame.
392 This identifies the caller of the function just entered. */
393 frompcindex = (void *) __builtin_return_address (1);
394#endif
395
638e2294 396 if(!already_setup) {
3bb5de61 397 extern char etext[];
ca24c5ad 398#ifdef __sparc__
6dfaf9ba
RH
399 extern char _start[];
400 extern char _init[];
ca24c5ad 401#endif
638e2294 402 already_setup = 1;
ca24c5ad 403#if defined __sparc__
6dfaf9ba 404 monstartup(_start < _init ? _start : _init, etext);
ca24c5ad
RO
405#elif defined __x86_64__
406 monstartup(0, etext);
407#else
408 monstartup((char*)0x08040000, etext);
409#endif
638e2294
RS
410#ifdef USE_ONEXIT
411 on_exit(_mcleanup, 0);
412#else
413 atexit(_mcleanup);
414#endif
415 }
416 /*
417 * check that we are profiling
418 * and that we aren't recursively invoked.
419 */
420 if (profiling) {
421 goto out;
422 }
423 profiling++;
424 /*
425 * check that frompcindex is a reasonable pc value.
426 * for example: signal catchers get called from the stack,
427 * not from text space. too bad.
428 */
429 frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
430 if ((unsigned long)frompcindex > s_textsize) {
431 goto done;
432 }
433 frompcindex =
434 &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
435 toindex = *frompcindex;
436 if (toindex == 0) {
437 /*
438 * first time traversing this arc
439 */
440 toindex = ++tos[0].link;
441 if (toindex >= tolimit) {
442 goto overflow;
443 }
444 *frompcindex = toindex;
445 top = &tos[toindex];
446 top->selfpc = selfpc;
447 top->count = 1;
448 top->link = 0;
449 goto done;
450 }
451 top = &tos[toindex];
452 if (top->selfpc == selfpc) {
453 /*
454 * arc at front of chain; usual case.
455 */
456 top->count++;
457 goto done;
458 }
459 /*
460 * have to go looking down chain for it.
461 * top points to what we are looking at,
462 * prevtop points to previous top.
463 * we know it is not at the head of the chain.
464 */
465 for (; /* goto done */; ) {
466 if (top->link == 0) {
467 /*
468 * top is end of the chain and none of the chain
469 * had top->selfpc == selfpc.
470 * so we allocate a new tostruct
471 * and link it to the head of the chain.
472 */
473 toindex = ++tos[0].link;
474 if (toindex >= tolimit) {
475 goto overflow;
476 }
477 top = &tos[toindex];
478 top->selfpc = selfpc;
479 top->count = 1;
480 top->link = *frompcindex;
481 *frompcindex = toindex;
482 goto done;
483 }
484 /*
485 * otherwise, check the next arc on the chain.
486 */
487 prevtop = top;
488 top = &tos[top->link];
489 if (top->selfpc == selfpc) {
490 /*
491 * there it is.
492 * increment its count
493 * move it to the head of the chain.
494 */
495 top->count++;
496 toindex = prevtop->link;
497 prevtop->link = top->link;
498 top->link = *frompcindex;
499 *frompcindex = toindex;
500 goto done;
501 }
502
503 }
504done:
505 profiling--;
506 /* and fall through */
507out:
508 return; /* normal return restores saved registers */
509
510overflow:
511 profiling++; /* halt further profiling */
512# define TOLIMIT "mcount: tos overflow\n"
513 write(2, TOLIMIT, sizeof(TOLIMIT));
514 goto out;
515}
516
517/*
518 * Control profiling
519 * profiling is what mcount checks to see if
520 * all the data structures are ready.
521 */
f7835768 522static void moncontrol(int mode)
638e2294
RS
523{
524 if (mode) {
525 /* start */
526 profil((unsigned short *)(sbuf + sizeof(struct phdr)),
527 ssiz - sizeof(struct phdr),
ca24c5ad
RO
528 (size_t)s_lowpc, s_scale);
529
638e2294
RS
530 profiling = 0;
531 } else {
532 /* stop */
533 profil((unsigned short *)0, 0, 0, 0);
534 profiling = 3;
535 }
536}