Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / gprof / gprof.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1983, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)gprof.c  8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/gprof/gprof.c,v 1.11 1999/08/28 01:01:55 peter Exp $
36  * $DragonFly: src/usr.bin/gprof/gprof.c,v 1.2 2003/06/17 04:29:27 dillon Exp $
37  */
38
39 #include <err.h>
40 #include "gprof.h"
41
42 static int valcmp(const void *, const void *);
43
44
45 static struct gmonhdr   gmonhdr;
46 static int lflag;
47 static int Lflag;
48
49 main(argc, argv)
50     int argc;
51     char **argv;
52 {
53     char        **sp;
54     nltype      **timesortnlp;
55     char        **defaultEs;
56
57     --argc;
58     argv++;
59     debug = 0;
60     bflag = TRUE;
61     while ( *argv != 0 && **argv == '-' ) {
62         (*argv)++;
63         switch ( **argv ) {
64         case 'a':
65             aflag = TRUE;
66             break;
67         case 'b':
68             bflag = FALSE;
69             break;
70         case 'C':
71             Cflag = TRUE;
72             cyclethreshold = atoi( *++argv );
73             break;
74         case 'c':
75 #if defined(vax) || defined(tahoe)
76             cflag = TRUE;
77 #else
78             errx(1, "-c isn't supported on this architecture yet");
79 #endif
80             break;
81         case 'd':
82             dflag = TRUE;
83             setlinebuf(stdout);
84             debug |= atoi( *++argv );
85             debug |= ANYDEBUG;
86 #           ifdef DEBUG
87                 printf("[main] debug = %d\n", debug);
88 #           else not DEBUG
89                 printf("gprof: -d ignored\n");
90 #           endif DEBUG
91             break;
92         case 'E':
93             ++argv;
94             addlist( Elist , *argv );
95             Eflag = TRUE;
96             addlist( elist , *argv );
97             eflag = TRUE;
98             break;
99         case 'e':
100             addlist( elist , *++argv );
101             eflag = TRUE;
102             break;
103         case 'F':
104             ++argv;
105             addlist( Flist , *argv );
106             Fflag = TRUE;
107             addlist( flist , *argv );
108             fflag = TRUE;
109             break;
110         case 'f':
111             addlist( flist , *++argv );
112             fflag = TRUE;
113             break;
114         case 'k':
115             addlist( kfromlist , *++argv );
116             addlist( ktolist , *++argv );
117             kflag = TRUE;
118             break;
119     case 'l':
120             lflag = 1;
121             Lflag = 0;
122             break;
123     case 'L':
124             Lflag = 1;
125             lflag = 0;
126             break;
127     case 's':
128             sflag = TRUE;
129             break;
130         case 'u':
131             uflag = TRUE;
132             break;
133         case 'z':
134             zflag = TRUE;
135             break;
136         }
137         argv++;
138     }
139     if ( *argv != 0 ) {
140         a_outname  = *argv;
141         argv++;
142     } else {
143         a_outname  = A_OUTNAME;
144     }
145     if ( *argv != 0 ) {
146         gmonname = *argv;
147         argv++;
148     } else {
149         gmonname = (char *) malloc(strlen(a_outname)+6);
150         strcpy(gmonname, a_outname);
151         strcat(gmonname, ".gmon");
152     }
153         /*
154          *      get information from the executable file.
155          */
156     if (elf_getnfile(a_outname, &defaultEs) == -1 &&
157       aout_getnfile(a_outname, &defaultEs) == -1)
158         errx(1, "%s: bad format", a_outname);
159         /*
160          *      sort symbol table.
161          */
162     qsort(nl, nname, sizeof(nltype), valcmp);
163         /*
164          *      turn off default functions
165          */
166     for ( sp = defaultEs ; *sp ; sp++ ) {
167         Eflag = TRUE;
168         addlist( Elist , *sp );
169         eflag = TRUE;
170         addlist( elist , *sp );
171     }
172         /*
173          *      get information about mon.out file(s).
174          */
175     do  {
176         getpfile( gmonname );
177         if ( *argv != 0 ) {
178             gmonname = *argv;
179         }
180     } while ( *argv++ != 0 );
181         /*
182          *      how many ticks per second?
183          *      if we can't tell, report time in ticks.
184          */
185     if (hz == 0) {
186         hz = 1;
187         fprintf(stderr, "time is in ticks, not seconds\n");
188     }
189         /*
190          *      dump out a gmon.sum file if requested
191          */
192     if ( sflag ) {
193         dumpsum( GMONSUM );
194     }
195         /*
196          *      assign samples to procedures
197          */
198     asgnsamples();
199         /*
200          *      assemble the dynamic profile
201          */
202     timesortnlp = doarcs();
203         /*
204          *      print the dynamic profile
205          */
206     if(!lflag) {
207             printgprof( timesortnlp );
208     }
209         /*
210          *      print the flat profile
211          */
212     if(!Lflag) {
213             printprof();
214     }
215         /*
216          *      print the index
217          */
218     printindex();
219     done();
220 }
221
222     /*
223      *  information from a gmon.out file is in two parts:
224      *  an array of sampling hits within pc ranges,
225      *  and the arcs.
226      */
227 getpfile(filename)
228     char *filename;
229 {
230     FILE                *pfile;
231     FILE                *openpfile();
232     struct rawarc       arc;
233
234     pfile = openpfile(filename);
235     readsamples(pfile);
236         /*
237          *      the rest of the file consists of
238          *      a bunch of <from,self,count> tuples.
239          */
240     while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
241 #       ifdef DEBUG
242             if ( debug & SAMPLEDEBUG ) {
243                 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
244                         arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
245             }
246 #       endif DEBUG
247             /*
248              *  add this arc
249              */
250         tally( &arc );
251     }
252     fclose(pfile);
253 }
254
255 FILE *
256 openpfile(filename)
257     char *filename;
258 {
259     struct gmonhdr      tmp;
260     FILE                *pfile;
261     int                 size;
262     int                 rate;
263
264     if((pfile = fopen(filename, "r")) == NULL) {
265         perror(filename);
266         done();
267     }
268     fread(&tmp, sizeof(struct gmonhdr), 1, pfile);
269     if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc ||
270          tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) ) {
271         warnx("%s: incompatible with first gmon file", filename);
272         done();
273     }
274     gmonhdr = tmp;
275     if ( gmonhdr.version == GMONVERSION ) {
276         rate = gmonhdr.profrate;
277         size = sizeof(struct gmonhdr);
278     } else {
279         fseek(pfile, sizeof(struct ophdr), SEEK_SET);
280         size = sizeof(struct ophdr);
281         gmonhdr.profrate = rate = hertz();
282         gmonhdr.version = GMONVERSION;
283     }
284     if (hz == 0) {
285         hz = rate;
286     } else if (hz != rate) {
287         fprintf(stderr,
288             "%s: profile clock rate (%d) %s (%d) in first gmon file\n",
289             filename, rate, "incompatible with clock rate", hz);
290         done();
291     }
292     s_lowpc = (unsigned long) gmonhdr.lpc;
293     s_highpc = (unsigned long) gmonhdr.hpc;
294     lowpc = (unsigned long)gmonhdr.lpc / sizeof(UNIT);
295     highpc = (unsigned long)gmonhdr.hpc / sizeof(UNIT);
296     sampbytes = gmonhdr.ncnt - size;
297     nsamples = sampbytes / sizeof (UNIT);
298 #   ifdef DEBUG
299         if ( debug & SAMPLEDEBUG ) {
300             printf( "[openpfile] hdr.lpc 0x%x hdr.hpc 0x%x hdr.ncnt %d\n",
301                 gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt );
302             printf( "[openpfile]   s_lowpc 0x%x   s_highpc 0x%x\n" ,
303                 s_lowpc , s_highpc );
304             printf( "[openpfile]     lowpc 0x%x     highpc 0x%x\n" ,
305                 lowpc , highpc );
306             printf( "[openpfile] sampbytes %d nsamples %d\n" ,
307                 sampbytes , nsamples );
308             printf( "[openpfile] sample rate %d\n" , hz );
309         }
310 #   endif DEBUG
311     return(pfile);
312 }
313
314 tally( rawp )
315     struct rawarc       *rawp;
316 {
317     nltype              *parentp;
318     nltype              *childp;
319
320     parentp = nllookup( rawp -> raw_frompc );
321     childp = nllookup( rawp -> raw_selfpc );
322     if ( parentp == 0 || childp == 0 )
323         return;
324     if ( kflag
325          && onlist( kfromlist , parentp -> name )
326          && onlist( ktolist , childp -> name ) ) {
327         return;
328     }
329     childp -> ncall += rawp -> raw_count;
330 #   ifdef DEBUG
331         if ( debug & TALLYDEBUG ) {
332             printf( "[tally] arc from %s to %s traversed %d times\n" ,
333                     parentp -> name , childp -> name , rawp -> raw_count );
334         }
335 #   endif DEBUG
336     addarc( parentp , childp , rawp -> raw_count );
337 }
338
339 /*
340  * dump out the gmon.sum file
341  */
342 dumpsum( sumfile )
343     char *sumfile;
344 {
345     register nltype *nlp;
346     register arctype *arcp;
347     struct rawarc arc;
348     FILE *sfile;
349
350     if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) {
351         perror( sumfile );
352         done();
353     }
354     /*
355      * dump the header; use the last header read in
356      */
357     if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 ) {
358         perror( sumfile );
359         done();
360     }
361     /*
362      * dump the samples
363      */
364     if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) {
365         perror( sumfile );
366         done();
367     }
368     /*
369      * dump the normalized raw arc information
370      */
371     for ( nlp = nl ; nlp < npe ; nlp++ ) {
372         for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
373             arc.raw_frompc = arcp -> arc_parentp -> value;
374             arc.raw_selfpc = arcp -> arc_childp -> value;
375             arc.raw_count = arcp -> arc_count;
376             if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) {
377                 perror( sumfile );
378                 done();
379             }
380 #           ifdef DEBUG
381                 if ( debug & SAMPLEDEBUG ) {
382                     printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
383                             arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
384                 }
385 #           endif DEBUG
386         }
387     }
388     fclose( sfile );
389 }
390
391 static int
392 valcmp(v1, v2)
393     const void *v1;
394     const void *v2;
395 {
396     const nltype *p1 = (const nltype *)v1;
397     const nltype *p2 = (const nltype *)v2;
398
399     if ( p1 -> value < p2 -> value ) {
400         return LESSTHAN;
401     }
402     if ( p1 -> value > p2 -> value ) {
403         return GREATERTHAN;
404     }
405     return EQUALTO;
406 }
407
408 readsamples(pfile)
409     FILE        *pfile;
410 {
411     register i;
412     UNIT        sample;
413
414     if (samples == 0) {
415         samples = (UNIT *) calloc(sampbytes, sizeof (UNIT));
416         if (samples == 0) {
417             warnx("no room for %d sample pc's", sampbytes / sizeof (UNIT));
418             done();
419         }
420     }
421     for (i = 0; i < nsamples; i++) {
422         fread(&sample, sizeof (UNIT), 1, pfile);
423         if (feof(pfile))
424                 break;
425         samples[i] += sample;
426     }
427     if (i != nsamples) {
428         warnx("unexpected EOF after reading %d/%d samples", --i , nsamples );
429         done();
430     }
431 }
432
433 /*
434  *      Assign samples to the procedures to which they belong.
435  *
436  *      There are three cases as to where pcl and pch can be
437  *      with respect to the routine entry addresses svalue0 and svalue1
438  *      as shown in the following diagram.  overlap computes the
439  *      distance between the arrows, the fraction of the sample
440  *      that is to be credited to the routine which starts at svalue0.
441  *
442  *          svalue0                                         svalue1
443  *             |                                               |
444  *             v                                               v
445  *
446  *             +-----------------------------------------------+
447  *             |                                               |
448  *        |  ->|    |<-         ->|         |<-         ->|    |<-  |
449  *        |         |             |         |             |         |
450  *        +---------+             +---------+             +---------+
451  *
452  *        ^         ^             ^         ^             ^         ^
453  *        |         |             |         |             |         |
454  *       pcl       pch           pcl       pch           pcl       pch
455  *
456  *      For the vax we assert that samples will never fall in the first
457  *      two bytes of any routine, since that is the entry mask,
458  *      thus we give call alignentries() to adjust the entry points if
459  *      the entry mask falls in one bucket but the code for the routine
460  *      doesn't start until the next bucket.  In conjunction with the
461  *      alignment of routine addresses, this should allow us to have
462  *      only one sample for every four bytes of text space and never
463  *      have any overlap (the two end cases, above).
464  */
465 asgnsamples()
466 {
467     register int        j;
468     UNIT                ccnt;
469     double              time;
470     unsigned long       pcl, pch;
471     register int        i;
472     unsigned long       overlap;
473     unsigned long       svalue0, svalue1;
474
475     /* read samples and assign to namelist symbols */
476     scale = highpc - lowpc;
477     scale /= nsamples;
478     alignentries();
479     for (i = 0, j = 1; i < nsamples; i++) {
480         ccnt = samples[i];
481         if (ccnt == 0)
482                 continue;
483         pcl = lowpc + (unsigned long)(scale * i);
484         pch = lowpc + (unsigned long)(scale * (i + 1));
485         time = ccnt;
486 #       ifdef DEBUG
487             if ( debug & SAMPLEDEBUG ) {
488                 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
489                         pcl , pch , ccnt );
490             }
491 #       endif DEBUG
492         totime += time;
493         for (j = j - 1; j < nname; j++) {
494             svalue0 = nl[j].svalue;
495             svalue1 = nl[j+1].svalue;
496                 /*
497                  *      if high end of tick is below entry address,
498                  *      go for next tick.
499                  */
500             if (pch < svalue0)
501                     break;
502                 /*
503                  *      if low end of tick into next routine,
504                  *      go for next routine.
505                  */
506             if (pcl >= svalue1)
507                     continue;
508             overlap = min(pch, svalue1) - max(pcl, svalue0);
509             if (overlap > 0) {
510 #               ifdef DEBUG
511                     if (debug & SAMPLEDEBUG) {
512                         printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
513                                 nl[j].value/sizeof(UNIT), svalue0, svalue1,
514                                 nl[j].name,
515                                 overlap * time / scale, overlap);
516                     }
517 #               endif DEBUG
518                 nl[j].time += overlap * time / scale;
519             }
520         }
521     }
522 #   ifdef DEBUG
523         if (debug & SAMPLEDEBUG) {
524             printf("[asgnsamples] totime %f\n", totime);
525         }
526 #   endif DEBUG
527 }
528
529
530 unsigned long
531 min(a, b)
532     unsigned long a,b;
533 {
534     if (a<b)
535         return(a);
536     return(b);
537 }
538
539 unsigned long
540 max(a, b)
541     unsigned long a,b;
542 {
543     if (a>b)
544         return(a);
545     return(b);
546 }
547
548     /*
549      *  calculate scaled entry point addresses (to save time in asgnsamples),
550      *  and possibly push the scaled entry points over the entry mask,
551      *  if it turns out that the entry point is in one bucket and the code
552      *  for a routine is in the next bucket.
553      */
554 alignentries()
555 {
556     register struct nl  *nlp;
557     unsigned long       bucket_of_entry;
558     unsigned long       bucket_of_code;
559
560     for (nlp = nl; nlp < npe; nlp++) {
561         nlp -> svalue = nlp -> value / sizeof(UNIT);
562         bucket_of_entry = (nlp->svalue - lowpc) / scale;
563         bucket_of_code = (nlp->svalue + UNITS_TO_CODE - lowpc) / scale;
564         if (bucket_of_entry < bucket_of_code) {
565 #           ifdef DEBUG
566                 if (debug & SAMPLEDEBUG) {
567                     printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
568                             nlp->svalue, nlp->svalue + UNITS_TO_CODE);
569                 }
570 #           endif DEBUG
571             nlp->svalue += UNITS_TO_CODE;
572         }
573     }
574 }
575
576 done()
577 {
578
579     exit(0);
580 }