#include file moved
[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 #ifndef lint
21 char copyright[] =
22 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
23 All rights reserved.\n";
24 #endif /* not lint */
25
26 #ifndef lint
27 static char sccsid[] = "@(#)gprof.c 5.6 (Berkeley) 6/1/90";
28 #endif /* not lint */
29
30 #include "gprof.h"
31
32 bfd *abfd;
33
34 char *whoami = "gprof";
35
36 /*
37 * things which get -E excluded by default.
38 */
39 char *defaultEs[] = { "mcount" , "__mcleanup" , 0 };
40
41 int discard_underscores = 1; /* Should we discard initial underscores? */
42 int bsd_style_output = 0; /* As opposed to FSF style output */
43
44 main(argc, argv)
45 int argc;
46 char **argv;
47 {
48 char **sp;
49 nltype **timesortnlp;
50
51 --argc;
52 argv++;
53 debug = 0;
54 bflag = TRUE;
55 while ( *argv != 0 && **argv == '-' ) {
56 (*argv)++;
57 switch ( **argv ) {
58 case 'a':
59 aflag = TRUE;
60 break;
61 case 'b':
62 bflag = FALSE;
63 break;
64 case 'c':
65 cflag = TRUE;
66 break;
67 case 'd':
68 dflag = TRUE;
69 (*argv)++;
70 debug |= atoi( *argv );
71 debug |= ANYDEBUG;
72 # ifdef DEBUG
73 printf("[main] debug = %d\n", debug);
74 # else not DEBUG
75 printf("%s: -d ignored\n", whoami);
76 # endif DEBUG
77 break;
78 case 'E':
79 ++argv;
80 addlist( Elist , *argv );
81 Eflag = TRUE;
82 addlist( elist , *argv );
83 eflag = TRUE;
84 break;
85 case 'e':
86 addlist( elist , *++argv );
87 eflag = TRUE;
88 break;
89 case 'F':
90 ++argv;
91 addlist( Flist , *argv );
92 Fflag = TRUE;
93 addlist( flist , *argv );
94 fflag = TRUE;
95 break;
96 case 'f':
97 addlist( flist , *++argv );
98 fflag = TRUE;
99 break;
100 case 'k':
101 addlist( kfromlist , *++argv );
102 addlist( ktolist , *++argv );
103 kflag = TRUE;
104 break;
105 case 's':
106 sflag = TRUE;
107 break;
108 case 'T': /* "Traditional" output format */
109 bsd_style_output = 1;
110 break;
111 case 'z':
112 zflag = TRUE;
113 break;
114 }
115 argv++;
116 }
117 if ( *argv != 0 ) {
118 a_outname = *argv;
119 argv++;
120 } else {
121 a_outname = A_OUTNAME;
122 }
123 if ( *argv != 0 ) {
124 gmonname = *argv;
125 argv++;
126 } else {
127 gmonname = GMONNAME;
128 }
129 /*
130 * turn off default functions
131 */
132 for ( sp = &defaultEs[0] ; *sp ; sp++ ) {
133 Eflag = TRUE;
134 addlist( Elist , *sp );
135 eflag = TRUE;
136 addlist( elist , *sp );
137 }
138 /*
139 * how many ticks per second?
140 * if we can't tell, report time in ticks.
141 */
142 hz = hertz();
143 if (hz == 0) {
144 hz = 1;
145 fprintf(stderr, "time is in ticks, not seconds\n");
146 }
147 /*
148 * get information about a.out file.
149 */
150 getnfile();
151 /*
152 * get information about mon.out file(s).
153 */
154 do {
155 getpfile( gmonname );
156 if ( *argv != 0 ) {
157 gmonname = *argv;
158 }
159 } while ( *argv++ != 0 );
160 /*
161 * dump out a gmon.sum file if requested
162 */
163 if ( sflag ) {
164 dumpsum( GMONSUM );
165 }
166 /*
167 * assign samples to procedures
168 */
169 asgnsamples();
170 /*
171 * assemble the dynamic profile
172 */
173 timesortnlp = doarcs();
174
175 if (bsd_style_output) {
176 printgprof( timesortnlp ); /* print the dynamic profile */
177 printprof(); /* print the flat profile */
178 } else {
179 printprof(); /* print the flat profile */
180 printgprof( timesortnlp ); /* print the dynamic profile */
181 }
182 /*
183 * print the index
184 */
185 printindex();
186 done();
187 }
188
189 /*
190 * Set up string and symbol tables from a.out.
191 * and optionally the text space.
192 * On return symbol table is sorted by value.
193 */
194 getnfile()
195 {
196 int valcmp();
197
198 abfd = bfd_openr (a_outname, NULL);
199
200 if (abfd == NULL) {
201 perror (a_outname);
202 done();
203 }
204
205 if (!bfd_check_format (abfd, bfd_object)) {
206 fprintf (stderr, "%s: %s: bad format\n", whoami, a_outname);
207 done();
208 }
209
210 /* getstrtab(nfile); */
211 getsymtab(abfd);
212 gettextspace( abfd );
213 qsort(nl, nname, sizeof(nltype), valcmp);
214
215 # ifdef DEBUG
216 if ( debug & AOUTDEBUG ) {
217 register int j;
218
219 for (j = 0; j < nname; j++){
220 printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name);
221 }
222 }
223 # endif DEBUG
224 }
225
226 /*
227 * Read in symbol table
228 */
229 getsymtab(abfd)
230 bfd *abfd;
231 {
232 register long i;
233 int askfor;
234 int nosyms;
235 asymbol **syms;
236 i = get_symtab_upper_bound (abfd); /* This will probably give us more
237 * than we need, but that's ok.
238 */
239 syms = (asymbol**)xmalloc (i);
240 nosyms = bfd_canonicalize_symtab (abfd, syms);
241
242 nname = 0;
243 for (i = 0; i < nosyms; i++) {
244 if (!funcsymbol (syms[i]))
245 continue;
246 nname++;
247 }
248
249 if (nname == 0) {
250 fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname );
251 done();
252 }
253 askfor = nname + 1;
254 nl = (nltype *) calloc( askfor , sizeof(nltype) );
255 if (nl == 0) {
256 fprintf(stderr, "%s: No room for %d bytes of symbol table\n",
257 whoami, askfor * sizeof(nltype) );
258 done();
259 }
260
261 /* pass2 - read symbols */
262 npe = nl;
263 nname = 0;
264 for (i = 0; i < nosyms; i++) {
265 if (!funcsymbol (syms[i])) {
266 # ifdef DEBUG
267 if ( debug & AOUTDEBUG ) {
268 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
269 syms[i]->value, syms[i]->name);
270 }
271 # endif DEBUG
272 continue;
273 }
274 /* Symbol offsets are always section-relative. */
275 npe->value = syms[i]->value + syms[i]->section->vma;
276 npe->name = syms[i]->name;
277
278 /* If we see "main" without an initial '_', we assume
279 names are *not* prefixed by '_'. */
280 if (npe->name[0] == 'm' && discard_underscores
281 && strcmp(npe->name, "main") == 0)
282 discard_underscores = 0;
283
284 # ifdef DEBUG
285 if ( debug & AOUTDEBUG ) {
286 printf( "[getsymtab] %d %s 0x%08x\n" ,
287 nname , npe -> name , npe -> value );
288 }
289 # endif DEBUG
290 npe++;
291 nname++;
292 }
293 npe->value = -1;
294 }
295
296 /*
297 * read in the text space of an a.out file
298 */
299 gettextspace( abfd )
300 bfd *abfd;
301 {
302 asection *texsec;
303
304 if ( cflag == 0 ) {
305 return;
306 }
307
308 texsec = bfd_get_section_by_name (abfd, ".text");
309 if (texsec == NULL) {
310 return;
311 }
312
313 textspace = (u_char *) malloc( texsec->_cooked_size );
314
315 if ( textspace == 0 ) {
316 fprintf( stderr , "%s: ran out room for %d bytes of text space: " ,
317 whoami , texsec->_cooked_size);
318 fprintf( stderr , "can't do -c\n" );
319 return;
320 }
321 bfd_get_section_contents (abfd, texsec, textspace, texsec->filepos,
322 texsec->_cooked_size);
323 }
324 /*
325 * information from a gmon.out file is in two parts:
326 * an array of sampling hits within pc ranges,
327 * and the arcs.
328 */
329 getpfile(filename)
330 char *filename;
331 {
332 FILE *pfile;
333 FILE *openpfile();
334 struct rawarc arc;
335
336 pfile = openpfile(filename);
337 readsamples(pfile);
338 /*
339 * the rest of the file consists of
340 * a bunch of <from,self,count> tuples.
341 */
342 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
343 arc.raw_frompc = bfd_get_32 (abfd, (bfd_byte *) &arc.raw_frompc);
344 arc.raw_selfpc = bfd_get_32 (abfd, (bfd_byte *) &arc.raw_selfpc);
345 arc.raw_count = bfd_get_32 (abfd, (bfd_byte *) &arc.raw_count);
346 # ifdef DEBUG
347 if ( debug & SAMPLEDEBUG ) {
348 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
349 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
350 }
351 # endif DEBUG
352 /*
353 * add this arc
354 */
355 tally( &arc );
356 }
357 fclose(pfile);
358 }
359
360 FILE *
361 openpfile(filename)
362 char *filename;
363 {
364 struct hdr tmp;
365 FILE *pfile;
366
367 if((pfile = fopen(filename, "r")) == NULL) {
368 perror(filename);
369 done();
370 }
371 fread(&tmp, sizeof(struct hdr), 1, pfile);
372 tmp.lowpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &tmp.lowpc);
373 tmp.highpc = (UNIT *)bfd_get_32 (abfd, (bfd_byte *) &tmp.highpc);
374 tmp.ncnt = bfd_get_32 (abfd, (bfd_byte *) &tmp.ncnt);
375
376 if ( s_highpc != 0 && ( tmp.lowpc != h.lowpc ||
377 tmp.highpc != h.highpc || tmp.ncnt != h.ncnt ) ) {
378 fprintf(stderr, "%s: incompatible with first gmon file\n", filename);
379 done();
380 }
381 h = tmp;
382 s_lowpc = (unsigned long) h.lowpc;
383 s_highpc = (unsigned long) h.highpc;
384 lowpc = (unsigned long)h.lowpc / sizeof(UNIT);
385 highpc = (unsigned long)h.highpc / sizeof(UNIT);
386 sampbytes = h.ncnt - sizeof(struct hdr);
387 nsamples = sampbytes / sizeof (UNIT);
388 # ifdef DEBUG
389 if ( debug & SAMPLEDEBUG ) {
390 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
391 h.lowpc , h.highpc , h.ncnt );
392 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
393 s_lowpc , s_highpc );
394 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
395 lowpc , highpc );
396 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
397 sampbytes , nsamples );
398 }
399 # endif DEBUG
400 return(pfile);
401 }
402
403 tally( rawp )
404 struct rawarc *rawp;
405 {
406 nltype *parentp;
407 nltype *childp;
408
409 parentp = nllookup( rawp -> raw_frompc );
410 childp = nllookup( rawp -> raw_selfpc );
411 if ( kflag
412 && onlist( kfromlist , parentp -> name )
413 && onlist( ktolist , childp -> name ) ) {
414 return;
415 }
416 childp -> ncall += rawp -> raw_count;
417 # ifdef DEBUG
418 if ( debug & TALLYDEBUG ) {
419 printf( "[tally] arc from %s to %s traversed %d times\n" ,
420 parentp -> name , childp -> name , rawp -> raw_count );
421 }
422 # endif DEBUG
423 addarc( parentp , childp , rawp -> raw_count );
424 }
425
426 /*
427 * dump out the gmon.sum file
428 */
429 dumpsum( sumfile )
430 char *sumfile;
431 {
432 register nltype *nlp;
433 register arctype *arcp;
434 struct rawarc arc;
435 FILE *sfile;
436
437 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) {
438 perror( sumfile );
439 done();
440 }
441 /*
442 * dump the header; use the last header read in
443 */
444 if ( fwrite( &h , sizeof h , 1 , sfile ) != 1 ) {
445 perror( sumfile );
446 done();
447 }
448 /*
449 * dump the samples
450 */
451 if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) {
452 perror( sumfile );
453 done();
454 }
455 /*
456 * dump the normalized raw arc information
457 */
458 for ( nlp = nl ; nlp < npe ; nlp++ ) {
459 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
460 arc.raw_frompc = arcp -> arc_parentp -> value;
461 arc.raw_selfpc = arcp -> arc_childp -> value;
462 arc.raw_count = arcp -> arc_count;
463 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) {
464 perror( sumfile );
465 done();
466 }
467 # ifdef DEBUG
468 if ( debug & SAMPLEDEBUG ) {
469 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
470 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
471 }
472 # endif DEBUG
473 }
474 }
475 fclose( sfile );
476 }
477
478 valcmp(p1, p2)
479 nltype *p1, *p2;
480 {
481 if ( p1 -> value < p2 -> value ) {
482 return LESSTHAN;
483 }
484 if ( p1 -> value > p2 -> value ) {
485 return GREATERTHAN;
486 }
487 return EQUALTO;
488 }
489
490 readsamples(pfile)
491 FILE *pfile;
492 {
493 register i;
494 UNIT sample;
495
496 if (samples == 0) {
497 samples = (UNIT *) malloc (sampbytes * sizeof(UNIT));
498 if (samples == 0) {
499 fprintf( stderr , "%s: No room for %d sample pc's\n",
500 whoami , sampbytes / sizeof (UNIT));
501 done();
502 }
503 memset (samples, 0, sampbytes * sizeof(UNIT));
504 }
505 for (i = 0; i < nsamples; i++) {
506 fread(&sample, sizeof (UNIT), 1, pfile);
507 sample = bfd_get_16 (abfd, (bfd_byte *) &sample);
508 if (feof(pfile))
509 break;
510 samples[i] += sample;
511 }
512 if (i != nsamples) {
513 fprintf(stderr,
514 "%s: unexpected EOF after reading %d/%d samples\n",
515 whoami , --i , nsamples );
516 done();
517 }
518 }
519
520 /*
521 * Assign samples to the procedures to which they belong.
522 *
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.
528 *
529 * svalue0 svalue1
530 * | |
531 * v v
532 *
533 * +-----------------------------------------------+
534 * | |
535 * | ->| |<- ->| |<- ->| |<- |
536 * | | | | | |
537 * +---------+ +---------+ +---------+
538 *
539 * ^ ^ ^ ^ ^ ^
540 * | | | | | |
541 * pcl pch pcl pch pcl pch
542 *
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).
551 */
552 asgnsamples()
553 {
554 register int j;
555 UNIT ccnt;
556 double time;
557 unsigned long pcl, pch;
558 register int i;
559 unsigned long overlap;
560 unsigned long svalue0, svalue1;
561
562 /* read samples and assign to namelist symbols */
563 scale = highpc - lowpc;
564 scale /= nsamples;
565 alignentries();
566 for (i = 0, j = 1; i < nsamples; i++) {
567 ccnt = samples[i];
568 if (ccnt == 0)
569 continue;
570 pcl = lowpc + scale * i;
571 pch = lowpc + scale * (i + 1);
572 time = ccnt;
573 # ifdef DEBUG
574 if ( debug & SAMPLEDEBUG ) {
575 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
576 pcl , pch , ccnt );
577 }
578 # endif DEBUG
579 totime += time;
580 for (j = j - 1; j < nname; j++) {
581 svalue0 = nl[j].svalue;
582 svalue1 = nl[j+1].svalue;
583 /*
584 * if high end of tick is below entry address,
585 * go for next tick.
586 */
587 if (pch < svalue0)
588 break;
589 /*
590 * if low end of tick into next routine,
591 * go for next routine.
592 */
593 if (pcl >= svalue1)
594 continue;
595 overlap = min(pch, svalue1) - max(pcl, svalue0);
596 if (overlap > 0) {
597 # ifdef DEBUG
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,
601 nl[j].name,
602 overlap * time / scale, overlap);
603 }
604 # endif DEBUG
605 nl[j].time += overlap * time / scale;
606 }
607 }
608 }
609 # ifdef DEBUG
610 if (debug & SAMPLEDEBUG) {
611 printf("[asgnsamples] totime %f\n", totime);
612 }
613 # endif DEBUG
614 }
615
616
617 unsigned long
618 min(a, b)
619 unsigned long a,b;
620 {
621 if (a<b)
622 return(a);
623 return(b);
624 }
625
626 unsigned long
627 max(a, b)
628 unsigned long a,b;
629 {
630 if (a>b)
631 return(a);
632 return(b);
633 }
634
635 /*
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.
640 */
641 alignentries()
642 {
643 register struct nl *nlp;
644 unsigned long bucket_of_entry;
645 unsigned long bucket_of_code;
646
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) {
652 # ifdef DEBUG
653 if (debug & SAMPLEDEBUG) {
654 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
655 nlp->svalue, nlp->svalue + UNITS_TO_CODE);
656 }
657 # endif DEBUG
658 nlp->svalue += UNITS_TO_CODE;
659 }
660 }
661 }
662
663 bool
664 funcsymbol( symp )
665 asymbol *symp;
666 {
667 extern char *strtab; /* string table from a.out */
668 extern int aflag; /* if static functions aren't desired */
669 CONST char *name;
670 int i;
671
672 /*
673 * must be a text symbol,
674 * and static text symbols don't qualify if aflag set.
675 */
676
677
678 if (!symp->section)
679 return FALSE;
680
681 if (!aflag && (symp->flags&BSF_LOCAL)) {
682 #if defined(DEBUG)
683 fprintf (stderr, "%s(%d): %s: not a function\n", __FILE__, __LINE__, symp->name);
684 #endif
685 return FALSE;
686 }
687 /*
688 * can't have any `funny' characters in name,
689 * where `funny' includes `.', .o file names
690 * and `$', pascal labels.
691 */
692 if (!symp->name)
693 return FALSE;
694
695 for (name = symp->name; *name; name++) {
696 if ( *name == '.' || *name == '$' ) {
697 return FALSE;
698 }
699 }
700
701 i = bfd_decode_symclass (symp);
702 #if defined(DEBUG) && 0
703 if (i != 'T' && i != 't')
704 fprintf (stderr, "%s(%d): %s is of class %c\n", __FILE__, __LINE__, symp->name, i);
705 #endif
706
707 return (i == 'T' || i == 't');
708 }
709
710 done()
711 {
712
713 exit(0);
714 }
This page took 0.047872 seconds and 4 git commands to generate.