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";
32 char *whoami = "gprof";
35 * things which get -E excluded by default.
37 char *defaultEs[] = { "mcount" , "__mcleanup" , 0 };
50 while ( *argv != 0 && **argv == '-' ) {
65 debug |= atoi( *argv );
68 printf("[main] debug = %d\n", debug);
70 printf("%s: -d ignored\n", whoami);
75 addlist( Elist , *argv );
77 addlist( elist , *argv );
81 addlist( elist , *++argv );
86 addlist( Flist , *argv );
88 addlist( flist , *argv );
92 addlist( flist , *++argv );
96 addlist( kfromlist , *++argv );
97 addlist( ktolist , *++argv );
113 a_outname = A_OUTNAME;
122 * turn off default functions
124 for ( sp = &defaultEs[0] ; *sp ; sp++ ) {
126 addlist( Elist , *sp );
128 addlist( elist , *sp );
131 * how many ticks per second?
132 * if we can't tell, report time in ticks.
137 fprintf(stderr, "time is in ticks, not seconds\n");
140 * get information about a.out file.
144 * get information about mon.out file(s).
147 getpfile( gmonname );
151 } while ( *argv++ != 0 );
153 * dump out a gmon.sum file if requested
159 * assign samples to procedures
163 * assemble the dynamic profile
165 timesortnlp = doarcs();
167 * print the dynamic profile
169 printgprof( timesortnlp );
171 * print the flat profile
182 * Set up string and symbol tables from a.out.
183 * and optionally the text space.
184 * On return symbol table is sorted by value.
191 abfd = bfd_openr (a_outname, NULL);
198 if (!bfd_check_format (abfd, bfd_object)) {
199 fprintf (stderr, "%s: %s: bad format\n", whoami, a_outname);
203 /* getstrtab(nfile); */
205 gettextspace( abfd );
206 qsort(nl, nname, sizeof(nltype), valcmp);
209 if ( debug & AOUTDEBUG ) {
212 for (j = 0; j < nname; j++){
213 printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name);
224 fseek(nfile, (long)(N_SYMOFF(xbuf) + xbuf.a_syms), 0);
225 if (fread(&ssiz, sizeof (ssiz), 1, nfile) == 0) {
226 fprintf(stderr, "%s: %s: no string table (old format?)\n" ,
227 whoami , a_outname );
230 strtab = (char *)calloc(ssiz, 1);
231 if (strtab == NULL) {
232 fprintf(stderr, "%s: %s: no room for %d bytes of string table",
233 whoami , a_outname , ssiz);
236 if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1) {
237 fprintf(stderr, "%s: %s: error reading string table\n",
238 whoami , a_outname );
244 * Read in symbol table
253 i = get_symtab_upper_bound (abfd); /* This will probably give us more
254 * than we need, but that's ok.
257 nosyms = bfd_canonicalize_symtab (abfd, syms);
260 for (i = 0; i < nosyms; i++) {
261 if (!funcsymbol (syms[i]))
267 fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname );
271 nl = (nltype *) calloc( askfor , sizeof(nltype) );
273 fprintf(stderr, "%s: No room for %d bytes of symbol table\n",
274 whoami, askfor * sizeof(nltype) );
278 /* pass2 - read symbols */
281 for (i = 0; i < nosyms; i++) {
282 if (!funcsymbol (syms[i])) {
284 if ( debug & AOUTDEBUG ) {
285 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
286 syms[i]->value, syms[i]->name);
291 npe->value = syms[i]->value + syms[i]->section->vma;
292 npe->name = syms[i]->name;
294 if ( debug & AOUTDEBUG ) {
295 printf( "[getsymtab] %d %s 0x%08x\n" ,
296 nname , npe -> name , npe -> value );
306 * read in the text space of an a.out file
317 texsec = bfd_get_section_by_name (abfd, ".text");
318 if (texsec == NULL) {
322 textspace = (u_char *) malloc( texsec->_cooked_size );
324 if ( textspace == 0 ) {
325 fprintf( stderr , "%s: ran out room for %d bytes of text space: " ,
326 whoami , texsec->_cooked_size);
327 fprintf( stderr , "can't do -c\n" );
330 bfd_get_section_contents (abfd, texsec, textspace, texsec->filepos,
331 texsec->_cooked_size);
334 * information from a gmon.out file is in two parts:
335 * an array of sampling hits within pc ranges,
345 pfile = openpfile(filename);
348 * the rest of the file consists of
349 * a bunch of <from,self,count> tuples.
351 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
353 if ( debug & SAMPLEDEBUG ) {
354 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
355 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
373 if((pfile = fopen(filename, "r")) == NULL) {
377 fread(&tmp, sizeof(struct hdr), 1, pfile);
378 if ( s_highpc != 0 && ( tmp.lowpc != h.lowpc ||
379 tmp.highpc != h.highpc || tmp.ncnt != h.ncnt ) ) {
380 fprintf(stderr, "%s: incompatible with first gmon file\n", filename);
384 s_lowpc = (unsigned long) h.lowpc;
385 s_highpc = (unsigned long) h.highpc;
386 lowpc = (unsigned long)h.lowpc / sizeof(UNIT);
387 highpc = (unsigned long)h.highpc / sizeof(UNIT);
388 sampbytes = h.ncnt - sizeof(struct hdr);
389 nsamples = sampbytes / sizeof (UNIT);
391 if ( debug & SAMPLEDEBUG ) {
392 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
393 h.lowpc , h.highpc , h.ncnt );
394 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
395 s_lowpc , s_highpc );
396 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
398 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
399 sampbytes , nsamples );
411 parentp = nllookup( rawp -> raw_frompc );
412 childp = nllookup( rawp -> raw_selfpc );
414 && onlist( kfromlist , parentp -> name )
415 && onlist( ktolist , childp -> name ) ) {
418 childp -> ncall += rawp -> raw_count;
420 if ( debug & TALLYDEBUG ) {
421 printf( "[tally] arc from %s to %s traversed %d times\n" ,
422 parentp -> name , childp -> name , rawp -> raw_count );
425 addarc( parentp , childp , rawp -> raw_count );
429 * dump out the gmon.sum file
434 register nltype *nlp;
435 register arctype *arcp;
439 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) {
444 * dump the header; use the last header read in
446 if ( fwrite( &h , sizeof h , 1 , sfile ) != 1 ) {
453 if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) {
458 * dump the normalized raw arc information
460 for ( nlp = nl ; nlp < npe ; nlp++ ) {
461 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
462 arc.raw_frompc = arcp -> arc_parentp -> value;
463 arc.raw_selfpc = arcp -> arc_childp -> value;
464 arc.raw_count = arcp -> arc_count;
465 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) {
470 if ( debug & SAMPLEDEBUG ) {
471 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
472 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
483 if ( p1 -> value < p2 -> value ) {
486 if ( p1 -> value > p2 -> value ) {
499 samples = (UNIT *) calloc(sampbytes, sizeof (UNIT));
501 fprintf( stderr , "%s: No room for %d sample pc's\n",
502 whoami , sampbytes / sizeof (UNIT));
506 for (i = 0; i < nsamples; i++) {
507 fread(&sample, sizeof (UNIT), 1, pfile);
510 samples[i] += sample;
514 "%s: unexpected EOF after reading %d/%d samples\n",
515 whoami , --i , nsamples );
521 * Assign samples to the procedures to which they belong.
523 * There are three cases as to where pcl and pch can be
524 * with respect to the routine entry addresses svalue0 and svalue1
525 * as shown in the following diagram. overlap computes the
526 * distance between the arrows, the fraction of the sample
527 * that is to be credited to the routine which starts at svalue0.
533 * +-----------------------------------------------+
535 * | ->| |<- ->| |<- ->| |<- |
537 * +---------+ +---------+ +---------+
541 * pcl pch pcl pch pcl pch
543 * For the vax we assert that samples will never fall in the first
544 * two bytes of any routine, since that is the entry mask,
545 * thus we give call alignentries() to adjust the entry points if
546 * the entry mask falls in one bucket but the code for the routine
547 * doesn't start until the next bucket. In conjunction with the
548 * alignment of routine addresses, this should allow us to have
549 * only one sample for every four bytes of text space and never
550 * have any overlap (the two end cases, above).
557 unsigned long pcl, pch;
559 unsigned long overlap;
560 unsigned long svalue0, svalue1;
562 /* read samples and assign to namelist symbols */
563 scale = highpc - lowpc;
566 for (i = 0, j = 1; i < nsamples; i++) {
570 pcl = lowpc + scale * i;
571 pch = lowpc + scale * (i + 1);
574 if ( debug & SAMPLEDEBUG ) {
575 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
580 for (j = j - 1; j < nname; j++) {
581 svalue0 = nl[j].svalue;
582 svalue1 = nl[j+1].svalue;
584 * if high end of tick is below entry address,
590 * if low end of tick into next routine,
591 * go for next routine.
595 overlap = min(pch, svalue1) - max(pcl, svalue0);
598 if (debug & SAMPLEDEBUG) {
599 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
600 nl[j].value/sizeof(UNIT), svalue0, svalue1,
602 overlap * time / scale, overlap);
605 nl[j].time += overlap * time / scale;
610 if (debug & SAMPLEDEBUG) {
611 printf("[asgnsamples] totime %f\n", totime);
636 * calculate scaled entry point addresses (to save time in asgnsamples),
637 * and possibly push the scaled entry points over the entry mask,
638 * if it turns out that the entry point is in one bucket and the code
639 * for a routine is in the next bucket.
643 register struct nl *nlp;
644 unsigned long bucket_of_entry;
645 unsigned long bucket_of_code;
647 for (nlp = nl; nlp < npe; nlp++) {
648 nlp -> svalue = nlp -> value / sizeof(UNIT);
649 bucket_of_entry = (nlp->svalue - lowpc) / scale;
650 bucket_of_code = (nlp->svalue + UNITS_TO_CODE - lowpc) / scale;
651 if (bucket_of_entry < bucket_of_code) {
653 if (debug & SAMPLEDEBUG) {
654 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
655 nlp->svalue, nlp->svalue + UNITS_TO_CODE);
658 nlp->svalue += UNITS_TO_CODE;
667 extern char *strtab; /* string table from a.out */
668 extern int aflag; /* if static functions aren't desired */
672 * must be a text symbol,
673 * and static text symbols don't qualify if aflag set.
679 if (!aflag && (symp->flags&BSF_LOCAL)) {
681 fprintf (stderr, "%s(%d): %s: not a function\n", __FILE__, __LINE__, symp->name);
687 * can't have any `funny' characters in name,
688 * where `funny' includes `.', .o file names
689 * and `$', pascal labels.
694 for (name = symp->name; *name; name++) {
695 if ( *name == '.' || *name == '$' ) {