1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 * 31 * @(#)gprof.h 8.1 (Berkeley) 6/6/93 32 * $FreeBSD$ 33 */ 34 35 #include <sys/types.h> 36 #include <sys/stat.h> 37 #include <sys/gmon.h> 38 39 #include <stdio.h> 40 #include <stdlib.h> 41 42 #if __amd64__ 43 # include "amd64.h" 44 #endif 45 #if __arm__ 46 # include "arm.h" 47 #endif 48 #if __i386__ 49 # include "i386.h" 50 #endif 51 #if __mips__ 52 # include "mips.h" 53 #endif 54 #if __powerpc__ 55 # include "powerpc.h" 56 #endif 57 #if __sparc64__ 58 # include "sparc64.h" 59 #endif 60 61 /* 62 * booleans 63 */ 64 typedef int bool; 65 #define FALSE 0 66 #define TRUE 1 67 68 /* 69 * Historical scale factor in profil(2)'s algorithm for converting 70 * pc addresses to bucket numbers. This now just complicates the 71 * scaling and makes bucket:pc densities of more than 1/2 useless. 72 */ 73 #define HISTORICAL_SCALE_2 2 74 75 /* 76 * ticks per second 77 */ 78 long hz; 79 80 size_t histcounter_size; 81 int histcounter_type; 82 83 char *a_outname; 84 #define A_OUTNAME "a.out" 85 86 char *gmonname; 87 #define GMONSUM "gmon.sum" 88 89 /* 90 * a constructed arc, 91 * with pointers to the namelist entry of the parent and the child, 92 * a count of how many times this arc was traversed, 93 * and pointers to the next parent of this child and 94 * the next child of this parent. 95 */ 96 struct arcstruct { 97 struct nl *arc_parentp; /* pointer to parent's nl entry */ 98 struct nl *arc_childp; /* pointer to child's nl entry */ 99 long arc_count; /* num calls from parent to child */ 100 double arc_time; /* time inherited along arc */ 101 double arc_childtime; /* childtime inherited along arc */ 102 struct arcstruct *arc_parentlist; /* parents-of-this-child list */ 103 struct arcstruct *arc_childlist; /* children-of-this-parent list */ 104 struct arcstruct *arc_next; /* list of arcs on cycle */ 105 unsigned short arc_cyclecnt; /* num cycles involved in */ 106 unsigned short arc_flags; /* see below */ 107 }; 108 typedef struct arcstruct arctype; 109 110 /* 111 * arc flags 112 */ 113 #define DEADARC 0x01 /* time should not propagate across the arc */ 114 #define ONLIST 0x02 /* arc is on list of arcs in cycles */ 115 116 /* 117 * The symbol table; 118 * for each external in the specified file we gather 119 * its address, the number of calls and compute its share of CPU time. 120 */ 121 struct nl { 122 const char *name; /* the name */ 123 unsigned long value; /* the pc entry point */ 124 unsigned long svalue; /* entry point aligned to histograms */ 125 double time; /* ticks in this routine */ 126 double childtime; /* cumulative ticks in children */ 127 long ncall; /* how many times called */ 128 long npropcall; /* times called by live arcs */ 129 long selfcalls; /* how many calls to self */ 130 double propfraction; /* what % of time propagates */ 131 double propself; /* how much self time propagates */ 132 double propchild; /* how much child time propagates */ 133 short printflag; /* should this be printed? */ 134 short flags; /* see below */ 135 int index; /* index in the graph list */ 136 int toporder; /* graph call chain top-sort order */ 137 int cycleno; /* internal number of cycle on */ 138 int parentcnt; /* number of live parent arcs */ 139 struct nl *cyclehead; /* pointer to head of cycle */ 140 struct nl *cnext; /* pointer to next member of cycle */ 141 arctype *parents; /* list of caller arcs */ 142 arctype *children; /* list of callee arcs */ 143 }; 144 typedef struct nl nltype; 145 146 nltype *nl; /* the whole namelist */ 147 nltype *npe; /* the virtual end of the namelist */ 148 int nname; /* the number of function names */ 149 150 #define HASCYCLEXIT 0x08 /* node has arc exiting from cycle */ 151 #define CYCLEHEAD 0x10 /* node marked as head of a cycle */ 152 #define VISITED 0x20 /* node visited during a cycle */ 153 154 /* 155 * The cycle list. 156 * for each subcycle within an identified cycle, we gather 157 * its size and the list of included arcs. 158 */ 159 struct cl { 160 int size; /* length of cycle */ 161 struct cl *next; /* next member of list */ 162 arctype *list[1]; /* list of arcs in cycle */ 163 /* actually longer */ 164 }; 165 typedef struct cl cltype; 166 167 arctype *archead; /* the head of arcs in current cycle list */ 168 cltype *cyclehead; /* the head of the list */ 169 int cyclecnt; /* the number of cycles found */ 170 #define CYCLEMAX 100 /* maximum cycles before cutting one of them */ 171 172 /* 173 * flag which marks a nl entry as topologically ``busy'' 174 * flag which marks a nl entry as topologically ``not_numbered'' 175 */ 176 #define DFN_BUSY -1 177 #define DFN_NAN 0 178 179 /* 180 * namelist entries for cycle headers. 181 * the number of discovered cycles. 182 */ 183 nltype *cyclenl; /* cycle header namelist */ 184 int ncycle; /* number of cycles discovered */ 185 186 /* 187 * The header on the gmon.out file. 188 * gmon.out consists of a struct phdr (defined in gmon.h) 189 * and then an array of ncnt samples representing the 190 * discretized program counter values. 191 * 192 * Backward compatible old style header 193 */ 194 struct ophdr { 195 u_short *lpc; 196 u_short *hpc; 197 int ncnt; 198 }; 199 200 int debug; 201 202 /* 203 * Each discretized pc sample has 204 * a count of the number of samples in its range 205 */ 206 double *samples; 207 208 unsigned long s_lowpc; /* lowpc from the profile file */ 209 unsigned long s_highpc; /* highpc from the profile file */ 210 unsigned long lowpc, highpc; /* range profiled, in historical units */ 211 unsigned sampbytes; /* number of bytes of samples */ 212 int nsamples; /* number of samples */ 213 double actime; /* accumulated time thus far for putprofline */ 214 double totime; /* total time for all routines */ 215 double printtime; /* total of time being printed */ 216 double scale; /* scale factor converting samples to pc 217 values: each sample covers scale bytes */ 218 unsigned char *textspace; /* text space of a.out in core */ 219 int cyclethreshold; /* with -C, minimum cycle size to ignore */ 220 221 /* 222 * option flags, from a to z. 223 */ 224 bool aflag; /* suppress static functions */ 225 bool bflag; /* blurbs, too */ 226 bool Cflag; /* find cut-set to eliminate cycles */ 227 bool dflag; /* debugging options */ 228 bool eflag; /* specific functions excluded */ 229 bool Eflag; /* functions excluded with time */ 230 bool fflag; /* specific functions requested */ 231 bool Fflag; /* functions requested with time */ 232 bool kflag; /* arcs to be deleted */ 233 bool Kflag; /* use the running kernel for symbols */ 234 bool sflag; /* sum multiple gmon.out files */ 235 bool uflag; /* suppress symbols hidden from C */ 236 bool zflag; /* zero time/called functions, too */ 237 238 /* 239 * structure for various string lists 240 */ 241 struct stringlist { 242 struct stringlist *next; 243 char *string; 244 }; 245 struct stringlist *elist; 246 struct stringlist *Elist; 247 struct stringlist *flist; 248 struct stringlist *Flist; 249 struct stringlist *kfromlist; 250 struct stringlist *ktolist; 251 252 /* 253 * function declarations 254 */ 255 void addarc(nltype *, nltype *, long); 256 bool addcycle(arctype **, arctype **); 257 void addlist(struct stringlist *, char *); 258 void alignentries(void); 259 int aout_getnfile(const char *, char ***); 260 int arccmp(arctype *, arctype *); 261 arctype *arclookup(nltype *, nltype *); 262 void asgnsamples(void); 263 void compresslist(void); 264 bool cycleanalyze(void); 265 void cyclelink(void); 266 void cycletime(void); 267 bool descend(nltype *, arctype **, arctype **); 268 void dfn(nltype *); 269 bool dfn_busy(nltype *); 270 void dfn_findcycle(nltype *); 271 void dfn_init(void); 272 bool dfn_numbered(nltype *); 273 void dfn_post_visit(nltype *); 274 void dfn_pre_visit(nltype *); 275 void dfn_self_cycle(nltype *); 276 nltype **doarcs(void); 277 void doflags(void); 278 void dotime(void); 279 void dumpsum(const char *); 280 int elf_getnfile(const char *, char ***); 281 void flatprofheader(void); 282 void flatprofline(nltype *); 283 void getpfile(char *); 284 void gprofheader(void); 285 void gprofline(register nltype *); 286 int hertz(void); 287 void inheritflags(nltype *); 288 int kernel_getnfile(const char *, char ***); 289 /* 290 main(); 291 */ 292 unsigned long max(unsigned long, unsigned long); 293 int membercmp(nltype *, nltype *); 294 unsigned long min(unsigned long, unsigned long); 295 nltype *nllookup(unsigned long); 296 bool onlist(struct stringlist *, const char *); 297 FILE *openpfile(char *); 298 void printblurb(const char *); 299 void printchildren(nltype *); 300 void printcycle(nltype *); 301 void printgprof(nltype **); 302 void printindex(void); 303 void printmembers(nltype *); 304 void printname(nltype *); 305 void printparents(nltype *); 306 void printprof(void); 307 void printsubcycle(cltype *); 308 void readsamples(FILE *); 309 void sortchildren(nltype *); 310 void sortmembers(nltype *); 311 void sortparents(nltype *); 312 void tally(struct rawarc *); 313 void timepropagate(nltype *); 314 int totalcmp(const void *, const void *); 315 316 #define LESSTHAN -1 317 #define EQUALTO 0 318 #define GREATERTHAN 1 319 320 #define DFNDEBUG 1 321 #define CYCLEDEBUG 2 322 #define ARCDEBUG 4 323 #define TALLYDEBUG 8 324 #define TIMEDEBUG 16 325 #define SAMPLEDEBUG 32 326 #define AOUTDEBUG 64 327 #define CALLDEBUG 128 328 #define LOOKUPDEBUG 256 329 #define PROPDEBUG 512 330 #define BREAKCYCLE 1024 331 #define SUBCYCLELIST 2048 332 #define ANYDEBUG 4096 333