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 */ 35 36 #include <sys/types.h> 37 #include <sys/stat.h> 38 #include <sys/gmon.h> 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 43 #if vax 44 # include "vax.h" 45 #endif 46 #if sparc 47 # include "sparc.h" 48 #endif 49 #if tahoe 50 # include "tahoe.h" 51 #endif 52 #if hp300 53 # include "hp300.h" 54 #endif 55 #if luna68k 56 # include "luna68k.h" 57 #endif 58 #if i386 59 # include "i386.h" 60 #endif 61 #if mips 62 # include "mips.h" 63 #endif 64 65 66 /* 67 * booleans 68 */ 69 typedef int bool; 70 #define FALSE 0 71 #define TRUE 1 72 73 /* 74 * ticks per second 75 */ 76 long hz; 77 78 #ifdef GPROF4 79 typedef int64_t UNIT; 80 #else 81 typedef u_short UNIT; /* unit of profiling */ 82 #endif 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 UNIT *lpc; 196 UNIT *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 UNIT *samples; 207 208 unsigned long s_lowpc; /* lowpc from the profile file */ 209 unsigned long s_highpc; /* highpc from the profile file */ 210 unsigned lowpc, highpc; /* range profiled, in UNIT's */ 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; /* discovered call graph, too */ 227 bool Cflag; /* find cut-set to eliminate cycles */ 228 bool dflag; /* debugging options */ 229 bool eflag; /* specific functions excluded */ 230 bool Eflag; /* functions excluded with time */ 231 bool fflag; /* specific functions requested */ 232 bool Fflag; /* functions requested with time */ 233 bool kflag; /* arcs to be deleted */ 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 /* 256 addarc(); 257 */ 258 int aout_getnfile(const char *, char ***); 259 int arccmp(); 260 arctype *arclookup(); 261 /* 262 asgnsamples(); 263 printblurb(); 264 cyclelink(); 265 dfn(); 266 */ 267 bool dfn_busy(); 268 /* 269 dfn_findcycle(); 270 */ 271 bool dfn_numbered(); 272 /* 273 dfn_post_visit(); 274 dfn_pre_visit(); 275 dfn_self_cycle(); 276 */ 277 nltype **doarcs(); 278 /* 279 done(); 280 */ 281 int elf_getnfile(const char *, char ***); 282 /* 283 findcalls(); 284 flatprofheader(); 285 flatprofline(); 286 */ 287 /* 288 getpfile(); 289 gprofheader(); 290 gprofline(); 291 main(); 292 */ 293 unsigned long max(); 294 int membercmp(); 295 unsigned long min(); 296 nltype *nllookup(); 297 FILE *openpfile(); 298 long operandlength(); 299 operandenum operandmode(); 300 char *operandname(); 301 /* 302 printchildren(); 303 printcycle(); 304 printgprof(); 305 printmembers(); 306 printname(); 307 printparents(); 308 printprof(); 309 readsamples(); 310 */ 311 unsigned long reladdr(); 312 /* 313 sortchildren(); 314 sortmembers(); 315 sortparents(); 316 tally(); 317 timecmp(); 318 topcmp(); 319 */ 320 int totalcmp(); 321 /* 322 valcmp(); 323 */ 324 325 #define LESSTHAN -1 326 #define EQUALTO 0 327 #define GREATERTHAN 1 328 329 #define DFNDEBUG 1 330 #define CYCLEDEBUG 2 331 #define ARCDEBUG 4 332 #define TALLYDEBUG 8 333 #define TIMEDEBUG 16 334 #define SAMPLEDEBUG 32 335 #define AOUTDEBUG 64 336 #define CALLDEBUG 128 337 #define LOOKUPDEBUG 256 338 #define PROPDEBUG 512 339 #define BREAKCYCLE 1024 340 #define SUBCYCLELIST 2048 341 #define ANYDEBUG 4096 342