Commit | Line | Data |
---|---|---|
c0bc4ef7 | 1 | /* |
7e9de516 KB |
2 | * Copyright (c) 1983, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
ddb85eed | 4 | * |
6ecf3d85 | 5 | * %sccs.include.redist.c% |
c0bc4ef7 DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
7e9de516 KB |
9 | static char copyright[] = |
10 | "@(#) Copyright (c) 1983, 1993\n\ | |
11 | The Regents of the University of California. All rights reserved.\n"; | |
ddb85eed | 12 | #endif /* not lint */ |
c0bc4ef7 | 13 | |
3966d652 | 14 | #ifndef lint |
7e9de516 | 15 | static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) %G%"; |
ddb85eed | 16 | #endif /* not lint */ |
3966d652 | 17 | |
31f0a970 | 18 | #include "gprof.h" |
3966d652 | 19 | |
ad3b82ad PK |
20 | char *whoami = "gprof"; |
21 | ||
a441395b PK |
22 | /* |
23 | * things which get -E excluded by default. | |
24 | */ | |
25 | char *defaultEs[] = { "mcount" , "__mcleanup" , 0 }; | |
7ec9eedc | 26 | |
8012f2fd | 27 | static struct gmonhdr gmonhdr; |
e27719e1 | 28 | |
3966d652 | 29 | main(argc, argv) |
7ec9eedc PK |
30 | int argc; |
31 | char **argv; | |
3966d652 | 32 | { |
7ec9eedc | 33 | char **sp; |
b461ee6c | 34 | nltype **timesortnlp; |
3966d652 PK |
35 | |
36 | --argc; | |
37 | argv++; | |
38 | debug = 0; | |
355ab5a4 | 39 | bflag = TRUE; |
3966d652 PK |
40 | while ( *argv != 0 && **argv == '-' ) { |
41 | (*argv)++; | |
0c0ac747 | 42 | switch ( **argv ) { |
7ec9eedc PK |
43 | case 'a': |
44 | aflag = TRUE; | |
45 | break; | |
46 | case 'b': | |
355ab5a4 | 47 | bflag = FALSE; |
7ec9eedc | 48 | break; |
91541716 KM |
49 | case 'C': |
50 | Cflag = TRUE; | |
51 | cyclethreshold = atoi( *++argv ); | |
52 | break; | |
7ec9eedc | 53 | case 'c': |
d2e9c935 | 54 | #if defined(vax) || defined(tahoe) |
7ec9eedc | 55 | cflag = TRUE; |
d2e9c935 DS |
56 | #else |
57 | fprintf(stderr, "gprof: -c isn't supported on this architecture yet\n"); | |
58 | exit(1); | |
59 | #endif | |
7ec9eedc | 60 | break; |
0c0ac747 | 61 | case 'd': |
7ec9eedc | 62 | dflag = TRUE; |
8012f2fd | 63 | setlinebuf(stdout); |
91541716 | 64 | debug |= atoi( *++argv ); |
3966d652 PK |
65 | debug |= ANYDEBUG; |
66 | # ifdef DEBUG | |
074671d4 KM |
67 | printf("[main] debug = %d\n", debug); |
68 | # else not DEBUG | |
69 | printf("%s: -d ignored\n", whoami); | |
3966d652 | 70 | # endif DEBUG |
0c0ac747 | 71 | break; |
a441395b PK |
72 | case 'E': |
73 | ++argv; | |
74 | addlist( Elist , *argv ); | |
75 | Eflag = TRUE; | |
76 | addlist( elist , *argv ); | |
77 | eflag = TRUE; | |
78 | break; | |
7ec9eedc | 79 | case 'e': |
a441395b | 80 | addlist( elist , *++argv ); |
7ec9eedc | 81 | eflag = TRUE; |
a441395b PK |
82 | break; |
83 | case 'F': | |
84 | ++argv; | |
85 | addlist( Flist , *argv ); | |
86 | Fflag = TRUE; | |
87 | addlist( flist , *argv ); | |
88 | fflag = TRUE; | |
0c0ac747 | 89 | break; |
7ec9eedc | 90 | case 'f': |
a441395b | 91 | addlist( flist , *++argv ); |
7ec9eedc | 92 | fflag = TRUE; |
0c0ac747 | 93 | break; |
3cdecae8 KM |
94 | case 'k': |
95 | addlist( kfromlist , *++argv ); | |
96 | addlist( ktolist , *++argv ); | |
97 | kflag = TRUE; | |
98 | break; | |
0c0ac747 | 99 | case 's': |
7ec9eedc | 100 | sflag = TRUE; |
0c0ac747 KM |
101 | break; |
102 | case 'z': | |
7ec9eedc | 103 | zflag = TRUE; |
0c0ac747 | 104 | break; |
3966d652 PK |
105 | } |
106 | argv++; | |
107 | } | |
108 | if ( *argv != 0 ) { | |
109 | a_outname = *argv; | |
110 | argv++; | |
111 | } else { | |
112 | a_outname = A_OUTNAME; | |
113 | } | |
114 | if ( *argv != 0 ) { | |
31f0a970 | 115 | gmonname = *argv; |
3966d652 PK |
116 | argv++; |
117 | } else { | |
31f0a970 | 118 | gmonname = GMONNAME; |
7ec9eedc PK |
119 | } |
120 | /* | |
121 | * turn off default functions | |
122 | */ | |
a441395b PK |
123 | for ( sp = &defaultEs[0] ; *sp ; sp++ ) { |
124 | Eflag = TRUE; | |
125 | addlist( Elist , *sp ); | |
7ec9eedc | 126 | eflag = TRUE; |
a441395b | 127 | addlist( elist , *sp ); |
82fa08a4 | 128 | } |
3966d652 PK |
129 | /* |
130 | * get information about a.out file. | |
131 | */ | |
132 | getnfile(); | |
133 | /* | |
134 | * get information about mon.out file(s). | |
135 | */ | |
0c0ac747 KM |
136 | do { |
137 | getpfile( gmonname ); | |
138 | if ( *argv != 0 ) { | |
139 | gmonname = *argv; | |
140 | } | |
ad3b82ad | 141 | } while ( *argv++ != 0 ); |
e27719e1 KM |
142 | /* |
143 | * how many ticks per second? | |
144 | * if we can't tell, report time in ticks. | |
145 | */ | |
146 | if (hz == 0) { | |
147 | hz = 1; | |
148 | fprintf(stderr, "time is in ticks, not seconds\n"); | |
149 | } | |
0c0ac747 KM |
150 | /* |
151 | * dump out a gmon.sum file if requested | |
152 | */ | |
ad3b82ad PK |
153 | if ( sflag ) { |
154 | dumpsum( GMONSUM ); | |
155 | } | |
3966d652 PK |
156 | /* |
157 | * assign samples to procedures | |
158 | */ | |
159 | asgnsamples(); | |
160 | /* | |
b461ee6c KM |
161 | * assemble the dynamic profile |
162 | */ | |
163 | timesortnlp = doarcs(); | |
164 | /* | |
165 | * print the dynamic profile | |
166 | */ | |
167 | printgprof( timesortnlp ); | |
168 | /* | |
169 | * print the flat profile | |
3966d652 PK |
170 | */ |
171 | printprof(); | |
172 | /* | |
b461ee6c | 173 | * print the index |
3966d652 | 174 | */ |
b461ee6c | 175 | printindex(); |
3966d652 PK |
176 | done(); |
177 | } | |
178 | ||
ad3b82ad PK |
179 | /* |
180 | * Set up string and symbol tables from a.out. | |
181 | * and optionally the text space. | |
182 | * On return symbol table is sorted by value. | |
183 | */ | |
3966d652 PK |
184 | getnfile() |
185 | { | |
186 | FILE *nfile; | |
104ad219 | 187 | int valcmp(); |
3966d652 PK |
188 | |
189 | nfile = fopen( a_outname ,"r"); | |
190 | if (nfile == NULL) { | |
191 | perror( a_outname ); | |
192 | done(); | |
193 | } | |
194 | fread(&xbuf, 1, sizeof(xbuf), nfile); | |
195 | if (N_BADMAG(xbuf)) { | |
ad3b82ad | 196 | fprintf(stderr, "%s: %s: bad format\n", whoami , a_outname ); |
3966d652 PK |
197 | done(); |
198 | } | |
199 | getstrtab(nfile); | |
200 | getsymtab(nfile); | |
29da1d26 | 201 | gettextspace( nfile ); |
3966d652 PK |
202 | qsort(nl, nname, sizeof(nltype), valcmp); |
203 | fclose(nfile); | |
204 | # ifdef DEBUG | |
205 | if ( debug & AOUTDEBUG ) { | |
206 | register int j; | |
207 | ||
208 | for (j = 0; j < nname; j++){ | |
209 | printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name); | |
210 | } | |
211 | } | |
212 | # endif DEBUG | |
213 | } | |
214 | ||
215 | getstrtab(nfile) | |
216 | FILE *nfile; | |
217 | { | |
218 | ||
219 | fseek(nfile, (long)(N_SYMOFF(xbuf) + xbuf.a_syms), 0); | |
220 | if (fread(&ssiz, sizeof (ssiz), 1, nfile) == 0) { | |
ad3b82ad PK |
221 | fprintf(stderr, "%s: %s: no string table (old format?)\n" , |
222 | whoami , a_outname ); | |
3966d652 PK |
223 | done(); |
224 | } | |
8012f2fd | 225 | strtab = calloc(ssiz, 1); |
3966d652 | 226 | if (strtab == NULL) { |
8012f2fd | 227 | fprintf(stderr, "%s: %s: no room for %d bytes of string table\n", |
ad3b82ad | 228 | whoami , a_outname , ssiz); |
3966d652 PK |
229 | done(); |
230 | } | |
231 | if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1) { | |
ad3b82ad PK |
232 | fprintf(stderr, "%s: %s: error reading string table\n", |
233 | whoami , a_outname ); | |
3966d652 PK |
234 | done(); |
235 | } | |
236 | } | |
237 | ||
238 | /* | |
239 | * Read in symbol table | |
240 | */ | |
241 | getsymtab(nfile) | |
242 | FILE *nfile; | |
243 | { | |
244 | register long i; | |
245 | int askfor; | |
246 | struct nlist nbuf; | |
247 | ||
248 | /* pass1 - count symbols */ | |
249 | fseek(nfile, (long)N_SYMOFF(xbuf), 0); | |
250 | nname = 0; | |
251 | for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) { | |
252 | fread(&nbuf, sizeof(nbuf), 1, nfile); | |
3f722622 | 253 | if ( ! funcsymbol( &nbuf ) ) { |
3966d652 PK |
254 | continue; |
255 | } | |
256 | nname++; | |
257 | } | |
258 | if (nname == 0) { | |
ad3b82ad | 259 | fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname ); |
3966d652 PK |
260 | done(); |
261 | } | |
ad3b82ad | 262 | askfor = nname + 1; |
3966d652 PK |
263 | nl = (nltype *) calloc( askfor , sizeof(nltype) ); |
264 | if (nl == 0) { | |
ad3b82ad PK |
265 | fprintf(stderr, "%s: No room for %d bytes of symbol table\n", |
266 | whoami, askfor * sizeof(nltype) ); | |
3966d652 PK |
267 | done(); |
268 | } | |
269 | ||
270 | /* pass2 - read symbols */ | |
271 | fseek(nfile, (long)N_SYMOFF(xbuf), 0); | |
272 | npe = nl; | |
273 | nname = 0; | |
274 | for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) { | |
275 | fread(&nbuf, sizeof(nbuf), 1, nfile); | |
3f722622 PK |
276 | if ( ! funcsymbol( &nbuf ) ) { |
277 | # ifdef DEBUG | |
278 | if ( debug & AOUTDEBUG ) { | |
279 | printf( "[getsymtab] rejecting: 0x%x %s\n" , | |
280 | nbuf.n_type , strtab + nbuf.n_un.n_strx ); | |
281 | } | |
282 | # endif DEBUG | |
3966d652 PK |
283 | continue; |
284 | } | |
285 | npe->value = nbuf.n_value; | |
286 | npe->name = strtab+nbuf.n_un.n_strx; | |
287 | # ifdef DEBUG | |
288 | if ( debug & AOUTDEBUG ) { | |
289 | printf( "[getsymtab] %d %s 0x%08x\n" , | |
290 | nname , npe -> name , npe -> value ); | |
291 | } | |
292 | # endif DEBUG | |
293 | npe++; | |
294 | nname++; | |
295 | } | |
296 | npe->value = -1; | |
3966d652 PK |
297 | } |
298 | ||
29da1d26 PK |
299 | /* |
300 | * read in the text space of an a.out file | |
301 | */ | |
302 | gettextspace( nfile ) | |
303 | FILE *nfile; | |
304 | { | |
8012f2fd | 305 | |
29da1d26 PK |
306 | if ( cflag == 0 ) { |
307 | return; | |
308 | } | |
104ad219 | 309 | textspace = (u_char *) malloc( xbuf.a_text ); |
29da1d26 | 310 | if ( textspace == 0 ) { |
ad3b82ad PK |
311 | fprintf( stderr , "%s: ran out room for %d bytes of text space: " , |
312 | whoami , xbuf.a_text ); | |
313 | fprintf( stderr , "can't do -c\n" ); | |
29da1d26 PK |
314 | return; |
315 | } | |
316 | (void) fseek( nfile , N_TXTOFF( xbuf ) , 0 ); | |
317 | if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) { | |
ad3b82ad PK |
318 | fprintf( stderr , "%s: couldn't read text space: " , whoami ); |
319 | fprintf( stderr , "can't do -c\n" ); | |
29da1d26 PK |
320 | free( textspace ); |
321 | textspace = 0; | |
322 | return; | |
323 | } | |
324 | } | |
3966d652 | 325 | /* |
31f0a970 | 326 | * information from a gmon.out file is in two parts: |
3966d652 PK |
327 | * an array of sampling hits within pc ranges, |
328 | * and the arcs. | |
329 | */ | |
330 | getpfile(filename) | |
331 | char *filename; | |
332 | { | |
333 | FILE *pfile; | |
334 | FILE *openpfile(); | |
335 | struct rawarc arc; | |
336 | ||
337 | pfile = openpfile(filename); | |
338 | readsamples(pfile); | |
339 | /* | |
340 | * the rest of the file consists of | |
341 | * a bunch of <from,self,count> tuples. | |
342 | */ | |
343 | while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) { | |
344 | # ifdef DEBUG | |
345 | if ( debug & SAMPLEDEBUG ) { | |
06ac0fb1 | 346 | printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" , |
3966d652 PK |
347 | arc.raw_frompc , arc.raw_selfpc , arc.raw_count ); |
348 | } | |
349 | # endif DEBUG | |
350 | /* | |
351 | * add this arc | |
352 | */ | |
353 | tally( &arc ); | |
354 | } | |
355 | fclose(pfile); | |
356 | } | |
357 | ||
35e3e365 PK |
358 | FILE * |
359 | openpfile(filename) | |
3966d652 PK |
360 | char *filename; |
361 | { | |
8012f2fd | 362 | struct gmonhdr tmp; |
e27719e1 KM |
363 | FILE *pfile; |
364 | int size; | |
365 | int rate; | |
3966d652 PK |
366 | |
367 | if((pfile = fopen(filename, "r")) == NULL) { | |
368 | perror(filename); | |
369 | done(); | |
370 | } | |
8012f2fd CT |
371 | fread(&tmp, sizeof(struct gmonhdr), 1, pfile); |
372 | if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc || | |
373 | tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) ) { | |
0c0ac747 KM |
374 | fprintf(stderr, "%s: incompatible with first gmon file\n", filename); |
375 | done(); | |
376 | } | |
8012f2fd CT |
377 | gmonhdr = tmp; |
378 | if ( gmonhdr.version == GMONVERSION ) { | |
379 | rate = gmonhdr.profrate; | |
380 | size = sizeof(struct gmonhdr); | |
e27719e1 | 381 | } else { |
8012f2fd | 382 | fseek(pfile, sizeof(struct ophdr), SEEK_SET); |
e27719e1 | 383 | size = sizeof(struct ophdr); |
8012f2fd CT |
384 | gmonhdr.profrate = rate = hertz(); |
385 | gmonhdr.version = GMONVERSION; | |
e27719e1 KM |
386 | } |
387 | if (hz == 0) { | |
388 | hz = rate; | |
389 | } else if (hz != rate) { | |
390 | fprintf(stderr, | |
391 | "%s: profile clock rate (%d) %s (%d) in first gmon file\n", | |
392 | filename, rate, "incompatible with clock rate", hz); | |
393 | done(); | |
394 | } | |
8012f2fd CT |
395 | s_lowpc = (unsigned long) gmonhdr.lpc; |
396 | s_highpc = (unsigned long) gmonhdr.hpc; | |
397 | lowpc = (unsigned long)gmonhdr.lpc / sizeof(UNIT); | |
398 | highpc = (unsigned long)gmonhdr.hpc / sizeof(UNIT); | |
399 | sampbytes = gmonhdr.ncnt - size; | |
104ad219 | 400 | nsamples = sampbytes / sizeof (UNIT); |
33e41a83 PK |
401 | # ifdef DEBUG |
402 | if ( debug & SAMPLEDEBUG ) { | |
e27719e1 | 403 | printf( "[openpfile] hdr.lpc 0x%x hdr.hpc 0x%x hdr.ncnt %d\n", |
8012f2fd | 404 | gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt ); |
33e41a83 PK |
405 | printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" , |
406 | s_lowpc , s_highpc ); | |
407 | printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" , | |
408 | lowpc , highpc ); | |
409 | printf( "[openpfile] sampbytes %d nsamples %d\n" , | |
410 | sampbytes , nsamples ); | |
e27719e1 | 411 | printf( "[openpfile] sample rate %d\n" , hz ); |
33e41a83 PK |
412 | } |
413 | # endif DEBUG | |
3966d652 PK |
414 | return(pfile); |
415 | } | |
416 | ||
417 | tally( rawp ) | |
418 | struct rawarc *rawp; | |
419 | { | |
420 | nltype *parentp; | |
421 | nltype *childp; | |
3966d652 PK |
422 | |
423 | parentp = nllookup( rawp -> raw_frompc ); | |
424 | childp = nllookup( rawp -> raw_selfpc ); | |
5fc99333 KM |
425 | if ( parentp == 0 || childp == 0 ) |
426 | return; | |
3cdecae8 KM |
427 | if ( kflag |
428 | && onlist( kfromlist , parentp -> name ) | |
429 | && onlist( ktolist , childp -> name ) ) { | |
430 | return; | |
431 | } | |
3966d652 PK |
432 | childp -> ncall += rawp -> raw_count; |
433 | # ifdef DEBUG | |
434 | if ( debug & TALLYDEBUG ) { | |
435 | printf( "[tally] arc from %s to %s traversed %d times\n" , | |
436 | parentp -> name , childp -> name , rawp -> raw_count ); | |
437 | } | |
438 | # endif DEBUG | |
29da1d26 | 439 | addarc( parentp , childp , rawp -> raw_count ); |
3966d652 PK |
440 | } |
441 | ||
0c0ac747 KM |
442 | /* |
443 | * dump out the gmon.sum file | |
444 | */ | |
445 | dumpsum( sumfile ) | |
446 | char *sumfile; | |
447 | { | |
448 | register nltype *nlp; | |
449 | register arctype *arcp; | |
450 | struct rawarc arc; | |
451 | FILE *sfile; | |
452 | ||
453 | if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) { | |
454 | perror( sumfile ); | |
455 | done(); | |
456 | } | |
457 | /* | |
458 | * dump the header; use the last header read in | |
459 | */ | |
8012f2fd | 460 | if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 ) { |
0c0ac747 KM |
461 | perror( sumfile ); |
462 | done(); | |
463 | } | |
464 | /* | |
465 | * dump the samples | |
466 | */ | |
104ad219 | 467 | if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) { |
0c0ac747 KM |
468 | perror( sumfile ); |
469 | done(); | |
470 | } | |
471 | /* | |
472 | * dump the normalized raw arc information | |
473 | */ | |
074671d4 | 474 | for ( nlp = nl ; nlp < npe ; nlp++ ) { |
0c0ac747 KM |
475 | for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) { |
476 | arc.raw_frompc = arcp -> arc_parentp -> value; | |
477 | arc.raw_selfpc = arcp -> arc_childp -> value; | |
478 | arc.raw_count = arcp -> arc_count; | |
479 | if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) { | |
480 | perror( sumfile ); | |
481 | done(); | |
482 | } | |
483 | # ifdef DEBUG | |
484 | if ( debug & SAMPLEDEBUG ) { | |
485 | printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" , | |
486 | arc.raw_frompc , arc.raw_selfpc , arc.raw_count ); | |
487 | } | |
488 | # endif DEBUG | |
489 | } | |
490 | } | |
491 | fclose( sfile ); | |
492 | } | |
493 | ||
3966d652 PK |
494 | valcmp(p1, p2) |
495 | nltype *p1, *p2; | |
496 | { | |
497 | if ( p1 -> value < p2 -> value ) { | |
498 | return LESSTHAN; | |
499 | } | |
500 | if ( p1 -> value > p2 -> value ) { | |
501 | return GREATERTHAN; | |
502 | } | |
503 | return EQUALTO; | |
504 | } | |
505 | ||
506 | readsamples(pfile) | |
507 | FILE *pfile; | |
508 | { | |
509 | register i; | |
104ad219 | 510 | UNIT sample; |
3966d652 PK |
511 | |
512 | if (samples == 0) { | |
104ad219 | 513 | samples = (UNIT *) calloc(sampbytes, sizeof (UNIT)); |
3966d652 | 514 | if (samples == 0) { |
ad3b82ad | 515 | fprintf( stderr , "%s: No room for %d sample pc's\n", |
104ad219 | 516 | whoami , sampbytes / sizeof (UNIT)); |
3966d652 PK |
517 | done(); |
518 | } | |
519 | } | |
520 | for (i = 0; i < nsamples; i++) { | |
104ad219 | 521 | fread(&sample, sizeof (UNIT), 1, pfile); |
3966d652 PK |
522 | if (feof(pfile)) |
523 | break; | |
524 | samples[i] += sample; | |
525 | } | |
526 | if (i != nsamples) { | |
527 | fprintf(stderr, | |
ad3b82ad PK |
528 | "%s: unexpected EOF after reading %d/%d samples\n", |
529 | whoami , --i , nsamples ); | |
3966d652 PK |
530 | done(); |
531 | } | |
532 | } | |
533 | ||
534 | /* | |
4855b699 KM |
535 | * Assign samples to the procedures to which they belong. |
536 | * | |
537 | * There are three cases as to where pcl and pch can be | |
538 | * with respect to the routine entry addresses svalue0 and svalue1 | |
539 | * as shown in the following diagram. overlap computes the | |
540 | * distance between the arrows, the fraction of the sample | |
541 | * that is to be credited to the routine which starts at svalue0. | |
542 | * | |
543 | * svalue0 svalue1 | |
544 | * | | | |
545 | * v v | |
546 | * | |
547 | * +-----------------------------------------------+ | |
548 | * | | | |
549 | * | ->| |<- ->| |<- ->| |<- | | |
550 | * | | | | | | | |
551 | * +---------+ +---------+ +---------+ | |
552 | * | |
553 | * ^ ^ ^ ^ ^ ^ | |
554 | * | | | | | | | |
555 | * pcl pch pcl pch pcl pch | |
556 | * | |
557 | * For the vax we assert that samples will never fall in the first | |
074671d4 KM |
558 | * two bytes of any routine, since that is the entry mask, |
559 | * thus we give call alignentries() to adjust the entry points if | |
560 | * the entry mask falls in one bucket but the code for the routine | |
561 | * doesn't start until the next bucket. In conjunction with the | |
562 | * alignment of routine addresses, this should allow us to have | |
563 | * only one sample for every four bytes of text space and never | |
564 | * have any overlap (the two end cases, above). | |
3966d652 PK |
565 | */ |
566 | asgnsamples() | |
567 | { | |
568 | register int j; | |
104ad219 | 569 | UNIT ccnt; |
3966d652 PK |
570 | double time; |
571 | unsigned long pcl, pch; | |
572 | register int i; | |
35e3e365 | 573 | unsigned long overlap; |
3966d652 PK |
574 | unsigned long svalue0, svalue1; |
575 | ||
576 | /* read samples and assign to namelist symbols */ | |
577 | scale = highpc - lowpc; | |
578 | scale /= nsamples; | |
074671d4 | 579 | alignentries(); |
0fcb1e15 | 580 | for (i = 0, j = 1; i < nsamples; i++) { |
3966d652 PK |
581 | ccnt = samples[i]; |
582 | if (ccnt == 0) | |
583 | continue; | |
4855b699 KM |
584 | pcl = lowpc + scale * i; |
585 | pch = lowpc + scale * (i + 1); | |
3966d652 PK |
586 | time = ccnt; |
587 | # ifdef DEBUG | |
588 | if ( debug & SAMPLEDEBUG ) { | |
eb6d6a16 KM |
589 | printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" , |
590 | pcl , pch , ccnt ); | |
3966d652 PK |
591 | } |
592 | # endif DEBUG | |
593 | totime += time; | |
16051da5 | 594 | for (j = j - 1; j < nname; j++) { |
074671d4 KM |
595 | svalue0 = nl[j].svalue; |
596 | svalue1 = nl[j+1].svalue; | |
597 | /* | |
598 | * if high end of tick is below entry address, | |
599 | * go for next tick. | |
600 | */ | |
3966d652 PK |
601 | if (pch < svalue0) |
602 | break; | |
074671d4 KM |
603 | /* |
604 | * if low end of tick into next routine, | |
605 | * go for next routine. | |
606 | */ | |
3966d652 PK |
607 | if (pcl >= svalue1) |
608 | continue; | |
074671d4 | 609 | overlap = min(pch, svalue1) - max(pcl, svalue0); |
4855b699 | 610 | if (overlap > 0) { |
3966d652 | 611 | # ifdef DEBUG |
4855b699 | 612 | if (debug & SAMPLEDEBUG) { |
074671d4 KM |
613 | printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n", |
614 | nl[j].value/sizeof(UNIT), svalue0, svalue1, | |
615 | nl[j].name, | |
616 | overlap * time / scale, overlap); | |
3966d652 PK |
617 | } |
618 | # endif DEBUG | |
4855b699 | 619 | nl[j].time += overlap * time / scale; |
3966d652 PK |
620 | } |
621 | } | |
622 | } | |
623 | # ifdef DEBUG | |
4855b699 KM |
624 | if (debug & SAMPLEDEBUG) { |
625 | printf("[asgnsamples] totime %f\n", totime); | |
3966d652 PK |
626 | } |
627 | # endif DEBUG | |
3966d652 PK |
628 | } |
629 | ||
630 | ||
35e3e365 | 631 | unsigned long |
3966d652 | 632 | min(a, b) |
35e3e365 | 633 | unsigned long a,b; |
3966d652 PK |
634 | { |
635 | if (a<b) | |
636 | return(a); | |
637 | return(b); | |
638 | } | |
639 | ||
35e3e365 | 640 | unsigned long |
3966d652 | 641 | max(a, b) |
35e3e365 | 642 | unsigned long a,b; |
3966d652 PK |
643 | { |
644 | if (a>b) | |
645 | return(a); | |
646 | return(b); | |
647 | } | |
648 | ||
074671d4 KM |
649 | /* |
650 | * calculate scaled entry point addresses (to save time in asgnsamples), | |
651 | * and possibly push the scaled entry points over the entry mask, | |
652 | * if it turns out that the entry point is in one bucket and the code | |
653 | * for a routine is in the next bucket. | |
654 | */ | |
655 | alignentries() | |
656 | { | |
657 | register struct nl *nlp; | |
658 | unsigned long bucket_of_entry; | |
659 | unsigned long bucket_of_code; | |
660 | ||
661 | for (nlp = nl; nlp < npe; nlp++) { | |
662 | nlp -> svalue = nlp -> value / sizeof(UNIT); | |
663 | bucket_of_entry = (nlp->svalue - lowpc) / scale; | |
664 | bucket_of_code = (nlp->svalue + UNITS_TO_CODE - lowpc) / scale; | |
665 | if (bucket_of_entry < bucket_of_code) { | |
666 | # ifdef DEBUG | |
667 | if (debug & SAMPLEDEBUG) { | |
668 | printf("[alignentries] pushing svalue 0x%x to 0x%x\n", | |
669 | nlp->svalue, nlp->svalue + UNITS_TO_CODE); | |
670 | } | |
671 | # endif DEBUG | |
672 | nlp->svalue += UNITS_TO_CODE; | |
673 | } | |
674 | } | |
675 | } | |
676 | ||
3f722622 PK |
677 | bool |
678 | funcsymbol( nlistp ) | |
679 | struct nlist *nlistp; | |
680 | { | |
681 | extern char *strtab; /* string table from a.out */ | |
d51dad81 | 682 | extern int aflag; /* if static functions aren't desired */ |
8012f2fd | 683 | char *name, c; |
3f722622 PK |
684 | |
685 | /* | |
686 | * must be a text symbol, | |
d51dad81 | 687 | * and static text symbols don't qualify if aflag set. |
3f722622 PK |
688 | */ |
689 | if ( ! ( ( nlistp -> n_type == ( N_TEXT | N_EXT ) ) | |
d51dad81 | 690 | || ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) { |
3f722622 PK |
691 | return FALSE; |
692 | } | |
693 | /* | |
ad3b82ad | 694 | * can't have any `funny' characters in name, |
3f722622 PK |
695 | * where `funny' includes `.', .o file names |
696 | * and `$', pascal labels. | |
8012f2fd CT |
697 | * need to make an exception for sparc .mul & co. |
698 | * perhaps we should just drop this code entirely... | |
3f722622 | 699 | */ |
8012f2fd CT |
700 | name = strtab + nlistp -> n_un.n_strx; |
701 | #ifdef sparc | |
702 | if ( *name == '.' ) { | |
703 | char *p = name + 1; | |
704 | if ( *p == 'u' ) | |
705 | p++; | |
706 | if ( strcmp ( p, "mul" ) == 0 || strcmp ( p, "div" ) == 0 || | |
707 | strcmp ( p, "rem" ) == 0 ) | |
708 | return TRUE; | |
709 | } | |
710 | #endif | |
711 | while ( c = *name++ ) { | |
712 | if ( c == '.' || c == '$' ) { | |
3f722622 PK |
713 | return FALSE; |
714 | } | |
715 | } | |
716 | return TRUE; | |
717 | } | |
718 | ||
3966d652 PK |
719 | done() |
720 | { | |
721 | ||
722 | exit(0); | |
723 | } |