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