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> 40691071ffSPaul Richards #ifdef KERNEL 41691071ffSPaul Richards #include <i386/include/cpufunc.h> 42691071ffSPaul Richards #endif 4358f0484fSRodney W. Grimes 4458f0484fSRodney W. Grimes /* 4558f0484fSRodney W. Grimes * mcount is called on entry to each function compiled with the profiling 4658f0484fSRodney W. Grimes * switch set. _mcount(), which is declared in a machine-dependent way 4758f0484fSRodney W. Grimes * with _MCOUNT_DECL, does the actual work and is either inlined into a 4858f0484fSRodney W. Grimes * C routine or called by an assembly stub. In any case, this magic is 4958f0484fSRodney W. Grimes * taken care of by the MCOUNT definition in <machine/profile.h>. 5058f0484fSRodney W. Grimes * 5158f0484fSRodney W. Grimes * _mcount updates data structures that represent traversals of the 5258f0484fSRodney W. Grimes * program's call graph edges. frompc and selfpc are the return 5358f0484fSRodney W. Grimes * address and function address that represents the given call graph edge. 5458f0484fSRodney W. Grimes * 5558f0484fSRodney W. Grimes * Note: the original BSD code used the same variable (frompcindex) for 5658f0484fSRodney W. Grimes * both frompcindex and frompc. Any reasonable, modern compiler will 5758f0484fSRodney W. Grimes * perform this optimization. 5858f0484fSRodney W. Grimes */ 5958f0484fSRodney W. Grimes _MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */ 6058f0484fSRodney W. Grimes register u_long frompc, selfpc; 6158f0484fSRodney W. Grimes { 6258f0484fSRodney W. Grimes register u_short *frompcindex; 6358f0484fSRodney W. Grimes register struct tostruct *top, *prevtop; 6458f0484fSRodney W. Grimes register struct gmonparam *p; 6558f0484fSRodney W. Grimes register long toindex; 6658f0484fSRodney W. Grimes #ifdef KERNEL 6758f0484fSRodney W. Grimes register int s; 68691071ffSPaul Richards u_long save_eflags; 6958f0484fSRodney W. Grimes #endif 7058f0484fSRodney W. Grimes 7158f0484fSRodney W. Grimes p = &_gmonparam; 7258f0484fSRodney W. Grimes /* 7358f0484fSRodney W. Grimes * check that we are profiling 7458f0484fSRodney W. Grimes * and that we aren't recursively invoked. 7558f0484fSRodney W. Grimes */ 7658f0484fSRodney W. Grimes if (p->state != GMON_PROF_ON) 7758f0484fSRodney W. Grimes return; 7858f0484fSRodney W. Grimes #ifdef KERNEL 7958f0484fSRodney W. Grimes MCOUNT_ENTER; 8058f0484fSRodney W. Grimes #else 8158f0484fSRodney W. Grimes p->state = GMON_PROF_BUSY; 8258f0484fSRodney W. Grimes #endif 8358f0484fSRodney W. Grimes /* 8458f0484fSRodney W. Grimes * check that frompcindex is a reasonable pc value. 8558f0484fSRodney W. Grimes * for example: signal catchers get called from the stack, 8658f0484fSRodney W. Grimes * not from text space. too bad. 8758f0484fSRodney W. Grimes */ 8858f0484fSRodney W. Grimes frompc -= p->lowpc; 8958f0484fSRodney W. Grimes if (frompc > p->textsize) 9058f0484fSRodney W. Grimes goto done; 9158f0484fSRodney W. Grimes 9258f0484fSRodney W. Grimes frompcindex = &p->froms[frompc / (p->hashfraction * sizeof(*p->froms))]; 9358f0484fSRodney W. Grimes toindex = *frompcindex; 9458f0484fSRodney W. Grimes if (toindex == 0) { 9558f0484fSRodney W. Grimes /* 9658f0484fSRodney W. Grimes * first time traversing this arc 9758f0484fSRodney W. Grimes */ 9858f0484fSRodney W. Grimes toindex = ++p->tos[0].link; 9958f0484fSRodney W. Grimes if (toindex >= p->tolimit) 10058f0484fSRodney W. Grimes /* halt further profiling */ 10158f0484fSRodney W. Grimes goto overflow; 10258f0484fSRodney W. Grimes 10358f0484fSRodney W. Grimes *frompcindex = toindex; 10458f0484fSRodney W. Grimes top = &p->tos[toindex]; 10558f0484fSRodney W. Grimes top->selfpc = selfpc; 10658f0484fSRodney W. Grimes top->count = 1; 10758f0484fSRodney W. Grimes top->link = 0; 10858f0484fSRodney W. Grimes goto done; 10958f0484fSRodney W. Grimes } 11058f0484fSRodney W. Grimes top = &p->tos[toindex]; 11158f0484fSRodney W. Grimes if (top->selfpc == selfpc) { 11258f0484fSRodney W. Grimes /* 11358f0484fSRodney W. Grimes * arc at front of chain; usual case. 11458f0484fSRodney W. Grimes */ 11558f0484fSRodney W. Grimes top->count++; 11658f0484fSRodney W. Grimes goto done; 11758f0484fSRodney W. Grimes } 11858f0484fSRodney W. Grimes /* 11958f0484fSRodney W. Grimes * have to go looking down chain for it. 12058f0484fSRodney W. Grimes * top points to what we are looking at, 12158f0484fSRodney W. Grimes * prevtop points to previous top. 12258f0484fSRodney W. Grimes * we know it is not at the head of the chain. 12358f0484fSRodney W. Grimes */ 12458f0484fSRodney W. Grimes for (; /* goto done */; ) { 12558f0484fSRodney W. Grimes if (top->link == 0) { 12658f0484fSRodney W. Grimes /* 12758f0484fSRodney W. Grimes * top is end of the chain and none of the chain 12858f0484fSRodney W. Grimes * had top->selfpc == selfpc. 12958f0484fSRodney W. Grimes * so we allocate a new tostruct 13058f0484fSRodney W. Grimes * and link it to the head of the chain. 13158f0484fSRodney W. Grimes */ 13258f0484fSRodney W. Grimes toindex = ++p->tos[0].link; 13358f0484fSRodney W. Grimes if (toindex >= p->tolimit) 13458f0484fSRodney W. Grimes goto overflow; 13558f0484fSRodney W. Grimes 13658f0484fSRodney W. Grimes top = &p->tos[toindex]; 13758f0484fSRodney W. Grimes top->selfpc = selfpc; 13858f0484fSRodney W. Grimes top->count = 1; 13958f0484fSRodney W. Grimes top->link = *frompcindex; 14058f0484fSRodney W. Grimes *frompcindex = toindex; 14158f0484fSRodney W. Grimes goto done; 14258f0484fSRodney W. Grimes } 14358f0484fSRodney W. Grimes /* 14458f0484fSRodney W. Grimes * otherwise, check the next arc on the chain. 14558f0484fSRodney W. Grimes */ 14658f0484fSRodney W. Grimes prevtop = top; 14758f0484fSRodney W. Grimes top = &p->tos[top->link]; 14858f0484fSRodney W. Grimes if (top->selfpc == selfpc) { 14958f0484fSRodney W. Grimes /* 15058f0484fSRodney W. Grimes * there it is. 15158f0484fSRodney W. Grimes * increment its count 15258f0484fSRodney W. Grimes * move it to the head of the chain. 15358f0484fSRodney W. Grimes */ 15458f0484fSRodney W. Grimes top->count++; 15558f0484fSRodney W. Grimes toindex = prevtop->link; 15658f0484fSRodney W. Grimes prevtop->link = top->link; 15758f0484fSRodney W. Grimes top->link = *frompcindex; 15858f0484fSRodney W. Grimes *frompcindex = toindex; 15958f0484fSRodney W. Grimes goto done; 16058f0484fSRodney W. Grimes } 16158f0484fSRodney W. Grimes 16258f0484fSRodney W. Grimes } 16358f0484fSRodney W. Grimes done: 16458f0484fSRodney W. Grimes #ifdef KERNEL 16558f0484fSRodney W. Grimes MCOUNT_EXIT; 16658f0484fSRodney W. Grimes #else 16758f0484fSRodney W. Grimes p->state = GMON_PROF_ON; 16858f0484fSRodney W. Grimes #endif 16958f0484fSRodney W. Grimes return; 17058f0484fSRodney W. Grimes overflow: 17158f0484fSRodney W. Grimes p->state = GMON_PROF_ERROR; 17258f0484fSRodney W. Grimes #ifdef KERNEL 17358f0484fSRodney W. Grimes MCOUNT_EXIT; 17458f0484fSRodney W. Grimes #endif 17558f0484fSRodney W. Grimes return; 17658f0484fSRodney W. Grimes } 17758f0484fSRodney W. Grimes 17858f0484fSRodney W. Grimes /* 17958f0484fSRodney W. Grimes * Actual definition of mcount function. Defined in <machine/profile.h>, 18058f0484fSRodney W. Grimes * which is included by <sys/gmon.h>. 18158f0484fSRodney W. Grimes */ 18258f0484fSRodney W. Grimes MCOUNT 183