1 /* $NetBSD: mcount.c,v 1.17 2021/08/30 12:52:32 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2003, 2004 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Nathan J. Williams for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /*- 39 * Copyright (c) 1983, 1992, 1993 40 * The Regents of the University of California. All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 */ 66 67 #ifdef _KERNEL_OPT 68 #include "opt_multiprocessor.h" 69 #endif 70 71 /* If building a standalone libkern, don't include mcount. */ 72 #if (!defined(_KERNEL) || defined(GPROF)) && !defined(_STANDALONE) 73 74 #include <sys/cdefs.h> 75 #if !defined(lint) && !defined(_KERNEL) && defined(LIBC_SCCS) 76 #if 0 77 static char sccsid[] = "@(#)mcount.c 8.1 (Berkeley) 6/4/93"; 78 #else 79 __RCSID("$NetBSD: mcount.c,v 1.17 2021/08/30 12:52:32 christos Exp $"); 80 #endif 81 #endif 82 83 #include <sys/param.h> 84 #include <sys/gmon.h> 85 #include <sys/lock.h> 86 #include <sys/proc.h> 87 88 #ifndef _KERNEL 89 #include "reentrant.h" 90 #endif 91 92 #if defined(_REENTRANT) && !defined(_RUMPKERNEL) 93 extern thread_key_t _gmonkey; 94 extern struct gmonparam _gmondummy; 95 struct gmonparam *_m_gmon_alloc(void); 96 #endif 97 98 _MCOUNT_DECL(u_long, u_long) 99 #ifdef _KERNEL 100 __attribute__((__no_instrument_function__)) 101 #endif 102 __used; 103 104 /* XXX: make these interfaces */ 105 #ifdef _RUMPKERNEL 106 #undef MCOUNT_ENTER 107 #define MCOUNT_ENTER 108 #undef MCOUNT_EXIT 109 #define MCOUNT_EXIT 110 #undef MCOUNT 111 #define MCOUNT 112 #endif 113 114 /* 115 * mcount is called on entry to each function compiled with the profiling 116 * switch set. _mcount(), which is declared in a machine-dependent way 117 * with _MCOUNT_DECL, does the actual work and is either inlined into a 118 * C routine or called by an assembly stub. In any case, this magic is 119 * taken care of by the MCOUNT definition in <machine/profile.h>. 120 * 121 * _mcount updates data structures that represent traversals of the 122 * program's call graph edges. frompc and selfpc are the return 123 * address and function address that represents the given call graph edge. 124 * 125 * Note: the original BSD code used the same variable (frompcindex) for 126 * both frompcindex and frompc. Any reasonable, modern compiler will 127 * perform this optimization. 128 */ 129 /* _mcount; may be static, inline, etc */ 130 _MCOUNT_DECL(u_long frompc, u_long selfpc) 131 { 132 u_short *frompcindex; 133 struct tostruct *top, *prevtop; 134 struct gmonparam *p; 135 long toindex; 136 #if defined(_KERNEL) && !defined(_RUMPKERNEL) 137 int s; 138 #endif 139 140 #if defined(_REENTRANT) && !defined(_KERNEL) && !defined(_RUMPKERNEL) 141 if (__isthreaded) { 142 /* prevent re-entry via thr_getspecific */ 143 if (_gmonparam.state != GMON_PROF_ON) 144 return; 145 _gmonparam.state = GMON_PROF_BUSY; 146 p = thr_getspecific(_gmonkey); 147 if (p == NULL) { 148 /* Prevent recursive calls while allocating */ 149 thr_setspecific(_gmonkey, &_gmondummy); 150 p = _m_gmon_alloc(); 151 } 152 _gmonparam.state = GMON_PROF_ON; 153 } else 154 #endif 155 p = &_gmonparam; 156 /* 157 * check that we are profiling 158 * and that we aren't recursively invoked. 159 */ 160 if (p->state != GMON_PROF_ON) 161 return; 162 #if defined(_KERNEL) && !defined(_RUMPKERNEL) 163 MCOUNT_ENTER; 164 #ifdef MULTIPROCESSOR 165 p = curcpu()->ci_gmon; 166 if (p == NULL || p->state != GMON_PROF_ON) { 167 MCOUNT_EXIT; 168 return; 169 } 170 #endif 171 #endif 172 p->state = GMON_PROF_BUSY; 173 /* 174 * check that frompcindex is a reasonable pc value. 175 * for example: signal catchers get called from the stack, 176 * not from text space. too bad. 177 */ 178 frompc -= p->lowpc; 179 if (frompc > p->textsize) 180 goto done; 181 182 #if (HASHFRACTION & (HASHFRACTION - 1)) == 0 183 if (p->hashfraction == HASHFRACTION) 184 frompcindex = 185 &p->froms[ 186 (size_t)(frompc / (HASHFRACTION * sizeof(*p->froms)))]; 187 else 188 #endif 189 frompcindex = 190 &p->froms[ 191 (size_t)(frompc / (p->hashfraction * sizeof(*p->froms)))]; 192 toindex = *frompcindex; 193 if (toindex == 0) { 194 /* 195 * first time traversing this arc 196 */ 197 toindex = ++p->tos[0].link; 198 if (toindex >= p->tolimit) 199 /* halt further profiling */ 200 goto overflow; 201 202 *frompcindex = (u_short)toindex; 203 top = &p->tos[(size_t)toindex]; 204 top->selfpc = selfpc; 205 top->count = 1; 206 top->link = 0; 207 goto done; 208 } 209 top = &p->tos[(size_t)toindex]; 210 if (top->selfpc == selfpc) { 211 /* 212 * arc at front of chain; usual case. 213 */ 214 top->count++; 215 goto done; 216 } 217 /* 218 * have to go looking down chain for it. 219 * top points to what we are looking at, 220 * prevtop points to previous top. 221 * we know it is not at the head of the chain. 222 */ 223 for (; /* goto done */; ) { 224 if (top->link == 0) { 225 /* 226 * top is end of the chain and none of the chain 227 * had top->selfpc == selfpc. 228 * so we allocate a new tostruct 229 * and link it to the head of the chain. 230 */ 231 toindex = ++p->tos[0].link; 232 if (toindex >= p->tolimit) 233 goto overflow; 234 235 top = &p->tos[(size_t)toindex]; 236 top->selfpc = selfpc; 237 top->count = 1; 238 top->link = *frompcindex; 239 *frompcindex = (u_short)toindex; 240 goto done; 241 } 242 /* 243 * otherwise, check the next arc on the chain. 244 */ 245 prevtop = top; 246 top = &p->tos[top->link]; 247 if (top->selfpc == selfpc) { 248 /* 249 * there it is. 250 * increment its count 251 * move it to the head of the chain. 252 */ 253 top->count++; 254 toindex = prevtop->link; 255 prevtop->link = top->link; 256 top->link = *frompcindex; 257 *frompcindex = (u_short)toindex; 258 goto done; 259 } 260 } 261 done: 262 p->state = GMON_PROF_ON; 263 #if defined(_KERNEL) && !defined(_RUMPKERNEL) 264 MCOUNT_EXIT; 265 #endif 266 return; 267 268 overflow: 269 p->state = GMON_PROF_ERROR; 270 #if defined(_KERNEL) && !defined(_RUMPKERNEL) 271 MCOUNT_EXIT; 272 #endif 273 return; 274 } 275 276 #ifdef MCOUNT 277 /* 278 * Actual definition of mcount function. Defined in <machine/profile.h>, 279 * which is included by <sys/gmon.h>. 280 */ 281 MCOUNT 282 #endif 283 284 #if defined(_KERNEL) && !defined(_RUMPKERNEL) && defined(MULTIPROCESSOR) 285 void _gmonparam_merge(struct gmonparam *, struct gmonparam *); 286 287 void 288 _gmonparam_merge(struct gmonparam *p, struct gmonparam *q) 289 { 290 u_long fromindex; 291 u_short *frompcindex, qtoindex, toindex; 292 u_long selfpc; 293 u_long endfrom; 294 long count; 295 struct tostruct *top; 296 int i; 297 298 count = q->kcountsize / sizeof(*q->kcount); 299 for (i = 0; i < count; i++) 300 p->kcount[i] += q->kcount[i]; 301 302 endfrom = (q->fromssize / sizeof(*q->froms)); 303 for (fromindex = 0; fromindex < endfrom; fromindex++) { 304 if (q->froms[fromindex] == 0) 305 continue; 306 for (qtoindex = q->froms[fromindex]; qtoindex != 0; 307 qtoindex = q->tos[qtoindex].link) { 308 selfpc = q->tos[qtoindex].selfpc; 309 count = q->tos[qtoindex].count; 310 /* cribbed from mcount */ 311 frompcindex = &p->froms[fromindex]; 312 toindex = *frompcindex; 313 if (toindex == 0) { 314 /* 315 * first time traversing this arc 316 */ 317 toindex = ++p->tos[0].link; 318 if (toindex >= p->tolimit) 319 /* halt further profiling */ 320 goto overflow; 321 322 *frompcindex = (u_short)toindex; 323 top = &p->tos[(size_t)toindex]; 324 top->selfpc = selfpc; 325 top->count = count; 326 top->link = 0; 327 goto done; 328 } 329 top = &p->tos[(size_t)toindex]; 330 if (top->selfpc == selfpc) { 331 /* 332 * arc at front of chain; usual case. 333 */ 334 top->count+= count; 335 goto done; 336 } 337 /* 338 * have to go looking down chain for it. 339 * top points to what we are looking at, 340 * we know it is not at the head of the chain. 341 */ 342 for (; /* goto done */; ) { 343 if (top->link == 0) { 344 /* 345 * top is end of the chain and 346 * none of the chain had 347 * top->selfpc == selfpc. so 348 * we allocate a new tostruct 349 * and link it to the head of 350 * the chain. 351 */ 352 toindex = ++p->tos[0].link; 353 if (toindex >= p->tolimit) 354 goto overflow; 355 356 top = &p->tos[(size_t)toindex]; 357 top->selfpc = selfpc; 358 top->count = count; 359 top->link = *frompcindex; 360 *frompcindex = (u_short)toindex; 361 goto done; 362 } 363 /* 364 * otherwise, check the next arc on the chain. 365 */ 366 top = &p->tos[top->link]; 367 if (top->selfpc == selfpc) { 368 /* 369 * there it is. 370 * add to its count. 371 */ 372 top->count += count; 373 goto done; 374 } 375 } 376 377 done: ; 378 } 379 380 } 381 overflow: ; 382 383 } 384 #endif 385 386 #endif /* (!_KERNEL || GPROF) && !_STANDALONE */ 387