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   const HChar* 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 _BBCC        BBCC;
    216 typedef struct _jCC         jCC;
    217 typedef struct _fCC         fCC;
    218 typedef struct _fn_node     fn_node;
    219 typedef struct _file_node   file_node;
    220 typedef struct _obj_node    obj_node;
    221 typedef struct _fn_config   fn_config;
    222 typedef struct _call_entry  call_entry;
    223 typedef struct _thread_info thread_info;
    224 
    225 /* Costs of event sets. Aliases to arrays of 64-bit values */
    226 typedef ULong* SimCost;  /* All events the simulator can produce */
    227 typedef ULong* UserCost;
    228 typedef ULong* FullCost; /* Simulator + User */
    229 
    230 
    231 /* The types of control flow changes that can happen between
    232  * execution of two BBs in a thread.
    233  */
    234 typedef enum {
    235   jk_None = 0,   /* no explicit change by a guest instruction */
    236   jk_Jump,       /* regular jump */
    237   jk_Call,
    238   jk_Return,
    239   jk_CondJump    /* conditional jump taken (only used as jCC type) */
    240 } ClgJumpKind;
    241 
    242 
    243 /* JmpCall cost center
    244  * for subroutine call (from->bb->jmp_addr => to->bb->addr)
    245  *
    246  * Each BB has at most one CALL instruction. The list of JCC from
    247  * this call is a pointer to the list head (stored in BBCC), and
    248  * <next_from> in the JCC struct.
    249  *
    250  * For fast lookup, JCCs are reachable with a hash table, keyed by
    251  * the (from_bbcc,to) pair. <next_hash> is used for the JCC chain
    252  * of one hash table entry.
    253  *
    254  * Cost <sum> holds event counts for already returned executions.
    255  * <last> are the event counters at last enter of the subroutine.
    256  * <sum> is updated on returning from the subroutine by
    257  * adding the diff of <last> and current event counters to <sum>.
    258  *
    259  * After updating, <last> is set to current event counters. Thus,
    260  * events are not counted twice for recursive calls (TODO: True?)
    261  */
    262 
    263 struct _jCC {
    264   ClgJumpKind jmpkind; /* jk_Call, jk_Jump, jk_CondJump */
    265   jCC* next_hash;   /* for hash entry chain */
    266   jCC* next_from;   /* next JCC from a BBCC */
    267   BBCC *from, *to;  /* call arc from/to this BBCC */
    268   UInt jmp;         /* jump no. in source */
    269 
    270   ULong call_counter; /* no wraparound with 64 bit */
    271 
    272   FullCost cost; /* simulator + user counters */
    273 };
    274 
    275 
    276 /*
    277  * Info for one instruction of a basic block.
    278  */
    279 typedef struct _InstrInfo InstrInfo;
    280 struct _InstrInfo {
    281   UInt instr_offset;
    282   UInt instr_size;
    283   UInt cost_offset;
    284   EventSet* eventset;
    285 };
    286 
    287 
    288 
    289 /*
    290  * Info for a side exit in a BB
    291  */
    292 typedef struct _CJmpInfo CJmpInfo;
    293 struct _CJmpInfo {
    294   UInt instr;          /* instruction index for BB.instr array */
    295   ClgJumpKind jmpkind; /* jump kind when leaving BB at this side exit */
    296 };
    297 
    298 
    299 /**
    300  * An instrumented basic block (BB).
    301  *
    302  * BBs are put into a resizable hash to allow for fast detection if a
    303  * BB is to be retranslated but cost info is already available.
    304  * The key for a BB is a (object, offset) tupel making it independent
    305  * from possibly multiple mappings of the same ELF object.
    306  *
    307  * At the beginning of each instrumented BB,
    308  * a call to setup_bbcc(), specifying a pointer to the
    309  * according BB structure, is added.
    310  *
    311  * As cost of a BB has to be distinguished depending on the context,
    312  * multiple cost centers for one BB (struct BBCC) exist and the according
    313  * BBCC is set by setup_bbcc.
    314  */
    315 struct _BB {
    316   obj_node*  obj;         /* ELF object of BB */
    317   PtrdiffT   offset;      /* offset of BB in ELF object file */
    318   BB*        next;       /* chaining for a hash entry */
    319 
    320   VgSectKind sect_kind;  /* section of this BB, e.g. PLT */
    321   UInt       instr_count;
    322 
    323   /* filled by CLG_(get_fn_node) if debug info is available */
    324   fn_node*   fn;          /* debug info for this BB */
    325   UInt       line;
    326   Bool       is_entry;    /* True if this BB is a function entry */
    327 
    328   BBCC*      bbcc_list;  /* BBCCs for same BB (see next_bbcc in BBCC) */
    329   BBCC*      last_bbcc;  /* Temporary: Cached for faster access (LRU) */
    330 
    331   /* filled by CLG_(instrument) if not seen before */
    332   UInt       cjmp_count;  /* number of side exits */
    333   CJmpInfo*  jmp;         /* array of info for condition jumps,
    334 			   * allocated directly after this struct */
    335   Bool       cjmp_inverted; /* is last side exit actually fall through? */
    336 
    337   UInt       instr_len;
    338   UInt       cost_count;
    339   InstrInfo  instr[0];   /* info on instruction sizes and costs */
    340 };
    341 
    342 
    343 
    344 /**
    345  * Function context
    346  *
    347  * Basic blocks are always executed in the scope of a context.
    348  * A function context is a list of function nodes representing
    349  * the call chain to the current context: I.e. fn[0] is the
    350  * function we are currently in, fn[1] has called fn[0], and so on.
    351  * Recursion levels are used for fn[0].
    352  *
    353  * To get a unique number for a full execution context, use
    354  *  rec_index = min(<fn->rec_separation>,<active>) - 1;
    355  *  unique_no = <number> + rec_index
    356  *
    357  * For each Context, recursion index and BB, there can be a BBCC.
    358  */
    359 struct _Context {
    360     UInt size;        // number of function dependencies
    361     UInt base_number; // for context compression & dump array
    362     Context* next;    // entry chaining for hash
    363     UWord hash;       // for faster lookup...
    364     fn_node* fn[0];
    365 };
    366 
    367 
    368 /*
    369  * Cost info for a side exits from a BB
    370  */
    371 typedef struct _JmpData JmpData;
    372 struct _JmpData {
    373     ULong ecounter; /* number of times the BB was left at this exit */
    374     jCC*  jcc_list; /* JCCs used for this exit */
    375 };
    376 
    377 
    378 /*
    379  * Basic Block Cost Center
    380  *
    381  * On demand, multiple BBCCs will be created for the same BB
    382  * dependend on command line options and:
    383  * - current function (it's possible that a BB is executed in the
    384  *   context of different functions, e.g. in manual assembler/PLT)
    385  * - current thread ID
    386  * - position where current function is called from
    387  * - recursion level of current function
    388  *
    389  * The cost centres for the instructions of a basic block are
    390  * stored in a contiguous array.
    391  * They are distinguishable by their tag field.
    392  */
    393 struct _BBCC {
    394     BB*      bb;           /* BB for this cost center */
    395 
    396     Context* cxt;          /* execution context of this BBCC */
    397     ThreadId tid;          /* only for assertion check purpose */
    398     UInt     rec_index;    /* Recursion index in rec->bbcc for this bbcc */
    399     BBCC**   rec_array;    /* Variable sized array of pointers to
    400 			    * recursion BBCCs. Shared. */
    401     ULong    ret_counter;  /* how often returned from jccs of this bbcc;
    402 			    * used to check if a dump for this BBCC is needed */
    403 
    404     BBCC*    next_bbcc;    /* Chain of BBCCs for same BB */
    405     BBCC*    lru_next_bbcc; /* BBCC executed next the last time */
    406 
    407     jCC*     lru_from_jcc; /* Temporary: Cached for faster access (LRU) */
    408     jCC*     lru_to_jcc;   /* Temporary: Cached for faster access (LRU) */
    409     FullCost skipped;      /* cost for skipped functions called from
    410 			    * jmp_addr. Allocated lazy */
    411 
    412     BBCC*    next;         /* entry chain in hash */
    413     ULong*   cost;         /* start of 64bit costs for this BBCC */
    414     ULong    ecounter_sum; /* execution counter for first instruction of BB */
    415     JmpData  jmp[0];
    416 };
    417 
    418 
    419 /* the <number> of fn_node, file_node and obj_node are for compressed dumping
    420  * and a index into the dump boolean table and fn_info_table
    421  */
    422 
    423 struct _fn_node {
    424   HChar*     name;
    425   UInt       number;
    426   Context*   last_cxt; /* LRU info */
    427   Context*   pure_cxt; /* the context with only the function itself */
    428   file_node* file;     /* reverse mapping for 2nd hash */
    429   fn_node* next;
    430 
    431   Bool dump_before :1;
    432   Bool dump_after :1;
    433   Bool zero_before :1;
    434   Bool toggle_collect :1;
    435   Bool skip :1;
    436   Bool pop_on_jump : 1;
    437 
    438   Bool is_malloc :1;
    439   Bool is_realloc :1;
    440   Bool is_free :1;
    441 
    442   Int  group;
    443   Int  separate_callers;
    444   Int  separate_recursions;
    445 #if CLG_ENABLE_DEBUG
    446   Int  verbosity; /* Stores old verbosity level while in function */
    447 #endif
    448 };
    449 
    450 /* Quite arbitrary fixed hash sizes */
    451 
    452 #define   N_OBJ_ENTRIES         47
    453 #define  N_FILE_ENTRIES         53
    454 #define    N_FN_ENTRIES         87
    455 #define N_BBCC2_ENTRIES         37
    456 
    457 struct _file_node {
    458    HChar*     name;
    459    fn_node*   fns[N_FN_ENTRIES];
    460    UInt       number;
    461    obj_node*  obj;
    462    file_node* next;
    463 };
    464 
    465 /* If an object is dlopened multiple times, we hope that <name> is unique;
    466  * <start> and <offset> can change with each dlopen, and <start> is
    467  * zero when object is unmapped (possible at dump time).
    468  */
    469 struct _obj_node {
    470    const HChar* name;
    471    UInt       last_slash_pos;
    472 
    473    Addr       start;  /* Start address of text segment mapping */
    474    SizeT      size;   /* Length of mapping */
    475    PtrdiffT   offset; /* Offset between symbol address and file offset */
    476 
    477    file_node* files[N_FILE_ENTRIES];
    478    UInt       number;
    479    obj_node*  next;
    480 };
    481 
    482 /* an entry in the callstack
    483  *
    484  * <nonskipped> is 0 if the function called is not skipped (usual case).
    485  * Otherwise, it is the last non-skipped BBCC. This one gets all
    486  * the calls to non-skipped functions and all costs in skipped
    487  * instructions.
    488  */
    489 struct _call_entry {
    490     jCC* jcc;           /* jCC for this call */
    491     FullCost enter_cost; /* cost event counters at entering frame */
    492     Addr sp;            /* stack pointer directly after call */
    493     Addr ret_addr;      /* address to which to return to
    494 			 * is 0 on a simulated call */
    495     BBCC* nonskipped;   /* see above */
    496     Context* cxt;       /* context before call */
    497     Int fn_sp;          /* function stack index before call */
    498 };
    499 
    500 
    501 /*
    502  * Execution state of main thread or a running signal handler in
    503  * a thread while interrupted by another signal handler.
    504  * As there's no scheduling among running signal handlers of one thread,
    505  * we only need a subset of a full thread state:
    506  * - event counter
    507  * - collect state
    508  * - last BB, last jump kind, last nonskipped BB
    509  * - callstack pointer for sanity checking and correct unwinding
    510  *   after exit
    511  */
    512 typedef struct _exec_state exec_state;
    513 struct _exec_state {
    514 
    515   /* the signum of the handler, 0 for main thread context
    516    */
    517   Int sig;
    518 
    519   /* the old call stack pointer at entering the signal handler */
    520   Int orig_sp;
    521 
    522   FullCost cost;
    523   Bool     collect;
    524   Context* cxt;
    525 
    526   /* number of conditional jumps passed in last BB */
    527   Int   jmps_passed;
    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)(const HChar* arg);
    661     void (*post_clo_init)(void);
    662     void (*clear)(void);
    663     void (*getdesc)(HChar* 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     const HChar *log_1I0D_name, *log_2I0D_name, *log_3I0D_name;
    680     const HChar *log_1I1Dr_name, *log_1I1Dw_name;
    681     const HChar *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)(const HChar*);
    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, HChar filename[FILENAME_LEN],
    725 			 HChar fn_name[FN_NAME_LEN], UInt*, DebugInfo**);
    726 void CLG_(collectBlockInfo)(IRSB* bbIn, UInt*, UInt*, Bool*);
    727 void CLG_(set_instrument_state)(const HChar*,Bool);
    728 void CLG_(dump_profile)(const HChar* 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*, HChar* 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 HChar* CLG_(get_out_file)(void);
    817 HChar* 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 (UNLIKELY( (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 (UNLIKELY(!(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)(const HChar* cc, UWord s, const HChar* f);
    886 void* CLG_(free)(void* p, const HChar* 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