1 /* 2 * Copyright (c) 1982, 1986 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)subr_prof.c 7.3 (Berkeley) 06/06/87 7 */ 8 9 #ifdef GPROF 10 #include "gprof.h" 11 #include "param.h" 12 #include "systm.h" 13 #include "malloc.h" 14 15 /* 16 * Froms is actually a bunch of unsigned shorts indexing tos 17 */ 18 int profiling = 3; 19 u_short *froms; 20 struct tostruct *tos = 0; 21 long tolimit = 0; 22 #if defined(vax) 23 char *s_lowpc = (char *)0x80000000; 24 #endif 25 #if defined(tahoe) 26 char *s_lowpc = (char *)0xc0000000; 27 #endif 28 extern char etext; 29 char *s_highpc = &etext; 30 u_long s_textsize = 0; 31 int ssiz; 32 u_short *sbuf; 33 u_short *kcount; 34 35 kmstartup() 36 { 37 u_long fromssize, tossize; 38 39 /* 40 * Round lowpc and highpc to multiples of the density we're using 41 * so the rest of the scaling (here and in gprof) stays in ints. 42 */ 43 s_lowpc = (char *) 44 ROUNDDOWN((unsigned)s_lowpc, HISTFRACTION*sizeof (HISTCOUNTER)); 45 s_highpc = (char *) 46 ROUNDUP((unsigned)s_highpc, HISTFRACTION*sizeof (HISTCOUNTER)); 47 s_textsize = s_highpc - s_lowpc; 48 printf("Profiling kernel, s_textsize=%d [%x..%x]\n", 49 s_textsize, s_lowpc, s_highpc); 50 ssiz = (s_textsize / HISTFRACTION) + sizeof (struct phdr); 51 sbuf = (u_short *)malloc(ssiz, M_GPROF, M_WAITOK); 52 if (sbuf == 0) { 53 printf("No space for monitor buffer(s)\n"); 54 return; 55 } 56 fromssize = s_textsize / HASHFRACTION; 57 froms = (u_short *)malloc(fromssize, M_GPROF, M_WAITOK); 58 if (froms == 0) { 59 printf("No space for monitor buffer(s)\n"); 60 free(sbuf, M_GPROF); 61 sbuf = 0; 62 return; 63 } 64 tolimit = s_textsize * ARCDENSITY / 100; 65 if (tolimit < MINARCS) 66 tolimit = MINARCS; 67 else if (tolimit > (0xffff - 1)) 68 tolimit = 0xffff - 1; 69 tossize = tolimit * sizeof (struct tostruct); 70 tos = (struct tostruct *)malloc(tossize, M_GPROF, M_WAITOK); 71 if (tos == 0) { 72 printf("No space for monitor buffer(s)\n"); 73 free(sbuf, M_GPROF), sbuf = 0; 74 free(froms, M_GPROF), froms = 0; 75 return; 76 } 77 tos[0].link = 0; 78 ((struct phdr *)sbuf)->lpc = s_lowpc; 79 ((struct phdr *)sbuf)->hpc = s_highpc; 80 ((struct phdr *)sbuf)->ncnt = ssiz; 81 kcount = (u_short *)(((int)sbuf) + sizeof (struct phdr)); 82 #ifdef notdef 83 /* 84 * Profiling is what mcount checks to see if 85 * all the data structures are ready!!! 86 */ 87 profiling = 0; /* patch by hand when you're ready */ 88 #endif 89 } 90 91 /* 92 * This routine is massaged so that it may be jsb'ed to on vax. 93 */ 94 asm(".text"); 95 asm("#the beginning of mcount()"); 96 asm(".data"); 97 mcount() 98 { 99 register char *selfpc; /* r11 => r5 */ 100 register u_short *frompcindex; /* r10 => r4 */ 101 register struct tostruct *top; /* r9 => r3 */ 102 register struct tostruct *prevtop; /* r8 => r2 */ 103 register long toindex; /* r7 => r1 */ 104 static int s; 105 106 asm(" .text"); /* make sure we're in text space */ 107 /* 108 * Check that we are profiling. 109 */ 110 if (profiling) 111 goto out; 112 /* 113 * Find the return address for mcount, 114 * and the return address for mcount's caller. 115 */ 116 #ifdef lint 117 selfpc = (char *)0; 118 frompcindex = 0; 119 #else 120 #if defined(vax) 121 asm(" movl (sp), r11"); /* selfpc = ... (jsb frame) */ 122 asm(" movl 16(fp), r10"); /* frompcindex = (calls frame) */ 123 #endif 124 #if defined(tahoe) 125 ; /* avoid label botch */ 126 asm(" movl -8(fp),r12"); /* selfpc = callf frame */ 127 asm(" movl (fp),r11"); 128 asm(" movl -8(r11),r11"); /* frompcindex = 1 callf frame back */ 129 #endif 130 #endif 131 /* 132 * Insure that we cannot be recursively invoked. 133 * this requires that splhigh() and splx() below 134 * do NOT call mcount! 135 */ 136 s = splhigh(); 137 /* 138 * Check that frompcindex is a reasonable pc value. 139 * For example: signal catchers get called from the stack, 140 * not from text space. too bad. 141 */ 142 frompcindex = (u_short *)((long)frompcindex - (long)s_lowpc); 143 if ((u_long)frompcindex > s_textsize) 144 goto done; 145 frompcindex = 146 &froms[((long)frompcindex) / (HASHFRACTION * sizeof (*froms))]; 147 toindex = *frompcindex; 148 if (toindex == 0) { 149 /* 150 * First time traversing this arc 151 */ 152 toindex = ++tos[0].link; 153 if (toindex >= tolimit) 154 goto overflow; 155 *frompcindex = toindex; 156 top = &tos[toindex]; 157 top->selfpc = selfpc; 158 top->count = 1; 159 top->link = 0; 160 goto done; 161 } 162 top = &tos[toindex]; 163 if (top->selfpc == selfpc) { 164 /* 165 * Arc at front of chain; usual case. 166 */ 167 top->count++; 168 goto done; 169 } 170 /* 171 * Have to go looking down chain for it. 172 * Top points to what we are looking at, 173 * prevtop points to previous top. 174 * We know it is not at the head of the chain. 175 */ 176 for (; /* goto done */; ) { 177 if (top->link == 0) { 178 /* 179 * Top is end of the chain and none of the chain 180 * had top->selfpc == selfpc. 181 * So we allocate a new tostruct 182 * and link it to the head of the chain. 183 */ 184 toindex = ++tos[0].link; 185 if (toindex >= tolimit) 186 goto overflow; 187 top = &tos[toindex]; 188 top->selfpc = selfpc; 189 top->count = 1; 190 top->link = *frompcindex; 191 *frompcindex = toindex; 192 goto done; 193 } 194 /* 195 * Otherwise, check the next arc on the chain. 196 */ 197 prevtop = top; 198 top = &tos[top->link]; 199 if (top->selfpc == selfpc) { 200 /* 201 * There it is, increment its count and 202 * move it to the head of the chain. 203 */ 204 top->count++; 205 toindex = prevtop->link; 206 prevtop->link = top->link; 207 top->link = *frompcindex; 208 *frompcindex = toindex; 209 goto done; 210 } 211 212 } 213 done: 214 splx(s); 215 /* and fall through */ 216 out: 217 #if defined(vax) 218 asm(" rsb"); 219 #endif 220 return; 221 overflow: 222 profiling = 3; 223 printf("mcount: tos overflow\n"); 224 goto out; 225 } 226 asm(".text"); 227 asm("#the end of mcount()"); 228 asm(".data"); 229 #endif 230