2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1983, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
55 static int valcmp(const void *, const void *);
57 static struct gmonhdr gmonhdr;
62 main(int argc, char **argv)
72 while ( *argv != 0 && **argv == '-' ) {
83 cyclethreshold = atoi( *++argv );
88 debug |= atoi( *++argv );
91 printf("[main] debug = %d\n", debug);
92 # else /* not DEBUG */
93 printf("gprof: -d ignored\n");
98 addlist( Elist , *argv );
100 addlist( elist , *argv );
104 addlist( elist , *++argv );
109 addlist( Flist , *argv );
111 addlist( flist , *argv );
115 addlist( flist , *++argv );
119 addlist( kfromlist , *++argv );
120 addlist( ktolist , *++argv );
150 a_outname = A_OUTNAME;
156 gmonname = (char *) malloc(strlen(a_outname)+6);
157 strcpy(gmonname, a_outname);
158 strcat(gmonname, ".gmon");
161 * get information from the executable file.
163 if ((Kflag && kernel_getnfile(a_outname, &defaultEs) == -1) ||
164 (!Kflag && elf_getnfile(a_outname, &defaultEs) == -1
166 && aout_getnfile(a_outname, &defaultEs) == -1
169 errx(1, "%s: bad format", a_outname);
173 qsort(nl, nname, sizeof(nltype), valcmp);
175 * turn off default functions
177 for ( sp = defaultEs ; *sp ; sp++ ) {
179 addlist( Elist , *sp );
181 addlist( elist , *sp );
184 * get information about mon.out file(s).
187 getpfile( gmonname );
191 } while ( *argv++ != 0 );
193 * how many ticks per second?
194 * if we can't tell, report time in ticks.
198 fprintf(stderr, "time is in ticks, not seconds\n");
201 * dump out a gmon.sum file if requested
207 * assign samples to procedures
211 * assemble the dynamic profile
213 timesortnlp = doarcs();
215 * print the dynamic profile
218 printgprof( timesortnlp );
221 * print the flat profile
234 * information from a gmon.out file is in two parts:
235 * an array of sampling hits within pc ranges,
239 getpfile(char *filename)
244 pfile = openpfile(filename);
247 * the rest of the file consists of
248 * a bunch of <from,self,count> tuples.
250 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
252 if ( debug & SAMPLEDEBUG ) {
253 printf( "[getpfile] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
254 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
266 openpfile(char *filename)
273 if((pfile = fopen(filename, "r")) == NULL)
274 err(1, "%s", filename);
275 fread(&tmp, sizeof(struct gmonhdr), 1, pfile);
276 if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc ||
277 tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) )
278 errx(1, "%s: incompatible with first gmon file", filename);
280 if ( gmonhdr.version == GMONVERSION ) {
281 rate = gmonhdr.profrate;
282 size = sizeof(struct gmonhdr);
284 fseek(pfile, sizeof(struct ophdr), SEEK_SET);
285 size = sizeof(struct ophdr);
286 gmonhdr.profrate = rate = hertz();
287 gmonhdr.version = GMONVERSION;
291 } else if (hz != rate)
292 errx(0, "%s: profile clock rate (%d) %s (%ld) in first gmon file",
293 filename, rate, "incompatible with clock rate", hz);
294 if ( gmonhdr.histcounter_type == 0 ) {
295 /* Historical case. The type was u_short (2 bytes in practice). */
296 histcounter_type = 16;
297 histcounter_size = 2;
299 histcounter_type = gmonhdr.histcounter_type;
300 histcounter_size = abs(histcounter_type) / CHAR_BIT;
302 s_lowpc = (unsigned long) gmonhdr.lpc;
303 s_highpc = (unsigned long) gmonhdr.hpc;
304 lowpc = (unsigned long)gmonhdr.lpc / HISTORICAL_SCALE_2;
305 highpc = (unsigned long)gmonhdr.hpc / HISTORICAL_SCALE_2;
306 sampbytes = gmonhdr.ncnt - size;
307 nsamples = sampbytes / histcounter_size;
309 if ( debug & SAMPLEDEBUG ) {
310 printf( "[openpfile] hdr.lpc 0x%lx hdr.hpc 0x%lx hdr.ncnt %d\n",
311 gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt );
312 printf( "[openpfile] s_lowpc 0x%lx s_highpc 0x%lx\n" ,
313 s_lowpc , s_highpc );
314 printf( "[openpfile] lowpc 0x%lx highpc 0x%lx\n" ,
316 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
317 sampbytes , nsamples );
318 printf( "[openpfile] sample rate %ld\n" , hz );
325 tally(struct rawarc *rawp)
330 parentp = nllookup( rawp -> raw_frompc );
331 childp = nllookup( rawp -> raw_selfpc );
332 if ( parentp == 0 || childp == 0 )
335 && onlist( kfromlist , parentp -> name )
336 && onlist( ktolist , childp -> name ) ) {
339 childp -> ncall += rawp -> raw_count;
341 if ( debug & TALLYDEBUG ) {
342 printf( "[tally] arc from %s to %s traversed %ld times\n" ,
343 parentp -> name , childp -> name , rawp -> raw_count );
346 addarc( parentp , childp , rawp -> raw_count );
350 * dump out the gmon.sum file
353 dumpsum(const char *sumfile)
355 register nltype *nlp;
356 register arctype *arcp;
360 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL )
361 err( 1 , "%s" , sumfile );
363 * dump the header; use the last header read in
365 if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 )
366 err( 1 , "%s" , sumfile );
370 if (fwrite(samples, histcounter_size, nsamples, sfile) != nsamples)
371 err( 1 , "%s" , sumfile );
373 * dump the normalized raw arc information
375 for ( nlp = nl ; nlp < npe ; nlp++ ) {
376 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
377 arc.raw_frompc = arcp -> arc_parentp -> value;
378 arc.raw_selfpc = arcp -> arc_childp -> value;
379 arc.raw_count = arcp -> arc_count;
380 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 )
381 err( 1 , "%s" , sumfile );
383 if ( debug & SAMPLEDEBUG ) {
384 printf( "[dumpsum] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
385 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
394 valcmp(const void *v1, const void *v2)
396 const nltype *p1 = (const nltype *)v1;
397 const nltype *p2 = (const nltype *)v2;
399 if ( p1 -> value < p2 -> value ) {
402 if ( p1 -> value > p2 -> value ) {
409 readsamples(FILE *pfile)
415 samples = (double *) calloc(nsamples, sizeof(double));
417 errx(0, "no room for %d sample pc's", nsamples);
419 for (i = 0; i < nsamples; i++) {
420 fread(&sample, histcounter_size, 1, pfile);
423 switch ( histcounter_type ) {
425 samples[i] += *(int8_t *)&sample;
428 samples[i] += *(u_int8_t *)&sample;
431 samples[i] += *(int16_t *)&sample;
434 samples[i] += *(u_int16_t *)&sample;
437 samples[i] += *(int32_t *)&sample;
440 samples[i] += *(u_int32_t *)&sample;
443 samples[i] += *(int64_t *)&sample;
446 samples[i] += *(u_int64_t *)&sample;
449 err(1, "unsupported histogram counter type %d", histcounter_type);
453 errx(1, "unexpected EOF after reading %d/%d samples", --i , nsamples );
457 * Assign samples to the procedures to which they belong.
459 * There are three cases as to where pcl and pch can be
460 * with respect to the routine entry addresses svalue0 and svalue1
461 * as shown in the following diagram. overlap computes the
462 * distance between the arrows, the fraction of the sample
463 * that is to be credited to the routine which starts at svalue0.
469 * +-----------------------------------------------+
471 * | ->| |<- ->| |<- ->| |<- |
473 * +---------+ +---------+ +---------+
477 * pcl pch pcl pch pcl pch
479 * For the vax we assert that samples will never fall in the first
480 * two bytes of any routine, since that is the entry mask,
481 * thus we give call alignentries() to adjust the entry points if
482 * the entry mask falls in one bucket but the code for the routine
483 * doesn't start until the next bucket. In conjunction with the
484 * alignment of routine addresses, this should allow us to have
485 * only one sample for every four bytes of text space and never
486 * have any overlap (the two end cases, above).
494 unsigned long pcl, pch;
496 unsigned long overlap;
497 unsigned long svalue0, svalue1;
499 /* read samples and assign to namelist symbols */
500 scale = highpc - lowpc;
503 for (i = 0, j = 1; i < nsamples; i++) {
507 pcl = lowpc + (unsigned long)(scale * i);
508 pch = lowpc + (unsigned long)(scale * (i + 1));
511 if ( debug & SAMPLEDEBUG ) {
512 printf( "[asgnsamples] pcl 0x%lx pch 0x%lx ccnt %.0f\n" ,
517 for (j = j - 1; j < nname; j++) {
518 svalue0 = nl[j].svalue;
519 svalue1 = nl[j+1].svalue;
521 * if high end of tick is below entry address,
527 * if low end of tick into next routine,
528 * go for next routine.
532 overlap = min(pch, svalue1) - max(pcl, svalue0);
535 if (debug & SAMPLEDEBUG) {
536 printf("[asgnsamples] (0x%lx->0x%lx-0x%lx) %s gets %f ticks %lu overlap\n",
537 nl[j].value / HISTORICAL_SCALE_2,
538 svalue0, svalue1, nl[j].name,
539 overlap * thetime / scale, overlap);
542 nl[j].time += overlap * thetime / scale;
547 if (debug & SAMPLEDEBUG) {
548 printf("[asgnsamples] totime %f\n", totime);
555 min(unsigned long a, unsigned long b)
563 max(unsigned long a, unsigned long b)
571 * calculate scaled entry point addresses (to save time in asgnsamples),
572 * and possibly push the scaled entry points over the entry mask,
573 * if it turns out that the entry point is in one bucket and the code
574 * for a routine is in the next bucket.
579 register struct nl *nlp;
580 unsigned long bucket_of_entry;
581 unsigned long bucket_of_code;
583 for (nlp = nl; nlp < npe; nlp++) {
584 nlp -> svalue = nlp -> value / HISTORICAL_SCALE_2;
585 bucket_of_entry = (nlp->svalue - lowpc) / scale;
586 bucket_of_code = (nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2 -
588 if (bucket_of_entry < bucket_of_code) {
590 if (debug & SAMPLEDEBUG) {
591 printf("[alignentries] pushing svalue 0x%lx to 0x%lx\n",
593 nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2);
596 nlp->svalue += OFFSET_OF_CODE / HISTORICAL_SCALE_2;