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
40 static int valcmp(const void *, const void *);
42 static struct gmonhdr gmonhdr;
47 main(int argc, char **argv)
57 while ( *argv != 0 && **argv == '-' ) {
68 cyclethreshold = atoi( *++argv );
73 debug |= atoi( *++argv );
76 printf("[main] debug = %d\n", debug);
77 # else /* not DEBUG */
78 printf("gprof: -d ignored\n");
83 addlist( Elist , *argv );
85 addlist( elist , *argv );
89 addlist( elist , *++argv );
94 addlist( Flist , *argv );
96 addlist( flist , *argv );
100 addlist( flist , *++argv );
104 addlist( kfromlist , *++argv );
105 addlist( ktolist , *++argv );
135 a_outname = A_OUTNAME;
141 gmonname = (char *) malloc(strlen(a_outname)+6);
142 strcpy(gmonname, a_outname);
143 strcat(gmonname, ".gmon");
146 * get information from the executable file.
148 if ((Kflag && kernel_getnfile(a_outname, &defaultEs) == -1) ||
149 (!Kflag && elf_getnfile(a_outname, &defaultEs) == -1))
150 errx(1, "%s: bad format", a_outname);
154 qsort(nl, nname, sizeof(nltype), valcmp);
156 * turn off default functions
158 for ( sp = defaultEs ; *sp ; sp++ ) {
160 addlist( Elist , *sp );
162 addlist( elist , *sp );
165 * get information about mon.out file(s).
168 getpfile( gmonname );
172 } while ( *argv++ != 0 );
174 * how many ticks per second?
175 * if we can't tell, report time in ticks.
179 fprintf(stderr, "time is in ticks, not seconds\n");
182 * dump out a gmon.sum file if requested
188 * assign samples to procedures
192 * assemble the dynamic profile
194 timesortnlp = doarcs();
196 * print the dynamic profile
199 printgprof( timesortnlp );
202 * print the flat profile
215 * information from a gmon.out file is in two parts:
216 * an array of sampling hits within pc ranges,
220 getpfile(char *filename)
225 pfile = openpfile(filename);
228 * the rest of the file consists of
229 * a bunch of <from,self,count> tuples.
231 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
233 if ( debug & SAMPLEDEBUG ) {
234 printf( "[getpfile] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
235 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
247 openpfile(char *filename)
254 if((pfile = fopen(filename, "r")) == NULL)
255 err(1, "%s", filename);
256 fread(&tmp, sizeof(struct gmonhdr), 1, pfile);
257 if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc ||
258 tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) )
259 errx(1, "%s: incompatible with first gmon file", filename);
261 if ( gmonhdr.version == GMONVERSION ) {
262 rate = gmonhdr.profrate;
263 size = sizeof(struct gmonhdr);
265 fseek(pfile, sizeof(struct ophdr), SEEK_SET);
266 size = sizeof(struct ophdr);
267 gmonhdr.profrate = rate = hertz();
268 gmonhdr.version = GMONVERSION;
272 } else if (hz != rate)
273 errx(0, "%s: profile clock rate (%d) %s (%ld) in first gmon file",
274 filename, rate, "incompatible with clock rate", hz);
275 if ( gmonhdr.histcounter_type == 0 ) {
276 /* Historical case. The type was u_short (2 bytes in practice). */
277 histcounter_type = 16;
278 histcounter_size = 2;
280 histcounter_type = gmonhdr.histcounter_type;
281 histcounter_size = abs(histcounter_type) / CHAR_BIT;
283 s_lowpc = (unsigned long) gmonhdr.lpc;
284 s_highpc = (unsigned long) gmonhdr.hpc;
285 lowpc = (unsigned long)gmonhdr.lpc / HISTORICAL_SCALE_2;
286 highpc = (unsigned long)gmonhdr.hpc / HISTORICAL_SCALE_2;
287 sampbytes = gmonhdr.ncnt - size;
288 nsamples = sampbytes / histcounter_size;
290 if ( debug & SAMPLEDEBUG ) {
291 printf( "[openpfile] hdr.lpc 0x%lx hdr.hpc 0x%lx hdr.ncnt %d\n",
292 gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt );
293 printf( "[openpfile] s_lowpc 0x%lx s_highpc 0x%lx\n" ,
294 s_lowpc , s_highpc );
295 printf( "[openpfile] lowpc 0x%lx highpc 0x%lx\n" ,
297 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
298 sampbytes , nsamples );
299 printf( "[openpfile] sample rate %ld\n" , hz );
306 tally(struct rawarc *rawp)
311 parentp = nllookup( rawp -> raw_frompc );
312 childp = nllookup( rawp -> raw_selfpc );
313 if ( parentp == 0 || childp == 0 )
316 && onlist( kfromlist , parentp -> name )
317 && onlist( ktolist , childp -> name ) ) {
320 childp -> ncall += rawp -> raw_count;
322 if ( debug & TALLYDEBUG ) {
323 printf( "[tally] arc from %s to %s traversed %ld times\n" ,
324 parentp -> name , childp -> name , rawp -> raw_count );
327 addarc( parentp , childp , rawp -> raw_count );
331 * dump out the gmon.sum file
334 dumpsum(const char *sumfile)
336 register nltype *nlp;
337 register arctype *arcp;
341 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL )
342 err( 1 , "%s" , sumfile );
344 * dump the header; use the last header read in
346 if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 )
347 err( 1 , "%s" , sumfile );
351 if (fwrite(samples, histcounter_size, nsamples, sfile) != nsamples)
352 err( 1 , "%s" , sumfile );
354 * dump the normalized raw arc information
356 for ( nlp = nl ; nlp < npe ; nlp++ ) {
357 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
358 arc.raw_frompc = arcp -> arc_parentp -> value;
359 arc.raw_selfpc = arcp -> arc_childp -> value;
360 arc.raw_count = arcp -> arc_count;
361 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 )
362 err( 1 , "%s" , sumfile );
364 if ( debug & SAMPLEDEBUG ) {
365 printf( "[dumpsum] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
366 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
375 valcmp(const void *v1, const void *v2)
377 const nltype *p1 = (const nltype *)v1;
378 const nltype *p2 = (const nltype *)v2;
380 if ( p1 -> value < p2 -> value ) {
383 if ( p1 -> value > p2 -> value ) {
390 readsamples(FILE *pfile)
396 samples = (double *) calloc(nsamples, sizeof(double));
398 errx(0, "no room for %d sample pc's", nsamples);
400 for (i = 0; i < nsamples; i++) {
401 fread(&sample, histcounter_size, 1, pfile);
404 switch ( histcounter_type ) {
406 samples[i] += *(int8_t *)&sample;
409 samples[i] += *(u_int8_t *)&sample;
412 samples[i] += *(int16_t *)&sample;
415 samples[i] += *(u_int16_t *)&sample;
418 samples[i] += *(int32_t *)&sample;
421 samples[i] += *(u_int32_t *)&sample;
424 samples[i] += *(int64_t *)&sample;
427 samples[i] += *(u_int64_t *)&sample;
430 err(1, "unsupported histogram counter type %d", histcounter_type);
434 errx(1, "unexpected EOF after reading %d/%d samples", --i , nsamples );
438 * Assign samples to the procedures to which they belong.
440 * There are three cases as to where pcl and pch can be
441 * with respect to the routine entry addresses svalue0 and svalue1
442 * as shown in the following diagram. overlap computes the
443 * distance between the arrows, the fraction of the sample
444 * that is to be credited to the routine which starts at svalue0.
450 * +-----------------------------------------------+
452 * | ->| |<- ->| |<- ->| |<- |
454 * +---------+ +---------+ +---------+
458 * pcl pch pcl pch pcl pch
460 * For the vax we assert that samples will never fall in the first
461 * two bytes of any routine, since that is the entry mask,
462 * thus we give call alignentries() to adjust the entry points if
463 * the entry mask falls in one bucket but the code for the routine
464 * doesn't start until the next bucket. In conjunction with the
465 * alignment of routine addresses, this should allow us to have
466 * only one sample for every four bytes of text space and never
467 * have any overlap (the two end cases, above).
475 unsigned long pcl, pch;
477 unsigned long overlap;
478 unsigned long svalue0, svalue1;
480 /* read samples and assign to namelist symbols */
481 scale = highpc - lowpc;
484 for (i = 0, j = 1; i < nsamples; i++) {
488 pcl = lowpc + (unsigned long)(scale * i);
489 pch = lowpc + (unsigned long)(scale * (i + 1));
492 if ( debug & SAMPLEDEBUG ) {
493 printf( "[asgnsamples] pcl 0x%lx pch 0x%lx ccnt %.0f\n" ,
498 for (j = j - 1; j < nname; j++) {
499 svalue0 = nl[j].svalue;
500 svalue1 = nl[j+1].svalue;
502 * if high end of tick is below entry address,
508 * if low end of tick into next routine,
509 * go for next routine.
513 overlap = min(pch, svalue1) - max(pcl, svalue0);
516 if (debug & SAMPLEDEBUG) {
517 printf("[asgnsamples] (0x%lx->0x%lx-0x%lx) %s gets %f ticks %lu overlap\n",
518 nl[j].value / HISTORICAL_SCALE_2,
519 svalue0, svalue1, nl[j].name,
520 overlap * thetime / scale, overlap);
523 nl[j].time += overlap * thetime / scale;
528 if (debug & SAMPLEDEBUG) {
529 printf("[asgnsamples] totime %f\n", totime);
536 min(unsigned long a, unsigned long b)
544 max(unsigned long a, unsigned long b)
552 * calculate scaled entry point addresses (to save time in asgnsamples),
553 * and possibly push the scaled entry points over the entry mask,
554 * if it turns out that the entry point is in one bucket and the code
555 * for a routine is in the next bucket.
560 register struct nl *nlp;
561 unsigned long bucket_of_entry;
562 unsigned long bucket_of_code;
564 for (nlp = nl; nlp < npe; nlp++) {
565 nlp -> svalue = nlp -> value / HISTORICAL_SCALE_2;
566 bucket_of_entry = (nlp->svalue - lowpc) / scale;
567 bucket_of_code = (nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2 -
569 if (bucket_of_entry < bucket_of_code) {
571 if (debug & SAMPLEDEBUG) {
572 printf("[alignentries] pushing svalue 0x%lx to 0x%lx\n",
574 nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2);
577 nlp->svalue += OFFSET_OF_CODE / HISTORICAL_SCALE_2;