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))
165 errx(1, "%s: bad format", a_outname);
169 qsort(nl, nname, sizeof(nltype), valcmp);
171 * turn off default functions
173 for ( sp = defaultEs ; *sp ; sp++ ) {
175 addlist( Elist , *sp );
177 addlist( elist , *sp );
180 * get information about mon.out file(s).
183 getpfile( gmonname );
187 } while ( *argv++ != 0 );
189 * how many ticks per second?
190 * if we can't tell, report time in ticks.
194 fprintf(stderr, "time is in ticks, not seconds\n");
197 * dump out a gmon.sum file if requested
203 * assign samples to procedures
207 * assemble the dynamic profile
209 timesortnlp = doarcs();
211 * print the dynamic profile
214 printgprof( timesortnlp );
217 * print the flat profile
230 * information from a gmon.out file is in two parts:
231 * an array of sampling hits within pc ranges,
235 getpfile(char *filename)
240 pfile = openpfile(filename);
243 * the rest of the file consists of
244 * a bunch of <from,self,count> tuples.
246 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
248 if ( debug & SAMPLEDEBUG ) {
249 printf( "[getpfile] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
250 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
262 openpfile(char *filename)
269 if((pfile = fopen(filename, "r")) == NULL)
270 err(1, "%s", filename);
271 fread(&tmp, sizeof(struct gmonhdr), 1, pfile);
272 if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc ||
273 tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) )
274 errx(1, "%s: incompatible with first gmon file", filename);
276 if ( gmonhdr.version == GMONVERSION ) {
277 rate = gmonhdr.profrate;
278 size = sizeof(struct gmonhdr);
280 fseek(pfile, sizeof(struct ophdr), SEEK_SET);
281 size = sizeof(struct ophdr);
282 gmonhdr.profrate = rate = hertz();
283 gmonhdr.version = GMONVERSION;
287 } else if (hz != rate)
288 errx(0, "%s: profile clock rate (%d) %s (%ld) in first gmon file",
289 filename, rate, "incompatible with clock rate", hz);
290 if ( gmonhdr.histcounter_type == 0 ) {
291 /* Historical case. The type was u_short (2 bytes in practice). */
292 histcounter_type = 16;
293 histcounter_size = 2;
295 histcounter_type = gmonhdr.histcounter_type;
296 histcounter_size = abs(histcounter_type) / CHAR_BIT;
298 s_lowpc = (unsigned long) gmonhdr.lpc;
299 s_highpc = (unsigned long) gmonhdr.hpc;
300 lowpc = (unsigned long)gmonhdr.lpc / HISTORICAL_SCALE_2;
301 highpc = (unsigned long)gmonhdr.hpc / HISTORICAL_SCALE_2;
302 sampbytes = gmonhdr.ncnt - size;
303 nsamples = sampbytes / histcounter_size;
305 if ( debug & SAMPLEDEBUG ) {
306 printf( "[openpfile] hdr.lpc 0x%lx hdr.hpc 0x%lx hdr.ncnt %d\n",
307 gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt );
308 printf( "[openpfile] s_lowpc 0x%lx s_highpc 0x%lx\n" ,
309 s_lowpc , s_highpc );
310 printf( "[openpfile] lowpc 0x%lx highpc 0x%lx\n" ,
312 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
313 sampbytes , nsamples );
314 printf( "[openpfile] sample rate %ld\n" , hz );
321 tally(struct rawarc *rawp)
326 parentp = nllookup( rawp -> raw_frompc );
327 childp = nllookup( rawp -> raw_selfpc );
328 if ( parentp == 0 || childp == 0 )
331 && onlist( kfromlist , parentp -> name )
332 && onlist( ktolist , childp -> name ) ) {
335 childp -> ncall += rawp -> raw_count;
337 if ( debug & TALLYDEBUG ) {
338 printf( "[tally] arc from %s to %s traversed %ld times\n" ,
339 parentp -> name , childp -> name , rawp -> raw_count );
342 addarc( parentp , childp , rawp -> raw_count );
346 * dump out the gmon.sum file
349 dumpsum(const char *sumfile)
351 register nltype *nlp;
352 register arctype *arcp;
356 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL )
357 err( 1 , "%s" , sumfile );
359 * dump the header; use the last header read in
361 if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 )
362 err( 1 , "%s" , sumfile );
366 if (fwrite(samples, histcounter_size, nsamples, sfile) != nsamples)
367 err( 1 , "%s" , sumfile );
369 * dump the normalized raw arc information
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 err( 1 , "%s" , sumfile );
379 if ( debug & SAMPLEDEBUG ) {
380 printf( "[dumpsum] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
381 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
390 valcmp(const void *v1, const void *v2)
392 const nltype *p1 = (const nltype *)v1;
393 const nltype *p2 = (const nltype *)v2;
395 if ( p1 -> value < p2 -> value ) {
398 if ( p1 -> value > p2 -> value ) {
405 readsamples(FILE *pfile)
411 samples = (double *) calloc(nsamples, sizeof(double));
413 errx(0, "no room for %d sample pc's", nsamples);
415 for (i = 0; i < nsamples; i++) {
416 fread(&sample, histcounter_size, 1, pfile);
419 switch ( histcounter_type ) {
421 samples[i] += *(int8_t *)&sample;
424 samples[i] += *(u_int8_t *)&sample;
427 samples[i] += *(int16_t *)&sample;
430 samples[i] += *(u_int16_t *)&sample;
433 samples[i] += *(int32_t *)&sample;
436 samples[i] += *(u_int32_t *)&sample;
439 samples[i] += *(int64_t *)&sample;
442 samples[i] += *(u_int64_t *)&sample;
445 err(1, "unsupported histogram counter type %d", histcounter_type);
449 errx(1, "unexpected EOF after reading %d/%d samples", --i , nsamples );
453 * Assign samples to the procedures to which they belong.
455 * There are three cases as to where pcl and pch can be
456 * with respect to the routine entry addresses svalue0 and svalue1
457 * as shown in the following diagram. overlap computes the
458 * distance between the arrows, the fraction of the sample
459 * that is to be credited to the routine which starts at svalue0.
465 * +-----------------------------------------------+
467 * | ->| |<- ->| |<- ->| |<- |
469 * +---------+ +---------+ +---------+
473 * pcl pch pcl pch pcl pch
475 * For the vax we assert that samples will never fall in the first
476 * two bytes of any routine, since that is the entry mask,
477 * thus we give call alignentries() to adjust the entry points if
478 * the entry mask falls in one bucket but the code for the routine
479 * doesn't start until the next bucket. In conjunction with the
480 * alignment of routine addresses, this should allow us to have
481 * only one sample for every four bytes of text space and never
482 * have any overlap (the two end cases, above).
490 unsigned long pcl, pch;
492 unsigned long overlap;
493 unsigned long svalue0, svalue1;
495 /* read samples and assign to namelist symbols */
496 scale = highpc - lowpc;
499 for (i = 0, j = 1; i < nsamples; i++) {
503 pcl = lowpc + (unsigned long)(scale * i);
504 pch = lowpc + (unsigned long)(scale * (i + 1));
507 if ( debug & SAMPLEDEBUG ) {
508 printf( "[asgnsamples] pcl 0x%lx pch 0x%lx ccnt %.0f\n" ,
513 for (j = j - 1; j < nname; j++) {
514 svalue0 = nl[j].svalue;
515 svalue1 = nl[j+1].svalue;
517 * if high end of tick is below entry address,
523 * if low end of tick into next routine,
524 * go for next routine.
528 overlap = min(pch, svalue1) - max(pcl, svalue0);
531 if (debug & SAMPLEDEBUG) {
532 printf("[asgnsamples] (0x%lx->0x%lx-0x%lx) %s gets %f ticks %lu overlap\n",
533 nl[j].value / HISTORICAL_SCALE_2,
534 svalue0, svalue1, nl[j].name,
535 overlap * thetime / scale, overlap);
538 nl[j].time += overlap * thetime / scale;
543 if (debug & SAMPLEDEBUG) {
544 printf("[asgnsamples] totime %f\n", totime);
551 min(unsigned long a, unsigned long b)
559 max(unsigned long a, unsigned long b)
567 * calculate scaled entry point addresses (to save time in asgnsamples),
568 * and possibly push the scaled entry points over the entry mask,
569 * if it turns out that the entry point is in one bucket and the code
570 * for a routine is in the next bucket.
575 register struct nl *nlp;
576 unsigned long bucket_of_entry;
577 unsigned long bucket_of_code;
579 for (nlp = nl; nlp < npe; nlp++) {
580 nlp -> svalue = nlp -> value / HISTORICAL_SCALE_2;
581 bucket_of_entry = (nlp->svalue - lowpc) / scale;
582 bucket_of_code = (nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2 -
584 if (bucket_of_entry < bucket_of_code) {
586 if (debug & SAMPLEDEBUG) {
587 printf("[alignentries] pushing svalue 0x%lx to 0x%lx\n",
589 nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2);
592 nlp->svalue += OFFSET_OF_CODE / HISTORICAL_SCALE_2;