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 };
52 while ( *argv != 0 && **argv == '-' ) {
67 debug |= atoi( *argv );
70 printf("[main] debug = %d\n", debug);
72 printf("%s: -d ignored\n", whoami);
77 addlist( Elist , *argv );
79 addlist( elist , *argv );
83 addlist( elist , *++argv );
88 addlist( Flist , *argv );
90 addlist( flist , *argv );
94 addlist( flist , *++argv );
98 addlist( kfromlist , *++argv );
99 addlist( ktolist , *++argv );
115 a_outname = A_OUTNAME;
124 * turn off default functions
126 for ( sp = &defaultEs[0] ; *sp ; sp++ ) {
128 addlist( Elist , *sp );
130 addlist( elist , *sp );
133 * how many ticks per second?
134 * if we can't tell, report time in ticks.
139 fprintf(stderr, "time is in ticks, not seconds\n");
142 * get information about a.out file.
146 * get information about mon.out file(s).
149 getpfile( gmonname );
153 } while ( *argv++ != 0 );
155 * dump out a gmon.sum file if requested
161 * assign samples to procedures
165 * assemble the dynamic profile
167 timesortnlp = doarcs();
169 * print the dynamic profile
171 printgprof( timesortnlp );
173 * print the flat profile
184 * Set up string and symbol tables from a.out.
185 * and optionally the text space.
186 * On return symbol table is sorted by value.
192 abfd = bfd_openr (a_outname, NULL);
199 if (!bfd_check_format (abfd, bfd_object)) {
200 fprintf (stderr, "%s: %s: bad format\n", whoami, a_outname);
204 /* getstrtab(nfile); */
206 gettextspace( abfd );
207 qsort(nl, nname, sizeof(nltype), valcmp);
210 if ( debug & AOUTDEBUG ) {
213 for (j = 0; j < nname; j++){
214 printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name);
221 * Read in symbol table
230 i = get_symtab_upper_bound (abfd); /* This will probably give us more
231 * than we need, but that's ok.
233 syms = (asymbol**)malloc (i);
234 nosyms = bfd_canonicalize_symtab (abfd, syms);
237 for (i = 0; i < nosyms; i++) {
238 if (!funcsymbol (syms[i]))
244 fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname );
248 nl = (nltype *) calloc( askfor , sizeof(nltype) );
250 fprintf(stderr, "%s: No room for %d bytes of symbol table\n",
251 whoami, askfor * sizeof(nltype) );
255 /* pass2 - read symbols */
258 for (i = 0; i < nosyms; i++) {
259 if (!funcsymbol (syms[i])) {
261 if ( debug & AOUTDEBUG ) {
262 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
263 syms[i]->value, syms[i]->name);
269 npe->value = syms[i]->value + syms[i]->section->vma;
271 npe->value = syms[i]->value;
273 npe->name = syms[i]->name;
275 if ( debug & AOUTDEBUG ) {
276 printf( "[getsymtab] %d %s 0x%08x\n" ,
277 nname , npe -> name , npe -> value );
287 * read in the text space of an a.out file
298 texsec = bfd_get_section_by_name (abfd, ".text");
299 if (texsec == NULL) {
303 textspace = (u_char *) malloc( texsec->_cooked_size );
305 if ( textspace == 0 ) {
306 fprintf( stderr , "%s: ran out room for %d bytes of text space: " ,
307 whoami , texsec->_cooked_size);
308 fprintf( stderr , "can't do -c\n" );
311 bfd_get_section_contents (abfd, texsec, textspace, texsec->filepos,
312 texsec->_cooked_size);
315 * information from a gmon.out file is in two parts:
316 * an array of sampling hits within pc ranges,
326 pfile = openpfile(filename);
329 * the rest of the file consists of
330 * a bunch of <from,self,count> tuples.
332 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
333 arc.raw_frompc = bfd_get_32 (abfd, (bfd_byte *) &arc.raw_frompc);
334 arc.raw_selfpc = bfd_get_32 (abfd, (bfd_byte *) &arc.raw_selfpc);
335 arc.raw_count = bfd_get_32 (abfd, (bfd_byte *) &arc.raw_count);
337 if ( debug & SAMPLEDEBUG ) {
338 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
339 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
357 if((pfile = fopen(filename, "r")) == NULL) {
361 fread(&tmp, sizeof(struct hdr), 1, pfile);
362 tmp.lowpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &tmp.lowpc);
363 tmp.highpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &tmp.highpc);
364 tmp.ncnt = bfd_get_32 (abfd, (bfd_byte *) &tmp.ncnt);
366 if ( s_highpc != 0 && ( tmp.lowpc != h.lowpc ||
367 tmp.highpc != h.highpc || tmp.ncnt != h.ncnt ) ) {
368 fprintf(stderr, "%s: incompatible with first gmon file\n", filename);
372 s_lowpc = (unsigned long) h.lowpc;
373 s_highpc = (unsigned long) h.highpc;
374 lowpc = (unsigned long)h.lowpc / sizeof(UNIT);
375 highpc = (unsigned long)h.highpc / sizeof(UNIT);
376 sampbytes = h.ncnt - sizeof(struct hdr);
377 nsamples = sampbytes / sizeof (UNIT);
379 if ( debug & SAMPLEDEBUG ) {
380 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
381 h.lowpc , h.highpc , h.ncnt );
382 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
383 s_lowpc , s_highpc );
384 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
386 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
387 sampbytes , nsamples );
399 parentp = nllookup( rawp -> raw_frompc );
400 childp = nllookup( rawp -> raw_selfpc );
402 && onlist( kfromlist , parentp -> name )
403 && onlist( ktolist , childp -> name ) ) {
406 childp -> ncall += rawp -> raw_count;
408 if ( debug & TALLYDEBUG ) {
409 printf( "[tally] arc from %s to %s traversed %d times\n" ,
410 parentp -> name , childp -> name , rawp -> raw_count );
413 addarc( parentp , childp , rawp -> raw_count );
417 * dump out the gmon.sum file
422 register nltype *nlp;
423 register arctype *arcp;
427 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) {
432 * dump the header; use the last header read in
434 if ( fwrite( &h , sizeof h , 1 , sfile ) != 1 ) {
441 if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) {
446 * dump the normalized raw arc information
448 for ( nlp = nl ; nlp < npe ; nlp++ ) {
449 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
450 arc.raw_frompc = arcp -> arc_parentp -> value;
451 arc.raw_selfpc = arcp -> arc_childp -> value;
452 arc.raw_count = arcp -> arc_count;
453 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) {
458 if ( debug & SAMPLEDEBUG ) {
459 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
460 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
471 if ( p1 -> value < p2 -> value ) {
474 if ( p1 -> value > p2 -> value ) {
487 samples = (UNIT *) malloc (sampbytes * sizeof(UNIT));
489 fprintf( stderr , "%s: No room for %d sample pc's\n",
490 whoami , sampbytes / sizeof (UNIT));
493 memset (samples, 0, sampbytes * sizeof(UNIT));
495 for (i = 0; i < nsamples; i++) {
496 fread(&sample, sizeof (UNIT), 1, pfile);
497 sample = bfd_get_16 (abfd, (bfd_byte *) &sample);
500 samples[i] += sample;
504 "%s: unexpected EOF after reading %d/%d samples\n",
505 whoami , --i , nsamples );
511 * Assign samples to the procedures to which they belong.
513 * There are three cases as to where pcl and pch can be
514 * with respect to the routine entry addresses svalue0 and svalue1
515 * as shown in the following diagram. overlap computes the
516 * distance between the arrows, the fraction of the sample
517 * that is to be credited to the routine which starts at svalue0.
523 * +-----------------------------------------------+
525 * | ->| |<- ->| |<- ->| |<- |
527 * +---------+ +---------+ +---------+
531 * pcl pch pcl pch pcl pch
533 * For the vax we assert that samples will never fall in the first
534 * two bytes of any routine, since that is the entry mask,
535 * thus we give call alignentries() to adjust the entry points if
536 * the entry mask falls in one bucket but the code for the routine
537 * doesn't start until the next bucket. In conjunction with the
538 * alignment of routine addresses, this should allow us to have
539 * only one sample for every four bytes of text space and never
540 * have any overlap (the two end cases, above).
547 unsigned long pcl, pch;
549 unsigned long overlap;
550 unsigned long svalue0, svalue1;
552 /* read samples and assign to namelist symbols */
553 scale = highpc - lowpc;
556 for (i = 0, j = 1; i < nsamples; i++) {
560 pcl = lowpc + scale * i;
561 pch = lowpc + scale * (i + 1);
564 if ( debug & SAMPLEDEBUG ) {
565 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
570 for (j = j - 1; j < nname; j++) {
571 svalue0 = nl[j].svalue;
572 svalue1 = nl[j+1].svalue;
574 * if high end of tick is below entry address,
580 * if low end of tick into next routine,
581 * go for next routine.
585 overlap = min(pch, svalue1) - max(pcl, svalue0);
588 if (debug & SAMPLEDEBUG) {
589 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
590 nl[j].value/sizeof(UNIT), svalue0, svalue1,
592 overlap * time / scale, overlap);
595 nl[j].time += overlap * time / scale;
600 if (debug & SAMPLEDEBUG) {
601 printf("[asgnsamples] totime %f\n", totime);
626 * calculate scaled entry point addresses (to save time in asgnsamples),
627 * and possibly push the scaled entry points over the entry mask,
628 * if it turns out that the entry point is in one bucket and the code
629 * for a routine is in the next bucket.
633 register struct nl *nlp;
634 unsigned long bucket_of_entry;
635 unsigned long bucket_of_code;
637 for (nlp = nl; nlp < npe; nlp++) {
638 nlp -> svalue = nlp -> value / sizeof(UNIT);
639 bucket_of_entry = (nlp->svalue - lowpc) / scale;
640 bucket_of_code = (nlp->svalue + UNITS_TO_CODE - lowpc) / scale;
641 if (bucket_of_entry < bucket_of_code) {
643 if (debug & SAMPLEDEBUG) {
644 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
645 nlp->svalue, nlp->svalue + UNITS_TO_CODE);
648 nlp->svalue += UNITS_TO_CODE;
657 extern char *strtab; /* string table from a.out */
658 extern int aflag; /* if static functions aren't desired */
663 * must be a text symbol,
664 * and static text symbols don't qualify if aflag set.
671 if (!aflag && (symp->flags&BSF_LOCAL)) {
673 fprintf (stderr, "%s(%d): %s: not a function\n", __FILE__, __LINE__, symp->name);
678 * can't have any `funny' characters in name,
679 * where `funny' includes `.', .o file names
680 * and `$', pascal labels.
685 for (name = symp->name; *name; name++) {
686 if ( *name == '.' || *name == '$' ) {
691 i = bfd_decode_symclass (symp);
692 #if defined(DEBUG) && 0
693 if (i != 'T' && i != 't')
694 fprintf (stderr, "%s(%d): %s is of class %c\n", __FILE__, __LINE__, symp->name, i);
697 return (i == 'T' || i == 't');