weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 1 | /*--------------------------------------------------------------------*/ |
| 2 | /*--- Callgrind ---*/ |
| 3 | /*--- global.h ---*/ |
| 4 | /*--- (C) 2004, 2005 Josef Weidendorfer ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | #ifndef CLG_GLOBAL |
| 8 | #define CLG_GLOBAL |
| 9 | |
| 10 | #include "pub_tool_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 11 | #include "pub_tool_vki.h" |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 12 | #include "pub_tool_debuginfo.h" |
| 13 | #include "pub_tool_libcbase.h" |
| 14 | #include "pub_tool_libcassert.h" |
| 15 | #include "pub_tool_libcfile.h" |
| 16 | #include "pub_tool_libcprint.h" |
| 17 | #include "pub_tool_libcproc.h" |
| 18 | #include "pub_tool_machine.h" |
| 19 | #include "pub_tool_mallocfree.h" |
| 20 | #include "pub_tool_options.h" |
| 21 | #include "pub_tool_tooliface.h" |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 22 | #include "pub_tool_xarray.h" |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 23 | #include "pub_tool_clientstate.h" |
| 24 | #include "pub_tool_machine.h" // VG_(fnptr_to_fnentry) |
| 25 | |
| 26 | #include "events.h" // defines CLG_ macro |
| 27 | #include "costs.h" |
| 28 | |
| 29 | |
| 30 | /*------------------------------------------------------------*/ |
| 31 | /*--- Calltree compile options --- */ |
| 32 | /*------------------------------------------------------------*/ |
| 33 | |
| 34 | /* Enable debug output */ |
| 35 | #define CLG_ENABLE_DEBUG 1 |
| 36 | |
| 37 | /* Enable experimental features? */ |
| 38 | #define CLG_EXPERIMENTAL 0 |
| 39 | |
| 40 | /* Syscall Timing in microseconds? |
| 41 | * (define to 0 if you get compile errors) */ |
| 42 | #define CLG_MICROSYSTIME 0 |
| 43 | |
| 44 | /* Set to 1 if you want full sanity checks for JCC */ |
| 45 | #define JCC_CHECK 0 |
| 46 | |
| 47 | |
| 48 | |
| 49 | /*------------------------------------------------------------*/ |
| 50 | /*--- Command line options ---*/ |
| 51 | /*------------------------------------------------------------*/ |
| 52 | |
weidendo | cbf4e19 | 2007-11-27 01:27:12 +0000 | [diff] [blame] | 53 | #define DEFAULT_OUTFORMAT "callgrind.out.%p" |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 54 | #define DEFAULT_COMMANDNAME "callgrind.cmd" |
| 55 | #define DEFAULT_RESULTNAME "callgrind.res" |
| 56 | #define DEFAULT_INFONAME "/tmp/callgrind.info" |
| 57 | |
| 58 | typedef struct _CommandLineOptions CommandLineOptions; |
| 59 | struct _CommandLineOptions { |
| 60 | |
| 61 | /* Dump format options */ |
weidendo | cbf4e19 | 2007-11-27 01:27:12 +0000 | [diff] [blame] | 62 | Char* out_format; /* Format string for callgrind output file name */ |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 63 | Bool combine_dumps; /* Dump trace parts into same file? */ |
| 64 | Bool compress_strings; |
| 65 | Bool compress_events; |
| 66 | Bool compress_pos; |
| 67 | Bool mangle_names; |
| 68 | Bool compress_mangled; |
| 69 | Bool dump_line; |
| 70 | Bool dump_instr; |
| 71 | Bool dump_bb; |
| 72 | Bool dump_bbs; /* Dump basic block information? */ |
| 73 | |
| 74 | /* Dump generation options */ |
weidendo | 9e326b7 | 2006-03-31 13:16:15 +0000 | [diff] [blame] | 75 | ULong dump_every_bb; /* Dump every xxx BBs. */ |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 76 | |
| 77 | /* Collection options */ |
| 78 | Bool separate_threads; /* Separate threads in dump? */ |
| 79 | Int separate_callers; /* Separate dependent on how many callers? */ |
| 80 | Int separate_recursions; /* Max level of recursions to separate */ |
| 81 | Bool skip_plt; /* Skip functions in PLT section? */ |
| 82 | Bool skip_direct_recursion; /* Increment direct recursions the level? */ |
| 83 | |
| 84 | Bool collect_atstart; /* Start in collecting state ? */ |
| 85 | Bool collect_jumps; /* Collect (cond.) jumps in functions ? */ |
| 86 | |
| 87 | Bool collect_alloc; /* Collect size of allocated memory */ |
| 88 | Bool collect_systime; /* Collect time for system calls */ |
| 89 | |
| 90 | /* Instrument options */ |
| 91 | Bool instrument_atstart; /* Instrument at start? */ |
| 92 | Bool simulate_cache; /* Call into cache simulator ? */ |
| 93 | |
weidendo | a762b0f | 2006-05-01 00:55:54 +0000 | [diff] [blame] | 94 | /* Call graph generation */ |
| 95 | Bool pop_on_jump; /* Handle a jump between functions as ret+call */ |
| 96 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 97 | #if CLG_ENABLE_DEBUG |
| 98 | Int verbose; |
| 99 | ULong verbose_start; |
| 100 | #endif |
| 101 | }; |
| 102 | |
| 103 | /*------------------------------------------------------------*/ |
| 104 | /*--- Constants ---*/ |
| 105 | /*------------------------------------------------------------*/ |
| 106 | |
| 107 | |
| 108 | /* According to IA-32 Intel Architecture Software Developer's Manual: Vol 2 */ |
| 109 | #define MAX_x86_INSTR_SIZE 16 |
| 110 | |
| 111 | /* Minimum cache line size allowed */ |
| 112 | #define MIN_LINE_SIZE 16 |
| 113 | |
| 114 | /* Size of various buffers used for storing strings */ |
| 115 | #define FILENAME_LEN 256 |
| 116 | #define FN_NAME_LEN 4096 /* for C++ code :-) */ |
| 117 | #define OBJ_NAME_LEN 256 |
| 118 | #define BUF_LEN 512 |
| 119 | #define COMMIFY_BUF_LEN 128 |
| 120 | #define RESULTS_BUF_LEN 128 |
| 121 | #define LINE_BUF_LEN 64 |
| 122 | |
| 123 | |
weidendo | 5bd7eca | 2007-02-26 00:16:09 +0000 | [diff] [blame] | 124 | /* Convenience macros */ |
| 125 | |
| 126 | /* Use this only when size of sprintf args are known to fit into |
| 127 | * given buffer; for strings of unknown length, use WRITE_STR below |
| 128 | */ |
| 129 | #define WRITE_SPRINTF(fd, zz_buf, fmt, args...) \ |
| 130 | do { Int len = VG_(sprintf)(zz_buf, fmt, ## args); \ |
| 131 | VG_(write)(fd, (void*)zz_buf, len); \ |
| 132 | } while (0) |
| 133 | |
| 134 | #define WRITE_STR(fd, str) \ |
| 135 | do { if (str) { Int len = VG_(strlen)(str); \ |
| 136 | VG_(write)(fd, (void*)str, len); } \ |
| 137 | else VG_(write)(fd, "(null)", 6); \ |
| 138 | } while (0) |
| 139 | |
| 140 | #define WRITE_STR2(fd, str1, str2) \ |
| 141 | do { if (str1) { Int len = VG_(strlen)(str1); \ |
| 142 | VG_(write)(fd, (void*)str1, len); } \ |
| 143 | else VG_(write)(fd, "(null)", 6); \ |
| 144 | if (str2) { Int len = VG_(strlen)(str2); \ |
| 145 | VG_(write)(fd, (void*)str2, len); } \ |
| 146 | else VG_(write)(fd, "(null)", 6); \ |
| 147 | } while (0) |
| 148 | |
| 149 | #define WRITE_STR3(fd, str1, str2, str3) \ |
| 150 | do { if (str1) { Int len = VG_(strlen)(str1); \ |
| 151 | VG_(write)(fd, (void*)str1, len); } \ |
| 152 | else VG_(write)(fd, "(null)", 6); \ |
| 153 | if (str2) { Int len = VG_(strlen)(str2); \ |
| 154 | VG_(write)(fd, (void*)str2, len); } \ |
| 155 | else VG_(write)(fd, "(null)", 6); \ |
| 156 | if (str3) { Int len = VG_(strlen)(str3); \ |
| 157 | VG_(write)(fd, (void*)str3, len); } \ |
| 158 | else VG_(write)(fd, "(null)", 6); \ |
| 159 | } while (0) |
| 160 | |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 161 | |
| 162 | /*------------------------------------------------------------*/ |
| 163 | /*--- Statistics ---*/ |
| 164 | /*------------------------------------------------------------*/ |
| 165 | |
| 166 | typedef struct _Statistics Statistics; |
| 167 | struct _Statistics { |
| 168 | ULong call_counter; |
| 169 | ULong jcnd_counter; |
| 170 | ULong jump_counter; |
| 171 | ULong rec_call_counter; |
| 172 | ULong ret_counter; |
| 173 | ULong bb_executions; |
| 174 | |
| 175 | Int context_counter; |
| 176 | Int bb_retranslations; |
| 177 | |
| 178 | Int distinct_objs; |
| 179 | Int distinct_files; |
| 180 | Int distinct_fns; |
| 181 | Int distinct_contexts; |
| 182 | Int distinct_bbs; |
| 183 | Int distinct_jccs; |
| 184 | Int distinct_bbccs; |
| 185 | Int distinct_instrs; |
| 186 | Int distinct_skips; |
| 187 | |
| 188 | Int bb_hash_resizes; |
| 189 | Int bbcc_hash_resizes; |
| 190 | Int jcc_hash_resizes; |
| 191 | Int cxt_hash_resizes; |
| 192 | Int fn_array_resizes; |
| 193 | Int call_stack_resizes; |
| 194 | Int fn_stack_resizes; |
| 195 | |
| 196 | Int full_debug_BBs; |
| 197 | Int file_line_debug_BBs; |
| 198 | Int fn_name_debug_BBs; |
| 199 | Int no_debug_BBs; |
| 200 | Int bbcc_lru_misses; |
| 201 | Int jcc_lru_misses; |
| 202 | Int cxt_lru_misses; |
| 203 | Int bbcc_clones; |
| 204 | }; |
| 205 | |
| 206 | |
| 207 | /*------------------------------------------------------------*/ |
| 208 | /*--- Structure declarations ---*/ |
| 209 | /*------------------------------------------------------------*/ |
| 210 | |
| 211 | typedef struct _Context Context; |
| 212 | typedef struct _CC CC; |
| 213 | typedef struct _BB BB; |
| 214 | typedef struct _Skipped Skipped; |
| 215 | typedef struct _BBCC BBCC; |
| 216 | typedef struct _jCC jCC; |
| 217 | typedef struct _fCC fCC; |
| 218 | typedef struct _fn_node fn_node; |
| 219 | typedef struct _file_node file_node; |
| 220 | typedef struct _obj_node obj_node; |
| 221 | typedef struct _fn_config fn_config; |
| 222 | typedef struct _call_entry call_entry; |
| 223 | typedef struct _thread_info thread_info; |
| 224 | |
| 225 | /* Costs of event sets. Aliases to arrays of 64-bit values */ |
| 226 | typedef ULong* SimCost; /* All events the simulator can produce */ |
| 227 | typedef ULong* UserCost; |
| 228 | typedef ULong* FullCost; /* Simulator + User */ |
| 229 | |
| 230 | |
| 231 | /* JmpCall cost center |
| 232 | * for subroutine call (from->bb->jmp_addr => to->bb->addr) |
| 233 | * |
| 234 | * Each BB has at most one CALL instruction. The list of JCC from |
| 235 | * this call is a pointer to the list head (stored in BBCC), and |
| 236 | * <next_from> in the JCC struct. |
| 237 | * |
| 238 | * For fast lookup, JCCs are reachable with a hash table, keyed by |
| 239 | * the (from_bbcc,to) pair. <next_hash> is used for the JCC chain |
| 240 | * of one hash table entry. |
| 241 | * |
| 242 | * Cost <sum> holds event counts for already returned executions. |
| 243 | * <last> are the event counters at last enter of the subroutine. |
| 244 | * <sum> is updated on returning from the subroutine by |
| 245 | * adding the diff of <last> and current event counters to <sum>. |
| 246 | * |
| 247 | * After updating, <last> is set to current event counters. Thus, |
| 248 | * events are not counted twice for recursive calls (TODO: True?) |
| 249 | */ |
| 250 | #define JmpNone (Ijk_Boring+30) |
| 251 | #define JmpCond (Ijk_Boring+31) |
| 252 | |
| 253 | struct _jCC { |
| 254 | Int jmpkind; /* JmpCall, JmpBoring, JmpCond */ |
| 255 | jCC* next_hash; /* for hash entry chain */ |
| 256 | jCC* next_from; /* next JCC from a BBCC */ |
| 257 | BBCC *from, *to; /* call arc from/to this BBCC */ |
| 258 | UInt jmp; /* jump no. in source */ |
| 259 | |
| 260 | ULong call_counter; /* no wraparound with 64 bit */ |
| 261 | |
| 262 | FullCost cost; /* simulator + user counters */ |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | /* |
| 267 | * Info for one instruction of a basic block. |
| 268 | */ |
| 269 | typedef struct _InstrInfo InstrInfo; |
| 270 | struct _InstrInfo { |
| 271 | UInt instr_offset; |
| 272 | UInt instr_size; |
| 273 | UInt data_size; |
| 274 | UInt cost_offset; |
| 275 | EventSet* eventset; |
| 276 | }; |
| 277 | |
| 278 | |
| 279 | /* |
| 280 | * Info for a conditional jump in a basic block |
| 281 | */ |
| 282 | typedef struct _CJmpInfo CJmpInfo; |
| 283 | struct _CJmpInfo { |
| 284 | UInt instr; /* instruction index in this basic block */ |
| 285 | Bool skip; /* Cond.Jumps to next instruction should be ignored */ |
| 286 | }; |
| 287 | |
| 288 | |
| 289 | /** |
| 290 | * An instrumented basic block (BB). |
| 291 | * |
| 292 | * BBs are put into a resizable hash to allow for fast detection if a |
| 293 | * BB is to be retranslated but cost info is already available. |
| 294 | * The key for a BB is a (object, offset) tupel making it independent |
| 295 | * from possibly multiple mappings of the same ELF object. |
| 296 | * |
| 297 | * At the beginning of each instrumented BB, |
| 298 | * a call to setup_bbcc(), specifying a pointer to the |
| 299 | * according BB structure, is added. |
| 300 | * |
| 301 | * As cost of a BB has to be distinguished depending on the context, |
| 302 | * multiple cost centers for one BB (struct BBCC) exist and the according |
| 303 | * BBCC is set by setup_bbcc. |
| 304 | */ |
| 305 | struct _BB { |
| 306 | obj_node* obj; /* ELF object of BB */ |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 307 | PtrdiffT offset; /* offset of BB in ELF object file */ |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 308 | BB* next; /* chaining for a hash entry */ |
| 309 | |
| 310 | VgSectKind sect_kind; /* section of this BB, e.g. PLT */ |
| 311 | UInt instr_count; |
| 312 | |
| 313 | /* filled by CLG_(get_fn_node) if debug info is available */ |
| 314 | fn_node* fn; /* debug info for this BB */ |
| 315 | UInt line; |
| 316 | Bool is_entry; /* True if this BB is a function entry */ |
| 317 | |
| 318 | BBCC* bbcc_list; /* BBCCs for same BB (see next_bbcc in BBCC) */ |
| 319 | BBCC* last_bbcc; /* Temporary: Cached for faster access (LRU) */ |
| 320 | |
| 321 | /* filled by CLG_(instrument) if not seen before */ |
| 322 | UInt cjmp_count; /* number of conditional exits */ |
| 323 | CJmpInfo* jmp; /* array of info for condition jumps, |
| 324 | * allocated directly after this struct */ |
| 325 | Int jmpkind; /* remember jump kind of final exit */ |
| 326 | Bool cjmp_inverted; /* condition of last cond.jump can be inverted by VEX */ |
| 327 | |
| 328 | UInt instr_len; |
| 329 | UInt cost_count; |
| 330 | InstrInfo instr[0]; /* info on instruction sizes and costs */ |
| 331 | }; |
| 332 | |
| 333 | |
| 334 | |
| 335 | /** |
| 336 | * Function context |
| 337 | * |
| 338 | * Basic blocks are always executed in the scope of a context. |
| 339 | * A function context is a list of function nodes representing |
| 340 | * the call chain to the current context: I.e. fn[0] is the |
| 341 | * function we are currently in, fn[1] has called fn[0], and so on. |
| 342 | * Recursion levels are used for fn[0]. |
| 343 | * |
| 344 | * To get a unique number for a full execution context, use |
| 345 | * rec_index = min(<fn->rec_separation>,<active>) - 1; |
| 346 | * unique_no = <number> + rec_index |
| 347 | * |
| 348 | * For each Context, recursion index and BB, there can be a BBCC. |
| 349 | */ |
| 350 | struct _Context { |
| 351 | UInt size; // number of function dependencies |
| 352 | UInt base_number; // for context compression & dump array |
| 353 | Context* next; // entry chaining for hash |
| 354 | UWord hash; // for faster lookup... |
| 355 | fn_node* fn[0]; |
| 356 | }; |
| 357 | |
| 358 | |
| 359 | /* |
| 360 | * Info for a conditional jump in a basic block |
| 361 | */ |
| 362 | typedef struct _JmpData JmpData; |
| 363 | struct _JmpData { |
| 364 | ULong ecounter; /* number of times the BB was left at this exit */ |
| 365 | jCC* jcc_list; /* JCCs for Cond.Jumps from this exit */ |
| 366 | }; |
| 367 | |
| 368 | |
| 369 | /* |
| 370 | * Basic Block Cost Center |
| 371 | * |
| 372 | * On demand, multiple BBCCs will be created for the same BB |
| 373 | * dependend on command line options and: |
| 374 | * - current function (it's possible that a BB is executed in the |
| 375 | * context of different functions, e.g. in manual assembler/PLT) |
| 376 | * - current thread ID |
| 377 | * - position where current function is called from |
| 378 | * - recursion level of current function |
| 379 | * |
| 380 | * The cost centres for the instructions of a basic block are |
| 381 | * stored in a contiguous array. |
| 382 | * They are distinguishable by their tag field. |
| 383 | */ |
| 384 | struct _BBCC { |
| 385 | BB* bb; /* BB for this cost center */ |
| 386 | |
| 387 | Context* cxt; /* execution context of this BBCC */ |
| 388 | ThreadId tid; /* only for assertion check purpose */ |
| 389 | UInt rec_index; /* Recursion index in rec->bbcc for this bbcc */ |
| 390 | BBCC** rec_array; /* Variable sized array of pointers to |
| 391 | * recursion BBCCs. Shared. */ |
| 392 | ULong ret_counter; /* how often returned from jccs of this bbcc; |
| 393 | * used to check if a dump for this BBCC is needed */ |
| 394 | |
| 395 | BBCC* next_bbcc; /* Chain of BBCCs for same BB */ |
| 396 | BBCC* lru_next_bbcc; /* BBCC executed next the last time */ |
| 397 | |
| 398 | jCC* lru_from_jcc; /* Temporary: Cached for faster access (LRU) */ |
| 399 | jCC* lru_to_jcc; /* Temporary: Cached for faster access (LRU) */ |
| 400 | FullCost skipped; /* cost for skipped functions called from |
| 401 | * jmp_addr. Allocated lazy */ |
| 402 | |
| 403 | BBCC* next; /* entry chain in hash */ |
| 404 | ULong* cost; /* start of 64bit costs for this BBCC */ |
| 405 | ULong ecounter_sum; /* execution counter for first instruction of BB */ |
| 406 | JmpData jmp[0]; |
| 407 | }; |
| 408 | |
| 409 | |
| 410 | /* the <number> of fn_node, file_node and obj_node are for compressed dumping |
| 411 | * and a index into the dump boolean table and fn_info_table |
| 412 | */ |
| 413 | |
| 414 | struct _fn_node { |
| 415 | Char* name; |
| 416 | UInt number; |
| 417 | Context* last_cxt; /* LRU info */ |
| 418 | Context* pure_cxt; /* the context with only the function itself */ |
| 419 | file_node* file; /* reverse mapping for 2nd hash */ |
| 420 | fn_node* next; |
| 421 | |
| 422 | Bool dump_before :1; |
| 423 | Bool dump_after :1; |
| 424 | Bool zero_before :1; |
| 425 | Bool toggle_collect :1; |
| 426 | Bool skip :1; |
| 427 | Bool pop_on_jump : 1; |
| 428 | |
| 429 | Bool is_malloc :1; |
| 430 | Bool is_realloc :1; |
| 431 | Bool is_free :1; |
| 432 | |
| 433 | Int group; |
| 434 | Int separate_callers; |
| 435 | Int separate_recursions; |
| 436 | #if CLG_ENABLE_DEBUG |
| 437 | Int verbosity; /* Stores old verbosity level while in function */ |
| 438 | #endif |
| 439 | }; |
| 440 | |
| 441 | /* Quite arbitrary fixed hash sizes */ |
| 442 | |
| 443 | #define N_OBJ_ENTRIES 47 |
| 444 | #define N_FILE_ENTRIES 53 |
| 445 | #define N_FN_ENTRIES 87 |
| 446 | #define N_BBCC2_ENTRIES 37 |
| 447 | |
| 448 | struct _file_node { |
| 449 | Char* name; |
| 450 | fn_node* fns[N_FN_ENTRIES]; |
| 451 | UInt number; |
| 452 | obj_node* obj; |
| 453 | file_node* next; |
| 454 | }; |
| 455 | |
| 456 | /* If an object is dlopened multiple times, we hope that <name> is unique; |
| 457 | * <start> and <offset> can change with each dlopen, and <start> is |
| 458 | * zero when object is unmapped (possible at dump time). |
| 459 | */ |
| 460 | struct _obj_node { |
| 461 | Char* name; |
| 462 | UInt last_slash_pos; |
| 463 | |
| 464 | Addr start; /* Start address of text segment mapping */ |
| 465 | SizeT size; /* Length of mapping */ |
njn | c4431bf | 2009-01-15 21:29:24 +0000 | [diff] [blame] | 466 | PtrdiffT offset; /* Offset between symbol address and file offset */ |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 467 | |
| 468 | file_node* files[N_FILE_ENTRIES]; |
| 469 | UInt number; |
| 470 | obj_node* next; |
| 471 | }; |
| 472 | |
| 473 | /* an entry in the callstack |
| 474 | * |
| 475 | * <nonskipped> is 0 if the function called is not skipped (usual case). |
| 476 | * Otherwise, it is the last non-skipped BBCC. This one gets all |
| 477 | * the calls to non-skipped functions and all costs in skipped |
| 478 | * instructions. |
| 479 | */ |
| 480 | struct _call_entry { |
| 481 | jCC* jcc; /* jCC for this call */ |
| 482 | FullCost enter_cost; /* cost event counters at entering frame */ |
| 483 | Addr sp; /* stack pointer directly after call */ |
| 484 | Addr ret_addr; /* address to which to return to |
| 485 | * is 0 on a simulated call */ |
| 486 | BBCC* nonskipped; /* see above */ |
| 487 | Context* cxt; /* context before call */ |
| 488 | Int fn_sp; /* function stack index before call */ |
| 489 | }; |
| 490 | |
| 491 | |
| 492 | /* |
| 493 | * Execution state of main thread or a running signal handler in |
| 494 | * a thread while interrupted by another signal handler. |
| 495 | * As there's no scheduling among running signal handlers of one thread, |
| 496 | * we only need a subset of a full thread state: |
| 497 | * - event counter |
| 498 | * - collect state |
| 499 | * - last BB, last jump kind, last nonskipped BB |
| 500 | * - callstack pointer for sanity checking and correct unwinding |
| 501 | * after exit |
| 502 | */ |
| 503 | typedef struct _exec_state exec_state; |
| 504 | struct _exec_state { |
| 505 | |
| 506 | /* the signum of the handler, 0 for main thread context |
| 507 | */ |
| 508 | Int sig; |
| 509 | |
| 510 | /* the old call stack pointer at entering the signal handler */ |
| 511 | Int orig_sp; |
| 512 | |
| 513 | FullCost cost; |
| 514 | Bool collect; |
| 515 | Context* cxt; |
| 516 | |
| 517 | Int jmps_passed; /* number of conditional jumps passed in last BB */ |
| 518 | BBCC* bbcc; /* last BB executed */ |
| 519 | BBCC* nonskipped; |
| 520 | |
| 521 | Int call_stack_bottom; /* Index into fn_stack */ |
| 522 | }; |
| 523 | |
| 524 | /* Global state structures */ |
| 525 | typedef struct _bb_hash bb_hash; |
| 526 | struct _bb_hash { |
| 527 | UInt size, entries; |
| 528 | BB** table; |
| 529 | }; |
| 530 | |
| 531 | typedef struct _cxt_hash cxt_hash; |
| 532 | struct _cxt_hash { |
| 533 | UInt size, entries; |
| 534 | Context** table; |
| 535 | }; |
| 536 | |
| 537 | /* Thread specific state structures, i.e. parts of a thread state. |
| 538 | * There are variables for the current state of each part, |
| 539 | * on which a thread state is copied at thread switch. |
| 540 | */ |
| 541 | typedef struct _bbcc_hash bbcc_hash; |
| 542 | struct _bbcc_hash { |
| 543 | UInt size, entries; |
| 544 | BBCC** table; |
| 545 | }; |
| 546 | |
| 547 | typedef struct _jcc_hash jcc_hash; |
| 548 | struct _jcc_hash { |
| 549 | UInt size, entries; |
| 550 | jCC** table; |
| 551 | jCC* spontaneous; |
| 552 | }; |
| 553 | |
| 554 | typedef struct _fn_array fn_array; |
| 555 | struct _fn_array { |
| 556 | UInt size; |
| 557 | UInt* array; |
| 558 | }; |
| 559 | |
| 560 | typedef struct _call_stack call_stack; |
| 561 | struct _call_stack { |
| 562 | UInt size; |
| 563 | Int sp; |
| 564 | call_entry* entry; |
| 565 | }; |
| 566 | |
| 567 | typedef struct _fn_stack fn_stack; |
| 568 | struct _fn_stack { |
| 569 | UInt size; |
| 570 | fn_node **bottom, **top; |
| 571 | }; |
| 572 | |
| 573 | /* The maximum number of simultaneous running signal handlers per thread. |
| 574 | * This is the number of execution states storable in a thread. |
| 575 | */ |
| 576 | #define MAX_SIGHANDLERS 10 |
| 577 | |
| 578 | typedef struct _exec_stack exec_stack; |
| 579 | struct _exec_stack { |
| 580 | Int sp; /* > 0 if a handler is running */ |
| 581 | exec_state* entry[MAX_SIGHANDLERS]; |
| 582 | }; |
| 583 | |
| 584 | /* Thread State |
| 585 | * |
| 586 | * This structure stores thread specific info while a thread is *not* |
| 587 | * running. See function switch_thread() for save/restore on thread switch. |
| 588 | * |
| 589 | * If --separate-threads=no, BBCCs and JCCs can be shared by all threads, i.e. |
| 590 | * only structures of thread 1 are used. |
| 591 | * This involves variables fn_info_table, bbcc_table and jcc_table. |
| 592 | */ |
| 593 | struct _thread_info { |
| 594 | |
| 595 | /* state */ |
| 596 | fn_stack fns; /* function stack */ |
| 597 | call_stack calls; /* context call arc stack */ |
| 598 | exec_stack states; /* execution states interrupted by signals */ |
| 599 | |
| 600 | /* dump statistics */ |
| 601 | FullCost lastdump_cost; /* Cost at last dump */ |
| 602 | FullCost sighandler_cost; |
| 603 | |
| 604 | /* thread specific data structure containers */ |
| 605 | fn_array fn_active; |
| 606 | jcc_hash jccs; |
| 607 | bbcc_hash bbccs; |
| 608 | }; |
| 609 | |
| 610 | /* Structs used for dumping */ |
| 611 | |
| 612 | /* Address position inside of a BBCC: |
| 613 | * This includes |
| 614 | * - the address offset from the BB start address |
| 615 | * - file/line from debug info for that address (can change inside a BB) |
| 616 | */ |
| 617 | typedef struct _AddrPos AddrPos; |
| 618 | struct _AddrPos { |
| 619 | Addr addr; |
| 620 | Addr bb_addr; |
| 621 | file_node* file; |
| 622 | UInt line; |
| 623 | }; |
| 624 | |
| 625 | /* a simulator cost entity that can be written out in one line */ |
| 626 | typedef struct _AddrCost AddrCost; |
| 627 | struct _AddrCost { |
| 628 | AddrPos p; |
| 629 | SimCost cost; |
| 630 | }; |
| 631 | |
| 632 | /* A function in an execution context */ |
| 633 | typedef struct _FnPos FnPos; |
| 634 | struct _FnPos { |
| 635 | file_node* file; |
| 636 | fn_node* fn; |
| 637 | obj_node* obj; |
| 638 | Context* cxt; |
| 639 | int rec_index; |
| 640 | UInt line; |
| 641 | }; |
| 642 | |
| 643 | /*------------------------------------------------------------*/ |
| 644 | /*--- Cache simulator interface ---*/ |
| 645 | /*------------------------------------------------------------*/ |
| 646 | |
| 647 | struct cachesim_if |
| 648 | { |
| 649 | void (*print_opts)(void); |
| 650 | Bool (*parse_opt)(Char* arg); |
| 651 | void (*post_clo_init)(void); |
| 652 | void (*clear)(void); |
| 653 | void (*getdesc)(Char* buf); |
| 654 | void (*printstat)(void); |
| 655 | void (*add_icost)(SimCost, BBCC*, InstrInfo*, ULong); |
| 656 | void (*after_bbsetup)(void); |
| 657 | void (*finish)(void); |
| 658 | |
| 659 | void (*log_1I0D)(InstrInfo*) VG_REGPARM(1); |
| 660 | |
| 661 | void (*log_1I1Dr)(InstrInfo*, Addr) VG_REGPARM(2); |
| 662 | void (*log_1I1Dw)(InstrInfo*, Addr) VG_REGPARM(2); |
| 663 | void (*log_1I2D)(InstrInfo*, Addr, Addr) VG_REGPARM(3); |
| 664 | |
| 665 | void (*log_0I1Dr)(InstrInfo*, Addr) VG_REGPARM(2); |
| 666 | void (*log_0I1Dw)(InstrInfo*, Addr) VG_REGPARM(2); |
| 667 | void (*log_0I2D)(InstrInfo*, Addr, Addr) VG_REGPARM(3); |
| 668 | |
| 669 | // function names of helpers (for debugging generated code) |
| 670 | Char *log_1I0D_name; |
| 671 | Char *log_1I1Dr_name, *log_1I1Dw_name, *log_1I2D_name; |
| 672 | Char *log_0I1Dr_name, *log_0I1Dw_name, *log_0I2D_name; |
| 673 | }; |
| 674 | |
| 675 | |
| 676 | /*------------------------------------------------------------*/ |
| 677 | /*--- Functions ---*/ |
| 678 | /*------------------------------------------------------------*/ |
| 679 | |
| 680 | /* from clo.c */ |
| 681 | |
| 682 | void CLG_(set_clo_defaults)(void); |
| 683 | void CLG_(update_fn_config)(fn_node*); |
| 684 | Bool CLG_(process_cmd_line_option)(Char*); |
| 685 | void CLG_(print_usage)(void); |
| 686 | void CLG_(print_debug_usage)(void); |
| 687 | |
| 688 | /* from sim.c */ |
| 689 | struct event_sets { |
| 690 | EventSet *use, *Ir, *Dr, *Dw; |
| 691 | EventSet *D0, *D1r, *D1w, *D2; |
| 692 | EventSet *sim; |
| 693 | EventSet *full; /* sim plus user events */ |
| 694 | |
| 695 | /* offsets into eventsets */ |
| 696 | Int off_sim_Ir, off_sim_Dr, off_sim_Dw; |
| 697 | Int off_full_Ir, off_full_Dr, off_full_Dw; |
| 698 | Int off_full_user, off_full_alloc, off_full_systime; |
| 699 | }; |
| 700 | |
| 701 | extern struct event_sets CLG_(sets); |
| 702 | extern struct cachesim_if CLG_(cachesim); |
| 703 | |
| 704 | void CLG_(init_eventsets)(Int user); |
| 705 | |
| 706 | /* from main.c */ |
| 707 | Bool CLG_(get_debug_info)(Addr, Char filename[FILENAME_LEN], |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 708 | Char fn_name[FN_NAME_LEN], UInt*, DebugInfo**); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 709 | void CLG_(collectBlockInfo)(IRSB* bbIn, UInt*, UInt*, Bool*); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 710 | void CLG_(set_instrument_state)(Char*,Bool); |
| 711 | void CLG_(dump_profile)(Char* trigger,Bool only_current_thread); |
| 712 | void CLG_(zero_all_cost)(Bool only_current_thread); |
| 713 | Int CLG_(get_dump_counter)(void); |
| 714 | void CLG_(fini)(Int exitcode); |
| 715 | |
| 716 | /* from command.c */ |
weidendo | 4ce5e79 | 2006-09-20 21:29:39 +0000 | [diff] [blame] | 717 | void CLG_(init_command)(void); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 718 | void CLG_(check_command)(void); |
| 719 | void CLG_(finish_command)(void); |
| 720 | |
| 721 | /* from bb.c */ |
| 722 | void CLG_(init_bb_hash)(void); |
| 723 | bb_hash* CLG_(get_bb_hash)(void); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 724 | BB* CLG_(get_bb)(Addr addr, IRSB* bb_in, Bool *seen_before); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 725 | void CLG_(delete_bb)(Addr addr); |
| 726 | |
| 727 | static __inline__ Addr bb_addr(BB* bb) |
| 728 | { return bb->offset + bb->obj->offset; } |
| 729 | static __inline__ Addr bb_jmpaddr(BB* bb) |
weidendo | 90741fb | 2006-09-12 19:10:08 +0000 | [diff] [blame] | 730 | { UInt off = (bb->instr_count > 0) ? bb->instr[bb->instr_count-1].instr_offset : 0; |
| 731 | return off + bb->offset + bb->obj->offset; } |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 732 | |
| 733 | /* from fn.c */ |
| 734 | void CLG_(init_fn_array)(fn_array*); |
| 735 | void CLG_(copy_current_fn_array)(fn_array* dst); |
| 736 | fn_array* CLG_(get_current_fn_array)(void); |
| 737 | void CLG_(set_current_fn_array)(fn_array*); |
| 738 | UInt* CLG_(get_fn_entry)(Int n); |
| 739 | |
| 740 | void CLG_(init_obj_table)(void); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 741 | obj_node* CLG_(get_obj_node)(DebugInfo* si); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 742 | file_node* CLG_(get_file_node)(obj_node*, Char* filename); |
| 743 | fn_node* CLG_(get_fn_node)(BB* bb); |
| 744 | |
| 745 | /* from bbcc.c */ |
| 746 | void CLG_(init_bbcc_hash)(bbcc_hash* bbccs); |
| 747 | void CLG_(copy_current_bbcc_hash)(bbcc_hash* dst); |
| 748 | bbcc_hash* CLG_(get_current_bbcc_hash)(void); |
| 749 | void CLG_(set_current_bbcc_hash)(bbcc_hash*); |
| 750 | void CLG_(forall_bbccs)(void (*func)(BBCC*)); |
| 751 | void CLG_(zero_bbcc)(BBCC* bbcc); |
| 752 | BBCC* CLG_(get_bbcc)(BB* bb); |
| 753 | BBCC* CLG_(clone_bbcc)(BBCC* orig, Context* cxt, Int rec_index); |
| 754 | void CLG_(setup_bbcc)(BB* bb) VG_REGPARM(1); |
| 755 | |
| 756 | |
| 757 | /* from jumps.c */ |
| 758 | void CLG_(init_jcc_hash)(jcc_hash*); |
| 759 | void CLG_(copy_current_jcc_hash)(jcc_hash* dst); |
| 760 | jcc_hash* CLG_(get_current_jcc_hash)(void); |
| 761 | void CLG_(set_current_jcc_hash)(jcc_hash*); |
| 762 | jCC* CLG_(get_jcc)(BBCC* from, UInt, BBCC* to); |
| 763 | |
| 764 | /* from callstack.c */ |
| 765 | void CLG_(init_call_stack)(call_stack*); |
| 766 | void CLG_(copy_current_call_stack)(call_stack* dst); |
| 767 | void CLG_(set_current_call_stack)(call_stack*); |
| 768 | call_entry* CLG_(get_call_entry)(Int n); |
| 769 | |
| 770 | void CLG_(push_call_stack)(BBCC* from, UInt jmp, BBCC* to, Addr sp, Bool skip); |
| 771 | void CLG_(pop_call_stack)(void); |
| 772 | void CLG_(unwind_call_stack)(Addr sp, Int); |
| 773 | |
| 774 | /* from context.c */ |
| 775 | void CLG_(init_fn_stack)(fn_stack*); |
| 776 | void CLG_(copy_current_fn_stack)(fn_stack*); |
| 777 | fn_stack* CLG_(get_current_fn_stack)(void); |
| 778 | void CLG_(set_current_fn_stack)(fn_stack*); |
| 779 | |
| 780 | void CLG_(init_cxt_table)(void); |
| 781 | cxt_hash* CLG_(get_cxt_hash)(void); |
| 782 | Context* CLG_(get_cxt)(fn_node** fn); |
| 783 | void CLG_(push_cxt)(fn_node* fn); |
| 784 | |
| 785 | /* from threads.c */ |
| 786 | void CLG_(init_threads)(void); |
| 787 | thread_info** CLG_(get_threads)(void); |
| 788 | thread_info* CLG_(get_current_thread)(void); |
| 789 | void CLG_(switch_thread)(ThreadId tid); |
| 790 | void CLG_(forall_threads)(void (*func)(thread_info*)); |
| 791 | void CLG_(run_thread)(ThreadId tid); |
| 792 | |
| 793 | void CLG_(init_exec_state)(exec_state* es); |
| 794 | void CLG_(init_exec_stack)(exec_stack*); |
| 795 | void CLG_(copy_current_exec_stack)(exec_stack*); |
| 796 | void CLG_(set_current_exec_stack)(exec_stack*); |
| 797 | void CLG_(pre_signal)(ThreadId tid, Int sigNum, Bool alt_stack); |
| 798 | void CLG_(post_signal)(ThreadId tid, Int sigNum); |
| 799 | void CLG_(run_post_signal_on_call_stack_bottom)(void); |
| 800 | |
| 801 | /* from dump.c */ |
| 802 | extern FullCost CLG_(total_cost); |
weidendo | 4ce5e79 | 2006-09-20 21:29:39 +0000 | [diff] [blame] | 803 | void CLG_(init_dumps)(void); |
weidendo | cbf4e19 | 2007-11-27 01:27:12 +0000 | [diff] [blame] | 804 | Char* CLG_(get_out_file)(void); |
| 805 | Char* CLG_(get_out_directory)(void); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 806 | |
| 807 | /*------------------------------------------------------------*/ |
| 808 | /*--- Exported global variables ---*/ |
| 809 | /*------------------------------------------------------------*/ |
| 810 | |
| 811 | extern CommandLineOptions CLG_(clo); |
| 812 | extern Statistics CLG_(stat); |
| 813 | extern EventMapping* CLG_(dumpmap); |
| 814 | |
| 815 | /* Function active counter array, indexed by function number */ |
| 816 | extern UInt* CLG_(fn_active_array); |
| 817 | extern Bool CLG_(instrument_state); |
| 818 | |
| 819 | extern call_stack CLG_(current_call_stack); |
| 820 | extern fn_stack CLG_(current_fn_stack); |
| 821 | extern exec_state CLG_(current_state); |
| 822 | extern ThreadId CLG_(current_tid); |
| 823 | |
| 824 | |
| 825 | /*------------------------------------------------------------*/ |
| 826 | /*--- Debug output ---*/ |
| 827 | /*------------------------------------------------------------*/ |
| 828 | |
| 829 | #if CLG_ENABLE_DEBUG |
| 830 | |
| 831 | #define CLG_DEBUGIF(x) \ |
| 832 | if ( (CLG_(clo).verbose >x) && \ |
| 833 | (CLG_(stat).bb_executions >= CLG_(clo).verbose_start)) |
| 834 | |
| 835 | #define CLG_DEBUG(x,format,args...) \ |
| 836 | CLG_DEBUGIF(x) { \ |
| 837 | CLG_(print_bbno)(); \ |
| 838 | VG_(printf)(format,##args); \ |
| 839 | } |
| 840 | |
| 841 | #define CLG_ASSERT(cond) \ |
| 842 | if (!(cond)) { \ |
| 843 | CLG_(print_context)(); \ |
| 844 | CLG_(print_bbno)(); \ |
| 845 | tl_assert(cond); \ |
| 846 | } |
| 847 | |
| 848 | #else |
| 849 | #define CLG_DEBUGIF(x) if (0) |
| 850 | #define CLG_DEBUG(x...) {} |
| 851 | #define CLG_ASSERT(cond) tl_assert(cond); |
| 852 | #endif |
| 853 | |
| 854 | /* from debug.c */ |
| 855 | void CLG_(print_bbno)(void); |
| 856 | void CLG_(print_context)(void); |
| 857 | void CLG_(print_jcc)(int s, jCC* jcc); |
| 858 | void CLG_(print_bbcc)(int s, BBCC* bbcc, Bool); |
| 859 | void CLG_(print_bbcc_fn)(BBCC* bbcc); |
| 860 | void CLG_(print_execstate)(int s, exec_state* es); |
| 861 | void CLG_(print_eventset)(int s, EventSet* es); |
| 862 | void CLG_(print_cost)(int s, EventSet*, ULong* cost); |
| 863 | void CLG_(print_bb)(int s, BB* bb); |
| 864 | void CLG_(print_bbcc_cost)(int s, BBCC*); |
| 865 | void CLG_(print_cxt)(int s, Context* cxt, int rec_index); |
| 866 | void CLG_(print_short_jcc)(jCC* jcc); |
| 867 | void CLG_(print_stackentry)(int s, int sp); |
| 868 | void CLG_(print_addr)(Addr addr); |
| 869 | void CLG_(print_addr_ln)(Addr addr); |
| 870 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 871 | void* CLG_(malloc)(HChar* cc, UWord s, char* f); |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 872 | void* CLG_(free)(void* p, char* f); |
| 873 | #if 0 |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 874 | #define CLG_MALLOC(_cc,x) CLG_(malloc)((_cc),x,__FUNCTION__) |
| 875 | #define CLG_FREE(p) CLG_(free)(p,__FUNCTION__) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 876 | #else |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 877 | #define CLG_MALLOC(_cc,x) VG_(malloc)((_cc),x) |
| 878 | #define CLG_FREE(p) VG_(free)(p) |
weidendo | a17f2a3 | 2006-03-20 10:27:30 +0000 | [diff] [blame] | 879 | #endif |
| 880 | |
| 881 | #endif /* CLG_GLOBAL */ |