home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LIBSRC.ZOO / libsrc / gmon.c < prev    next >
C/C++ Source or Header  |  1992-02-03  |  9KB  |  329 lines

  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.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)gmon.c    5.3 (Berkeley) 5/22/91";
  36. #endif /* not lint */
  37.  
  38. #include <unistd.h>
  39.  
  40. #ifdef DEBUG
  41. #include <stdio.h>
  42. #endif
  43.  
  44. #include "gmon.h"
  45.  
  46. extern mcount() asm ("mcount");
  47. extern char *minbrk asm ("minbrk");
  48.  
  49.     /*
  50.      *    froms is actually a bunch of unsigned shorts indexing tos
  51.      */
  52. static int        profiling = 3;
  53. static unsigned short    *froms;
  54. static struct tostruct    *tos = 0;
  55. static long        tolimit = 0;
  56. static char        *s_lowpc = 0;
  57. static char        *s_highpc = 0;
  58. static unsigned long    s_textsize = 0;
  59.  
  60. static int    ssiz;
  61. static char    *sbuf;
  62. static int    s_scale;
  63.     /* see profil(2) where this is describe (incorrectly) */
  64. #define        SCALE_1_TO_1    0x10000L
  65.  
  66. #define    MSG "No space for profiling buffer(s)\n"
  67.  
  68. monstartup(lowpc, highpc)
  69.     char    *lowpc;
  70.     char    *highpc;
  71. {
  72.     int            monsize;
  73.     char        *buffer;
  74.     register int    o;
  75.  
  76.     /*
  77.      *    round lowpc and highpc to multiples of the density we're using
  78.      *    so the rest of the scaling (here and in gprof) stays in ints.
  79.      */
  80.     lowpc = (char *)
  81.         ROUNDDOWN((unsigned)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
  82.     s_lowpc = lowpc;
  83.     highpc = (char *)
  84.         ROUNDUP((unsigned)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
  85.     s_highpc = highpc;
  86.     s_textsize = highpc - lowpc;
  87.     monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
  88.     buffer = sbrk( monsize );
  89.     if ( buffer == (char *) -1 ) {
  90.     write( 2 , MSG , sizeof(MSG) );
  91.     return;
  92.     }
  93.     froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION );
  94.     if ( froms == (unsigned short *) -1 ) {
  95.     write( 2 , MSG , sizeof(MSG) );
  96.     froms = 0;
  97.     return;
  98.     }
  99.     tolimit = s_textsize * ARCDENSITY / 100;
  100.     if ( tolimit < MINARCS ) {
  101.     tolimit = MINARCS;
  102.     } else if ( tolimit > 65534 ) {
  103.     tolimit = 65534;
  104.     }
  105.     tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) );
  106.     if ( tos == (struct tostruct *) -1 ) {
  107.     write( 2 , MSG , sizeof(MSG) );
  108.     froms = 0;
  109.     tos = 0;
  110.     return;
  111.     }
  112.     minbrk = sbrk(0);
  113.     tos[0].link = 0;
  114.     sbuf = buffer;
  115.     ssiz = monsize;
  116.     ( (struct phdr *) buffer ) -> lpc = lowpc;
  117.     ( (struct phdr *) buffer ) -> hpc = highpc;
  118.     ( (struct phdr *) buffer ) -> ncnt = ssiz;
  119.     monsize -= sizeof(struct phdr);
  120.     if ( monsize <= 0 )
  121.     return;
  122.     o = highpc - lowpc;
  123.     if( monsize < o )
  124. #ifndef hp300
  125.     s_scale = ( (float) monsize / o ) * SCALE_1_TO_1;
  126. #else /* avoid floating point */
  127.     {
  128.     int quot = o / monsize;
  129.  
  130.     if (quot >= 0x10000)
  131.         s_scale = 1;
  132.     else if (quot >= 0x100)
  133.         s_scale = 0x10000 / quot;
  134.     else if (o >= 0x800000)
  135.         s_scale = 0x1000000 / (o / (monsize >> 8));
  136.     else
  137.         s_scale = 0x1000000 / ((o << 8) / monsize);
  138.     }
  139. #endif
  140.     else
  141.     s_scale = SCALE_1_TO_1;
  142.     moncontrol(1);
  143. }
  144.  
  145. _mcleanup()
  146. {
  147.     int            fd;
  148.     int            fromindex;
  149.     int            endfrom;
  150.     char        *frompc;
  151.     int            toindex;
  152.     struct rawarc    rawarc;
  153.  
  154.     moncontrol(0);
  155.     fd = creat( "gmon.out" , 0666 );
  156.     if ( fd < 0 ) {
  157.     perror( "mcount: gmon.out" );
  158.     return;
  159.     }
  160. #   ifdef DEBUG
  161.     fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
  162. #   endif DEBUG
  163.     write( fd , sbuf , ssiz );
  164.     endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
  165.     for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
  166.     if ( froms[fromindex] == 0 ) {
  167.         continue;
  168.     }
  169.     frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
  170.     for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
  171. #        ifdef DEBUG
  172.         fprintf( stderr ,
  173.             "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
  174.             frompc , tos[toindex].selfpc , tos[toindex].count );
  175. #        endif DEBUG
  176.         rawarc.raw_frompc = (unsigned long) frompc;
  177.         rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
  178.         rawarc.raw_count = tos[toindex].count;
  179.         write( fd , &rawarc , sizeof rawarc );
  180.     }
  181.     }
  182.     close( fd );
  183. }
  184.  
  185. mcount()
  186. {
  187.     register char            *selfpc;
  188.     register unsigned short        *frompcindex;
  189.     register struct tostruct    *top;
  190.     register struct tostruct    *prevtop;
  191.     register long            toindex;
  192.  
  193.     /*
  194.      *    find the return address for mcount,
  195.      *    and the return address for mcount's caller.
  196.      */
  197.     asm(".text");        /* make sure we're in text space */
  198.     /*
  199.      * selfpc = pc pushed by mcount call
  200.      */
  201.     asm("movl 4(%%ebp),%0" : "=r" (selfpc));
  202.     /*
  203.      * frompcindex = pc pushed by jsr into self.
  204.      * In GCC the caller's stack frame has already been built so we
  205.      * have to chase a6 to find caller's raddr.
  206.      */
  207.     asm("movl (%%ebp),%0" : "=r" (frompcindex));
  208.     frompcindex = ((unsigned short **)frompcindex)[1];
  209.     /*
  210.      *    check that we are profiling
  211.      *    and that we aren't recursively invoked.
  212.      */
  213.     if (profiling) {
  214.         goto out;
  215.     }
  216.     profiling++;
  217.     /*
  218.      *    check that frompcindex is a reasonable pc value.
  219.      *    for example:    signal catchers get called from the stack,
  220.      *            not from text space.  too bad.
  221.      */
  222.     frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
  223.     if ((unsigned long)frompcindex > s_textsize) {
  224.         goto done;
  225.     }
  226.     frompcindex =
  227.         &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
  228.     toindex = *frompcindex;
  229.     if (toindex == 0) {
  230.         /*
  231.          *    first time traversing this arc
  232.          */
  233.         toindex = ++tos[0].link;
  234.         if (toindex >= tolimit) {
  235.             goto overflow;
  236.         }
  237.         *frompcindex = toindex;
  238.         top = &tos[toindex];
  239.         top->selfpc = selfpc;
  240.         top->count = 1;
  241.         top->link = 0;
  242.         goto done;
  243.     }
  244.     top = &tos[toindex];
  245.     if (top->selfpc == selfpc) {
  246.         /*
  247.          *    arc at front of chain; usual case.
  248.          */
  249.         top->count++;
  250.         goto done;
  251.     }
  252.     /*
  253.      *    have to go looking down chain for it.
  254.      *    top points to what we are looking at,
  255.      *    prevtop points to previous top.
  256.      *    we know it is not at the head of the chain.
  257.      */
  258.     for (; /* goto done */; ) {
  259.         if (top->link == 0) {
  260.             /*
  261.              *    top is end of the chain and none of the chain
  262.              *    had top->selfpc == selfpc.
  263.              *    so we allocate a new tostruct
  264.              *    and link it to the head of the chain.
  265.              */
  266.             toindex = ++tos[0].link;
  267.             if (toindex >= tolimit) {
  268.                 goto overflow;
  269.             }
  270.             top = &tos[toindex];
  271.             top->selfpc = selfpc;
  272.             top->count = 1;
  273.             top->link = *frompcindex;
  274.             *frompcindex = toindex;
  275.             goto done;
  276.         }
  277.         /*
  278.          *    otherwise, check the next arc on the chain.
  279.          */
  280.         prevtop = top;
  281.         top = &tos[top->link];
  282.         if (top->selfpc == selfpc) {
  283.             /*
  284.              *    there it is.
  285.              *    increment its count
  286.              *    move it to the head of the chain.
  287.              */
  288.             top->count++;
  289.             toindex = prevtop->link;
  290.             prevtop->link = top->link;
  291.             top->link = *frompcindex;
  292.             *frompcindex = toindex;
  293.             goto done;
  294.         }
  295.  
  296.     }
  297. done:
  298.     profiling--;
  299.     /* and fall through */
  300. out:
  301.     return;        /* normal return restores saved registers */
  302.  
  303. overflow:
  304.     profiling++; /* halt further profiling */
  305. #   define    TOLIMIT    "mcount: tos overflow\n"
  306.     write(2, TOLIMIT, sizeof(TOLIMIT));
  307.     goto out;
  308. }
  309.  
  310. /*
  311.  * Control profiling
  312.  *    profiling is what mcount checks to see if
  313.  *    all the data structures are ready.
  314.  */
  315. moncontrol(mode)
  316.     int mode;
  317. {
  318.     if (mode) {
  319.     /* start */
  320.     profil(sbuf + sizeof(struct phdr), ssiz - sizeof(struct phdr),
  321.         (int)s_lowpc, s_scale);
  322.     profiling = 0;
  323.     } else {
  324.     /* stop */
  325.     profil((char *)0, 0, 0, 0);
  326.     profiling = 3;
  327.     }
  328. }
  329.