2 * Copyright (c) 1983, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $";
35 * This file is taken from Cygwin distribution. Please keep it in sync.
36 * The differences should be within __MINGW32__ guard.
40 #include <sys/param.h>
42 #include <sys/types.h>
47 * mcount is called on entry to each function compiled with the profiling
48 * switch set. _mcount(), which is declared in a machine-dependent way
49 * with _MCOUNT_DECL, does the actual work and is either inlined into a
50 * C routine or called by an assembly stub. In any case, this magic is
51 * taken care of by the MCOUNT definition in <machine/profile.h>.
53 * _mcount updates data structures that represent traversals of the
54 * program's call graph edges. frompc and selfpc are the return
55 * address and function address that represents the given call graph edge.
57 * Note: the original BSD code used the same variable (frompcindex) for
58 * both frompcindex and frompc. Any reasonable, modern compiler will
59 * perform this optimization.
61 /* _mcount; may be static, inline, etc */
62 _MCOUNT_DECL (size_t, size_t);
64 _MCOUNT_DECL (size_t frompc
, size_t selfpc
)
66 register u_int16_t
*frompcindex
;
67 register struct tostruct
*top
, *prevtop
;
68 register struct gmonparam
*p
;
69 register long toindex
;
73 * check that we are profiling
74 * and that we aren't recursively invoked by this thread
75 * or entered anew by any other thread.
77 if (InterlockedCompareExchange (
78 &p
->state
, GMON_PROF_BUSY
, GMON_PROF_ON
) != GMON_PROF_ON
)
81 * check that frompcindex is a reasonable pc value.
82 * for example: signal catchers get called from the stack,
83 * not from text space. too bad.
86 if (frompc
> p
->textsize
)
89 #if (HASHFRACTION & (HASHFRACTION - 1)) == 0
90 if (p
->hashfraction
== HASHFRACTION
)
92 &p
->froms
[frompc
/ (HASHFRACTION
* sizeof(*p
->froms
))];
96 &p
->froms
[frompc
/ (p
->hashfraction
* sizeof(*p
->froms
))];
97 toindex
= *frompcindex
;
100 * first time traversing this arc
102 toindex
= ++p
->tos
[0].link
;
103 if (toindex
>= p
->tolimit
)
104 /* halt further profiling */
107 *frompcindex
= toindex
;
108 top
= &p
->tos
[toindex
];
109 top
->selfpc
= selfpc
;
114 top
= &p
->tos
[toindex
];
115 if (top
->selfpc
== selfpc
) {
117 * arc at front of chain; usual case.
123 * have to go looking down chain for it.
124 * top points to what we are looking at,
125 * prevtop points to previous top.
126 * we know it is not at the head of the chain.
128 for (; /* goto done */; ) {
129 if (top
->link
== 0) {
131 * top is end of the chain and none of the chain
132 * had top->selfpc == selfpc.
133 * so we allocate a new tostruct
134 * and link it to the head of the chain.
136 toindex
= ++p
->tos
[0].link
;
137 if (toindex
>= p
->tolimit
)
140 top
= &p
->tos
[toindex
];
141 top
->selfpc
= selfpc
;
143 top
->link
= *frompcindex
;
144 *frompcindex
= toindex
;
148 * otherwise, check the next arc on the chain.
151 top
= &p
->tos
[top
->link
];
152 if (top
->selfpc
== selfpc
) {
155 * increment its count
156 * move it to the head of the chain.
159 toindex
= prevtop
->link
;
160 prevtop
->link
= top
->link
;
161 top
->link
= *frompcindex
;
162 *frompcindex
= toindex
;
167 InterlockedExchange (&p
->state
, GMON_PROF_ON
);
170 InterlockedExchange (&p
->state
, GMON_PROF_ERROR
);
175 * Actual definition of mcount function. Defined in <machine/profile.h>,
176 * which is included by <sys/gmon.h>