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