1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 1990-1998 by Sun Microsystems, Inc. 24 * All rights reserved. 25 */ 26 27 #ifndef _SGS_GPROF_H 28 #define _SGS_GPROF_H 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <sys/types.h> 40 #include <sys/stat.h> 41 #include <fcntl.h> 42 #include <sys/mman.h> 43 #include <elf.h> 44 45 #include "sparc.h" 46 #include "gelf.h" 47 #include "monv.h" 48 49 50 /* 51 * who am i, for error messages. 52 */ 53 extern char *whoami; 54 55 /* 56 * booleans 57 */ 58 typedef int bool; 59 #define FALSE 0 60 #define TRUE 1 61 62 /* 63 * Alignment related constants 64 */ 65 #define PGSZ 4096 66 #define STRUCT_ALIGN 8 67 68 /* 69 * Macros related to structure alignment 70 */ 71 #define FLOOR(x, align) (((Address) x) & ~((align) - 1l)) 72 #define CEIL(x, align) FLOOR(((Address) x) + (align) - 1l, align) 73 74 #define PROFHDR_SZ (CEIL(sizeof (ProfHeader), STRUCT_ALIGN)) 75 #define PROFMODLIST_SZ (CEIL(sizeof (ProfModuleList), STRUCT_ALIGN)) 76 #define PROFMOD_SZ (CEIL(sizeof (ProfModule), STRUCT_ALIGN)) 77 #define PROFBUF_SZ (CEIL(sizeof (ProfBuffer), STRUCT_ALIGN)) 78 #define PROFCGRAPH_SZ (CEIL(sizeof (ProfCallGraph), STRUCT_ALIGN)) 79 #define PROFFUNC_SZ (CEIL(sizeof (ProfFunction), STRUCT_ALIGN)) 80 81 #define HDR_FILLER (PROFHDR_SZ - sizeof (ProfHeader)) 82 #define MODLIST_FILLER (PROFMODLIST_SZ - sizeof (ProfModuleList)) 83 #define MOD_FILLER (PROFMOD_SZ - sizeof (ProfModule)) 84 #define BUF_FILLER (PROFBUF_SZ - sizeof (ProfBuffer)) 85 #define CGRAPH_FILLER (PROFCGRAPH_SZ - sizeof (ProfCallGraph)) 86 #define FUNC_FILLER (PROFFUNC_SZ - sizeof (ProfFunction)) 87 88 /* 89 * ticks per second 90 */ 91 long hz; 92 93 typedef short UNIT; /* unit of profiling */ 94 typedef unsigned short unsigned_UNIT; /* to remove warnings from gprof.c */ 95 char *a_outname; 96 char *prog_name; /* keep the program name for error messages */ 97 #define A_OUTNAME "a.out" 98 99 typedef unsigned long long pctype; 100 typedef uint32_t pctype32; 101 typedef size_t sztype; 102 103 /* 104 * Type definition for the arc count. 105 */ 106 typedef long long actype; 107 typedef int32_t actype32; 108 109 char *gmonname; 110 #define GMONNAME "gmon.out" 111 #define GMONSUM "gmon.sum" 112 113 /* 114 * Special symbols used for profiling of shared libraries through 115 * the run-time linker. 116 */ 117 #define PRF_ETEXT "_etext" 118 #define PRF_EXTSYM "<external>" 119 #define PRF_MEMTERM "_END_OF_VIRTUAL_MEMORY" 120 #define PRF_SYMCNT 3 121 122 /* 123 * Special symbol needed to determine the program exec's end addr. 124 * Note that since this symbol doesn't get added to the nameslist, 125 * it doesn't have to be counted in PRF_SYMCNT 126 */ 127 #define PRF_END "_end" 128 129 /* 130 * blurbs on the flat and graph profiles. 131 */ 132 #define FLAT_BLURB "/gprof.flat.blurb" 133 #define CALLG_BLURB "/gprof.callg.blurb" 134 135 /* 136 * a raw arc, 137 * with pointers to the calling site and the called site 138 * and a count. 139 */ 140 struct rawarc { 141 pctype raw_frompc; 142 pctype raw_selfpc; 143 actype raw_count; 144 }; 145 146 struct rawarc32 { 147 pctype32 raw_frompc; 148 pctype32 raw_selfpc; 149 actype32 raw_count; 150 }; 151 152 /* 153 * a constructed arc, 154 * with pointers to the namelist entry of the parent and the child, 155 * a count of how many times this arc was traversed, 156 * and pointers to the next parent of this child and 157 * the next child of this parent. 158 */ 159 struct arcstruct { 160 struct nl *arc_parentp; /* pointer to parent's nl entry */ 161 struct nl *arc_childp; /* pointer to child's nl entry */ 162 actype arc_count; /* how calls from parent to child */ 163 double arc_time; /* time inherited along arc */ 164 double arc_childtime; /* childtime inherited along arc */ 165 struct arcstruct *arc_parentlist; /* parents-of-this-child list */ 166 struct arcstruct *arc_childlist; /* children-of-this-parent list */ 167 }; 168 typedef struct arcstruct arctype; 169 170 171 /* 172 * Additions for new-style gmon.out 173 */ 174 bool old_style; /* gmon.out versioned/non-versioned ? */ 175 176 /* 177 * Executable file info. 178 * 179 * All info that is required to identify a file or see if it has changed 180 * relative to another file. 181 */ 182 struct fl_info { 183 dev_t dev; /* device associated with this file */ 184 ino_t ino; /* i-number of this file */ 185 time_t mtime; /* last modified time of this file */ 186 off_t size; /* size of file */ 187 }; 188 typedef struct fl_info fl_info_t; 189 190 /* 191 * Saved file info. 192 */ 193 fl_info_t aout_info; /* saved file info for program exec */ 194 fl_info_t gmonout_info; /* current gmonout's info */ 195 196 197 /* 198 * Module info. 199 */ 200 struct mod_info { 201 struct mod_info *next; /* ptr to next in the modules list */ 202 char *name; /* name of this module */ 203 int id; /* id, used while printing */ 204 bool active; /* is this module active or not ? */ 205 struct nl *nl; /* ptr to nameslist for this module */ 206 struct nl *npe; /* virtual end of module's namelist */ 207 sztype nname; /* number of funcs in this module */ 208 GElf_Addr txt_origin; /* module's start as given in file */ 209 GElf_Addr data_end; /* module's end addr as in file */ 210 Address load_base; /* actual pcaddr where modl's loaded */ 211 Address load_end; /* actual pcaddr where modl ends */ 212 }; 213 typedef struct mod_info mod_info_t; 214 215 sztype total_names; /* from all modules */ 216 217 /* 218 * List of shared object modules. Note that this always includes the 219 * program executable as the first element. 220 */ 221 mod_info_t modules; 222 sztype n_modules; 223 224 225 226 /* 227 * The symbol table; 228 * for each external in the specified file we gather 229 * its address, the number of calls and compute its share of cpu time. 230 */ 231 struct nl { 232 char *name; /* the name */ 233 mod_info_t *module; /* module to which this belongs */ 234 pctype value; /* the pc entry point */ 235 pctype svalue; /* entry point aligned to histograms */ 236 unsigned long sz; /* function size */ 237 unsigned char syminfo; /* sym info */ 238 size_t nticks; /* ticks in this routine */ 239 double time; /* ticks in this routine as double */ 240 double childtime; /* cumulative ticks in children */ 241 actype ncall; /* how many times called */ 242 actype selfcalls; /* how many calls to self */ 243 double propfraction; /* what % of time propagates */ 244 double propself; /* how much self time propagates */ 245 double propchild; /* how much child time propagates */ 246 bool printflag; /* should this be printed? */ 247 int index; /* index in the graph list */ 248 int toporder; /* graph call chain top-sort order */ 249 int cycleno; /* internal number of cycle on */ 250 struct nl *cyclehead; /* pointer to head of cycle */ 251 struct nl *cnext; /* pointer to next member of cycle */ 252 arctype *parents; /* list of caller arcs */ 253 arctype *children; /* list of callee arcs */ 254 unsigned long ncallers; /* no. of callers - dumpsum use only */ 255 }; 256 typedef struct nl nltype; 257 258 /* 259 * flag which marks a nl entry as topologically ``busy'' 260 * flag which marks a nl entry as topologically ``not_numbered'' 261 */ 262 #define DFN_BUSY -1 263 #define DFN_NAN 0 264 265 /* 266 * namelist entries for cycle headers. 267 * the number of discovered cycles. 268 */ 269 nltype *cyclenl; /* cycle header namelist */ 270 int ncycle; /* number of cycles discovered */ 271 272 /* 273 * The header on the gmon.out file. 274 * old-style gmon.out consists of one of these headers, 275 * and then an array of ncnt samples 276 * representing the discretized program counter values. 277 * this should be a struct phdr, but since everything is done 278 * as UNITs, this is in UNITs too. 279 */ 280 struct hdr { 281 pctype lowpc; 282 pctype highpc; 283 pctype ncnt; 284 }; 285 286 struct hdr32 { 287 pctype32 lowpc; 288 pctype32 highpc; 289 pctype32 ncnt; 290 }; 291 292 struct hdr h; /* header of profiled data */ 293 294 int debug; 295 int number_funcs_toprint; 296 297 /* 298 * Each discretized pc sample has 299 * a count of the number of samples in its range 300 */ 301 unsigned short *samples; 302 303 pctype s_lowpc; /* lowpc from profile file in o-s gmon.out */ 304 pctype s_highpc; /* highpc from profile file in o-s gmon.out */ 305 sztype sampbytes; /* number of bytes of samples in o-s gmon.out */ 306 sztype nsamples; /* number of samples for old-style gmon.out */ 307 308 double actime; /* accumulated time thus far for putprofline */ 309 double totime; /* total time for all routines */ 310 double printtime; /* total of time being printed */ 311 double scale; /* scale factor converting samples to pc */ 312 /* values: each sample covers scale bytes */ 313 /* -- all this is for old-style gmon.out only */ 314 315 off_t ssiz; /* size of the string table */ 316 317 unsigned char *textspace; /* text space of a.out in core */ 318 bool first_file; /* for difference option */ 319 320 /* 321 * Total number of pcsamples read so far (across gmon.out's) 322 */ 323 Size n_pcsamples; 324 325 /* 326 * option flags, from a to z. 327 */ 328 bool aflag; /* suppress static functions */ 329 bool bflag; /* blurbs, too */ 330 bool Bflag; /* big pc's (i.e. 64 bits) */ 331 bool cflag; /* discovered call graph, too */ 332 bool Cflag; /* gprofing c++ -- need demangling */ 333 bool dflag; /* debugging options */ 334 bool Dflag; /* difference option */ 335 bool eflag; /* specific functions excluded */ 336 bool Eflag; /* functions excluded with time */ 337 bool fflag; /* specific functions requested */ 338 bool Fflag; /* functions requested with time */ 339 bool lflag; /* exclude LOCAL syms in output */ 340 bool sflag; /* sum multiple gmon.out files */ 341 bool zflag; /* zero time/called functions, too */ 342 bool nflag; /* print only n functions in report */ 343 bool rflag; /* profiling input generated by */ 344 /* run-time linker */ 345 346 347 /* 348 * structure for various string lists 349 */ 350 struct stringlist { 351 struct stringlist *next; 352 char *string; 353 }; 354 extern struct stringlist *elist; 355 extern struct stringlist *Elist; 356 extern struct stringlist *flist; 357 extern struct stringlist *Flist; 358 359 /* 360 * function declarations 361 */ 362 void addlist(struct stringlist *, char *); 363 void addarc(nltype *, nltype *, actype); 364 int arccmp(arctype *, arctype *); 365 arctype *arclookup(nltype *, nltype *); 366 void printblurb(char *); 367 void dfn(nltype *); 368 bool dfn_busy(nltype *); 369 void dfn_findcycle(nltype *); 370 bool dfn_numbered(nltype *); 371 void dfn_post_visit(nltype *); 372 void dfn_pre_visit(nltype *); 373 void dfn_self_cycle(nltype *); 374 nltype **doarcs(void); 375 void done(); 376 void findcalls(nltype *, pctype, pctype); 377 void flatprofheader(void); 378 void flatprofline(nltype *); 379 bool is_shared_obj(char *); 380 void getnfile(char *); 381 void process_namelist(mod_info_t *); 382 void gprofheader(void); 383 void gprofline(nltype *); 384 int pc_cmp(Address *, Address *); 385 int main(); 386 int membercmp(nltype *, nltype *); 387 nltype *nllookup(mod_info_t *, pctype, pctype *); 388 bool onlist(struct stringlist *, char *); 389 void printchildren(nltype *); 390 void printcycle(nltype *); 391 void printgprof(nltype **); 392 void printindex(void); 393 void printmembers(nltype *); 394 void printmodules(void); 395 void printname(nltype *); 396 void printparents(nltype *); 397 void printprof(void); 398 void sortchildren(nltype *); 399 void sortmembers(nltype *); 400 void sortparents(nltype *); 401 int timecmp(nltype **, nltype **); 402 int totalcmp(nltype **, nltype **); 403 404 #define LESSTHAN -1 405 #define EQUALTO 0 406 #define GREATERTHAN 1 407 408 /* 409 * Macros related to debug messages. 410 */ 411 #define DFNDEBUG 0x0001 412 #define CYCLEDEBUG 0x0002 413 #define ARCDEBUG 0x0004 414 #define TALLYDEBUG 0x0008 415 #define TIMEDEBUG 0x0010 416 #define SAMPLEDEBUG 0x0020 417 #define ELFDEBUG 0x0040 418 #define CALLSDEBUG 0x0080 419 #define LOOKUPDEBUG 0x0100 420 #define PROPDEBUG 0x0200 421 #define ANYDEBUG 0x0400 422 423 #define MONOUTDEBUG 0x0800 424 #define MODULEDEBUG 0x1000 425 #define CGRAPHDEBUG 0x2000 426 #define PCSMPLDEBUG 0x4000 427 428 #ifdef __cplusplus 429 } 430 #endif 431 432 #endif /* _SGS_GPROF_H */ 433