1/*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * 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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34/* Mangled into a form that works on Sparc Solaris 2 by Mark Eichin 35 * for Cygnus Support, July 1992. 36 */ 37 38#include "config.h" 39#include "system.h" 40 41#if 0 42#include "sparc/gmon.h" 43#else 44struct phdr { 45 char *lpc; 46 char *hpc; 47 int ncnt; 48}; 49#define HISTFRACTION 2 50#define HISTCOUNTER unsigned short 51#define HASHFRACTION 1 52#define ARCDENSITY 2 53#define MINARCS 50 54struct tostruct { 55 char *selfpc; 56 long count; 57 unsigned short link; 58}; 59struct rawarc { 60 unsigned long raw_frompc; 61 unsigned long raw_selfpc; 62 long raw_count; 63}; 64#define ROUNDDOWN(x,y) (((x)/(y))*(y)) 65#define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y)) 66 67#endif 68 69/* extern mcount() asm ("mcount"); */ 70/*extern*/ char *minbrk /* asm ("minbrk") */; 71 72 /* 73 * froms is actually a bunch of unsigned shorts indexing tos 74 */ 75static int profiling = 3; 76static unsigned short *froms; 77static struct tostruct *tos = 0; 78static long tolimit = 0; 79static char *s_lowpc = 0; 80static char *s_highpc = 0; 81static unsigned long s_textsize = 0; 82 83static int ssiz; 84static char *sbuf; 85static int s_scale; 86 /* see profil(2) where this is describe (incorrectly) */ 87#define SCALE_1_TO_1 0x10000L 88 89#define MSG "No space for profiling buffer(s)\n" 90 91static void moncontrol PROTO ((int)); 92extern void monstartup PROTO ((char *, char *)); 93extern void _mcleanup PROTO ((void)); 94 95void monstartup(lowpc, highpc) 96 char *lowpc; 97 char *highpc; 98{ 99 int monsize; 100 char *buffer; 101 register int o; 102 103 /* 104 * round lowpc and highpc to multiples of the density we're using 105 * so the rest of the scaling (here and in gprof) stays in ints. 106 */ 107 lowpc = (char *) 108 ROUNDDOWN((unsigned)lowpc, HISTFRACTION*sizeof(HISTCOUNTER)); 109 s_lowpc = lowpc; 110 highpc = (char *) 111 ROUNDUP((unsigned)highpc, HISTFRACTION*sizeof(HISTCOUNTER)); 112 s_highpc = highpc; 113 s_textsize = highpc - lowpc; 114 monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr); 115 buffer = sbrk( monsize ); 116 if ( buffer == (char *) -1 ) { 117 write( 2 , MSG , sizeof(MSG) ); 118 return; 119 } 120 froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION ); 121 if ( froms == (unsigned short *) -1 ) { 122 write( 2 , MSG , sizeof(MSG) ); 123 froms = 0; 124 return; 125 } 126 tolimit = s_textsize * ARCDENSITY / 100; 127 if ( tolimit < MINARCS ) { 128 tolimit = MINARCS; 129 } else if ( tolimit > 65534 ) { 130 tolimit = 65534; 131 } 132 tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) ); 133 if ( tos == (struct tostruct *) -1 ) { 134 write( 2 , MSG , sizeof(MSG) ); 135 froms = 0; 136 tos = 0; 137 return; 138 } 139 minbrk = sbrk(0); 140 tos[0].link = 0; 141 sbuf = buffer; 142 ssiz = monsize; 143 ( (struct phdr *) buffer ) -> lpc = lowpc; 144 ( (struct phdr *) buffer ) -> hpc = highpc; 145 ( (struct phdr *) buffer ) -> ncnt = ssiz; 146 monsize -= sizeof(struct phdr); 147 if ( monsize <= 0 ) 148 return; 149 o = highpc - lowpc; 150 if( monsize < o ) 151#ifndef hp300 152 s_scale = ( (float) monsize / o ) * SCALE_1_TO_1; 153#else /* avoid floating point */ 154 { 155 int quot = o / monsize; 156 157 if (quot >= 0x10000) 158 s_scale = 1; 159 else if (quot >= 0x100) 160 s_scale = 0x10000 / quot; 161 else if (o >= 0x800000) 162 s_scale = 0x1000000 / (o / (monsize >> 8)); 163 else 164 s_scale = 0x1000000 / ((o << 8) / monsize); 165 } 166#endif 167 else 168 s_scale = SCALE_1_TO_1; 169 moncontrol(1); 170} 171 172void 173_mcleanup() 174{ 175 int fd; 176 int fromindex; 177 int endfrom; 178 char *frompc; 179 int toindex; 180 struct rawarc rawarc; 181 char *profdir; 182 const char *proffile; 183 char *progname; 184 char buf[PATH_MAX]; 185 extern char **___Argv; 186 187 moncontrol(0); 188 189 if ((profdir = getenv("PROFDIR")) != NULL) { 190 /* If PROFDIR contains a null value, no profiling output is produced */ 191 if (*profdir == '\0') { 192 return; 193 } 194 195 progname=strrchr(___Argv[0], '/'); 196 if (progname == NULL) 197 progname=___Argv[0]; 198 else 199 progname++; 200 201 sprintf(buf, "%s/%ld.%s", profdir, getpid(), progname); 202 proffile = buf; 203 } else { 204 proffile = "gmon.out"; 205 } 206 207 fd = creat( proffile, 0666 ); 208 if ( fd < 0 ) { 209 perror( proffile ); 210 return; 211 } 212# ifdef DEBUG 213 fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz ); 214# endif DEBUG 215 write( fd , sbuf , ssiz ); 216 endfrom = s_textsize / (HASHFRACTION * sizeof(*froms)); 217 for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) { 218 if ( froms[fromindex] == 0 ) { 219 continue; 220 } 221 frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms)); 222 for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) { 223# ifdef DEBUG 224 fprintf( stderr , 225 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" , 226 frompc , tos[toindex].selfpc , tos[toindex].count ); 227# endif DEBUG 228 rawarc.raw_frompc = (unsigned long) frompc; 229 rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc; 230 rawarc.raw_count = tos[toindex].count; 231 write( fd , &rawarc , sizeof rawarc ); 232 } 233 } 234 close( fd ); 235} 236 237/* 238 * The Sparc stack frame is only held together by the frame pointers 239 * in the register windows. According to the SVR4 SPARC ABI 240 * Supplement, Low Level System Information/Operating System 241 * Interface/Software Trap Types, a type 3 trap will flush all of the 242 * register windows to the stack, which will make it possible to walk 243 * the frames and find the return addresses. 244 * However, it seems awfully expensive to incur a trap (system 245 * call) for every function call. It turns out that "call" simply puts 246 * the return address in %o7 expecting the "save" in the procedure to 247 * shift it into %i7; this means that before the "save" occurs, %o7 248 * contains the address of the call to mcount, and %i7 still contains 249 * the caller above that. The asm mcount here simply saves those 250 * registers in argument registers and branches to internal_mcount, 251 * simulating a call with arguments. 252 * Kludges: 253 * 1) the branch to internal_mcount is hard coded; it should be 254 * possible to tell asm to use the assembler-name of a symbol. 255 * 2) in theory, the function calling mcount could have saved %i7 256 * somewhere and reused the register; in practice, I *think* this will 257 * break longjmp (and maybe the debugger) but I'm not certain. (I take 258 * some comfort in the knowledge that it will break the native mcount 259 * as well.) 260 * 3) if builtin_return_address worked, this could be portable. 261 * However, it would really have to be optimized for arguments of 0 262 * and 1 and do something like what we have here in order to avoid the 263 * trap per function call performance hit. 264 * 4) the atexit and monsetup calls prevent this from simply 265 * being a leaf routine that doesn't do a "save" (and would thus have 266 * access to %o7 and %i7 directly) but the call to write() at the end 267 * would have also prevented this. 268 * 269 * -- [eichin:19920702.1107EST] 270 */ 271 272static void internal_mcount PROTO((char *, unsigned short *)) ATTRIBUTE_UNUSED; 273 274/* i7 == last ret, -> frompcindex */ 275/* o7 == current ret, -> selfpc */ 276/* Solaris 2 libraries use _mcount. */ 277asm(".global _mcount; _mcount: mov %i7,%o1; mov %o7,%o0;b,a internal_mcount"); 278/* This is for compatibility with old versions of gcc which used mcount. */ 279asm(".global mcount; mcount: mov %i7,%o1; mov %o7,%o0;b,a internal_mcount"); 280 281static void internal_mcount(selfpc, frompcindex) 282 register char *selfpc; 283 register unsigned short *frompcindex; 284{ 285 register struct tostruct *top; 286 register struct tostruct *prevtop; 287 register long toindex; 288 static char already_setup; 289 290 /* 291 * find the return address for mcount, 292 * and the return address for mcount's caller. 293 */ 294 295 if(!already_setup) { 296 extern char etext[]; 297 already_setup = 1; 298 monstartup(0, (char *)etext); 299#ifdef USE_ONEXIT 300 on_exit(_mcleanup, 0); 301#else 302 atexit(_mcleanup); 303#endif 304 } 305 /* 306 * check that we are profiling 307 * and that we aren't recursively invoked. 308 */ 309 if (profiling) { 310 goto out; 311 } 312 profiling++; 313 /* 314 * check that frompcindex is a reasonable pc value. 315 * for example: signal catchers get called from the stack, 316 * not from text space. too bad. 317 */ 318 frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc); 319 if ((unsigned long)frompcindex > s_textsize) { 320 goto done; 321 } 322 frompcindex = 323 &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))]; 324 toindex = *frompcindex; 325 if (toindex == 0) { 326 /* 327 * first time traversing this arc 328 */ 329 toindex = ++tos[0].link; 330 if (toindex >= tolimit) { 331 goto overflow; 332 } 333 *frompcindex = toindex; 334 top = &tos[toindex]; 335 top->selfpc = selfpc; 336 top->count = 1; 337 top->link = 0; 338 goto done; 339 } 340 top = &tos[toindex]; 341 if (top->selfpc == selfpc) { 342 /* 343 * arc at front of chain; usual case. 344 */ 345 top->count++; 346 goto done; 347 } 348 /* 349 * have to go looking down chain for it. 350 * top points to what we are looking at, 351 * prevtop points to previous top. 352 * we know it is not at the head of the chain. 353 */ 354 for (; /* goto done */; ) { 355 if (top->link == 0) { 356 /* 357 * top is end of the chain and none of the chain 358 * had top->selfpc == selfpc. 359 * so we allocate a new tostruct 360 * and link it to the head of the chain. 361 */ 362 toindex = ++tos[0].link; 363 if (toindex >= tolimit) { 364 goto overflow; 365 } 366 top = &tos[toindex]; 367 top->selfpc = selfpc; 368 top->count = 1; 369 top->link = *frompcindex; 370 *frompcindex = toindex; 371 goto done; 372 } 373 /* 374 * otherwise, check the next arc on the chain. 375 */ 376 prevtop = top; 377 top = &tos[top->link]; 378 if (top->selfpc == selfpc) { 379 /* 380 * there it is. 381 * increment its count 382 * move it to the head of the chain. 383 */ 384 top->count++; 385 toindex = prevtop->link; 386 prevtop->link = top->link; 387 top->link = *frompcindex; 388 *frompcindex = toindex; 389 goto done; 390 } 391 392 } 393done: 394 profiling--; 395 /* and fall through */ 396out: 397 return; /* normal return restores saved registers */ 398 399overflow: 400 profiling++; /* halt further profiling */ 401# define TOLIMIT "mcount: tos overflow\n" 402 write(2, TOLIMIT, sizeof(TOLIMIT)); 403 goto out; 404} 405 406/* 407 * Control profiling 408 * profiling is what mcount checks to see if 409 * all the data structures are ready. 410 */ 411static void moncontrol(mode) 412 int mode; 413{ 414 if (mode) { 415 /* start */ 416 profil((unsigned short *)(sbuf + sizeof(struct phdr)), 417 ssiz - sizeof(struct phdr), 418 (int)s_lowpc, s_scale); 419 profiling = 0; 420 } else { 421 /* stop */ 422 profil((unsigned short *)0, 0, 0, 0); 423 profiling = 3; 424 } 425} 426