158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1983, 1992, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 758f0484fSRodney W. Grimes * are met: 858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1358f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1458f0484fSRodney W. Grimes * must display the following acknowledgement: 1558f0484fSRodney W. Grimes * This product includes software developed by the University of 1658f0484fSRodney W. Grimes * California, Berkeley and its contributors. 1758f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1858f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1958f0484fSRodney W. Grimes * without specific prior written permission. 2058f0484fSRodney W. Grimes * 2158f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2258f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2358f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2458f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2558f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2658f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2758f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2858f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2958f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3058f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3158f0484fSRodney W. Grimes * SUCH DAMAGE. 3258f0484fSRodney W. Grimes */ 3358f0484fSRodney W. Grimes 3458f0484fSRodney W. Grimes #if !defined(lint) && !defined(KERNEL) && defined(LIBC_SCCS) 3558f0484fSRodney W. Grimes static char sccsid[] = "@(#)mcount.c 8.1 (Berkeley) 6/4/93"; 3658f0484fSRodney W. Grimes #endif 3758f0484fSRodney W. Grimes 3858f0484fSRodney W. Grimes #include <sys/param.h> 3958f0484fSRodney W. Grimes #include <sys/gmon.h> 4058f0484fSRodney W. Grimes 4158f0484fSRodney W. Grimes /* 4258f0484fSRodney W. Grimes * mcount is called on entry to each function compiled with the profiling 4358f0484fSRodney W. Grimes * switch set. _mcount(), which is declared in a machine-dependent way 4458f0484fSRodney W. Grimes * with _MCOUNT_DECL, does the actual work and is either inlined into a 4558f0484fSRodney W. Grimes * C routine or called by an assembly stub. In any case, this magic is 4658f0484fSRodney W. Grimes * taken care of by the MCOUNT definition in <machine/profile.h>. 4758f0484fSRodney W. Grimes * 4858f0484fSRodney W. Grimes * _mcount updates data structures that represent traversals of the 4958f0484fSRodney W. Grimes * program's call graph edges. frompc and selfpc are the return 5058f0484fSRodney W. Grimes * address and function address that represents the given call graph edge. 5158f0484fSRodney W. Grimes * 5258f0484fSRodney W. Grimes * Note: the original BSD code used the same variable (frompcindex) for 5358f0484fSRodney W. Grimes * both frompcindex and frompc. Any reasonable, modern compiler will 5458f0484fSRodney W. Grimes * perform this optimization. 5558f0484fSRodney W. Grimes */ 5658f0484fSRodney W. Grimes _MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */ 5758f0484fSRodney W. Grimes register u_long frompc, selfpc; 5858f0484fSRodney W. Grimes { 5958f0484fSRodney W. Grimes register u_short *frompcindex; 6058f0484fSRodney W. Grimes register struct tostruct *top, *prevtop; 6158f0484fSRodney W. Grimes register struct gmonparam *p; 6258f0484fSRodney W. Grimes register long toindex; 6358f0484fSRodney W. Grimes #ifdef KERNEL 6458f0484fSRodney W. Grimes register int s; 6558f0484fSRodney W. Grimes #endif 6658f0484fSRodney W. Grimes 6758f0484fSRodney W. Grimes p = &_gmonparam; 6858f0484fSRodney W. Grimes /* 6958f0484fSRodney W. Grimes * check that we are profiling 7058f0484fSRodney W. Grimes * and that we aren't recursively invoked. 7158f0484fSRodney W. Grimes */ 7258f0484fSRodney W. Grimes if (p->state != GMON_PROF_ON) 7358f0484fSRodney W. Grimes return; 7458f0484fSRodney W. Grimes #ifdef KERNEL 7558f0484fSRodney W. Grimes MCOUNT_ENTER; 7658f0484fSRodney W. Grimes #else 7758f0484fSRodney W. Grimes p->state = GMON_PROF_BUSY; 7858f0484fSRodney W. Grimes #endif 7958f0484fSRodney W. Grimes /* 8058f0484fSRodney W. Grimes * check that frompcindex is a reasonable pc value. 8158f0484fSRodney W. Grimes * for example: signal catchers get called from the stack, 8258f0484fSRodney W. Grimes * not from text space. too bad. 8358f0484fSRodney W. Grimes */ 8458f0484fSRodney W. Grimes frompc -= p->lowpc; 8558f0484fSRodney W. Grimes if (frompc > p->textsize) 8658f0484fSRodney W. Grimes goto done; 8758f0484fSRodney W. Grimes 8858f0484fSRodney W. Grimes frompcindex = &p->froms[frompc / (p->hashfraction * sizeof(*p->froms))]; 8958f0484fSRodney W. Grimes toindex = *frompcindex; 9058f0484fSRodney W. Grimes if (toindex == 0) { 9158f0484fSRodney W. Grimes /* 9258f0484fSRodney W. Grimes * first time traversing this arc 9358f0484fSRodney W. Grimes */ 9458f0484fSRodney W. Grimes toindex = ++p->tos[0].link; 9558f0484fSRodney W. Grimes if (toindex >= p->tolimit) 9658f0484fSRodney W. Grimes /* halt further profiling */ 9758f0484fSRodney W. Grimes goto overflow; 9858f0484fSRodney W. Grimes 9958f0484fSRodney W. Grimes *frompcindex = toindex; 10058f0484fSRodney W. Grimes top = &p->tos[toindex]; 10158f0484fSRodney W. Grimes top->selfpc = selfpc; 10258f0484fSRodney W. Grimes top->count = 1; 10358f0484fSRodney W. Grimes top->link = 0; 10458f0484fSRodney W. Grimes goto done; 10558f0484fSRodney W. Grimes } 10658f0484fSRodney W. Grimes top = &p->tos[toindex]; 10758f0484fSRodney W. Grimes if (top->selfpc == selfpc) { 10858f0484fSRodney W. Grimes /* 10958f0484fSRodney W. Grimes * arc at front of chain; usual case. 11058f0484fSRodney W. Grimes */ 11158f0484fSRodney W. Grimes top->count++; 11258f0484fSRodney W. Grimes goto done; 11358f0484fSRodney W. Grimes } 11458f0484fSRodney W. Grimes /* 11558f0484fSRodney W. Grimes * have to go looking down chain for it. 11658f0484fSRodney W. Grimes * top points to what we are looking at, 11758f0484fSRodney W. Grimes * prevtop points to previous top. 11858f0484fSRodney W. Grimes * we know it is not at the head of the chain. 11958f0484fSRodney W. Grimes */ 12058f0484fSRodney W. Grimes for (; /* goto done */; ) { 12158f0484fSRodney W. Grimes if (top->link == 0) { 12258f0484fSRodney W. Grimes /* 12358f0484fSRodney W. Grimes * top is end of the chain and none of the chain 12458f0484fSRodney W. Grimes * had top->selfpc == selfpc. 12558f0484fSRodney W. Grimes * so we allocate a new tostruct 12658f0484fSRodney W. Grimes * and link it to the head of the chain. 12758f0484fSRodney W. Grimes */ 12858f0484fSRodney W. Grimes toindex = ++p->tos[0].link; 12958f0484fSRodney W. Grimes if (toindex >= p->tolimit) 13058f0484fSRodney W. Grimes goto overflow; 13158f0484fSRodney W. Grimes 13258f0484fSRodney W. Grimes top = &p->tos[toindex]; 13358f0484fSRodney W. Grimes top->selfpc = selfpc; 13458f0484fSRodney W. Grimes top->count = 1; 13558f0484fSRodney W. Grimes top->link = *frompcindex; 13658f0484fSRodney W. Grimes *frompcindex = toindex; 13758f0484fSRodney W. Grimes goto done; 13858f0484fSRodney W. Grimes } 13958f0484fSRodney W. Grimes /* 14058f0484fSRodney W. Grimes * otherwise, check the next arc on the chain. 14158f0484fSRodney W. Grimes */ 14258f0484fSRodney W. Grimes prevtop = top; 14358f0484fSRodney W. Grimes top = &p->tos[top->link]; 14458f0484fSRodney W. Grimes if (top->selfpc == selfpc) { 14558f0484fSRodney W. Grimes /* 14658f0484fSRodney W. Grimes * there it is. 14758f0484fSRodney W. Grimes * increment its count 14858f0484fSRodney W. Grimes * move it to the head of the chain. 14958f0484fSRodney W. Grimes */ 15058f0484fSRodney W. Grimes top->count++; 15158f0484fSRodney W. Grimes toindex = prevtop->link; 15258f0484fSRodney W. Grimes prevtop->link = top->link; 15358f0484fSRodney W. Grimes top->link = *frompcindex; 15458f0484fSRodney W. Grimes *frompcindex = toindex; 15558f0484fSRodney W. Grimes goto done; 15658f0484fSRodney W. Grimes } 15758f0484fSRodney W. Grimes 15858f0484fSRodney W. Grimes } 15958f0484fSRodney W. Grimes done: 16058f0484fSRodney W. Grimes #ifdef KERNEL 16158f0484fSRodney W. Grimes MCOUNT_EXIT; 16258f0484fSRodney W. Grimes #else 16358f0484fSRodney W. Grimes p->state = GMON_PROF_ON; 16458f0484fSRodney W. Grimes #endif 16558f0484fSRodney W. Grimes return; 16658f0484fSRodney W. Grimes overflow: 16758f0484fSRodney W. Grimes p->state = GMON_PROF_ERROR; 16858f0484fSRodney W. Grimes #ifdef KERNEL 16958f0484fSRodney W. Grimes MCOUNT_EXIT; 17058f0484fSRodney W. Grimes #endif 17158f0484fSRodney W. Grimes return; 17258f0484fSRodney W. Grimes } 17358f0484fSRodney W. Grimes 17458f0484fSRodney W. Grimes /* 17558f0484fSRodney W. Grimes * Actual definition of mcount function. Defined in <machine/profile.h>, 17658f0484fSRodney W. Grimes * which is included by <sys/gmon.h>. 17758f0484fSRodney W. Grimes */ 17858f0484fSRodney W. Grimes MCOUNT 179