gmon-sol2.c revision 132718
189051Sjake/*- 289051Sjake * Copyright (c) 1991 The Regents of the University of California. 389051Sjake * All rights reserved. 489051Sjake * 589051Sjake * Redistribution and use in source and binary forms, with or without 689051Sjake * modification, are permitted provided that the following conditions 789051Sjake * are met: 889051Sjake * 1. Redistributions of source code must retain the above copyright 989051Sjake * notice, this list of conditions and the following disclaimer. 1089051Sjake * 2. Redistributions in binary form must reproduce the above copyright 1189051Sjake * notice, this list of conditions and the following disclaimer in the 1289051Sjake * documentation and/or other materials provided with the distribution. 1389051Sjake * 3. [rescinded 22 July 1999] 1489051Sjake * 4. Neither the name of the University nor the names of its contributors 1589051Sjake * may be used to endorse or promote products derived from this software 1689051Sjake * without specific prior written permission. 1789051Sjake * 1889051Sjake * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1989051Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2089051Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2189051Sjake * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2289051Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2389051Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2489051Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2589051Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2689051Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2789051Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2889051Sjake * SUCH DAMAGE. 2989051Sjake */ 3089051Sjake 3189051Sjake/* Mangled into a form that works on SPARC Solaris 2 by Mark Eichin 3289051Sjake * for Cygnus Support, July 1992. 3389051Sjake */ 3489051Sjake 3589051Sjake#include "tconfig.h" 3689051Sjake#include "tsystem.h" 3789051Sjake#include <fcntl.h> /* for creat() */ 3889051Sjake#include "coretypes.h" 3989051Sjake#include "tm.h" 4089051Sjake 4189051Sjake#if 0 4289051Sjake#include "sparc/gmon.h" 4389051Sjake#else 4489051Sjakestruct phdr { 4589051Sjake char *lpc; 4689051Sjake char *hpc; 4789051Sjake int ncnt; 4889051Sjake}; 4989051Sjake#define HISTFRACTION 2 5089051Sjake#define HISTCOUNTER unsigned short 5189051Sjake#define HASHFRACTION 1 5289051Sjake#define ARCDENSITY 2 5389051Sjake#define MINARCS 50 5489051Sjakestruct tostruct { 5589051Sjake char *selfpc; 5689051Sjake long count; 5789051Sjake unsigned short link; 5889051Sjake}; 5989051Sjakestruct rawarc { 6089051Sjake unsigned long raw_frompc; 6189051Sjake unsigned long raw_selfpc; 6289051Sjake long raw_count; 6389051Sjake}; 6489051Sjake#define ROUNDDOWN(x,y) (((x)/(y))*(y)) 6589051Sjake#define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y)) 6689051Sjake 6789051Sjake#endif 6889051Sjake 6989051Sjake/* extern mcount() asm ("mcount"); */ 7089051Sjake/*extern*/ char *minbrk /* asm ("minbrk") */; 7189051Sjake 7289051Sjake /* 7389051Sjake * froms is actually a bunch of unsigned shorts indexing tos 7489051Sjake */ 7589051Sjakestatic int profiling = 3; 7689051Sjakestatic unsigned short *froms; 7789051Sjakestatic struct tostruct *tos = 0; 7889051Sjakestatic long tolimit = 0; 7989051Sjakestatic char *s_lowpc = 0; 8091617Sjakestatic char *s_highpc = 0; 8189051Sjakestatic unsigned long s_textsize = 0; 8289051Sjake 8389051Sjakestatic int ssiz; 8489051Sjakestatic char *sbuf; 8589051Sjakestatic int s_scale; 8689051Sjake /* see profil(2) where this is describe (incorrectly) */ 8789051Sjake#define SCALE_1_TO_1 0x10000L 8889051Sjake 8989051Sjake#define MSG "No space for profiling buffer(s)\n" 9089051Sjake 9189051Sjakestatic void moncontrol (int); 9289051Sjakeextern void monstartup (char *, char *); 9389051Sjakeextern void _mcleanup (void); 9491617Sjake 9591617Sjakevoid monstartup(char *lowpc, char *highpc) 9689051Sjake{ 9789051Sjake int monsize; 9889051Sjake char *buffer; 9989051Sjake register int o; 10091617Sjake 10191617Sjake /* 10291617Sjake * round lowpc and highpc to multiples of the density we're using 10391617Sjake * so the rest of the scaling (here and in gprof) stays in ints. 10491617Sjake */ 10591617Sjake lowpc = (char *) 10691617Sjake ROUNDDOWN((unsigned long)lowpc, HISTFRACTION*sizeof(HISTCOUNTER)); 10791617Sjake s_lowpc = lowpc; 10891617Sjake highpc = (char *) 10991617Sjake ROUNDUP((unsigned long)highpc, HISTFRACTION*sizeof(HISTCOUNTER)); 11091617Sjake s_highpc = highpc; 11191617Sjake s_textsize = highpc - lowpc; 11291617Sjake monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr); 11391617Sjake buffer = sbrk( monsize ); 11491617Sjake if ( buffer == (char *) -1 ) { 11591617Sjake write( 2 , MSG , sizeof(MSG) ); 11691617Sjake return; 11791617Sjake } 11891617Sjake froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION ); 11991617Sjake if ( froms == (unsigned short *) -1 ) { 12091617Sjake write( 2 , MSG , sizeof(MSG) ); 12191617Sjake froms = 0; 12291617Sjake return; 12391617Sjake } 12489051Sjake tolimit = s_textsize * ARCDENSITY / 100; 12589051Sjake if ( tolimit < MINARCS ) { 12689051Sjake tolimit = MINARCS; 12789051Sjake } else if ( tolimit > 65534 ) { 12889051Sjake tolimit = 65534; 12989051Sjake } 13089051Sjake tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) ); 13189051Sjake if ( tos == (struct tostruct *) -1 ) { 13289051Sjake write( 2 , MSG , sizeof(MSG) ); 13389051Sjake froms = 0; 13489051Sjake tos = 0; 13589051Sjake return; 13689051Sjake } 13789051Sjake minbrk = sbrk(0); 13889051Sjake tos[0].link = 0; 13989051Sjake sbuf = buffer; 14089051Sjake ssiz = monsize; 14189051Sjake ( (struct phdr *) buffer ) -> lpc = lowpc; 14289051Sjake ( (struct phdr *) buffer ) -> hpc = highpc; 14389051Sjake ( (struct phdr *) buffer ) -> ncnt = ssiz; 14489051Sjake monsize -= sizeof(struct phdr); 14589051Sjake if ( monsize <= 0 ) 14689051Sjake return; 14789051Sjake o = highpc - lowpc; 14889051Sjake if( monsize < o ) 14991617Sjake#ifndef hp300 15091617Sjake s_scale = ( (float) monsize / o ) * SCALE_1_TO_1; 15191617Sjake#else /* avoid floating point */ 15291617Sjake { 15391617Sjake int quot = o / monsize; 15491617Sjake 15591617Sjake if (quot >= 0x10000) 15691617Sjake s_scale = 1; 15791617Sjake else if (quot >= 0x100) 15891617Sjake s_scale = 0x10000 / quot; 15991617Sjake else if (o >= 0x800000) 16091617Sjake s_scale = 0x1000000 / (o / (monsize >> 8)); 16191617Sjake else 16291617Sjake s_scale = 0x1000000 / ((o << 8) / monsize); 16391617Sjake } 16491617Sjake#endif 16591617Sjake else 16691617Sjake s_scale = SCALE_1_TO_1; 16791617Sjake moncontrol(1); 16891617Sjake} 16991617Sjake 17091617Sjakevoid 17191617Sjake_mcleanup(void) 17291617Sjake{ 17391617Sjake int fd; 17489051Sjake int fromindex; 17589051Sjake int endfrom; 17689051Sjake char *frompc; 17789051Sjake int toindex; 17889051Sjake struct rawarc rawarc; 17989051Sjake char *profdir; 18089051Sjake const char *proffile; 18189051Sjake char *progname; 18289051Sjake char buf[PATH_MAX]; 18389051Sjake extern char **___Argv; 18489051Sjake 18589051Sjake moncontrol(0); 18691617Sjake 18791617Sjake if ((profdir = getenv("PROFDIR")) != NULL) { 18889051Sjake /* If PROFDIR contains a null value, no profiling output is produced */ 18991617Sjake if (*profdir == '\0') { 19089051Sjake return; 19189051Sjake } 19289051Sjake 19389051Sjake progname=strrchr(___Argv[0], '/'); 19489051Sjake if (progname == NULL) 19589051Sjake progname=___Argv[0]; 19689051Sjake else 19789051Sjake progname++; 19889051Sjake 19989051Sjake sprintf(buf, "%s/%ld.%s", profdir, (long) getpid(), progname); 20089051Sjake proffile = buf; 20189051Sjake } else { 20289051Sjake proffile = "gmon.out"; 20389051Sjake } 20489051Sjake 20589051Sjake fd = creat( proffile, 0666 ); 20689051Sjake if ( fd < 0 ) { 20789051Sjake perror( proffile ); 20891617Sjake return; 20991617Sjake } 21091617Sjake# ifdef DEBUG 21189051Sjake fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz ); 21291617Sjake# endif /* DEBUG */ 21391617Sjake write( fd , sbuf , ssiz ); 21491617Sjake endfrom = s_textsize / (HASHFRACTION * sizeof(*froms)); 21591617Sjake for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) { 21691617Sjake if ( froms[fromindex] == 0 ) { 21791617Sjake continue; 21891617Sjake } 21991617Sjake frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms)); 22091617Sjake for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) { 22191617Sjake# ifdef DEBUG 22291617Sjake fprintf( stderr , 22391617Sjake "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" , 22489051Sjake frompc , tos[toindex].selfpc , tos[toindex].count ); 22591617Sjake# endif /* DEBUG */ 22691617Sjake rawarc.raw_frompc = (unsigned long) frompc; 22791617Sjake rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc; 22891617Sjake rawarc.raw_count = tos[toindex].count; 22989051Sjake write( fd , &rawarc , sizeof rawarc ); 23091617Sjake } 23189051Sjake } 23289051Sjake close( fd ); 23389051Sjake} 23489051Sjake 23589051Sjake/* 23689051Sjake * The SPARC stack frame is only held together by the frame pointers 23789051Sjake * in the register windows. According to the SVR4 SPARC ABI 23889051Sjake * Supplement, Low Level System Information/Operating System 23989051Sjake * Interface/Software Trap Types, a type 3 trap will flush all of the 24089051Sjake * register windows to the stack, which will make it possible to walk 24189051Sjake * the frames and find the return addresses. 24289051Sjake * However, it seems awfully expensive to incur a trap (system 24389051Sjake * call) for every function call. It turns out that "call" simply puts 24491617Sjake * the return address in %o7 expecting the "save" in the procedure to 24589051Sjake * shift it into %i7; this means that before the "save" occurs, %o7 24691617Sjake * contains the address of the call to mcount, and %i7 still contains 24791617Sjake * the caller above that. The asm mcount here simply saves those 24891617Sjake * registers in argument registers and branches to internal_mcount, 24991617Sjake * simulating a call with arguments. 25091617Sjake * Kludges: 25191617Sjake * 1) the branch to internal_mcount is hard coded; it should be 25291617Sjake * possible to tell asm to use the assembler-name of a symbol. 25391617Sjake * 2) in theory, the function calling mcount could have saved %i7 25489051Sjake * somewhere and reused the register; in practice, I *think* this will 25591617Sjake * break longjmp (and maybe the debugger) but I'm not certain. (I take 25691617Sjake * some comfort in the knowledge that it will break the native mcount 25789051Sjake * as well.) 25891617Sjake * 3) if builtin_return_address worked, this could be portable. 25991617Sjake * However, it would really have to be optimized for arguments of 0 26091617Sjake * and 1 and do something like what we have here in order to avoid the 26191617Sjake * trap per function call performance hit. 26291617Sjake * 4) the atexit and monsetup calls prevent this from simply 26389051Sjake * being a leaf routine that doesn't do a "save" (and would thus have 26491617Sjake * access to %o7 and %i7 directly) but the call to write() at the end 26591617Sjake * would have also prevented this. 26691617Sjake * 26791617Sjake * -- [eichin:19920702.1107EST] 26891617Sjake */ 26991617Sjake 27091617Sjakestatic void internal_mcount (char *, unsigned short *) 27191617Sjake __attribute__ ((__unused__)); 27291617Sjake 27391617Sjake/* i7 == last ret, -> frompcindex */ 27491617Sjake/* o7 == current ret, -> selfpc */ 27591617Sjake/* Solaris 2 libraries use _mcount. */ 27691617Sjakeasm(".global _mcount; _mcount: mov %i7,%o1; mov %o7,%o0;b,a internal_mcount"); 27791617Sjake/* This is for compatibility with old versions of gcc which used mcount. */ 27891617Sjakeasm(".global mcount; mcount: mov %i7,%o1; mov %o7,%o0;b,a internal_mcount"); 27991617Sjake 28091617Sjakestatic void internal_mcount(char *selfpc, unsigned short *frompcindex) 28191617Sjake{ 28291617Sjake register struct tostruct *top; 28391617Sjake register struct tostruct *prevtop; 28491617Sjake register long toindex; 28591617Sjake static char already_setup; 28691617Sjake 28791617Sjake /* 28891617Sjake * find the return address for mcount, 28989051Sjake * and the return address for mcount's caller. 29091617Sjake */ 29191617Sjake 29291617Sjake if(!already_setup) { 29391617Sjake extern char etext[]; 29489051Sjake extern char _start[]; 29591617Sjake extern char _init[]; 29691617Sjake already_setup = 1; 29791617Sjake monstartup(_start < _init ? _start : _init, etext); 29889051Sjake#ifdef USE_ONEXIT 29989051Sjake on_exit(_mcleanup, 0); 30089051Sjake#else 30189051Sjake atexit(_mcleanup); 30289051Sjake#endif 30391617Sjake } 30491617Sjake /* 30591617Sjake * check that we are profiling 30689051Sjake * and that we aren't recursively invoked. 30791066Sphk */ 30889051Sjake if (profiling) { 30989051Sjake goto out; 31089051Sjake } 31189051Sjake profiling++; 31289051Sjake /* 31389051Sjake * check that frompcindex is a reasonable pc value. 31489051Sjake * for example: signal catchers get called from the stack, 31589051Sjake * not from text space. too bad. 31689051Sjake */ 31789051Sjake frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc); 31889051Sjake if ((unsigned long)frompcindex > s_textsize) { 31989051Sjake goto done; 32089051Sjake } 32189051Sjake frompcindex = 32289051Sjake &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))]; 32389051Sjake toindex = *frompcindex; 32489051Sjake if (toindex == 0) { 32589051Sjake /* 32689051Sjake * first time traversing this arc 32789051Sjake */ 32889051Sjake toindex = ++tos[0].link; 32989051Sjake if (toindex >= tolimit) { 33089051Sjake goto overflow; 33189051Sjake } 33289051Sjake *frompcindex = toindex; 33389051Sjake top = &tos[toindex]; 33489051Sjake top->selfpc = selfpc; 33589051Sjake top->count = 1; 33689051Sjake top->link = 0; 33789051Sjake goto done; 33889051Sjake } 33989051Sjake top = &tos[toindex]; 34089051Sjake if (top->selfpc == selfpc) { 34189051Sjake /* 34289051Sjake * arc at front of chain; usual case. 34389051Sjake */ 34489051Sjake top->count++; 34589051Sjake goto done; 34689051Sjake } 34789051Sjake /* 34889051Sjake * have to go looking down chain for it. 34989051Sjake * top points to what we are looking at, 35089051Sjake * prevtop points to previous top. 35189051Sjake * we know it is not at the head of the chain. 35289051Sjake */ 35389051Sjake for (; /* goto done */; ) { 35489051Sjake if (top->link == 0) { 35589051Sjake /* 35689051Sjake * top is end of the chain and none of the chain 35789051Sjake * had top->selfpc == selfpc. 35889051Sjake * so we allocate a new tostruct 35989051Sjake * and link it to the head of the chain. 36089051Sjake */ 36189051Sjake toindex = ++tos[0].link; 36289051Sjake if (toindex >= tolimit) { 36389051Sjake goto overflow; 36489051Sjake } 36589051Sjake top = &tos[toindex]; 36689051Sjake top->selfpc = selfpc; 36789051Sjake top->count = 1; 36889051Sjake top->link = *frompcindex; 36989051Sjake *frompcindex = toindex; 37089051Sjake goto done; 37189051Sjake } 37289051Sjake /* 37389051Sjake * otherwise, check the next arc on the chain. 37489051Sjake */ 37589051Sjake prevtop = top; 37689051Sjake top = &tos[top->link]; 37789051Sjake if (top->selfpc == selfpc) { 37889051Sjake /* 37989051Sjake * there it is. 38089051Sjake * increment its count 38189051Sjake * move it to the head of the chain. 38289051Sjake */ 383 top->count++; 384 toindex = prevtop->link; 385 prevtop->link = top->link; 386 top->link = *frompcindex; 387 *frompcindex = toindex; 388 goto done; 389 } 390 391 } 392done: 393 profiling--; 394 /* and fall through */ 395out: 396 return; /* normal return restores saved registers */ 397 398overflow: 399 profiling++; /* halt further profiling */ 400# define TOLIMIT "mcount: tos overflow\n" 401 write(2, TOLIMIT, sizeof(TOLIMIT)); 402 goto out; 403} 404 405/* 406 * Control profiling 407 * profiling is what mcount checks to see if 408 * all the data structures are ready. 409 */ 410static void moncontrol(int mode) 411{ 412 if (mode) { 413 /* start */ 414 profil((unsigned short *)(sbuf + sizeof(struct phdr)), 415 ssiz - sizeof(struct phdr), 416 (long)s_lowpc, s_scale); 417 profiling = 0; 418 } else { 419 /* stop */ 420 profil((unsigned short *)0, 0, 0, 0); 421 profiling = 3; 422 } 423} 424