2 * Copyright (c) 1983 Regents of the University of California.
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.
20 #define VERSION "5.6 (Cygnus)"
24 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
25 All rights reserved.\n";
29 static char sccsid[] = "@(#)gprof.c 5.6 (Berkeley) 6/1/90";
43 * things which get -E excluded by default.
45 char *defaultEs[] = { "mcount" , "__mcleanup" , 0 };
47 int discard_underscores = 1; /* Should we discard initial underscores? */
48 int bsd_style_output = 0; /* As opposed to FSF style output */
62 while ( *argv != 0 && **argv == '-' ) {
77 debug |= atoi( *argv );
80 printf("[main] debug = %d\n", debug);
82 printf("%s: -d ignored\n", whoami);
87 addlist( Elist , *argv );
89 addlist( elist , *argv );
93 addlist( elist , *++argv );
98 addlist( Flist , *argv );
100 addlist( flist , *argv );
104 addlist( flist , *++argv );
108 addlist( kfromlist , *++argv );
109 addlist( ktolist , *++argv );
115 case 'T': /* "Traditional" output format */
116 bsd_style_output = 1;
119 printf ("gprof version %s\n", VERSION);
127 Usage: %s [-a] [-b] [-c] [-d[num]] [-E function-name] [-e function-name]\n\
128 [-F function-name] [-f function-name] [-k from to] [-s] [-T] [-z]\n\
129 [image-file] [profile-file...]\n", whoami);
138 a_outname = A_OUTNAME;
147 * turn off default functions
149 for ( sp = &defaultEs[0] ; *sp ; sp++ ) {
151 addlist( Elist , *sp );
153 addlist( elist , *sp );
156 * how many ticks per second?
157 * if we can't tell, report time in ticks.
162 fprintf(stderr, "time is in ticks, not seconds\n");
165 * get information about a.out file.
169 * get information about mon.out file(s).
172 getpfile( gmonname );
176 } while ( *argv++ != 0 );
178 * dump out a gmon.sum file if requested
184 * assign samples to procedures
188 * assemble the dynamic profile
190 timesortnlp = doarcs();
192 if (bsd_style_output) {
193 printgprof( timesortnlp ); /* print the dynamic profile */
194 printprof(); /* print the flat profile */
196 printprof(); /* print the flat profile */
197 printgprof( timesortnlp ); /* print the dynamic profile */
207 * Set up string and symbol tables from a.out.
208 * and optionally the text space.
209 * On return symbol table is sorted by value.
215 abfd = bfd_openr (a_outname, NULL);
222 if (!bfd_check_format (abfd, bfd_object)) {
223 fprintf (stderr, "%s: %s: bad format\n", whoami, a_outname);
227 /* getstrtab(nfile); */
229 gettextspace( abfd );
230 qsort(nl, nname, sizeof(nltype), valcmp);
233 if ( debug & AOUTDEBUG ) {
236 for (j = 0; j < nname; j++){
237 printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name);
244 * Read in symbol table
253 i = bfd_get_symtab_upper_bound (abfd);/* This will probably give us more
254 * than we need, but that's ok.
258 fprintf (stderr, "%s: %s: %s\n", whoami, a_outname,
259 bfd_errmsg (bfd_get_error ()));
262 syms = (asymbol**)xmalloc (i);
263 nosyms = bfd_canonicalize_symtab (abfd, syms);
266 fprintf (stderr, "%s: %s: %s\n", whoami, a_outname,
267 bfd_errmsg (bfd_get_error ()));
272 for (i = 0; i < nosyms; i++) {
273 if (!funcsymbol (syms[i]))
279 fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname );
283 nl = (nltype *) calloc( askfor , sizeof(nltype) );
285 fprintf(stderr, "%s: No room for %d bytes of symbol table\n",
286 whoami, askfor * sizeof(nltype) );
290 /* pass2 - read symbols */
293 for (i = 0; i < nosyms; i++) {
294 if (!funcsymbol (syms[i])) {
296 if ( debug & AOUTDEBUG ) {
297 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
298 syms[i]->value, syms[i]->name);
303 /* Symbol offsets are always section-relative. */
304 npe->value = syms[i]->value + syms[i]->section->vma;
305 npe->name = syms[i]->name;
307 /* If we see "main" without an initial '_', we assume
308 names are *not* prefixed by '_'. */
309 if (npe->name[0] == 'm' && discard_underscores
310 && strcmp(npe->name, "main") == 0)
311 discard_underscores = 0;
314 if ( debug & AOUTDEBUG ) {
315 printf( "[getsymtab] %d %s 0x%08x\n" ,
316 nname , npe -> name , npe -> value );
326 * read in the text space of an a.out file
337 texsec = bfd_get_section_by_name (abfd, ".text");
338 if (texsec == NULL) {
342 textspace = (u_char *) malloc( texsec->_cooked_size );
344 if ( textspace == 0 ) {
345 fprintf( stderr , "%s: ran out room for %d bytes of text space: " ,
346 whoami , texsec->_cooked_size);
347 fprintf( stderr , "can't do -c\n" );
350 bfd_get_section_contents (abfd, texsec, textspace, texsec->filepos,
351 texsec->_cooked_size);
354 * information from a gmon.out file is in two parts:
355 * an array of sampling hits within pc ranges,
364 struct veryrawarc rawarc;
366 pfile = openpfile(filename);
369 * the rest of the file consists of
370 * a bunch of <from,self,count> tuples.
372 while ( fread( &rawarc , sizeof rawarc , 1 , pfile ) == 1 ) {
373 arc.raw_frompc = bfd_get_32 (abfd, (bfd_byte *) rawarc.raw_frompc);
374 arc.raw_selfpc = bfd_get_32 (abfd, (bfd_byte *) rawarc.raw_selfpc);
375 arc.raw_count = bfd_get_32 (abfd, (bfd_byte *) rawarc.raw_count);
377 if ( debug & SAMPLEDEBUG ) {
378 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
379 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
398 if((pfile = fopen(filename, FOPEN_RB)) == NULL) {
402 if (sizeof(struct rawhdr) != fread(&raw, 1, sizeof(struct rawhdr), pfile))
404 fprintf(stderr, "%s: file too short to be a gmon file\n", filename);
407 tmp.lowpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &raw.lowpc[0]);
408 tmp.highpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &raw.highpc[0]);
409 tmp.ncnt = bfd_get_32 (abfd, (bfd_byte *) &raw.ncnt[0]);
411 if ( s_highpc != 0 && ( tmp.lowpc != h.lowpc ||
412 tmp.highpc != h.highpc || tmp.ncnt != h.ncnt ) ) {
413 fprintf(stderr, "%s: incompatible with first gmon file\n", filename);
417 s_lowpc = (unsigned long) h.lowpc;
418 s_highpc = (unsigned long) h.highpc;
419 lowpc = (unsigned long)h.lowpc / sizeof(UNIT);
420 highpc = (unsigned long)h.highpc / sizeof(UNIT);
421 sampbytes = h.ncnt - sizeof(struct rawhdr);
422 nsamples = sampbytes / sizeof (UNIT);
424 if ( debug & SAMPLEDEBUG ) {
425 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
426 h.lowpc , h.highpc , h.ncnt );
427 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
428 s_lowpc , s_highpc );
429 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
431 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
432 sampbytes , nsamples );
444 parentp = nllookup( rawp -> raw_frompc );
445 childp = nllookup( rawp -> raw_selfpc );
447 && onlist( kfromlist , parentp -> name )
448 && onlist( ktolist , childp -> name ) ) {
451 childp -> ncall += rawp -> raw_count;
453 if ( debug & TALLYDEBUG ) {
454 printf( "[tally] arc from %s to %s traversed %d times\n" ,
455 parentp -> name , childp -> name , rawp -> raw_count );
458 addarc( parentp , childp , rawp -> raw_count );
462 * dump out the gmon.sum file
467 register nltype *nlp;
468 register arctype *arcp;
472 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) {
477 * dump the header; use the last header read in
479 if ( fwrite( &h , sizeof h , 1 , sfile ) != 1 ) {
486 if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) {
491 * dump the normalized raw arc information
493 for ( nlp = nl ; nlp < npe ; nlp++ ) {
494 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
495 arc.raw_frompc = arcp -> arc_parentp -> value;
496 arc.raw_selfpc = arcp -> arc_childp -> value;
497 arc.raw_count = arcp -> arc_count;
498 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) {
503 if ( debug & SAMPLEDEBUG ) {
504 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
505 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
516 if ( p1 -> value < p2 -> value ) {
519 if ( p1 -> value > p2 -> value ) {
532 samples = (int *) calloc (nsamples, sizeof(int));
534 fprintf( stderr , "%s: No room for %d sample pc's\n",
539 for (i = 0; i < nsamples; i++) {
543 fread(raw, sizeof (raw), 1, pfile);
544 value = bfd_get_16 (abfd, (bfd_byte *) raw);
551 "%s: unexpected EOF after reading %d/%d samples\n",
552 whoami , --i , nsamples );
558 * Assign samples to the procedures to which they belong.
560 * There are three cases as to where pcl and pch can be
561 * with respect to the routine entry addresses svalue0 and svalue1
562 * as shown in the following diagram. overlap computes the
563 * distance between the arrows, the fraction of the sample
564 * that is to be credited to the routine which starts at svalue0.
570 * +-----------------------------------------------+
572 * | ->| |<- ->| |<- ->| |<- |
574 * +---------+ +---------+ +---------+
578 * pcl pch pcl pch pcl pch
580 * For the vax we assert that samples will never fall in the first
581 * two bytes of any routine, since that is the entry mask,
582 * thus we give call alignentries() to adjust the entry points if
583 * the entry mask falls in one bucket but the code for the routine
584 * doesn't start until the next bucket. In conjunction with the
585 * alignment of routine addresses, this should allow us to have
586 * only one sample for every four bytes of text space and never
587 * have any overlap (the two end cases, above).
594 unsigned long pcl, pch;
596 unsigned long overlap;
597 unsigned long svalue0, svalue1;
599 /* read samples and assign to namelist symbols */
600 scale = highpc - lowpc;
601 scale /= nsamples - 1;
603 for (i = 0, j = 1; i < nsamples; i++) {
607 pcl = lowpc + scale * i;
608 pch = lowpc + scale * (i + 1);
611 if ( debug & SAMPLEDEBUG ) {
612 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
617 for (j = j - 1; j < nname; j++) {
618 svalue0 = nl[j].svalue;
619 svalue1 = nl[j+1].svalue;
621 * if high end of tick is below entry address,
627 * if low end of tick into next routine,
628 * go for next routine.
632 overlap = min(pch, svalue1) - max(pcl, svalue0);
635 if (debug & SAMPLEDEBUG) {
636 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
637 nl[j].value/sizeof(UNIT), svalue0, svalue1,
639 overlap * time / scale, overlap);
642 nl[j].time += overlap * time / scale;
647 if (debug & SAMPLEDEBUG) {
648 printf("[asgnsamples] totime %f\n", totime);
673 * calculate scaled entry point addresses (to save time in asgnsamples),
674 * and possibly push the scaled entry points over the entry mask,
675 * if it turns out that the entry point is in one bucket and the code
676 * for a routine is in the next bucket.
680 register struct nl *nlp;
681 unsigned long bucket_of_entry;
682 unsigned long bucket_of_code;
684 for (nlp = nl; nlp < npe; nlp++) {
685 nlp -> svalue = nlp -> value / sizeof(UNIT);
686 bucket_of_entry = (nlp->svalue - lowpc) / scale;
687 bucket_of_code = (nlp->svalue + UNITS_TO_CODE - lowpc) / scale;
688 if (bucket_of_entry < bucket_of_code) {
690 if (debug & SAMPLEDEBUG) {
691 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
692 nlp->svalue, nlp->svalue + UNITS_TO_CODE);
695 nlp->svalue += UNITS_TO_CODE;
704 extern char *strtab; /* string table from a.out */
705 extern int aflag; /* if static functions aren't desired */
712 * must be a text symbol,
713 * and static text symbols don't qualify if aflag set.
720 if (aflag && (symp->flags&BSF_LOCAL)) {
722 fprintf (stderr, "%s(%d): %s: not a function\n", __FILE__, __LINE__, symp->name);
728 symprefix = bfd_get_symbol_leading_char (abfd);
729 bfd_get_symbol_info (abfd, symp, &syminfo);
731 #if defined(DEBUG) && 0
732 if (i != 'T' && i != 't')
733 fprintf (stderr, "%s(%d): %s is of class %c\n", __FILE__, __LINE__, symp->name, i);
737 * Any external text symbol should be okay. (Only problem would be
738 * variables in the text section.)
745 * 't' is static text; -a says to ignore it. So if it's not
746 * a static text symbol, *or* it is and the user gave -a, we
750 if (i != 't' || aflag)
754 * can't have any `funny' characters in name,
755 * where `funny' includes `.', .o file names
756 * and `$', pascal labels.
761 for (name = symp->name; *name; name++) {
762 if ( *name == '.' || *name == '$' ) {
767 /* On systems where the C compiler adds an underscore to all names,
768 * static names without underscores seem usually to be labels in
769 * hand written assembler in the library. We don't want these
770 * names. This is certainly necessary on a Sparc running SunOS 4.1
771 * (try profiling a program that does a lot of division). I don't
772 * know whether it has harmful side effects on other systems.
773 * Perhaps it should be made configurable.
776 if (symprefix && symprefix != *symp->name
777 /* Gcc may add special symbols to help gdb figure out the file
778 language. We want to ignore these, since sometimes they
779 mask the real function. (dj@ctron) */
780 || !strncmp (symp->name, "__gnu_compiled", 14)
781 || !strncmp (symp->name, "___gnu_compiled", 15))