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.
22 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
23 All rights reserved.\n";
27 static char sccsid[] = "@(#)gprof.c 5.6 (Berkeley) 6/1/90";
34 char *whoami = "gprof";
37 * things which get -E excluded by default.
39 char *defaultEs[] = { "mcount" , "__mcleanup" , 0 };
41 int discard_underscores = 1; /* Should we discard initial underscores? */
42 int bsd_style_output = 0; /* As opposed to FSF style output */
55 while ( *argv != 0 && **argv == '-' ) {
70 debug |= atoi( *argv );
73 printf("[main] debug = %d\n", debug);
75 printf("%s: -d ignored\n", whoami);
80 addlist( Elist , *argv );
82 addlist( elist , *argv );
86 addlist( elist , *++argv );
91 addlist( Flist , *argv );
93 addlist( flist , *argv );
97 addlist( flist , *++argv );
101 addlist( kfromlist , *++argv );
102 addlist( ktolist , *++argv );
108 case 'T': /* "Traditional" output format */
109 bsd_style_output = 1;
115 fprintf (stderr, "usage: gproff [-a] [-b] [-c] [-d[num]] \
116 [-E function-name] [-e function-name] \
117 [-F function-name] [-f function-name] \
118 [-k from to] [-s] [-T] [-z] [image-file] \
128 a_outname = A_OUTNAME;
137 * turn off default functions
139 for ( sp = &defaultEs[0] ; *sp ; sp++ ) {
141 addlist( Elist , *sp );
143 addlist( elist , *sp );
146 * how many ticks per second?
147 * if we can't tell, report time in ticks.
152 fprintf(stderr, "time is in ticks, not seconds\n");
155 * get information about a.out file.
159 * get information about mon.out file(s).
162 getpfile( gmonname );
166 } while ( *argv++ != 0 );
168 * dump out a gmon.sum file if requested
174 * assign samples to procedures
178 * assemble the dynamic profile
180 timesortnlp = doarcs();
182 if (bsd_style_output) {
183 printgprof( timesortnlp ); /* print the dynamic profile */
184 printprof(); /* print the flat profile */
186 printprof(); /* print the flat profile */
187 printgprof( timesortnlp ); /* print the dynamic profile */
197 * Set up string and symbol tables from a.out.
198 * and optionally the text space.
199 * On return symbol table is sorted by value.
205 abfd = bfd_openr (a_outname, NULL);
212 if (!bfd_check_format (abfd, bfd_object)) {
213 fprintf (stderr, "%s: %s: bad format\n", whoami, a_outname);
217 /* getstrtab(nfile); */
219 gettextspace( abfd );
220 qsort(nl, nname, sizeof(nltype), valcmp);
223 if ( debug & AOUTDEBUG ) {
226 for (j = 0; j < nname; j++){
227 printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name);
234 * Read in symbol table
243 i = get_symtab_upper_bound (abfd); /* This will probably give us more
244 * than we need, but that's ok.
246 syms = (asymbol**)xmalloc (i);
247 nosyms = bfd_canonicalize_symtab (abfd, syms);
250 for (i = 0; i < nosyms; i++) {
251 if (!funcsymbol (syms[i]))
257 fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname );
261 nl = (nltype *) calloc( askfor , sizeof(nltype) );
263 fprintf(stderr, "%s: No room for %d bytes of symbol table\n",
264 whoami, askfor * sizeof(nltype) );
268 /* pass2 - read symbols */
271 for (i = 0; i < nosyms; i++) {
272 if (!funcsymbol (syms[i])) {
274 if ( debug & AOUTDEBUG ) {
275 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
276 syms[i]->value, syms[i]->name);
281 /* Symbol offsets are always section-relative. */
282 npe->value = syms[i]->value + syms[i]->section->vma;
283 npe->name = syms[i]->name;
285 /* If we see "main" without an initial '_', we assume
286 names are *not* prefixed by '_'. */
287 if (npe->name[0] == 'm' && discard_underscores
288 && strcmp(npe->name, "main") == 0)
289 discard_underscores = 0;
292 if ( debug & AOUTDEBUG ) {
293 printf( "[getsymtab] %d %s 0x%08x\n" ,
294 nname , npe -> name , npe -> value );
304 * read in the text space of an a.out file
315 texsec = bfd_get_section_by_name (abfd, ".text");
316 if (texsec == NULL) {
320 textspace = (u_char *) malloc( texsec->_cooked_size );
322 if ( textspace == 0 ) {
323 fprintf( stderr , "%s: ran out room for %d bytes of text space: " ,
324 whoami , texsec->_cooked_size);
325 fprintf( stderr , "can't do -c\n" );
328 bfd_get_section_contents (abfd, texsec, textspace, texsec->filepos,
329 texsec->_cooked_size);
332 * information from a gmon.out file is in two parts:
333 * an array of sampling hits within pc ranges,
343 pfile = openpfile(filename);
346 * the rest of the file consists of
347 * a bunch of <from,self,count> tuples.
349 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
350 arc.raw_frompc = bfd_get_32 (abfd, (bfd_byte *) &arc.raw_frompc);
351 arc.raw_selfpc = bfd_get_32 (abfd, (bfd_byte *) &arc.raw_selfpc);
352 arc.raw_count = bfd_get_32 (abfd, (bfd_byte *) &arc.raw_count);
354 if ( debug & SAMPLEDEBUG ) {
355 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
356 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
374 if((pfile = fopen(filename, "r")) == NULL) {
378 fread(&tmp, sizeof(struct hdr), 1, pfile);
379 tmp.lowpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &tmp.lowpc);
380 tmp.highpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &tmp.highpc);
381 tmp.ncnt = bfd_get_32 (abfd, (bfd_byte *) &tmp.ncnt);
383 if ( s_highpc != 0 && ( tmp.lowpc != h.lowpc ||
384 tmp.highpc != h.highpc || tmp.ncnt != h.ncnt ) ) {
385 fprintf(stderr, "%s: incompatible with first gmon file\n", filename);
389 s_lowpc = (unsigned long) h.lowpc;
390 s_highpc = (unsigned long) h.highpc;
391 lowpc = (unsigned long)h.lowpc / sizeof(UNIT);
392 highpc = (unsigned long)h.highpc / sizeof(UNIT);
393 sampbytes = h.ncnt - sizeof(struct hdr);
394 nsamples = sampbytes / sizeof (UNIT);
396 if ( debug & SAMPLEDEBUG ) {
397 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
398 h.lowpc , h.highpc , h.ncnt );
399 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
400 s_lowpc , s_highpc );
401 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
403 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
404 sampbytes , nsamples );
416 parentp = nllookup( rawp -> raw_frompc );
417 childp = nllookup( rawp -> raw_selfpc );
419 && onlist( kfromlist , parentp -> name )
420 && onlist( ktolist , childp -> name ) ) {
423 childp -> ncall += rawp -> raw_count;
425 if ( debug & TALLYDEBUG ) {
426 printf( "[tally] arc from %s to %s traversed %d times\n" ,
427 parentp -> name , childp -> name , rawp -> raw_count );
430 addarc( parentp , childp , rawp -> raw_count );
434 * dump out the gmon.sum file
439 register nltype *nlp;
440 register arctype *arcp;
444 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) {
449 * dump the header; use the last header read in
451 if ( fwrite( &h , sizeof h , 1 , sfile ) != 1 ) {
458 if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) {
463 * dump the normalized raw arc information
465 for ( nlp = nl ; nlp < npe ; nlp++ ) {
466 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
467 arc.raw_frompc = arcp -> arc_parentp -> value;
468 arc.raw_selfpc = arcp -> arc_childp -> value;
469 arc.raw_count = arcp -> arc_count;
470 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) {
475 if ( debug & SAMPLEDEBUG ) {
476 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
477 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
488 if ( p1 -> value < p2 -> value ) {
491 if ( p1 -> value > p2 -> value ) {
504 samples = (UNIT *) malloc (sampbytes * sizeof(UNIT));
506 fprintf( stderr , "%s: No room for %d sample pc's\n",
507 whoami , sampbytes / sizeof (UNIT));
510 memset (samples, 0, sampbytes * sizeof(UNIT));
512 for (i = 0; i < nsamples; i++) {
513 fread(&sample, sizeof (UNIT), 1, pfile);
514 sample = bfd_get_16 (abfd, (bfd_byte *) &sample);
517 samples[i] += sample;
521 "%s: unexpected EOF after reading %d/%d samples\n",
522 whoami , --i , nsamples );
528 * Assign samples to the procedures to which they belong.
530 * There are three cases as to where pcl and pch can be
531 * with respect to the routine entry addresses svalue0 and svalue1
532 * as shown in the following diagram. overlap computes the
533 * distance between the arrows, the fraction of the sample
534 * that is to be credited to the routine which starts at svalue0.
540 * +-----------------------------------------------+
542 * | ->| |<- ->| |<- ->| |<- |
544 * +---------+ +---------+ +---------+
548 * pcl pch pcl pch pcl pch
550 * For the vax we assert that samples will never fall in the first
551 * two bytes of any routine, since that is the entry mask,
552 * thus we give call alignentries() to adjust the entry points if
553 * the entry mask falls in one bucket but the code for the routine
554 * doesn't start until the next bucket. In conjunction with the
555 * alignment of routine addresses, this should allow us to have
556 * only one sample for every four bytes of text space and never
557 * have any overlap (the two end cases, above).
564 unsigned long pcl, pch;
566 unsigned long overlap;
567 unsigned long svalue0, svalue1;
569 /* read samples and assign to namelist symbols */
570 scale = highpc - lowpc;
573 for (i = 0, j = 1; i < nsamples; i++) {
577 pcl = lowpc + scale * i;
578 pch = lowpc + scale * (i + 1);
581 if ( debug & SAMPLEDEBUG ) {
582 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
587 for (j = j - 1; j < nname; j++) {
588 svalue0 = nl[j].svalue;
589 svalue1 = nl[j+1].svalue;
591 * if high end of tick is below entry address,
597 * if low end of tick into next routine,
598 * go for next routine.
602 overlap = min(pch, svalue1) - max(pcl, svalue0);
605 if (debug & SAMPLEDEBUG) {
606 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
607 nl[j].value/sizeof(UNIT), svalue0, svalue1,
609 overlap * time / scale, overlap);
612 nl[j].time += overlap * time / scale;
617 if (debug & SAMPLEDEBUG) {
618 printf("[asgnsamples] totime %f\n", totime);
643 * calculate scaled entry point addresses (to save time in asgnsamples),
644 * and possibly push the scaled entry points over the entry mask,
645 * if it turns out that the entry point is in one bucket and the code
646 * for a routine is in the next bucket.
650 register struct nl *nlp;
651 unsigned long bucket_of_entry;
652 unsigned long bucket_of_code;
654 for (nlp = nl; nlp < npe; nlp++) {
655 nlp -> svalue = nlp -> value / sizeof(UNIT);
656 bucket_of_entry = (nlp->svalue - lowpc) / scale;
657 bucket_of_code = (nlp->svalue + UNITS_TO_CODE - lowpc) / scale;
658 if (bucket_of_entry < bucket_of_code) {
660 if (debug & SAMPLEDEBUG) {
661 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
662 nlp->svalue, nlp->svalue + UNITS_TO_CODE);
665 nlp->svalue += UNITS_TO_CODE;
674 extern char *strtab; /* string table from a.out */
675 extern int aflag; /* if static functions aren't desired */
680 * must be a text symbol,
681 * and static text symbols don't qualify if aflag set.
688 if (!aflag && (symp->flags&BSF_LOCAL)) {
690 fprintf (stderr, "%s(%d): %s: not a function\n", __FILE__, __LINE__, symp->name);
695 * can't have any `funny' characters in name,
696 * where `funny' includes `.', .o file names
697 * and `$', pascal labels.
702 for (name = symp->name; *name; name++) {
703 if ( *name == '.' || *name == '$' ) {
708 i = bfd_decode_symclass (symp);
709 #if defined(DEBUG) && 0
710 if (i != 'T' && i != 't')
711 fprintf (stderr, "%s(%d): %s is of class %c\n", __FILE__, __LINE__, symp->name, i);
714 return (i == 'T' || i == 't');