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