home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / libsrc / gmon.c < prev    next >
C/C++ Source or Header  |  1996-10-01  |  9KB  |  343 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. #include <unistd.h>
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sys/fcntl.h>
  40.  
  41. #include "gmon.h"
  42.  
  43. extern void start_text () asm ("exec");
  44.  
  45. asm ("
  46.     .globl    mcount
  47. mcount:    movel    a1,sp@-        | Save A1 as it might be used to store the
  48.     jsr    _mcount        | address of a structure
  49.     movel    sp@+,a1
  50.     rts
  51. ");
  52.  
  53. void moncontrol(int mode);
  54.  
  55. static unsigned int    *store_last_pc;
  56. static unsigned int    dummy;
  57.     /*
  58.      *    froms is actually a bunch of unsigned shorts indexing tos
  59.      */
  60. static int        profiling = 3;
  61. static unsigned short    *froms;
  62. static struct tostruct    *tos = 0;
  63. static long        tolimit = 0;
  64. static char        *s_lowpc = 0;
  65. static char        *s_highpc = 0;
  66. static unsigned long    s_textsize = 0;
  67.  
  68. static int    ssiz;
  69. static char    *sbuf;
  70. static int    s_scale;
  71.     /* see profil(2) where this is describe (incorrectly) */
  72. #define        SCALE_1_TO_1    0x10000L
  73.  
  74. #define    MSG "No space for profiling buffer(s)\n"
  75.  
  76. static inline void *
  77. alloc(int s)
  78. {
  79.   void *res = malloc (s);
  80.   if (res)
  81.     bzero (res, s);
  82.   return res;
  83. }
  84.  
  85. void monstartup(char *lowpc, char *highpc)
  86. {
  87.     int            monsize;
  88.     char        *buffer;
  89.     register int    o;
  90.  
  91.     /*
  92.      *    round lowpc and highpc to multiples of the density we're using
  93.      *    so the rest of the scaling (here and in gprof) stays in ints.
  94.      */
  95.     lowpc = (char *)
  96.         ROUNDDOWN((unsigned)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
  97.     s_lowpc = lowpc;
  98.     highpc = (char *)
  99.         ROUNDUP((unsigned)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
  100.     s_highpc = highpc;
  101.     s_textsize = highpc - lowpc;
  102.     monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
  103.     buffer = alloc( monsize );
  104.     if ( buffer == 0 ) {
  105.     write( 2 , MSG , sizeof(MSG) );
  106.     return;
  107.     }
  108.     froms = (unsigned short *) alloc ( s_textsize / HASHFRACTION );
  109.     if ( froms == 0 ) {
  110.     write( 2 , MSG , sizeof(MSG) );
  111.     froms = 0;
  112.     return;
  113.     }
  114.     tolimit = s_textsize * ARCDENSITY / 100;
  115.     if ( tolimit < MINARCS ) {
  116.     tolimit = MINARCS;
  117.     } else if ( tolimit > 65534 ) {
  118.     tolimit = 65534;
  119.     }
  120.     tos = (struct tostruct *) alloc( tolimit * sizeof( struct tostruct ) );
  121.     if ( tos == 0 ) {
  122.     write( 2 , MSG , sizeof(MSG) );
  123.     froms = 0;
  124.     tos = 0;
  125.     return;
  126.     }
  127.     tos[0].link = 0;
  128.     sbuf = buffer;
  129.     ssiz = monsize;
  130.     ( (struct phdr *) buffer ) -> lpc = (void *)(lowpc - ((char *)start_text - 4));
  131.     ( (struct phdr *) buffer ) -> hpc = (void *)(highpc - ((char *)start_text - 4));
  132.     ( (struct phdr *) buffer ) -> ncnt = ssiz;
  133.     monsize -= sizeof(struct phdr);
  134.     if ( monsize <= 0 )
  135.     return;
  136.     o = highpc - lowpc;
  137.     if( monsize < o )
  138.     {
  139.     int quot = o / monsize;
  140.  
  141.     if (quot >= 0x10000)
  142.         s_scale = 1;
  143.     else if (quot >= 0x100)
  144.         s_scale = 0x10000 / quot;
  145.     else if (o >= 0x800000)
  146.         s_scale = 0x1000000 / (o / (monsize >> 8));
  147.     else
  148.         s_scale = 0x1000000 / ((o << 8) / monsize);
  149.     }
  150.     else
  151.     s_scale = SCALE_1_TO_1;
  152.     moncontrol(1);
  153. }
  154.  
  155. void _mcleanup(void)
  156. {
  157.     int            fd;
  158.     int            fromindex;
  159.     int            endfrom;
  160.     char        *frompc;
  161.     int            toindex;
  162.     struct rawarc    rawarc;
  163.  
  164.     moncontrol(0);
  165.     fd = creat( "gmon.out" , 0666 );
  166.     if ( fd < 0 ) {
  167.     perror( "mcount: gmon.out" );
  168.     return;
  169.     }
  170. #   ifdef DEBUG
  171.     fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
  172. #   endif DEBUG
  173.     write( fd , sbuf , ssiz );
  174.     endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
  175.     for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
  176.     if ( froms[fromindex] == 0 ) {
  177.         continue;
  178.     }
  179.     frompc = (void *)(s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms)) - ((char *)start_text - 4));
  180.     for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
  181. #        ifdef DEBUG
  182.         fprintf( stderr ,
  183.             "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
  184.             frompc , tos[toindex].selfpc , tos[toindex].count );
  185. #        endif DEBUG
  186.         rawarc.raw_frompc = (unsigned long) frompc;
  187.         rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
  188.         rawarc.raw_count = tos[toindex].count;
  189.         write( fd , &rawarc , sizeof rawarc );
  190.     }
  191.     }
  192.     close( fd );
  193. }
  194.  
  195. void mcount(void)
  196. {
  197.     register char            *selfpc;
  198.     register unsigned short        *frompcindex;
  199.     register struct tostruct    *top;
  200.     register struct tostruct    *prevtop;
  201.     register long            toindex;
  202.  
  203.     /*
  204.      *    find the return address for mcount,
  205.      *    and the return address for mcount's caller.
  206.      */
  207.  
  208.     /* selfpc = pc pushed by mcount call.
  209.        This identifies the function that was just entered.  */
  210.     selfpc = (char *) __builtin_return_address (0);
  211.     /* frompcindex = pc in preceding frame.
  212.        This identifies the caller of the function just entered.  */
  213.     frompcindex = (void *) __builtin_return_address (1);
  214.     /*
  215.      *    check that we are profiling
  216.      *    and that we aren't recursively invoked.
  217.      */
  218.     if (profiling) {
  219.         goto out;
  220.     }
  221.     profiling++;
  222.     *store_last_pc = (unsigned)selfpc;
  223.     selfpc = selfpc - (int)((char *)start_text - 4);
  224.     /*
  225.      *    check that frompcindex is a reasonable pc value.
  226.      *    for example:    signal catchers get called from the stack,
  227.      *            not from text space.  too bad.
  228.      */
  229. #ifdef DEBUG
  230.         fprintf (stderr, "from $%lx, self $%lx (low = $%lx)\n",
  231.              frompcindex, selfpc, s_lowpc);
  232. #endif
  233.     frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
  234.     if ((unsigned long)frompcindex > s_textsize) {
  235.  
  236.         goto done;
  237.     }
  238.     frompcindex =
  239.         &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
  240.     toindex = *frompcindex;
  241.     if (toindex == 0) {
  242.         /*
  243.          *    first time traversing this arc
  244.          */
  245.         toindex = ++tos[0].link;
  246.         if (toindex >= tolimit) {
  247.             goto overflow;
  248.         }
  249.         *frompcindex = toindex;
  250.         top = &tos[toindex];
  251.         top->selfpc = selfpc;
  252.         top->count = 1;
  253.         top->link = 0;
  254.         goto done;
  255.     }
  256.     top = &tos[toindex];
  257.     if (top->selfpc == selfpc) {
  258.         /*
  259.          *    arc at front of chain; usual case.
  260.          */
  261.         top->count++;
  262.         goto done;
  263.     }
  264.     /*
  265.      *    have to go looking down chain for it.
  266.      *    top points to what we are looking at,
  267.      *    prevtop points to previous top.
  268.      *    we know it is not at the head of the chain.
  269.      */
  270.     for (; /* goto done */; ) {
  271.         if (top->link == 0) {
  272.             /*
  273.              *    top is end of the chain and none of the chain
  274.              *    had top->selfpc == selfpc.
  275.              *    so we allocate a new tostruct
  276.              *    and link it to the head of the chain.
  277.              */
  278.             toindex = ++tos[0].link;
  279.             if (toindex >= tolimit) {
  280.                 goto overflow;
  281.             }
  282.             top = &tos[toindex];
  283.             top->selfpc = selfpc;
  284.             top->count = 1;
  285.             top->link = *frompcindex;
  286.             *frompcindex = toindex;
  287.             goto done;
  288.         }
  289.         /*
  290.          *    otherwise, check the next arc on the chain.
  291.          */
  292.         prevtop = top;
  293.         top = &tos[top->link];
  294.         if (top->selfpc == selfpc) {
  295.             /*
  296.              *    there it is.
  297.              *    increment its count
  298.              *    move it to the head of the chain.
  299.              */
  300.             top->count++;
  301.             toindex = prevtop->link;
  302.             prevtop->link = top->link;
  303.             top->link = *frompcindex;
  304.             *frompcindex = toindex;
  305.             goto done;
  306.         }
  307.  
  308.     }
  309. done:
  310.     profiling--;
  311.     /* and fall through */
  312. out:
  313.     return;        /* normal return restores saved registers */
  314.  
  315. overflow:
  316.     profiling++; /* halt further profiling */
  317. #   define    TOLIMIT    "mcount: tos overflow\n"
  318.     write(2, TOLIMIT, sizeof(TOLIMIT));
  319.     goto out;
  320. }
  321.  
  322. /*
  323.  * Control profiling
  324.  *    profiling is what mcount checks to see if
  325.  *    all the data structures are ready.
  326.  */
  327. void moncontrol(int mode)
  328. {
  329.     if (mode) {
  330.     /* start */
  331.     store_last_pc = (unsigned *)profil(sbuf + sizeof(struct phdr),
  332.                    ssiz - sizeof(struct phdr), (int)s_lowpc, s_scale);
  333.         if (store_last_pc == NULL)
  334.             store_last_pc = &dummy;
  335.     profiling = 0;
  336.     } else {
  337.     /* stop */
  338.     profil((char *)0, 0, 0, 0);
  339.     profiling = 3;
  340.     }
  341. }
  342.  
  343.