123380Smckusick /* 229100Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 323380Smckusick * All rights reserved. The Berkeley software License Agreement 423380Smckusick * specifies the terms and conditions for redistribution. 523380Smckusick * 6*31433Smckusick * @(#)subr_prof.c 7.3 (Berkeley) 06/06/87 723380Smckusick */ 87332Ssam 97332Ssam #ifdef GPROF 1017094Sbloom #include "gprof.h" 1117094Sbloom #include "param.h" 1217094Sbloom #include "systm.h" 13*31433Smckusick #include "malloc.h" 147332Ssam 157332Ssam /* 167332Ssam * Froms is actually a bunch of unsigned shorts indexing tos 177332Ssam */ 1829946Skarels int profiling = 3; 1929946Skarels u_short *froms; 2029946Skarels struct tostruct *tos = 0; 2129946Skarels long tolimit = 0; 2229946Skarels #if defined(vax) 237332Ssam char *s_lowpc = (char *)0x80000000; 249758Ssam #endif 2529946Skarels #if defined(tahoe) 2629946Skarels char *s_lowpc = (char *)0xc0000000; 2729946Skarels #endif 2829946Skarels extern char etext; 2929946Skarels char *s_highpc = &etext; 307332Ssam u_long s_textsize = 0; 3129946Skarels int ssiz; 327332Ssam u_short *sbuf; 337332Ssam u_short *kcount; 347332Ssam 357332Ssam kmstartup() 367332Ssam { 3729946Skarels u_long fromssize, tossize; 387332Ssam 3910292Smckusick /* 4029946Skarels * Round lowpc and highpc to multiples of the density we're using 4129946Skarels * so the rest of the scaling (here and in gprof) stays in ints. 4210292Smckusick */ 4310292Smckusick s_lowpc = (char *) 4429946Skarels ROUNDDOWN((unsigned)s_lowpc, HISTFRACTION*sizeof (HISTCOUNTER)); 4510292Smckusick s_highpc = (char *) 4629946Skarels ROUNDUP((unsigned)s_highpc, HISTFRACTION*sizeof (HISTCOUNTER)); 477332Ssam s_textsize = s_highpc - s_lowpc; 487332Ssam printf("Profiling kernel, s_textsize=%d [%x..%x]\n", 497332Ssam s_textsize, s_lowpc, s_highpc); 5029946Skarels ssiz = (s_textsize / HISTFRACTION) + sizeof (struct phdr); 51*31433Smckusick sbuf = (u_short *)malloc(ssiz, M_GPROF, M_WAITOK); 527332Ssam if (sbuf == 0) { 537332Ssam printf("No space for monitor buffer(s)\n"); 547332Ssam return; 557332Ssam } 5610292Smckusick fromssize = s_textsize / HASHFRACTION; 57*31433Smckusick froms = (u_short *)malloc(fromssize, M_GPROF, M_WAITOK); 587332Ssam if (froms == 0) { 597332Ssam printf("No space for monitor buffer(s)\n"); 60*31433Smckusick free(sbuf, M_GPROF); 617332Ssam sbuf = 0; 627332Ssam return; 637332Ssam } 6410292Smckusick tolimit = s_textsize * ARCDENSITY / 100; 6529946Skarels if (tolimit < MINARCS) 6610292Smckusick tolimit = MINARCS; 6729946Skarels else if (tolimit > (0xffff - 1)) 6829946Skarels tolimit = 0xffff - 1; 6929946Skarels tossize = tolimit * sizeof (struct tostruct); 70*31433Smckusick tos = (struct tostruct *)malloc(tossize, M_GPROF, M_WAITOK); 717332Ssam if (tos == 0) { 727332Ssam printf("No space for monitor buffer(s)\n"); 73*31433Smckusick free(sbuf, M_GPROF), sbuf = 0; 74*31433Smckusick free(froms, M_GPROF), froms = 0; 757332Ssam return; 767332Ssam } 777332Ssam tos[0].link = 0; 787332Ssam ((struct phdr *)sbuf)->lpc = s_lowpc; 797332Ssam ((struct phdr *)sbuf)->hpc = s_highpc; 807332Ssam ((struct phdr *)sbuf)->ncnt = ssiz; 8129946Skarels kcount = (u_short *)(((int)sbuf) + sizeof (struct phdr)); 827332Ssam #ifdef notdef 8310292Smckusick /* 8429946Skarels * Profiling is what mcount checks to see if 8529946Skarels * all the data structures are ready!!! 8610292Smckusick */ 877332Ssam profiling = 0; /* patch by hand when you're ready */ 887332Ssam #endif 897332Ssam } 907332Ssam 917332Ssam /* 9229946Skarels * This routine is massaged so that it may be jsb'ed to on vax. 937332Ssam */ 9410292Smckusick asm(".text"); 9510292Smckusick asm("#the beginning of mcount()"); 9610292Smckusick asm(".data"); 977332Ssam mcount() 987332Ssam { 9929946Skarels register char *selfpc; /* r11 => r5 */ 10029946Skarels register u_short *frompcindex; /* r10 => r4 */ 10129946Skarels register struct tostruct *top; /* r9 => r3 */ 10229946Skarels register struct tostruct *prevtop; /* r8 => r2 */ 10329946Skarels register long toindex; /* r7 => r1 */ 10416924Smckusick static int s; 1057332Ssam 10629946Skarels asm(" .text"); /* make sure we're in text space */ 10729946Skarels /* 10829946Skarels * Check that we are profiling. 10929946Skarels */ 11029946Skarels if (profiling) 11129946Skarels goto out; 11229946Skarels /* 11329946Skarels * Find the return address for mcount, 11429946Skarels * and the return address for mcount's caller. 11529946Skarels */ 1167332Ssam #ifdef lint 11710292Smckusick selfpc = (char *)0; 1187332Ssam frompcindex = 0; 11929946Skarels #else 12029946Skarels #if defined(vax) 1217332Ssam asm(" movl (sp), r11"); /* selfpc = ... (jsb frame) */ 1227332Ssam asm(" movl 16(fp), r10"); /* frompcindex = (calls frame) */ 12329946Skarels #endif 12429946Skarels #if defined(tahoe) 12529946Skarels ; /* avoid label botch */ 12629946Skarels asm(" movl -8(fp),r12"); /* selfpc = callf frame */ 12729946Skarels asm(" movl (fp),r11"); 12829946Skarels asm(" movl -8(r11),r11"); /* frompcindex = 1 callf frame back */ 12929946Skarels #endif 13029946Skarels #endif 1317332Ssam /* 13229946Skarels * Insure that we cannot be recursively invoked. 13329946Skarels * this requires that splhigh() and splx() below 13429946Skarels * do NOT call mcount! 1357332Ssam */ 13616924Smckusick s = splhigh(); 13716924Smckusick /* 13829946Skarels * Check that frompcindex is a reasonable pc value. 13929946Skarels * For example: signal catchers get called from the stack, 14029946Skarels * not from text space. too bad. 1417332Ssam */ 14229946Skarels frompcindex = (u_short *)((long)frompcindex - (long)s_lowpc); 14329946Skarels if ((u_long)frompcindex > s_textsize) 1447332Ssam goto done; 14510292Smckusick frompcindex = 14629946Skarels &froms[((long)frompcindex) / (HASHFRACTION * sizeof (*froms))]; 14710292Smckusick toindex = *frompcindex; 14810292Smckusick if (toindex == 0) { 14910292Smckusick /* 15029946Skarels * First time traversing this arc 15110292Smckusick */ 15210292Smckusick toindex = ++tos[0].link; 15329946Skarels if (toindex >= tolimit) 1547332Ssam goto overflow; 15510292Smckusick *frompcindex = toindex; 15610292Smckusick top = &tos[toindex]; 1577332Ssam top->selfpc = selfpc; 15810292Smckusick top->count = 1; 1597332Ssam top->link = 0; 16010292Smckusick goto done; 1617332Ssam } 16210292Smckusick top = &tos[toindex]; 16310292Smckusick if (top->selfpc == selfpc) { 16410292Smckusick /* 16529946Skarels * Arc at front of chain; usual case. 16610292Smckusick */ 16710292Smckusick top->count++; 16810292Smckusick goto done; 16910292Smckusick } 17010292Smckusick /* 17129946Skarels * Have to go looking down chain for it. 17229946Skarels * Top points to what we are looking at, 17329946Skarels * prevtop points to previous top. 17429946Skarels * We know it is not at the head of the chain. 17510292Smckusick */ 17610292Smckusick for (; /* goto done */; ) { 17710292Smckusick if (top->link == 0) { 17810292Smckusick /* 17929946Skarels * Top is end of the chain and none of the chain 18029946Skarels * had top->selfpc == selfpc. 18129946Skarels * So we allocate a new tostruct 18229946Skarels * and link it to the head of the chain. 18310292Smckusick */ 18410292Smckusick toindex = ++tos[0].link; 18529946Skarels if (toindex >= tolimit) 18610292Smckusick goto overflow; 18710292Smckusick top = &tos[toindex]; 18810292Smckusick top->selfpc = selfpc; 18910292Smckusick top->count = 1; 19010292Smckusick top->link = *frompcindex; 19110292Smckusick *frompcindex = toindex; 19210292Smckusick goto done; 19310292Smckusick } 19410292Smckusick /* 19529946Skarels * Otherwise, check the next arc on the chain. 19610292Smckusick */ 19710292Smckusick prevtop = top; 19810292Smckusick top = &tos[top->link]; 1997332Ssam if (top->selfpc == selfpc) { 20010292Smckusick /* 20129946Skarels * There it is, increment its count and 20229946Skarels * move it to the head of the chain. 20310292Smckusick */ 2047332Ssam top->count++; 20510292Smckusick toindex = prevtop->link; 20610292Smckusick prevtop->link = top->link; 20710292Smckusick top->link = *frompcindex; 20810292Smckusick *frompcindex = toindex; 20910292Smckusick goto done; 2107332Ssam } 21110292Smckusick 2127332Ssam } 2137332Ssam done: 21416924Smckusick splx(s); 2157332Ssam /* and fall through */ 2167332Ssam out: 21729946Skarels #if defined(vax) 2187332Ssam asm(" rsb"); 21929946Skarels #endif 22029946Skarels return; 2217332Ssam overflow: 22210292Smckusick profiling = 3; 2237332Ssam printf("mcount: tos overflow\n"); 2247332Ssam goto out; 2257332Ssam } 22610292Smckusick asm(".text"); 22710292Smckusick asm("#the end of mcount()"); 22810292Smckusick asm(".data"); 22929946Skarels #endif 230