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$");
54 static int valcmp(const void *, const void *);
56 static struct gmonhdr gmonhdr;
61 main(int argc, char **argv)
71 while ( *argv != 0 && **argv == '-' ) {
82 cyclethreshold = atoi( *++argv );
87 debug |= atoi( *++argv );
90 printf("[main] debug = %d\n", debug);
91 # else /* not DEBUG */
92 printf("gprof: -d ignored\n");
97 addlist( Elist , *argv );
99 addlist( elist , *argv );
103 addlist( elist , *++argv );
108 addlist( Flist , *argv );
110 addlist( flist , *argv );
114 addlist( flist , *++argv );
118 addlist( kfromlist , *++argv );
119 addlist( ktolist , *++argv );
149 a_outname = A_OUTNAME;
155 gmonname = (char *) malloc(strlen(a_outname)+6);
156 strcpy(gmonname, a_outname);
157 strcat(gmonname, ".gmon");
160 * get information from the executable file.
162 if ((Kflag && kernel_getnfile(a_outname, &defaultEs) == -1) ||
163 (!Kflag && elf_getnfile(a_outname, &defaultEs) == -1
165 && aout_getnfile(a_outname, &defaultEs) == -1
168 errx(1, "%s: bad format", a_outname);
172 qsort(nl, nname, sizeof(nltype), valcmp);
174 * turn off default functions
176 for ( sp = defaultEs ; *sp ; sp++ ) {
178 addlist( Elist , *sp );
180 addlist( elist , *sp );
183 * get information about mon.out file(s).
186 getpfile( gmonname );
190 } while ( *argv++ != 0 );
192 * how many ticks per second?
193 * if we can't tell, report time in ticks.
197 fprintf(stderr, "time is in ticks, not seconds\n");
200 * dump out a gmon.sum file if requested
206 * assign samples to procedures
210 * assemble the dynamic profile
212 timesortnlp = doarcs();
214 * print the dynamic profile
217 printgprof( timesortnlp );
220 * print the flat profile
233 * information from a gmon.out file is in two parts:
234 * an array of sampling hits within pc ranges,
238 getpfile(char *filename)
243 pfile = openpfile(filename);
246 * the rest of the file consists of
247 * a bunch of <from,self,count> tuples.
249 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
251 if ( debug & SAMPLEDEBUG ) {
252 printf( "[getpfile] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
253 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
265 openpfile(char *filename)
272 if((pfile = fopen(filename, "r")) == NULL)
273 err(1, "%s", filename);
274 fread(&tmp, sizeof(struct gmonhdr), 1, pfile);
275 if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc ||
276 tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) )
277 errx(1, "%s: incompatible with first gmon file", filename);
279 if ( gmonhdr.version == GMONVERSION ) {
280 rate = gmonhdr.profrate;
281 size = sizeof(struct gmonhdr);
283 fseek(pfile, sizeof(struct ophdr), SEEK_SET);
284 size = sizeof(struct ophdr);
285 gmonhdr.profrate = rate = hertz();
286 gmonhdr.version = GMONVERSION;
290 } else if (hz != rate)
291 errx(0, "%s: profile clock rate (%d) %s (%ld) in first gmon file",
292 filename, rate, "incompatible with clock rate", hz);
293 if ( gmonhdr.histcounter_type == 0 ) {
294 /* Historical case. The type was u_short (2 bytes in practice). */
295 histcounter_type = 16;
296 histcounter_size = 2;
298 histcounter_type = gmonhdr.histcounter_type;
299 histcounter_size = abs(histcounter_type) / CHAR_BIT;
301 s_lowpc = (unsigned long) gmonhdr.lpc;
302 s_highpc = (unsigned long) gmonhdr.hpc;
303 lowpc = (unsigned long)gmonhdr.lpc / HISTORICAL_SCALE_2;
304 highpc = (unsigned long)gmonhdr.hpc / HISTORICAL_SCALE_2;
305 sampbytes = gmonhdr.ncnt - size;
306 nsamples = sampbytes / histcounter_size;
308 if ( debug & SAMPLEDEBUG ) {
309 printf( "[openpfile] hdr.lpc 0x%lx hdr.hpc 0x%lx hdr.ncnt %d\n",
310 gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt );
311 printf( "[openpfile] s_lowpc 0x%lx s_highpc 0x%lx\n" ,
312 s_lowpc , s_highpc );
313 printf( "[openpfile] lowpc 0x%lx highpc 0x%lx\n" ,
315 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
316 sampbytes , nsamples );
317 printf( "[openpfile] sample rate %ld\n" , hz );
324 tally(struct rawarc *rawp)
329 parentp = nllookup( rawp -> raw_frompc );
330 childp = nllookup( rawp -> raw_selfpc );
331 if ( parentp == 0 || childp == 0 )
334 && onlist( kfromlist , parentp -> name )
335 && onlist( ktolist , childp -> name ) ) {
338 childp -> ncall += rawp -> raw_count;
340 if ( debug & TALLYDEBUG ) {
341 printf( "[tally] arc from %s to %s traversed %ld times\n" ,
342 parentp -> name , childp -> name , rawp -> raw_count );
345 addarc( parentp , childp , rawp -> raw_count );
349 * dump out the gmon.sum file
352 dumpsum(const char *sumfile)
354 register nltype *nlp;
355 register arctype *arcp;
359 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL )
360 err( 1 , "%s" , sumfile );
362 * dump the header; use the last header read in
364 if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 )
365 err( 1 , "%s" , sumfile );
369 if (fwrite(samples, histcounter_size, nsamples, sfile) != nsamples)
370 err( 1 , "%s" , sumfile );
372 * dump the normalized raw arc information
374 for ( nlp = nl ; nlp < npe ; nlp++ ) {
375 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
376 arc.raw_frompc = arcp -> arc_parentp -> value;
377 arc.raw_selfpc = arcp -> arc_childp -> value;
378 arc.raw_count = arcp -> arc_count;
379 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 )
380 err( 1 , "%s" , sumfile );
382 if ( debug & SAMPLEDEBUG ) {
383 printf( "[dumpsum] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
384 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
393 valcmp(const void *v1, const void *v2)
395 const nltype *p1 = (const nltype *)v1;
396 const nltype *p2 = (const nltype *)v2;
398 if ( p1 -> value < p2 -> value ) {
401 if ( p1 -> value > p2 -> value ) {
408 readsamples(FILE *pfile)
414 samples = (double *) calloc(nsamples, sizeof(double));
416 errx(0, "no room for %d sample pc's", nsamples);
418 for (i = 0; i < nsamples; i++) {
419 fread(&sample, histcounter_size, 1, pfile);
422 switch ( histcounter_type ) {
424 samples[i] += *(int8_t *)&sample;
427 samples[i] += *(u_int8_t *)&sample;
430 samples[i] += *(int16_t *)&sample;
433 samples[i] += *(u_int16_t *)&sample;
436 samples[i] += *(int32_t *)&sample;
439 samples[i] += *(u_int32_t *)&sample;
442 samples[i] += *(int64_t *)&sample;
445 samples[i] += *(u_int64_t *)&sample;
448 err(1, "unsupported histogram counter type %d", histcounter_type);
452 errx(1, "unexpected EOF after reading %d/%d samples", --i , nsamples );
456 * Assign samples to the procedures to which they belong.
458 * There are three cases as to where pcl and pch can be
459 * with respect to the routine entry addresses svalue0 and svalue1
460 * as shown in the following diagram. overlap computes the
461 * distance between the arrows, the fraction of the sample
462 * that is to be credited to the routine which starts at svalue0.
468 * +-----------------------------------------------+
470 * | ->| |<- ->| |<- ->| |<- |
472 * +---------+ +---------+ +---------+
476 * pcl pch pcl pch pcl pch
478 * For the vax we assert that samples will never fall in the first
479 * two bytes of any routine, since that is the entry mask,
480 * thus we give call alignentries() to adjust the entry points if
481 * the entry mask falls in one bucket but the code for the routine
482 * doesn't start until the next bucket. In conjunction with the
483 * alignment of routine addresses, this should allow us to have
484 * only one sample for every four bytes of text space and never
485 * have any overlap (the two end cases, above).
493 unsigned long pcl, pch;
495 unsigned long overlap;
496 unsigned long svalue0, svalue1;
498 /* read samples and assign to namelist symbols */
499 scale = highpc - lowpc;
502 for (i = 0, j = 1; i < nsamples; i++) {
506 pcl = lowpc + (unsigned long)(scale * i);
507 pch = lowpc + (unsigned long)(scale * (i + 1));
510 if ( debug & SAMPLEDEBUG ) {
511 printf( "[asgnsamples] pcl 0x%lx pch 0x%lx ccnt %.0f\n" ,
516 for (j = j - 1; j < nname; j++) {
517 svalue0 = nl[j].svalue;
518 svalue1 = nl[j+1].svalue;
520 * if high end of tick is below entry address,
526 * if low end of tick into next routine,
527 * go for next routine.
531 overlap = min(pch, svalue1) - max(pcl, svalue0);
534 if (debug & SAMPLEDEBUG) {
535 printf("[asgnsamples] (0x%lx->0x%lx-0x%lx) %s gets %f ticks %lu overlap\n",
536 nl[j].value / HISTORICAL_SCALE_2,
537 svalue0, svalue1, nl[j].name,
538 overlap * thetime / scale, overlap);
541 nl[j].time += overlap * thetime / scale;
546 if (debug & SAMPLEDEBUG) {
547 printf("[asgnsamples] totime %f\n", totime);
554 min(unsigned long a, unsigned long b)
562 max(unsigned long a, unsigned long b)
570 * calculate scaled entry point addresses (to save time in asgnsamples),
571 * and possibly push the scaled entry points over the entry mask,
572 * if it turns out that the entry point is in one bucket and the code
573 * for a routine is in the next bucket.
578 register struct nl *nlp;
579 unsigned long bucket_of_entry;
580 unsigned long bucket_of_code;
582 for (nlp = nl; nlp < npe; nlp++) {
583 nlp -> svalue = nlp -> value / HISTORICAL_SCALE_2;
584 bucket_of_entry = (nlp->svalue - lowpc) / scale;
585 bucket_of_code = (nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2 -
587 if (bucket_of_entry < bucket_of_code) {
589 if (debug & SAMPLEDEBUG) {
590 printf("[alignentries] pushing svalue 0x%lx to 0x%lx\n",
592 nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2);
595 nlp->svalue += OFFSET_OF_CODE / HISTORICAL_SCALE_2;