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