* gprof.c (getsymtab): Change nosyms to long. Rename
[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 "5.6 (Cygnus)"
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 long nosyms;
248 asymbol **syms;
249 i = bfd_get_symtab_upper_bound (abfd);/* This will probably give us more
250 * than we need, but that's ok.
251 */
252 if (i < 0)
253 {
254 fprintf (stderr, "%s: %s: %s\n", whoami, a_outname,
255 bfd_errmsg (bfd_get_error ()));
256 exit (1);
257 }
258 syms = (asymbol**)xmalloc (i);
259 nosyms = bfd_canonicalize_symtab (abfd, syms);
260 if (nosyms < 0)
261 {
262 fprintf (stderr, "%s: %s: %s\n", whoami, a_outname,
263 bfd_errmsg (bfd_get_error ()));
264 exit (1);
265 }
266
267 nname = 0;
268 for (i = 0; i < nosyms; i++) {
269 if (!funcsymbol (syms[i]))
270 continue;
271 nname++;
272 }
273
274 if (nname == 0) {
275 fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname );
276 done();
277 }
278 askfor = nname + 1;
279 nl = (nltype *) calloc( askfor , sizeof(nltype) );
280 if (nl == 0) {
281 fprintf(stderr, "%s: No room for %d bytes of symbol table\n",
282 whoami, askfor * sizeof(nltype) );
283 done();
284 }
285
286 /* pass2 - read symbols */
287 npe = nl;
288 nname = 0;
289 for (i = 0; i < nosyms; i++) {
290 if (!funcsymbol (syms[i])) {
291 # ifdef DEBUG
292 if ( debug & AOUTDEBUG ) {
293 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
294 syms[i]->value, syms[i]->name);
295 }
296 # endif DEBUG
297 continue;
298 }
299 /* Symbol offsets are always section-relative. */
300 npe->value = syms[i]->value + syms[i]->section->vma;
301 npe->name = syms[i]->name;
302
303 /* If we see "main" without an initial '_', we assume
304 names are *not* prefixed by '_'. */
305 if (npe->name[0] == 'm' && discard_underscores
306 && strcmp(npe->name, "main") == 0)
307 discard_underscores = 0;
308
309 # ifdef DEBUG
310 if ( debug & AOUTDEBUG ) {
311 printf( "[getsymtab] %d %s 0x%08x\n" ,
312 nname , npe -> name , npe -> value );
313 }
314 # endif DEBUG
315 npe++;
316 nname++;
317 }
318 npe->value = -1;
319 }
320
321 /*
322 * read in the text space of an a.out file
323 */
324 gettextspace( abfd )
325 bfd *abfd;
326 {
327 asection *texsec;
328
329 if ( cflag == 0 ) {
330 return;
331 }
332
333 texsec = bfd_get_section_by_name (abfd, ".text");
334 if (texsec == NULL) {
335 return;
336 }
337
338 textspace = (u_char *) malloc( texsec->_cooked_size );
339
340 if ( textspace == 0 ) {
341 fprintf( stderr , "%s: ran out room for %d bytes of text space: " ,
342 whoami , texsec->_cooked_size);
343 fprintf( stderr , "can't do -c\n" );
344 return;
345 }
346 bfd_get_section_contents (abfd, texsec, textspace, texsec->filepos,
347 texsec->_cooked_size);
348 }
349 /*
350 * information from a gmon.out file is in two parts:
351 * an array of sampling hits within pc ranges,
352 * and the arcs.
353 */
354 getpfile(filename)
355 char *filename;
356 {
357 FILE *pfile;
358 FILE *openpfile();
359 struct rawarc arc;
360 struct veryrawarc rawarc;
361
362 pfile = openpfile(filename);
363 readsamples(pfile);
364 /*
365 * the rest of the file consists of
366 * a bunch of <from,self,count> tuples.
367 */
368 while ( fread( &rawarc , sizeof rawarc , 1 , pfile ) == 1 ) {
369 arc.raw_frompc = bfd_get_32 (abfd, (bfd_byte *) rawarc.raw_frompc);
370 arc.raw_selfpc = bfd_get_32 (abfd, (bfd_byte *) rawarc.raw_selfpc);
371 arc.raw_count = bfd_get_32 (abfd, (bfd_byte *) rawarc.raw_count);
372 # ifdef DEBUG
373 if ( debug & SAMPLEDEBUG ) {
374 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
375 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
376 }
377 # endif DEBUG
378 /*
379 * add this arc
380 */
381 tally( &arc );
382 }
383 fclose(pfile);
384 }
385
386 FILE *
387 openpfile(filename)
388 char *filename;
389 {
390 struct hdr tmp;
391 struct rawhdr raw;
392 FILE *pfile;
393
394 if((pfile = fopen(filename, "r")) == NULL) {
395 perror(filename);
396 done();
397 }
398 if (sizeof(struct rawhdr) != fread(&raw, 1, sizeof(struct rawhdr), pfile))
399 {
400 fprintf(stderr, "%s: file too short to be a gmon file\n", filename);
401 done();
402 }
403 tmp.lowpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &raw.lowpc[0]);
404 tmp.highpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &raw.highpc[0]);
405 tmp.ncnt = bfd_get_32 (abfd, (bfd_byte *) &raw.ncnt[0]);
406
407 if ( s_highpc != 0 && ( tmp.lowpc != h.lowpc ||
408 tmp.highpc != h.highpc || tmp.ncnt != h.ncnt ) ) {
409 fprintf(stderr, "%s: incompatible with first gmon file\n", filename);
410 done();
411 }
412 h = tmp;
413 s_lowpc = (unsigned long) h.lowpc;
414 s_highpc = (unsigned long) h.highpc;
415 lowpc = (unsigned long)h.lowpc / sizeof(UNIT);
416 highpc = (unsigned long)h.highpc / sizeof(UNIT);
417 sampbytes = h.ncnt - sizeof(struct rawhdr);
418 nsamples = sampbytes / sizeof (UNIT);
419 # ifdef DEBUG
420 if ( debug & SAMPLEDEBUG ) {
421 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
422 h.lowpc , h.highpc , h.ncnt );
423 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
424 s_lowpc , s_highpc );
425 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
426 lowpc , highpc );
427 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
428 sampbytes , nsamples );
429 }
430 # endif DEBUG
431 return(pfile);
432 }
433
434 tally( rawp )
435 struct rawarc *rawp;
436 {
437 nltype *parentp;
438 nltype *childp;
439
440 parentp = nllookup( rawp -> raw_frompc );
441 childp = nllookup( rawp -> raw_selfpc );
442 if ( kflag
443 && onlist( kfromlist , parentp -> name )
444 && onlist( ktolist , childp -> name ) ) {
445 return;
446 }
447 childp -> ncall += rawp -> raw_count;
448 # ifdef DEBUG
449 if ( debug & TALLYDEBUG ) {
450 printf( "[tally] arc from %s to %s traversed %d times\n" ,
451 parentp -> name , childp -> name , rawp -> raw_count );
452 }
453 # endif DEBUG
454 addarc( parentp , childp , rawp -> raw_count );
455 }
456
457 /*
458 * dump out the gmon.sum file
459 */
460 dumpsum( sumfile )
461 char *sumfile;
462 {
463 register nltype *nlp;
464 register arctype *arcp;
465 struct rawarc arc;
466 FILE *sfile;
467
468 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) {
469 perror( sumfile );
470 done();
471 }
472 /*
473 * dump the header; use the last header read in
474 */
475 if ( fwrite( &h , sizeof h , 1 , sfile ) != 1 ) {
476 perror( sumfile );
477 done();
478 }
479 /*
480 * dump the samples
481 */
482 if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) {
483 perror( sumfile );
484 done();
485 }
486 /*
487 * dump the normalized raw arc information
488 */
489 for ( nlp = nl ; nlp < npe ; nlp++ ) {
490 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
491 arc.raw_frompc = arcp -> arc_parentp -> value;
492 arc.raw_selfpc = arcp -> arc_childp -> value;
493 arc.raw_count = arcp -> arc_count;
494 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) {
495 perror( sumfile );
496 done();
497 }
498 # ifdef DEBUG
499 if ( debug & SAMPLEDEBUG ) {
500 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
501 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
502 }
503 # endif DEBUG
504 }
505 }
506 fclose( sfile );
507 }
508
509 valcmp(p1, p2)
510 nltype *p1, *p2;
511 {
512 if ( p1 -> value < p2 -> value ) {
513 return LESSTHAN;
514 }
515 if ( p1 -> value > p2 -> value ) {
516 return GREATERTHAN;
517 }
518 return EQUALTO;
519 }
520
521 readsamples(pfile)
522 FILE *pfile;
523 {
524 register i;
525
526
527 if (samples == 0) {
528 samples = (int *) calloc (nsamples, sizeof(int));
529 if (samples == 0) {
530 fprintf( stderr , "%s: No room for %d sample pc's\n",
531 whoami , nsamples);
532 done();
533 }
534 }
535 for (i = 0; i < nsamples; i++) {
536 UNIT raw;
537 int value;
538
539 fread(raw, sizeof (raw), 1, pfile);
540 value = bfd_get_16 (abfd, (bfd_byte *) raw);
541 if (feof(pfile))
542 break;
543 samples[i] += value;
544 }
545 if (i != nsamples) {
546 fprintf(stderr,
547 "%s: unexpected EOF after reading %d/%d samples\n",
548 whoami , --i , nsamples );
549 done();
550 }
551 }
552
553 /*
554 * Assign samples to the procedures to which they belong.
555 *
556 * There are three cases as to where pcl and pch can be
557 * with respect to the routine entry addresses svalue0 and svalue1
558 * as shown in the following diagram. overlap computes the
559 * distance between the arrows, the fraction of the sample
560 * that is to be credited to the routine which starts at svalue0.
561 *
562 * svalue0 svalue1
563 * | |
564 * v v
565 *
566 * +-----------------------------------------------+
567 * | |
568 * | ->| |<- ->| |<- ->| |<- |
569 * | | | | | |
570 * +---------+ +---------+ +---------+
571 *
572 * ^ ^ ^ ^ ^ ^
573 * | | | | | |
574 * pcl pch pcl pch pcl pch
575 *
576 * For the vax we assert that samples will never fall in the first
577 * two bytes of any routine, since that is the entry mask,
578 * thus we give call alignentries() to adjust the entry points if
579 * the entry mask falls in one bucket but the code for the routine
580 * doesn't start until the next bucket. In conjunction with the
581 * alignment of routine addresses, this should allow us to have
582 * only one sample for every four bytes of text space and never
583 * have any overlap (the two end cases, above).
584 */
585 asgnsamples()
586 {
587 register int j;
588 int ccnt;
589 double time;
590 unsigned long pcl, pch;
591 register int i;
592 unsigned long overlap;
593 unsigned long svalue0, svalue1;
594
595 /* read samples and assign to namelist symbols */
596 scale = highpc - lowpc;
597 scale /= nsamples - 1;
598 alignentries();
599 for (i = 0, j = 1; i < nsamples; i++) {
600 ccnt = samples[i];
601 if (ccnt == 0)
602 continue;
603 pcl = lowpc + scale * i;
604 pch = lowpc + scale * (i + 1);
605 time = ccnt;
606 # ifdef DEBUG
607 if ( debug & SAMPLEDEBUG ) {
608 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
609 pcl , pch , ccnt );
610 }
611 # endif DEBUG
612 totime += time;
613 for (j = j - 1; j < nname; j++) {
614 svalue0 = nl[j].svalue;
615 svalue1 = nl[j+1].svalue;
616 /*
617 * if high end of tick is below entry address,
618 * go for next tick.
619 */
620 if (pch < svalue0)
621 break;
622 /*
623 * if low end of tick into next routine,
624 * go for next routine.
625 */
626 if (pcl >= svalue1)
627 continue;
628 overlap = min(pch, svalue1) - max(pcl, svalue0);
629 if (overlap > 0) {
630 # ifdef DEBUG
631 if (debug & SAMPLEDEBUG) {
632 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
633 nl[j].value/sizeof(UNIT), svalue0, svalue1,
634 nl[j].name,
635 overlap * time / scale, overlap);
636 }
637 # endif DEBUG
638 nl[j].time += overlap * time / scale;
639 }
640 }
641 }
642 # ifdef DEBUG
643 if (debug & SAMPLEDEBUG) {
644 printf("[asgnsamples] totime %f\n", totime);
645 }
646 # endif DEBUG
647 }
648
649
650 unsigned long
651 min(a, b)
652 unsigned long a,b;
653 {
654 if (a<b)
655 return(a);
656 return(b);
657 }
658
659 unsigned long
660 max(a, b)
661 unsigned long a,b;
662 {
663 if (a>b)
664 return(a);
665 return(b);
666 }
667
668 /*
669 * calculate scaled entry point addresses (to save time in asgnsamples),
670 * and possibly push the scaled entry points over the entry mask,
671 * if it turns out that the entry point is in one bucket and the code
672 * for a routine is in the next bucket.
673 */
674 alignentries()
675 {
676 register struct nl *nlp;
677 unsigned long bucket_of_entry;
678 unsigned long bucket_of_code;
679
680 for (nlp = nl; nlp < npe; nlp++) {
681 nlp -> svalue = nlp -> value / sizeof(UNIT);
682 bucket_of_entry = (nlp->svalue - lowpc) / scale;
683 bucket_of_code = (nlp->svalue + UNITS_TO_CODE - lowpc) / scale;
684 if (bucket_of_entry < bucket_of_code) {
685 # ifdef DEBUG
686 if (debug & SAMPLEDEBUG) {
687 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
688 nlp->svalue, nlp->svalue + UNITS_TO_CODE);
689 }
690 # endif DEBUG
691 nlp->svalue += UNITS_TO_CODE;
692 }
693 }
694 }
695
696 bool
697 funcsymbol( symp )
698 asymbol *symp;
699 {
700 extern char *strtab; /* string table from a.out */
701 extern int aflag; /* if static functions aren't desired */
702 CONST char *name;
703 int i;
704 char symprefix;
705 symbol_info syminfo;
706
707 /*
708 * must be a text symbol,
709 * and static text symbols don't qualify if aflag set.
710 */
711
712
713 if (!symp->section)
714 return FALSE;
715
716 if (aflag && (symp->flags&BSF_LOCAL)) {
717 #if defined(DEBUG)
718 fprintf (stderr, "%s(%d): %s: not a function\n", __FILE__, __LINE__, symp->name);
719 #endif
720 return FALSE;
721 }
722
723
724 symprefix = bfd_get_symbol_leading_char (abfd);
725 bfd_get_symbol_info (abfd, symp, &syminfo);
726 i = syminfo.type;
727 #if defined(DEBUG) && 0
728 if (i != 'T' && i != 't')
729 fprintf (stderr, "%s(%d): %s is of class %c\n", __FILE__, __LINE__, symp->name, i);
730 #endif
731
732 /*
733 * Any external text symbol should be okay. (Only problem would be
734 * variables in the text section.)
735 */
736
737 if (i == 'T')
738 return TRUE;
739
740 /*
741 * 't' is static text; -a says to ignore it. So if it's not
742 * a static text symbol, *or* it is and the user gave -a, we
743 * ignore it.
744 */
745
746 if (i != 't' || aflag)
747 return FALSE;
748
749 /*
750 * can't have any `funny' characters in name,
751 * where `funny' includes `.', .o file names
752 * and `$', pascal labels.
753 */
754 if (!symp->name)
755 return FALSE;
756
757 for (name = symp->name; *name; name++) {
758 if ( *name == '.' || *name == '$' ) {
759 return FALSE;
760 }
761 }
762
763 /* On systems where the C compiler adds an underscore to all names,
764 * static names without underscores seem usually to be labels in
765 * hand written assembler in the library. We don't want these
766 * names. This is certainly necessary on a Sparc running SunOS 4.1
767 * (try profiling a program that does a lot of division). I don't
768 * know whether it has harmful side effects on other systems.
769 * Perhaps it should be made configurable.
770 */
771
772 if (symprefix && symprefix != *symp->name)
773 return FALSE;
774
775 return TRUE;
776 }
777
778 done()
779 {
780
781 exit(0);
782 }
This page took 0.044096 seconds and 5 git commands to generate.