add -v flag to gprof (print version number)
[deliverable/binutils-gdb.git] / gprof / gprof.c
1 /*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20 #define VERSION "0.0"
21
22 #ifndef lint
23 char copyright[] =
24 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
25 All rights reserved.\n";
26 #endif /* not lint */
27
28 #ifndef lint
29 static char sccsid[] = "@(#)gprof.c 5.6 (Berkeley) 6/1/90";
30 #endif /* not lint */
31
32 #include "gprof.h"
33
34 bfd *abfd;
35
36 char *whoami;
37
38 /*
39 * things which get -E excluded by default.
40 */
41 char *defaultEs[] = { "mcount" , "__mcleanup" , 0 };
42
43 int discard_underscores = 1; /* Should we discard initial underscores? */
44 int bsd_style_output = 0; /* As opposed to FSF style output */
45
46 main(argc, argv)
47 int argc;
48 char **argv;
49 {
50 char **sp;
51 nltype **timesortnlp;
52
53 whoami = argv[0];
54 --argc;
55 argv++;
56 debug = 0;
57 bflag = TRUE;
58 while ( *argv != 0 && **argv == '-' ) {
59 (*argv)++;
60 switch ( **argv ) {
61 case 'a':
62 aflag = TRUE;
63 break;
64 case 'b':
65 bflag = FALSE;
66 break;
67 case 'c':
68 cflag = TRUE;
69 break;
70 case 'd':
71 dflag = TRUE;
72 (*argv)++;
73 debug |= atoi( *argv );
74 debug |= ANYDEBUG;
75 # ifdef DEBUG
76 printf("[main] debug = %d\n", debug);
77 # else not DEBUG
78 printf("%s: -d ignored\n", whoami);
79 # endif DEBUG
80 break;
81 case 'E':
82 ++argv;
83 addlist( Elist , *argv );
84 Eflag = TRUE;
85 addlist( elist , *argv );
86 eflag = TRUE;
87 break;
88 case 'e':
89 addlist( elist , *++argv );
90 eflag = TRUE;
91 break;
92 case 'F':
93 ++argv;
94 addlist( Flist , *argv );
95 Fflag = TRUE;
96 addlist( flist , *argv );
97 fflag = TRUE;
98 break;
99 case 'f':
100 addlist( flist , *++argv );
101 fflag = TRUE;
102 break;
103 case 'k':
104 addlist( kfromlist , *++argv );
105 addlist( ktolist , *++argv );
106 kflag = TRUE;
107 break;
108 case 's':
109 sflag = TRUE;
110 break;
111 case 'T': /* "Traditional" output format */
112 bsd_style_output = 1;
113 break;
114 case 'v':
115 printf ("gprof version %s\n", VERSION);
116 exit(0);
117 break;
118 case 'z':
119 zflag = TRUE;
120 break;
121 default:
122 fprintf (stderr, "\
123 Usage: %s [-a] [-b] [-c] [-d[num]] [-E function-name] [-e function-name]\n\
124 [-F function-name] [-f function-name] [-k from to] [-s] [-T] [-z]\n\
125 [image-file] [profile-file...]\n", whoami);
126 exit (1);
127 }
128 argv++;
129 }
130 if ( *argv != 0 ) {
131 a_outname = *argv;
132 argv++;
133 } else {
134 a_outname = A_OUTNAME;
135 }
136 if ( *argv != 0 ) {
137 gmonname = *argv;
138 argv++;
139 } else {
140 gmonname = GMONNAME;
141 }
142 /*
143 * turn off default functions
144 */
145 for ( sp = &defaultEs[0] ; *sp ; sp++ ) {
146 Eflag = TRUE;
147 addlist( Elist , *sp );
148 eflag = TRUE;
149 addlist( elist , *sp );
150 }
151 /*
152 * how many ticks per second?
153 * if we can't tell, report time in ticks.
154 */
155 hz = hertz();
156 if (hz == 0) {
157 hz = 1;
158 fprintf(stderr, "time is in ticks, not seconds\n");
159 }
160 /*
161 * get information about a.out file.
162 */
163 getnfile();
164 /*
165 * get information about mon.out file(s).
166 */
167 do {
168 getpfile( gmonname );
169 if ( *argv != 0 ) {
170 gmonname = *argv;
171 }
172 } while ( *argv++ != 0 );
173 /*
174 * dump out a gmon.sum file if requested
175 */
176 if ( sflag ) {
177 dumpsum( GMONSUM );
178 }
179 /*
180 * assign samples to procedures
181 */
182 asgnsamples();
183 /*
184 * assemble the dynamic profile
185 */
186 timesortnlp = doarcs();
187
188 if (bsd_style_output) {
189 printgprof( timesortnlp ); /* print the dynamic profile */
190 printprof(); /* print the flat profile */
191 } else {
192 printprof(); /* print the flat profile */
193 printgprof( timesortnlp ); /* print the dynamic profile */
194 }
195 /*
196 * print the index
197 */
198 printindex();
199 done();
200 }
201
202 /*
203 * Set up string and symbol tables from a.out.
204 * and optionally the text space.
205 * On return symbol table is sorted by value.
206 */
207 getnfile()
208 {
209 int valcmp();
210
211 abfd = bfd_openr (a_outname, NULL);
212
213 if (abfd == NULL) {
214 perror (a_outname);
215 done();
216 }
217
218 if (!bfd_check_format (abfd, bfd_object)) {
219 fprintf (stderr, "%s: %s: bad format\n", whoami, a_outname);
220 done();
221 }
222
223 /* getstrtab(nfile); */
224 getsymtab(abfd);
225 gettextspace( abfd );
226 qsort(nl, nname, sizeof(nltype), valcmp);
227
228 # ifdef DEBUG
229 if ( debug & AOUTDEBUG ) {
230 register int j;
231
232 for (j = 0; j < nname; j++){
233 printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name);
234 }
235 }
236 # endif DEBUG
237 }
238
239 /*
240 * Read in symbol table
241 */
242 getsymtab(abfd)
243 bfd *abfd;
244 {
245 register long i;
246 int askfor;
247 int nosyms;
248 asymbol **syms;
249 i = get_symtab_upper_bound (abfd); /* This will probably give us more
250 * than we need, but that's ok.
251 */
252 syms = (asymbol**)xmalloc (i);
253 nosyms = bfd_canonicalize_symtab (abfd, syms);
254
255 nname = 0;
256 for (i = 0; i < nosyms; i++) {
257 if (!funcsymbol (syms[i]))
258 continue;
259 nname++;
260 }
261
262 if (nname == 0) {
263 fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname );
264 done();
265 }
266 askfor = nname + 1;
267 nl = (nltype *) calloc( askfor , sizeof(nltype) );
268 if (nl == 0) {
269 fprintf(stderr, "%s: No room for %d bytes of symbol table\n",
270 whoami, askfor * sizeof(nltype) );
271 done();
272 }
273
274 /* pass2 - read symbols */
275 npe = nl;
276 nname = 0;
277 for (i = 0; i < nosyms; i++) {
278 if (!funcsymbol (syms[i])) {
279 # ifdef DEBUG
280 if ( debug & AOUTDEBUG ) {
281 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
282 syms[i]->value, syms[i]->name);
283 }
284 # endif DEBUG
285 continue;
286 }
287 /* Symbol offsets are always section-relative. */
288 npe->value = syms[i]->value + syms[i]->section->vma;
289 npe->name = syms[i]->name;
290
291 /* If we see "main" without an initial '_', we assume
292 names are *not* prefixed by '_'. */
293 if (npe->name[0] == 'm' && discard_underscores
294 && strcmp(npe->name, "main") == 0)
295 discard_underscores = 0;
296
297 # ifdef DEBUG
298 if ( debug & AOUTDEBUG ) {
299 printf( "[getsymtab] %d %s 0x%08x\n" ,
300 nname , npe -> name , npe -> value );
301 }
302 # endif DEBUG
303 npe++;
304 nname++;
305 }
306 npe->value = -1;
307 }
308
309 /*
310 * read in the text space of an a.out file
311 */
312 gettextspace( abfd )
313 bfd *abfd;
314 {
315 asection *texsec;
316
317 if ( cflag == 0 ) {
318 return;
319 }
320
321 texsec = bfd_get_section_by_name (abfd, ".text");
322 if (texsec == NULL) {
323 return;
324 }
325
326 textspace = (u_char *) malloc( texsec->_cooked_size );
327
328 if ( textspace == 0 ) {
329 fprintf( stderr , "%s: ran out room for %d bytes of text space: " ,
330 whoami , texsec->_cooked_size);
331 fprintf( stderr , "can't do -c\n" );
332 return;
333 }
334 bfd_get_section_contents (abfd, texsec, textspace, texsec->filepos,
335 texsec->_cooked_size);
336 }
337 /*
338 * information from a gmon.out file is in two parts:
339 * an array of sampling hits within pc ranges,
340 * and the arcs.
341 */
342 getpfile(filename)
343 char *filename;
344 {
345 FILE *pfile;
346 FILE *openpfile();
347 struct rawarc arc;
348 struct veryrawarc rawarc;
349
350 pfile = openpfile(filename);
351 readsamples(pfile);
352 /*
353 * the rest of the file consists of
354 * a bunch of <from,self,count> tuples.
355 */
356 while ( fread( &rawarc , sizeof rawarc , 1 , pfile ) == 1 ) {
357 arc.raw_frompc = bfd_get_32 (abfd, (bfd_byte *) rawarc.raw_frompc);
358 arc.raw_selfpc = bfd_get_32 (abfd, (bfd_byte *) rawarc.raw_selfpc);
359 arc.raw_count = bfd_get_32 (abfd, (bfd_byte *) rawarc.raw_count);
360 # ifdef DEBUG
361 if ( debug & SAMPLEDEBUG ) {
362 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
363 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
364 }
365 # endif DEBUG
366 /*
367 * add this arc
368 */
369 tally( &arc );
370 }
371 fclose(pfile);
372 }
373
374 FILE *
375 openpfile(filename)
376 char *filename;
377 {
378 struct hdr tmp;
379 struct rawhdr raw;
380 FILE *pfile;
381
382 if((pfile = fopen(filename, "r")) == NULL) {
383 perror(filename);
384 done();
385 }
386 if (sizeof(struct rawhdr) != fread(&raw, 1, sizeof(struct rawhdr), pfile))
387 {
388 fprintf(stderr, "%s: file too short to be a gmon file\n", filename);
389 done();
390 }
391 tmp.lowpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &raw.lowpc[0]);
392 tmp.highpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &raw.highpc[0]);
393 tmp.ncnt = bfd_get_32 (abfd, (bfd_byte *) &raw.ncnt[0]);
394
395 if ( s_highpc != 0 && ( tmp.lowpc != h.lowpc ||
396 tmp.highpc != h.highpc || tmp.ncnt != h.ncnt ) ) {
397 fprintf(stderr, "%s: incompatible with first gmon file\n", filename);
398 done();
399 }
400 h = tmp;
401 s_lowpc = (unsigned long) h.lowpc;
402 s_highpc = (unsigned long) h.highpc;
403 lowpc = (unsigned long)h.lowpc / sizeof(UNIT);
404 highpc = (unsigned long)h.highpc / sizeof(UNIT);
405 sampbytes = h.ncnt - sizeof(struct rawhdr);
406 nsamples = sampbytes / sizeof (UNIT);
407 # ifdef DEBUG
408 if ( debug & SAMPLEDEBUG ) {
409 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
410 h.lowpc , h.highpc , h.ncnt );
411 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
412 s_lowpc , s_highpc );
413 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
414 lowpc , highpc );
415 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
416 sampbytes , nsamples );
417 }
418 # endif DEBUG
419 return(pfile);
420 }
421
422 tally( rawp )
423 struct rawarc *rawp;
424 {
425 nltype *parentp;
426 nltype *childp;
427
428 parentp = nllookup( rawp -> raw_frompc );
429 childp = nllookup( rawp -> raw_selfpc );
430 if ( kflag
431 && onlist( kfromlist , parentp -> name )
432 && onlist( ktolist , childp -> name ) ) {
433 return;
434 }
435 childp -> ncall += rawp -> raw_count;
436 # ifdef DEBUG
437 if ( debug & TALLYDEBUG ) {
438 printf( "[tally] arc from %s to %s traversed %d times\n" ,
439 parentp -> name , childp -> name , rawp -> raw_count );
440 }
441 # endif DEBUG
442 addarc( parentp , childp , rawp -> raw_count );
443 }
444
445 /*
446 * dump out the gmon.sum file
447 */
448 dumpsum( sumfile )
449 char *sumfile;
450 {
451 register nltype *nlp;
452 register arctype *arcp;
453 struct rawarc arc;
454 FILE *sfile;
455
456 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) {
457 perror( sumfile );
458 done();
459 }
460 /*
461 * dump the header; use the last header read in
462 */
463 if ( fwrite( &h , sizeof h , 1 , sfile ) != 1 ) {
464 perror( sumfile );
465 done();
466 }
467 /*
468 * dump the samples
469 */
470 if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) {
471 perror( sumfile );
472 done();
473 }
474 /*
475 * dump the normalized raw arc information
476 */
477 for ( nlp = nl ; nlp < npe ; nlp++ ) {
478 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
479 arc.raw_frompc = arcp -> arc_parentp -> value;
480 arc.raw_selfpc = arcp -> arc_childp -> value;
481 arc.raw_count = arcp -> arc_count;
482 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) {
483 perror( sumfile );
484 done();
485 }
486 # ifdef DEBUG
487 if ( debug & SAMPLEDEBUG ) {
488 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
489 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
490 }
491 # endif DEBUG
492 }
493 }
494 fclose( sfile );
495 }
496
497 valcmp(p1, p2)
498 nltype *p1, *p2;
499 {
500 if ( p1 -> value < p2 -> value ) {
501 return LESSTHAN;
502 }
503 if ( p1 -> value > p2 -> value ) {
504 return GREATERTHAN;
505 }
506 return EQUALTO;
507 }
508
509 readsamples(pfile)
510 FILE *pfile;
511 {
512 register i;
513
514
515 if (samples == 0) {
516 samples = (int *) calloc (nsamples, sizeof(int));
517 if (samples == 0) {
518 fprintf( stderr , "%s: No room for %d sample pc's\n",
519 whoami , nsamples);
520 done();
521 }
522 }
523 for (i = 0; i < nsamples; i++) {
524 UNIT raw;
525 int value;
526
527 fread(raw, sizeof (raw), 1, pfile);
528 value = bfd_get_16 (abfd, (bfd_byte *) raw);
529 if (feof(pfile))
530 break;
531 samples[i] += value;
532 }
533 if (i != nsamples) {
534 fprintf(stderr,
535 "%s: unexpected EOF after reading %d/%d samples\n",
536 whoami , --i , nsamples );
537 done();
538 }
539 }
540
541 /*
542 * Assign samples to the procedures to which they belong.
543 *
544 * There are three cases as to where pcl and pch can be
545 * with respect to the routine entry addresses svalue0 and svalue1
546 * as shown in the following diagram. overlap computes the
547 * distance between the arrows, the fraction of the sample
548 * that is to be credited to the routine which starts at svalue0.
549 *
550 * svalue0 svalue1
551 * | |
552 * v v
553 *
554 * +-----------------------------------------------+
555 * | |
556 * | ->| |<- ->| |<- ->| |<- |
557 * | | | | | |
558 * +---------+ +---------+ +---------+
559 *
560 * ^ ^ ^ ^ ^ ^
561 * | | | | | |
562 * pcl pch pcl pch pcl pch
563 *
564 * For the vax we assert that samples will never fall in the first
565 * two bytes of any routine, since that is the entry mask,
566 * thus we give call alignentries() to adjust the entry points if
567 * the entry mask falls in one bucket but the code for the routine
568 * doesn't start until the next bucket. In conjunction with the
569 * alignment of routine addresses, this should allow us to have
570 * only one sample for every four bytes of text space and never
571 * have any overlap (the two end cases, above).
572 */
573 asgnsamples()
574 {
575 register int j;
576 int ccnt;
577 double time;
578 unsigned long pcl, pch;
579 register int i;
580 unsigned long overlap;
581 unsigned long svalue0, svalue1;
582
583 /* read samples and assign to namelist symbols */
584 scale = highpc - lowpc;
585 scale /= nsamples - 1;
586 alignentries();
587 for (i = 0, j = 1; i < nsamples; i++) {
588 ccnt = samples[i];
589 if (ccnt == 0)
590 continue;
591 pcl = lowpc + scale * i;
592 pch = lowpc + scale * (i + 1);
593 time = ccnt;
594 # ifdef DEBUG
595 if ( debug & SAMPLEDEBUG ) {
596 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
597 pcl , pch , ccnt );
598 }
599 # endif DEBUG
600 totime += time;
601 for (j = j - 1; j < nname; j++) {
602 svalue0 = nl[j].svalue;
603 svalue1 = nl[j+1].svalue;
604 /*
605 * if high end of tick is below entry address,
606 * go for next tick.
607 */
608 if (pch < svalue0)
609 break;
610 /*
611 * if low end of tick into next routine,
612 * go for next routine.
613 */
614 if (pcl >= svalue1)
615 continue;
616 overlap = min(pch, svalue1) - max(pcl, svalue0);
617 if (overlap > 0) {
618 # ifdef DEBUG
619 if (debug & SAMPLEDEBUG) {
620 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
621 nl[j].value/sizeof(UNIT), svalue0, svalue1,
622 nl[j].name,
623 overlap * time / scale, overlap);
624 }
625 # endif DEBUG
626 nl[j].time += overlap * time / scale;
627 }
628 }
629 }
630 # ifdef DEBUG
631 if (debug & SAMPLEDEBUG) {
632 printf("[asgnsamples] totime %f\n", totime);
633 }
634 # endif DEBUG
635 }
636
637
638 unsigned long
639 min(a, b)
640 unsigned long a,b;
641 {
642 if (a<b)
643 return(a);
644 return(b);
645 }
646
647 unsigned long
648 max(a, b)
649 unsigned long a,b;
650 {
651 if (a>b)
652 return(a);
653 return(b);
654 }
655
656 /*
657 * calculate scaled entry point addresses (to save time in asgnsamples),
658 * and possibly push the scaled entry points over the entry mask,
659 * if it turns out that the entry point is in one bucket and the code
660 * for a routine is in the next bucket.
661 */
662 alignentries()
663 {
664 register struct nl *nlp;
665 unsigned long bucket_of_entry;
666 unsigned long bucket_of_code;
667
668 for (nlp = nl; nlp < npe; nlp++) {
669 nlp -> svalue = nlp -> value / sizeof(UNIT);
670 bucket_of_entry = (nlp->svalue - lowpc) / scale;
671 bucket_of_code = (nlp->svalue + UNITS_TO_CODE - lowpc) / scale;
672 if (bucket_of_entry < bucket_of_code) {
673 # ifdef DEBUG
674 if (debug & SAMPLEDEBUG) {
675 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
676 nlp->svalue, nlp->svalue + UNITS_TO_CODE);
677 }
678 # endif DEBUG
679 nlp->svalue += UNITS_TO_CODE;
680 }
681 }
682 }
683
684 bool
685 funcsymbol( symp )
686 asymbol *symp;
687 {
688 extern char *strtab; /* string table from a.out */
689 extern int aflag; /* if static functions aren't desired */
690 CONST char *name;
691 int i;
692 char symprefix;
693
694 /*
695 * must be a text symbol,
696 * and static text symbols don't qualify if aflag set.
697 */
698
699
700 if (!symp->section)
701 return FALSE;
702
703 if (aflag && (symp->flags&BSF_LOCAL)) {
704 #if defined(DEBUG)
705 fprintf (stderr, "%s(%d): %s: not a function\n", __FILE__, __LINE__, symp->name);
706 #endif
707 return FALSE;
708 }
709
710 symprefix = bfd_get_symbol_leading_char (abfd);
711 i = bfd_decode_symclass (symp);
712 #if defined(DEBUG) && 0
713 if (i != 'T' && i != 't')
714 fprintf (stderr, "%s(%d): %s is of class %c\n", __FILE__, __LINE__, symp->name, i);
715 #endif
716
717 /*
718 * Any external text symbol should be okay. (Only problem would be
719 * variables in the text section.)
720 */
721
722 if (i == 'T')
723 return TRUE;
724
725 /*
726 * 't' is static text; -a says to ignore it. So if it's not
727 * a static text symbol, *or* it is and the user gave -a, we
728 * ignore it.
729 */
730
731 if (i != 't' || aflag)
732 return FALSE;
733
734 /*
735 * can't have any `funny' characters in name,
736 * where `funny' includes `.', .o file names
737 * and `$', pascal labels.
738 */
739 if (!symp->name)
740 return FALSE;
741
742 for (name = symp->name; *name; name++) {
743 if ( *name == '.' || *name == '$' ) {
744 return FALSE;
745 }
746 }
747
748 /* On systems where the C compiler adds an underscore to all names,
749 * static names without underscores seem usually to be labels in
750 * hand written assembler in the library. We don't want these
751 * names. This is certainly necessary on a Sparc running SunOS 4.1
752 * (try profiling a program that does a lot of division). I don't
753 * know whether it has harmful side effects on other systems.
754 * Perhaps it should be made configurable.
755 */
756
757 if (symprefix && symprefix != *symp->name)
758 return FALSE;
759
760 return TRUE;
761 }
762
763 done()
764 {
765
766 exit(0);
767 }
This page took 0.092765 seconds and 5 git commands to generate.