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