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