| 1 | /*- | 
|---|
| 2 | * Copyright (c) 1983, 1992, 1993 | 
|---|
| 3 | *	The Regents of the University of California.  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 | * 4. Neither the name of the University nor the names of its contributors | 
|---|
| 14 | *    may be used to endorse or promote products derived from this software | 
|---|
| 15 | *    without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 
|---|
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
| 20 | * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 
|---|
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
|---|
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
|---|
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
|---|
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
|---|
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
|---|
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|---|
| 27 | * SUCH DAMAGE. | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | #if !defined(lint) && !defined(KERNEL) && defined(LIBC_SCCS) | 
|---|
| 31 | static char sccsid[] = "@(#)mcount.c	8.1 (Berkeley) 6/4/93"; | 
|---|
| 32 | #endif | 
|---|
| 33 |  | 
|---|
| 34 | #include <unistd.h> | 
|---|
| 35 | #include <sys/param.h> | 
|---|
| 36 | #include <sys/gmon.h> | 
|---|
| 37 |  | 
|---|
| 38 | /* This file provides the machine-dependent definitions of the _MCOUNT_DECL | 
|---|
| 39 | and MCOUNT macros.  */ | 
|---|
| 40 | #include <machine-gmon.h> | 
|---|
| 41 |  | 
|---|
| 42 | #include <atomic.h> | 
|---|
| 43 |  | 
|---|
| 44 | /* | 
|---|
| 45 | * mcount is called on entry to each function compiled with the profiling | 
|---|
| 46 | * switch set.  _mcount(), which is declared in a machine-dependent way | 
|---|
| 47 | * with _MCOUNT_DECL, does the actual work and is either inlined into a | 
|---|
| 48 | * C routine or called by an assembly stub.  In any case, this magic is | 
|---|
| 49 | * taken care of by the MCOUNT definition in <machine/profile.h>. | 
|---|
| 50 | * | 
|---|
| 51 | * _mcount updates data structures that represent traversals of the | 
|---|
| 52 | * program's call graph edges.  frompc and selfpc are the return | 
|---|
| 53 | * address and function address that represents the given call graph edge. | 
|---|
| 54 | * | 
|---|
| 55 | * Note: the original BSD code used the same variable (frompcindex) for | 
|---|
| 56 | * both frompcindex and frompc.  Any reasonable, modern compiler will | 
|---|
| 57 | * perform this optimization. | 
|---|
| 58 | */ | 
|---|
| 59 | _MCOUNT_DECL(frompc, selfpc)	/* _mcount; may be static, inline, etc */ | 
|---|
| 60 | { | 
|---|
| 61 | ARCINDEX *frompcindex; | 
|---|
| 62 | struct tostruct *top, *prevtop; | 
|---|
| 63 | struct gmonparam *p; | 
|---|
| 64 | ARCINDEX toindex; | 
|---|
| 65 | int i; | 
|---|
| 66 |  | 
|---|
| 67 | p = &_gmonparam; | 
|---|
| 68 | /* | 
|---|
| 69 | * check that we are profiling | 
|---|
| 70 | * and that we aren't recursively invoked. | 
|---|
| 71 | */ | 
|---|
| 72 | if (atomic_compare_and_exchange_bool_acq (&p->state, GMON_PROF_BUSY, | 
|---|
| 73 | GMON_PROF_ON)) | 
|---|
| 74 | return; | 
|---|
| 75 |  | 
|---|
| 76 | /* | 
|---|
| 77 | * check that frompcindex is a reasonable pc value. | 
|---|
| 78 | * for example:	signal catchers get called from the stack, | 
|---|
| 79 | *		not from text space.  too bad. | 
|---|
| 80 | */ | 
|---|
| 81 | frompc -= p->lowpc; | 
|---|
| 82 | if (frompc > p->textsize) | 
|---|
| 83 | goto done; | 
|---|
| 84 |  | 
|---|
| 85 | /* The following test used to be | 
|---|
| 86 | if (p->log_hashfraction >= 0) | 
|---|
| 87 | But we can simplify this if we assume the profiling data | 
|---|
| 88 | is always initialized by the functions in gmon.c.  But | 
|---|
| 89 | then it is possible to avoid a runtime check and use the | 
|---|
| 90 | smae `if' as in gmon.c.  So keep these tests in sync.  */ | 
|---|
| 91 | if ((HASHFRACTION & (HASHFRACTION - 1)) == 0) { | 
|---|
| 92 | /* avoid integer divide if possible: */ | 
|---|
| 93 | i = frompc >> p->log_hashfraction; | 
|---|
| 94 | } else { | 
|---|
| 95 | i = frompc / (p->hashfraction * sizeof(*p->froms)); | 
|---|
| 96 | } | 
|---|
| 97 | frompcindex = &p->froms[i]; | 
|---|
| 98 | toindex = *frompcindex; | 
|---|
| 99 | if (toindex == 0) { | 
|---|
| 100 | /* | 
|---|
| 101 | *	first time traversing this arc | 
|---|
| 102 | */ | 
|---|
| 103 | toindex = ++p->tos[0].link; | 
|---|
| 104 | if (toindex >= p->tolimit) | 
|---|
| 105 | /* halt further profiling */ | 
|---|
| 106 | goto overflow; | 
|---|
| 107 |  | 
|---|
| 108 | *frompcindex = toindex; | 
|---|
| 109 | top = &p->tos[toindex]; | 
|---|
| 110 | top->selfpc = selfpc; | 
|---|
| 111 | top->count = 1; | 
|---|
| 112 | top->link = 0; | 
|---|
| 113 | goto done; | 
|---|
| 114 | } | 
|---|
| 115 | top = &p->tos[toindex]; | 
|---|
| 116 | if (top->selfpc == selfpc) { | 
|---|
| 117 | /* | 
|---|
| 118 | * arc at front of chain; usual case. | 
|---|
| 119 | */ | 
|---|
| 120 | top->count++; | 
|---|
| 121 | goto done; | 
|---|
| 122 | } | 
|---|
| 123 | /* | 
|---|
| 124 | * have to go looking down chain for it. | 
|---|
| 125 | * top points to what we are looking at, | 
|---|
| 126 | * prevtop points to previous top. | 
|---|
| 127 | * we know it is not at the head of the chain. | 
|---|
| 128 | */ | 
|---|
| 129 | for (; /* goto done */; ) { | 
|---|
| 130 | if (top->link == 0) { | 
|---|
| 131 | /* | 
|---|
| 132 | * top is end of the chain and none of the chain | 
|---|
| 133 | * had top->selfpc == selfpc. | 
|---|
| 134 | * so we allocate a new tostruct | 
|---|
| 135 | * and link it to the head of the chain. | 
|---|
| 136 | */ | 
|---|
| 137 | toindex = ++p->tos[0].link; | 
|---|
| 138 | if (toindex >= p->tolimit) | 
|---|
| 139 | goto overflow; | 
|---|
| 140 |  | 
|---|
| 141 | top = &p->tos[toindex]; | 
|---|
| 142 | top->selfpc = selfpc; | 
|---|
| 143 | top->count = 1; | 
|---|
| 144 | top->link = *frompcindex; | 
|---|
| 145 | *frompcindex = toindex; | 
|---|
| 146 | goto done; | 
|---|
| 147 | } | 
|---|
| 148 | /* | 
|---|
| 149 | * otherwise, check the next arc on the chain. | 
|---|
| 150 | */ | 
|---|
| 151 | prevtop = top; | 
|---|
| 152 | top = &p->tos[top->link]; | 
|---|
| 153 | if (top->selfpc == selfpc) { | 
|---|
| 154 | /* | 
|---|
| 155 | * there it is. | 
|---|
| 156 | * increment its count | 
|---|
| 157 | * move it to the head of the chain. | 
|---|
| 158 | */ | 
|---|
| 159 | top->count++; | 
|---|
| 160 | toindex = prevtop->link; | 
|---|
| 161 | prevtop->link = top->link; | 
|---|
| 162 | top->link = *frompcindex; | 
|---|
| 163 | *frompcindex = toindex; | 
|---|
| 164 | goto done; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | } | 
|---|
| 168 | done: | 
|---|
| 169 | p->state = GMON_PROF_ON; | 
|---|
| 170 | return; | 
|---|
| 171 | overflow: | 
|---|
| 172 | p->state = GMON_PROF_ERROR; | 
|---|
| 173 | return; | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | /* | 
|---|
| 177 | * Actual definition of mcount function.  Defined in <machine/profile.h>, | 
|---|
| 178 | * which is included by <sys/gmon.h>. | 
|---|
| 179 | */ | 
|---|
| 180 | MCOUNT | 
|---|
| 181 |  | 
|---|