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