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