| 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 rcsid[] = "$OpenBSD: mcount.c,v 1.6 1997/07/23 21:11:27 kstailey Exp $"; |
| 32 |
#endif |
| 33 |
|
| 34 |
/* |
| 35 |
* This file is taken from Cygwin distribution. Please keep it in sync. |
| 36 |
* The differences should be within __MINGW32__ guard. |
| 37 |
*/ |
| 38 |
|
| 39 |
#ifndef __MINGW32__ |
| 40 |
#include <sys/param.h> |
| 41 |
#endif |
| 42 |
#include <sys/types.h> |
| 43 |
#include "gmon.h" |
| 44 |
|
| 45 |
/* |
| 46 |
* mcount is called on entry to each function compiled with the profiling |
| 47 |
* switch set. _mcount(), which is declared in a machine-dependent way |
| 48 |
* with _MCOUNT_DECL, does the actual work and is either inlined into a |
| 49 |
* C routine or called by an assembly stub. In any case, this magic is |
| 50 |
* taken care of by the MCOUNT definition in <machine/profile.h>. |
| 51 |
* |
| 52 |
* _mcount updates data structures that represent traversals of the |
| 53 |
* program's call graph edges. frompc and selfpc are the return |
| 54 |
* address and function address that represents the given call graph edge. |
| 55 |
* |
| 56 |
* Note: the original BSD code used the same variable (frompcindex) for |
| 57 |
* both frompcindex and frompc. Any reasonable, modern compiler will |
| 58 |
* perform this optimization. |
| 59 |
*/ |
| 60 |
/* _mcount; may be static, inline, etc */ |
| 61 |
_MCOUNT_DECL (size_t, size_t); |
| 62 |
|
| 63 |
_MCOUNT_DECL (size_t frompc, size_t selfpc) |
| 64 |
{ |
| 65 |
register u_short *frompcindex; |
| 66 |
register struct tostruct *top, *prevtop; |
| 67 |
register struct gmonparam *p; |
| 68 |
register long toindex; |
| 69 |
|
| 70 |
p = &_gmonparam; |
| 71 |
/* |
| 72 |
* check that we are profiling |
| 73 |
* and that we aren't recursively invoked. |
| 74 |
*/ |
| 75 |
if (p->state != GMON_PROF_ON) |
| 76 |
return; |
| 77 |
p->state = GMON_PROF_BUSY; |
| 78 |
/* |
| 79 |
* check that frompcindex is a reasonable pc value. |
| 80 |
* for example: signal catchers get called from the stack, |
| 81 |
* not from text space. too bad. |
| 82 |
*/ |
| 83 |
frompc -= p->lowpc; |
| 84 |
if (frompc > p->textsize) |
| 85 |
goto done; |
| 86 |
|
| 87 |
#if (HASHFRACTION & (HASHFRACTION - 1)) == 0 |
| 88 |
if (p->hashfraction == HASHFRACTION) |
| 89 |
frompcindex = |
| 90 |
&p->froms[frompc / (HASHFRACTION * sizeof(*p->froms))]; |
| 91 |
else |
| 92 |
#endif |
| 93 |
frompcindex = |
| 94 |
&p->froms[frompc / (p->hashfraction * sizeof(*p->froms))]; |
| 95 |
toindex = *frompcindex; |
| 96 |
if (toindex == 0) { |
| 97 |
/* |
| 98 |
* first time traversing this arc |
| 99 |
*/ |
| 100 |
toindex = ++p->tos[0].link; |
| 101 |
if (toindex >= p->tolimit) |
| 102 |
/* halt further profiling */ |
| 103 |
goto overflow; |
| 104 |
|
| 105 |
*frompcindex = toindex; |
| 106 |
top = &p->tos[toindex]; |
| 107 |
top->selfpc = selfpc; |
| 108 |
top->count = 1; |
| 109 |
top->link = 0; |
| 110 |
goto done; |
| 111 |
} |
| 112 |
top = &p->tos[toindex]; |
| 113 |
if (top->selfpc == selfpc) { |
| 114 |
/* |
| 115 |
* arc at front of chain; usual case. |
| 116 |
*/ |
| 117 |
top->count++; |
| 118 |
goto done; |
| 119 |
} |
| 120 |
/* |
| 121 |
* have to go looking down chain for it. |
| 122 |
* top points to what we are looking at, |
| 123 |
* prevtop points to previous top. |
| 124 |
* we know it is not at the head of the chain. |
| 125 |
*/ |
| 126 |
for (; /* goto done */; ) { |
| 127 |
if (top->link == 0) { |
| 128 |
/* |
| 129 |
* top is end of the chain and none of the chain |
| 130 |
* had top->selfpc == selfpc. |
| 131 |
* so we allocate a new tostruct |
| 132 |
* and link it to the head of the chain. |
| 133 |
*/ |
| 134 |
toindex = ++p->tos[0].link; |
| 135 |
if (toindex >= p->tolimit) |
| 136 |
goto overflow; |
| 137 |
|
| 138 |
top = &p->tos[toindex]; |
| 139 |
top->selfpc = selfpc; |
| 140 |
top->count = 1; |
| 141 |
top->link = *frompcindex; |
| 142 |
*frompcindex = toindex; |
| 143 |
goto done; |
| 144 |
} |
| 145 |
/* |
| 146 |
* otherwise, check the next arc on the chain. |
| 147 |
*/ |
| 148 |
prevtop = top; |
| 149 |
top = &p->tos[top->link]; |
| 150 |
if (top->selfpc == selfpc) { |
| 151 |
/* |
| 152 |
* there it is. |
| 153 |
* increment its count |
| 154 |
* move it to the head of the chain. |
| 155 |
*/ |
| 156 |
top->count++; |
| 157 |
toindex = prevtop->link; |
| 158 |
prevtop->link = top->link; |
| 159 |
top->link = *frompcindex; |
| 160 |
*frompcindex = toindex; |
| 161 |
goto done; |
| 162 |
} |
| 163 |
} |
| 164 |
done: |
| 165 |
p->state = GMON_PROF_ON; |
| 166 |
return; |
| 167 |
overflow: |
| 168 |
p->state = GMON_PROF_ERROR; |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* Actual definition of mcount function. Defined in <machine/profile.h>, |
| 174 |
* which is included by <sys/gmon.h> |
| 175 |
*/ |
| 176 |
MCOUNT |
| 177 |
|