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