1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1983, 1993\n\
11 The Regents of the University of California. All rights reserved.\n";
12 #endif /* not lint */
13
14 #ifndef lint
15 static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17
18 #include "gprof.h"
19
20 char *whoami = "gprof";
21
22 /*
23 * things which get -E excluded by default.
24 */
25 char *defaultEs[] = { "mcount" , "__mcleanup" , 0 };
26
27 static struct gmonhdr gmonhdr;
28
main(argc,argv)29 main(argc, argv)
30 int argc;
31 char **argv;
32 {
33 char **sp;
34 nltype **timesortnlp;
35
36 --argc;
37 argv++;
38 debug = 0;
39 bflag = TRUE;
40 while ( *argv != 0 && **argv == '-' ) {
41 (*argv)++;
42 switch ( **argv ) {
43 case 'a':
44 aflag = TRUE;
45 break;
46 case 'b':
47 bflag = FALSE;
48 break;
49 case 'C':
50 Cflag = TRUE;
51 cyclethreshold = atoi( *++argv );
52 break;
53 case 'c':
54 #if defined(vax) || defined(tahoe)
55 cflag = TRUE;
56 #else
57 fprintf(stderr, "gprof: -c isn't supported on this architecture yet\n");
58 exit(1);
59 #endif
60 break;
61 case 'd':
62 dflag = TRUE;
63 setlinebuf(stdout);
64 debug |= atoi( *++argv );
65 debug |= ANYDEBUG;
66 # ifdef DEBUG
67 printf("[main] debug = %d\n", debug);
68 # else not DEBUG
69 printf("%s: -d ignored\n", whoami);
70 # endif DEBUG
71 break;
72 case 'E':
73 ++argv;
74 addlist( Elist , *argv );
75 Eflag = TRUE;
76 addlist( elist , *argv );
77 eflag = TRUE;
78 break;
79 case 'e':
80 addlist( elist , *++argv );
81 eflag = TRUE;
82 break;
83 case 'F':
84 ++argv;
85 addlist( Flist , *argv );
86 Fflag = TRUE;
87 addlist( flist , *argv );
88 fflag = TRUE;
89 break;
90 case 'f':
91 addlist( flist , *++argv );
92 fflag = TRUE;
93 break;
94 case 'k':
95 addlist( kfromlist , *++argv );
96 addlist( ktolist , *++argv );
97 kflag = TRUE;
98 break;
99 case 's':
100 sflag = TRUE;
101 break;
102 case 'z':
103 zflag = TRUE;
104 break;
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 ) {
115 gmonname = *argv;
116 argv++;
117 } else {
118 gmonname = GMONNAME;
119 }
120 /*
121 * turn off default functions
122 */
123 for ( sp = &defaultEs[0] ; *sp ; sp++ ) {
124 Eflag = TRUE;
125 addlist( Elist , *sp );
126 eflag = TRUE;
127 addlist( elist , *sp );
128 }
129 /*
130 * get information about a.out file.
131 */
132 getnfile();
133 /*
134 * get information about mon.out file(s).
135 */
136 do {
137 getpfile( gmonname );
138 if ( *argv != 0 ) {
139 gmonname = *argv;
140 }
141 } while ( *argv++ != 0 );
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 }
150 /*
151 * dump out a gmon.sum file if requested
152 */
153 if ( sflag ) {
154 dumpsum( GMONSUM );
155 }
156 /*
157 * assign samples to procedures
158 */
159 asgnsamples();
160 /*
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
170 */
171 printprof();
172 /*
173 * print the index
174 */
175 printindex();
176 done();
177 }
178
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 */
getnfile()184 getnfile()
185 {
186 FILE *nfile;
187 int valcmp();
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)) {
196 fprintf(stderr, "%s: %s: bad format\n", whoami , a_outname );
197 done();
198 }
199 getstrtab(nfile);
200 getsymtab(nfile);
201 gettextspace( nfile );
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
getstrtab(nfile)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) {
221 fprintf(stderr, "%s: %s: no string table (old format?)\n" ,
222 whoami , a_outname );
223 done();
224 }
225 strtab = calloc(ssiz, 1);
226 if (strtab == NULL) {
227 fprintf(stderr, "%s: %s: no room for %d bytes of string table\n",
228 whoami , a_outname , ssiz);
229 done();
230 }
231 if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1) {
232 fprintf(stderr, "%s: %s: error reading string table\n",
233 whoami , a_outname );
234 done();
235 }
236 }
237
238 /*
239 * Read in symbol table
240 */
getsymtab(nfile)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);
253 if ( ! funcsymbol( &nbuf ) ) {
254 continue;
255 }
256 nname++;
257 }
258 if (nname == 0) {
259 fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname );
260 done();
261 }
262 askfor = nname + 1;
263 nl = (nltype *) calloc( askfor , sizeof(nltype) );
264 if (nl == 0) {
265 fprintf(stderr, "%s: No room for %d bytes of symbol table\n",
266 whoami, askfor * sizeof(nltype) );
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);
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
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;
297 }
298
299 /*
300 * read in the text space of an a.out file
301 */
gettextspace(nfile)302 gettextspace( nfile )
303 FILE *nfile;
304 {
305
306 if ( cflag == 0 ) {
307 return;
308 }
309 textspace = (u_char *) malloc( xbuf.a_text );
310 if ( textspace == 0 ) {
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" );
314 return;
315 }
316 (void) fseek( nfile , N_TXTOFF( xbuf ) , 0 );
317 if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) {
318 fprintf( stderr , "%s: couldn't read text space: " , whoami );
319 fprintf( stderr , "can't do -c\n" );
320 free( textspace );
321 textspace = 0;
322 return;
323 }
324 }
325 /*
326 * information from a gmon.out file is in two parts:
327 * an array of sampling hits within pc ranges,
328 * and the arcs.
329 */
getpfile(filename)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 ) {
346 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
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
358 FILE *
openpfile(filename)359 openpfile(filename)
360 char *filename;
361 {
362 struct gmonhdr tmp;
363 FILE *pfile;
364 int size;
365 int rate;
366
367 if((pfile = fopen(filename, "r")) == NULL) {
368 perror(filename);
369 done();
370 }
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 ) ) {
374 fprintf(stderr, "%s: incompatible with first gmon file\n", filename);
375 done();
376 }
377 gmonhdr = tmp;
378 if ( gmonhdr.version == GMONVERSION ) {
379 rate = gmonhdr.profrate;
380 size = sizeof(struct gmonhdr);
381 } else {
382 fseek(pfile, sizeof(struct ophdr), SEEK_SET);
383 size = sizeof(struct ophdr);
384 gmonhdr.profrate = rate = hertz();
385 gmonhdr.version = GMONVERSION;
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 }
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;
400 nsamples = sampbytes / sizeof (UNIT);
401 # ifdef DEBUG
402 if ( debug & SAMPLEDEBUG ) {
403 printf( "[openpfile] hdr.lpc 0x%x hdr.hpc 0x%x hdr.ncnt %d\n",
404 gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt );
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 );
411 printf( "[openpfile] sample rate %d\n" , hz );
412 }
413 # endif DEBUG
414 return(pfile);
415 }
416
417 tally( rawp )
418 struct rawarc *rawp;
419 {
420 nltype *parentp;
421 nltype *childp;
422
423 parentp = nllookup( rawp -> raw_frompc );
424 childp = nllookup( rawp -> raw_selfpc );
425 if ( parentp == 0 || childp == 0 )
426 return;
427 if ( kflag
428 && onlist( kfromlist , parentp -> name )
429 && onlist( ktolist , childp -> name ) ) {
430 return;
431 }
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
439 addarc( parentp , childp , rawp -> raw_count );
440 }
441
442 /*
443 * dump out the gmon.sum file
444 */
dumpsum(sumfile)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 */
460 if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 ) {
461 perror( sumfile );
462 done();
463 }
464 /*
465 * dump the samples
466 */
467 if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) {
468 perror( sumfile );
469 done();
470 }
471 /*
472 * dump the normalized raw arc information
473 */
474 for ( nlp = nl ; nlp < npe ; nlp++ ) {
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
valcmp(p1,p2)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
readsamples(pfile)506 readsamples(pfile)
507 FILE *pfile;
508 {
509 register i;
510 UNIT sample;
511
512 if (samples == 0) {
513 samples = (UNIT *) calloc(sampbytes, sizeof (UNIT));
514 if (samples == 0) {
515 fprintf( stderr , "%s: No room for %d sample pc's\n",
516 whoami , sampbytes / sizeof (UNIT));
517 done();
518 }
519 }
520 for (i = 0; i < nsamples; i++) {
521 fread(&sample, sizeof (UNIT), 1, pfile);
522 if (feof(pfile))
523 break;
524 samples[i] += sample;
525 }
526 if (i != nsamples) {
527 fprintf(stderr,
528 "%s: unexpected EOF after reading %d/%d samples\n",
529 whoami , --i , nsamples );
530 done();
531 }
532 }
533
534 /*
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
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).
565 */
asgnsamples()566 asgnsamples()
567 {
568 register int j;
569 UNIT ccnt;
570 double time;
571 unsigned long pcl, pch;
572 register int i;
573 unsigned long overlap;
574 unsigned long svalue0, svalue1;
575
576 /* read samples and assign to namelist symbols */
577 scale = highpc - lowpc;
578 scale /= nsamples;
579 alignentries();
580 for (i = 0, j = 1; i < nsamples; i++) {
581 ccnt = samples[i];
582 if (ccnt == 0)
583 continue;
584 pcl = lowpc + scale * i;
585 pch = lowpc + scale * (i + 1);
586 time = ccnt;
587 # ifdef DEBUG
588 if ( debug & SAMPLEDEBUG ) {
589 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
590 pcl , pch , ccnt );
591 }
592 # endif DEBUG
593 totime += time;
594 for (j = j - 1; j < nname; j++) {
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 */
601 if (pch < svalue0)
602 break;
603 /*
604 * if low end of tick into next routine,
605 * go for next routine.
606 */
607 if (pcl >= svalue1)
608 continue;
609 overlap = min(pch, svalue1) - max(pcl, svalue0);
610 if (overlap > 0) {
611 # ifdef DEBUG
612 if (debug & SAMPLEDEBUG) {
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);
617 }
618 # endif DEBUG
619 nl[j].time += overlap * time / scale;
620 }
621 }
622 }
623 # ifdef DEBUG
624 if (debug & SAMPLEDEBUG) {
625 printf("[asgnsamples] totime %f\n", totime);
626 }
627 # endif DEBUG
628 }
629
630
631 unsigned long
min(a,b)632 min(a, b)
633 unsigned long a,b;
634 {
635 if (a<b)
636 return(a);
637 return(b);
638 }
639
640 unsigned long
max(a,b)641 max(a, b)
642 unsigned long a,b;
643 {
644 if (a>b)
645 return(a);
646 return(b);
647 }
648
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 */
alignentries()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
677 bool
funcsymbol(nlistp)678 funcsymbol( nlistp )
679 struct nlist *nlistp;
680 {
681 extern char *strtab; /* string table from a.out */
682 extern int aflag; /* if static functions aren't desired */
683 char *name, c;
684
685 /*
686 * must be a text symbol,
687 * and static text symbols don't qualify if aflag set.
688 */
689 if ( ! ( ( nlistp -> n_type == ( N_TEXT | N_EXT ) )
690 || ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) {
691 return FALSE;
692 }
693 /*
694 * can't have any `funny' characters in name,
695 * where `funny' includes `.', .o file names
696 * and `$', pascal labels.
697 * need to make an exception for sparc .mul & co.
698 * perhaps we should just drop this code entirely...
699 */
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 == '$' ) {
713 return FALSE;
714 }
715 }
716 return TRUE;
717 }
718
done()719 done()
720 {
721
722 exit(0);
723 }
724