blob: 461218a8c4328c601f5b25f4aa1fcc8d0b3cd814 [file] [log] [blame]
weidendoa17f2a32006-03-20 10:27:30 +00001/*--------------------------------------------------------------------*/
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"
sewardj4cfea4f2006-10-14 19:26:10 +000011#include "pub_tool_vki.h"
weidendoa17f2a32006-03-20 10:27:30 +000012#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"
sewardj14c7cc52007-02-25 15:08:24 +000022#include "pub_tool_xarray.h"
weidendoa17f2a32006-03-20 10:27:30 +000023#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
weidendocbf4e192007-11-27 01:27:12 +000053#define DEFAULT_OUTFORMAT "callgrind.out.%p"
weidendoa17f2a32006-03-20 10:27:30 +000054#define DEFAULT_COMMANDNAME "callgrind.cmd"
55#define DEFAULT_RESULTNAME "callgrind.res"
56#define DEFAULT_INFONAME "/tmp/callgrind.info"
57
58typedef struct _CommandLineOptions CommandLineOptions;
59struct _CommandLineOptions {
60
61 /* Dump format options */
weidendocbf4e192007-11-27 01:27:12 +000062 Char* out_format; /* Format string for callgrind output file name */
weidendoa17f2a32006-03-20 10:27:30 +000063 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 */
weidendo9e326b72006-03-31 13:16:15 +000075 ULong dump_every_bb; /* Dump every xxx BBs. */
weidendoa17f2a32006-03-20 10:27:30 +000076
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
weidendoa762b0f2006-05-01 00:55:54 +000094 /* Call graph generation */
95 Bool pop_on_jump; /* Handle a jump between functions as ret+call */
96
weidendoa17f2a32006-03-20 10:27:30 +000097#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
weidendo5bd7eca2007-02-26 00:16:09 +0000124/* 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
weidendoa17f2a32006-03-20 10:27:30 +0000161
162/*------------------------------------------------------------*/
163/*--- Statistics ---*/
164/*------------------------------------------------------------*/
165
166typedef struct _Statistics Statistics;
167struct _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
211typedef struct _Context Context;
212typedef struct _CC CC;
213typedef struct _BB BB;
214typedef struct _Skipped Skipped;
215typedef struct _BBCC BBCC;
216typedef struct _jCC jCC;
217typedef struct _fCC fCC;
218typedef struct _fn_node fn_node;
219typedef struct _file_node file_node;
220typedef struct _obj_node obj_node;
221typedef struct _fn_config fn_config;
222typedef struct _call_entry call_entry;
223typedef struct _thread_info thread_info;
224
225/* Costs of event sets. Aliases to arrays of 64-bit values */
226typedef ULong* SimCost; /* All events the simulator can produce */
227typedef ULong* UserCost;
228typedef 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
253struct _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 */
269typedef struct _InstrInfo InstrInfo;
270struct _InstrInfo {
271 UInt instr_offset;
272 UInt instr_size;
273 UInt data_size;
274 UInt cost_offset;
275 EventSet* eventset;
276};
277
278
279/*
280 * Info for a conditional jump in a basic block
281 */
282typedef struct _CJmpInfo CJmpInfo;
283struct _CJmpInfo {
284 UInt instr; /* instruction index in this basic block */
285 Bool skip; /* Cond.Jumps to next instruction should be ignored */
286};
287
288
289/**
290 * An instrumented basic block (BB).
291 *
292 * BBs are put into a resizable hash to allow for fast detection if a
293 * BB is to be retranslated but cost info is already available.
294 * The key for a BB is a (object, offset) tupel making it independent
295 * from possibly multiple mappings of the same ELF object.
296 *
297 * At the beginning of each instrumented BB,
298 * a call to setup_bbcc(), specifying a pointer to the
299 * according BB structure, is added.
300 *
301 * As cost of a BB has to be distinguished depending on the context,
302 * multiple cost centers for one BB (struct BBCC) exist and the according
303 * BBCC is set by setup_bbcc.
304 */
305struct _BB {
306 obj_node* obj; /* ELF object of BB */
njnc4431bf2009-01-15 21:29:24 +0000307 PtrdiffT offset; /* offset of BB in ELF object file */
weidendoa17f2a32006-03-20 10:27:30 +0000308 BB* next; /* chaining for a hash entry */
309
310 VgSectKind sect_kind; /* section of this BB, e.g. PLT */
311 UInt instr_count;
312
313 /* filled by CLG_(get_fn_node) if debug info is available */
314 fn_node* fn; /* debug info for this BB */
315 UInt line;
316 Bool is_entry; /* True if this BB is a function entry */
317
318 BBCC* bbcc_list; /* BBCCs for same BB (see next_bbcc in BBCC) */
319 BBCC* last_bbcc; /* Temporary: Cached for faster access (LRU) */
320
321 /* filled by CLG_(instrument) if not seen before */
322 UInt cjmp_count; /* number of conditional exits */
323 CJmpInfo* jmp; /* array of info for condition jumps,
324 * allocated directly after this struct */
325 Int jmpkind; /* remember jump kind of final exit */
326 Bool cjmp_inverted; /* condition of last cond.jump can be inverted by VEX */
327
328 UInt instr_len;
329 UInt cost_count;
330 InstrInfo instr[0]; /* info on instruction sizes and costs */
331};
332
333
334
335/**
336 * Function context
337 *
338 * Basic blocks are always executed in the scope of a context.
339 * A function context is a list of function nodes representing
340 * the call chain to the current context: I.e. fn[0] is the
341 * function we are currently in, fn[1] has called fn[0], and so on.
342 * Recursion levels are used for fn[0].
343 *
344 * To get a unique number for a full execution context, use
345 * rec_index = min(<fn->rec_separation>,<active>) - 1;
346 * unique_no = <number> + rec_index
347 *
348 * For each Context, recursion index and BB, there can be a BBCC.
349 */
350struct _Context {
351 UInt size; // number of function dependencies
352 UInt base_number; // for context compression & dump array
353 Context* next; // entry chaining for hash
354 UWord hash; // for faster lookup...
355 fn_node* fn[0];
356};
357
358
359/*
360 * Info for a conditional jump in a basic block
361 */
362typedef struct _JmpData JmpData;
363struct _JmpData {
364 ULong ecounter; /* number of times the BB was left at this exit */
365 jCC* jcc_list; /* JCCs for Cond.Jumps from this exit */
366};
367
368
369/*
370 * Basic Block Cost Center
371 *
372 * On demand, multiple BBCCs will be created for the same BB
373 * dependend on command line options and:
374 * - current function (it's possible that a BB is executed in the
375 * context of different functions, e.g. in manual assembler/PLT)
376 * - current thread ID
377 * - position where current function is called from
378 * - recursion level of current function
379 *
380 * The cost centres for the instructions of a basic block are
381 * stored in a contiguous array.
382 * They are distinguishable by their tag field.
383 */
384struct _BBCC {
385 BB* bb; /* BB for this cost center */
386
387 Context* cxt; /* execution context of this BBCC */
388 ThreadId tid; /* only for assertion check purpose */
389 UInt rec_index; /* Recursion index in rec->bbcc for this bbcc */
390 BBCC** rec_array; /* Variable sized array of pointers to
391 * recursion BBCCs. Shared. */
392 ULong ret_counter; /* how often returned from jccs of this bbcc;
393 * used to check if a dump for this BBCC is needed */
394
395 BBCC* next_bbcc; /* Chain of BBCCs for same BB */
396 BBCC* lru_next_bbcc; /* BBCC executed next the last time */
397
398 jCC* lru_from_jcc; /* Temporary: Cached for faster access (LRU) */
399 jCC* lru_to_jcc; /* Temporary: Cached for faster access (LRU) */
400 FullCost skipped; /* cost for skipped functions called from
401 * jmp_addr. Allocated lazy */
402
403 BBCC* next; /* entry chain in hash */
404 ULong* cost; /* start of 64bit costs for this BBCC */
405 ULong ecounter_sum; /* execution counter for first instruction of BB */
406 JmpData jmp[0];
407};
408
409
410/* the <number> of fn_node, file_node and obj_node are for compressed dumping
411 * and a index into the dump boolean table and fn_info_table
412 */
413
414struct _fn_node {
415 Char* name;
416 UInt number;
417 Context* last_cxt; /* LRU info */
418 Context* pure_cxt; /* the context with only the function itself */
419 file_node* file; /* reverse mapping for 2nd hash */
420 fn_node* next;
421
422 Bool dump_before :1;
423 Bool dump_after :1;
424 Bool zero_before :1;
425 Bool toggle_collect :1;
426 Bool skip :1;
427 Bool pop_on_jump : 1;
428
429 Bool is_malloc :1;
430 Bool is_realloc :1;
431 Bool is_free :1;
432
433 Int group;
434 Int separate_callers;
435 Int separate_recursions;
436#if CLG_ENABLE_DEBUG
437 Int verbosity; /* Stores old verbosity level while in function */
438#endif
439};
440
441/* Quite arbitrary fixed hash sizes */
442
443#define N_OBJ_ENTRIES 47
444#define N_FILE_ENTRIES 53
445#define N_FN_ENTRIES 87
446#define N_BBCC2_ENTRIES 37
447
448struct _file_node {
449 Char* name;
450 fn_node* fns[N_FN_ENTRIES];
451 UInt number;
452 obj_node* obj;
453 file_node* next;
454};
455
456/* If an object is dlopened multiple times, we hope that <name> is unique;
457 * <start> and <offset> can change with each dlopen, and <start> is
458 * zero when object is unmapped (possible at dump time).
459 */
460struct _obj_node {
461 Char* name;
462 UInt last_slash_pos;
463
464 Addr start; /* Start address of text segment mapping */
465 SizeT size; /* Length of mapping */
njnc4431bf2009-01-15 21:29:24 +0000466 PtrdiffT offset; /* Offset between symbol address and file offset */
weidendoa17f2a32006-03-20 10:27:30 +0000467
468 file_node* files[N_FILE_ENTRIES];
469 UInt number;
470 obj_node* next;
471};
472
473/* an entry in the callstack
474 *
475 * <nonskipped> is 0 if the function called is not skipped (usual case).
476 * Otherwise, it is the last non-skipped BBCC. This one gets all
477 * the calls to non-skipped functions and all costs in skipped
478 * instructions.
479 */
480struct _call_entry {
481 jCC* jcc; /* jCC for this call */
482 FullCost enter_cost; /* cost event counters at entering frame */
483 Addr sp; /* stack pointer directly after call */
484 Addr ret_addr; /* address to which to return to
485 * is 0 on a simulated call */
486 BBCC* nonskipped; /* see above */
487 Context* cxt; /* context before call */
488 Int fn_sp; /* function stack index before call */
489};
490
491
492/*
493 * Execution state of main thread or a running signal handler in
494 * a thread while interrupted by another signal handler.
495 * As there's no scheduling among running signal handlers of one thread,
496 * we only need a subset of a full thread state:
497 * - event counter
498 * - collect state
499 * - last BB, last jump kind, last nonskipped BB
500 * - callstack pointer for sanity checking and correct unwinding
501 * after exit
502 */
503typedef struct _exec_state exec_state;
504struct _exec_state {
505
506 /* the signum of the handler, 0 for main thread context
507 */
508 Int sig;
509
510 /* the old call stack pointer at entering the signal handler */
511 Int orig_sp;
512
513 FullCost cost;
514 Bool collect;
515 Context* cxt;
516
517 Int jmps_passed; /* number of conditional jumps passed in last BB */
518 BBCC* bbcc; /* last BB executed */
519 BBCC* nonskipped;
520
521 Int call_stack_bottom; /* Index into fn_stack */
522};
523
524/* Global state structures */
525typedef struct _bb_hash bb_hash;
526struct _bb_hash {
527 UInt size, entries;
528 BB** table;
529};
530
531typedef struct _cxt_hash cxt_hash;
532struct _cxt_hash {
533 UInt size, entries;
534 Context** table;
535};
536
537/* Thread specific state structures, i.e. parts of a thread state.
538 * There are variables for the current state of each part,
539 * on which a thread state is copied at thread switch.
540 */
541typedef struct _bbcc_hash bbcc_hash;
542struct _bbcc_hash {
543 UInt size, entries;
544 BBCC** table;
545};
546
547typedef struct _jcc_hash jcc_hash;
548struct _jcc_hash {
549 UInt size, entries;
550 jCC** table;
551 jCC* spontaneous;
552};
553
554typedef struct _fn_array fn_array;
555struct _fn_array {
556 UInt size;
557 UInt* array;
558};
559
560typedef struct _call_stack call_stack;
561struct _call_stack {
562 UInt size;
563 Int sp;
564 call_entry* entry;
565};
566
567typedef struct _fn_stack fn_stack;
568struct _fn_stack {
569 UInt size;
570 fn_node **bottom, **top;
571};
572
573/* The maximum number of simultaneous running signal handlers per thread.
574 * This is the number of execution states storable in a thread.
575 */
576#define MAX_SIGHANDLERS 10
577
578typedef struct _exec_stack exec_stack;
579struct _exec_stack {
580 Int sp; /* > 0 if a handler is running */
581 exec_state* entry[MAX_SIGHANDLERS];
582};
583
584/* Thread State
585 *
586 * This structure stores thread specific info while a thread is *not*
587 * running. See function switch_thread() for save/restore on thread switch.
588 *
589 * If --separate-threads=no, BBCCs and JCCs can be shared by all threads, i.e.
590 * only structures of thread 1 are used.
591 * This involves variables fn_info_table, bbcc_table and jcc_table.
592 */
593struct _thread_info {
594
595 /* state */
596 fn_stack fns; /* function stack */
597 call_stack calls; /* context call arc stack */
598 exec_stack states; /* execution states interrupted by signals */
599
600 /* dump statistics */
601 FullCost lastdump_cost; /* Cost at last dump */
602 FullCost sighandler_cost;
603
604 /* thread specific data structure containers */
605 fn_array fn_active;
606 jcc_hash jccs;
607 bbcc_hash bbccs;
608};
609
610/* Structs used for dumping */
611
612/* Address position inside of a BBCC:
613 * This includes
614 * - the address offset from the BB start address
615 * - file/line from debug info for that address (can change inside a BB)
616 */
617typedef struct _AddrPos AddrPos;
618struct _AddrPos {
619 Addr addr;
620 Addr bb_addr;
621 file_node* file;
622 UInt line;
623};
624
625/* a simulator cost entity that can be written out in one line */
626typedef struct _AddrCost AddrCost;
627struct _AddrCost {
628 AddrPos p;
629 SimCost cost;
630};
631
632/* A function in an execution context */
633typedef struct _FnPos FnPos;
634struct _FnPos {
635 file_node* file;
636 fn_node* fn;
637 obj_node* obj;
638 Context* cxt;
639 int rec_index;
640 UInt line;
641};
642
643/*------------------------------------------------------------*/
644/*--- Cache simulator interface ---*/
645/*------------------------------------------------------------*/
646
647struct cachesim_if
648{
649 void (*print_opts)(void);
650 Bool (*parse_opt)(Char* arg);
651 void (*post_clo_init)(void);
652 void (*clear)(void);
653 void (*getdesc)(Char* buf);
654 void (*printstat)(void);
655 void (*add_icost)(SimCost, BBCC*, InstrInfo*, ULong);
656 void (*after_bbsetup)(void);
657 void (*finish)(void);
658
659 void (*log_1I0D)(InstrInfo*) VG_REGPARM(1);
660
661 void (*log_1I1Dr)(InstrInfo*, Addr) VG_REGPARM(2);
662 void (*log_1I1Dw)(InstrInfo*, Addr) VG_REGPARM(2);
663 void (*log_1I2D)(InstrInfo*, Addr, Addr) VG_REGPARM(3);
664
665 void (*log_0I1Dr)(InstrInfo*, Addr) VG_REGPARM(2);
666 void (*log_0I1Dw)(InstrInfo*, Addr) VG_REGPARM(2);
667 void (*log_0I2D)(InstrInfo*, Addr, Addr) VG_REGPARM(3);
668
669 // function names of helpers (for debugging generated code)
670 Char *log_1I0D_name;
671 Char *log_1I1Dr_name, *log_1I1Dw_name, *log_1I2D_name;
672 Char *log_0I1Dr_name, *log_0I1Dw_name, *log_0I2D_name;
673};
674
675
676/*------------------------------------------------------------*/
677/*--- Functions ---*/
678/*------------------------------------------------------------*/
679
680/* from clo.c */
681
682void CLG_(set_clo_defaults)(void);
683void CLG_(update_fn_config)(fn_node*);
684Bool CLG_(process_cmd_line_option)(Char*);
685void CLG_(print_usage)(void);
686void CLG_(print_debug_usage)(void);
687
688/* from sim.c */
689struct event_sets {
690 EventSet *use, *Ir, *Dr, *Dw;
691 EventSet *D0, *D1r, *D1w, *D2;
692 EventSet *sim;
693 EventSet *full; /* sim plus user events */
694
695 /* offsets into eventsets */
696 Int off_sim_Ir, off_sim_Dr, off_sim_Dw;
697 Int off_full_Ir, off_full_Dr, off_full_Dw;
698 Int off_full_user, off_full_alloc, off_full_systime;
699};
700
701extern struct event_sets CLG_(sets);
702extern struct cachesim_if CLG_(cachesim);
703
704void CLG_(init_eventsets)(Int user);
705
706/* from main.c */
707Bool CLG_(get_debug_info)(Addr, Char filename[FILENAME_LEN],
sewardjb8b79ad2008-03-03 01:35:41 +0000708 Char fn_name[FN_NAME_LEN], UInt*, DebugInfo**);
sewardj0b9d74a2006-12-24 02:24:11 +0000709void CLG_(collectBlockInfo)(IRSB* bbIn, UInt*, UInt*, Bool*);
weidendoa17f2a32006-03-20 10:27:30 +0000710void CLG_(set_instrument_state)(Char*,Bool);
711void CLG_(dump_profile)(Char* trigger,Bool only_current_thread);
712void CLG_(zero_all_cost)(Bool only_current_thread);
713Int CLG_(get_dump_counter)(void);
714void CLG_(fini)(Int exitcode);
715
716/* from command.c */
weidendo4ce5e792006-09-20 21:29:39 +0000717void CLG_(init_command)(void);
weidendoa17f2a32006-03-20 10:27:30 +0000718void CLG_(check_command)(void);
719void CLG_(finish_command)(void);
720
721/* from bb.c */
722void CLG_(init_bb_hash)(void);
723bb_hash* CLG_(get_bb_hash)(void);
sewardj0b9d74a2006-12-24 02:24:11 +0000724BB* CLG_(get_bb)(Addr addr, IRSB* bb_in, Bool *seen_before);
weidendoa17f2a32006-03-20 10:27:30 +0000725void CLG_(delete_bb)(Addr addr);
726
727static __inline__ Addr bb_addr(BB* bb)
728 { return bb->offset + bb->obj->offset; }
729static __inline__ Addr bb_jmpaddr(BB* bb)
weidendo90741fb2006-09-12 19:10:08 +0000730 { UInt off = (bb->instr_count > 0) ? bb->instr[bb->instr_count-1].instr_offset : 0;
731 return off + bb->offset + bb->obj->offset; }
weidendoa17f2a32006-03-20 10:27:30 +0000732
733/* from fn.c */
734void CLG_(init_fn_array)(fn_array*);
735void CLG_(copy_current_fn_array)(fn_array* dst);
736fn_array* CLG_(get_current_fn_array)(void);
737void CLG_(set_current_fn_array)(fn_array*);
738UInt* CLG_(get_fn_entry)(Int n);
739
740void CLG_(init_obj_table)(void);
sewardjb8b79ad2008-03-03 01:35:41 +0000741obj_node* CLG_(get_obj_node)(DebugInfo* si);
weidendoa17f2a32006-03-20 10:27:30 +0000742file_node* CLG_(get_file_node)(obj_node*, Char* filename);
743fn_node* CLG_(get_fn_node)(BB* bb);
744
745/* from bbcc.c */
746void CLG_(init_bbcc_hash)(bbcc_hash* bbccs);
747void CLG_(copy_current_bbcc_hash)(bbcc_hash* dst);
748bbcc_hash* CLG_(get_current_bbcc_hash)(void);
749void CLG_(set_current_bbcc_hash)(bbcc_hash*);
750void CLG_(forall_bbccs)(void (*func)(BBCC*));
751void CLG_(zero_bbcc)(BBCC* bbcc);
752BBCC* CLG_(get_bbcc)(BB* bb);
753BBCC* CLG_(clone_bbcc)(BBCC* orig, Context* cxt, Int rec_index);
754void CLG_(setup_bbcc)(BB* bb) VG_REGPARM(1);
755
756
757/* from jumps.c */
758void CLG_(init_jcc_hash)(jcc_hash*);
759void CLG_(copy_current_jcc_hash)(jcc_hash* dst);
760jcc_hash* CLG_(get_current_jcc_hash)(void);
761void CLG_(set_current_jcc_hash)(jcc_hash*);
762jCC* CLG_(get_jcc)(BBCC* from, UInt, BBCC* to);
763
764/* from callstack.c */
765void CLG_(init_call_stack)(call_stack*);
766void CLG_(copy_current_call_stack)(call_stack* dst);
767void CLG_(set_current_call_stack)(call_stack*);
768call_entry* CLG_(get_call_entry)(Int n);
769
770void CLG_(push_call_stack)(BBCC* from, UInt jmp, BBCC* to, Addr sp, Bool skip);
771void CLG_(pop_call_stack)(void);
772void CLG_(unwind_call_stack)(Addr sp, Int);
773
774/* from context.c */
775void CLG_(init_fn_stack)(fn_stack*);
776void CLG_(copy_current_fn_stack)(fn_stack*);
777fn_stack* CLG_(get_current_fn_stack)(void);
778void CLG_(set_current_fn_stack)(fn_stack*);
779
780void CLG_(init_cxt_table)(void);
781cxt_hash* CLG_(get_cxt_hash)(void);
782Context* CLG_(get_cxt)(fn_node** fn);
783void CLG_(push_cxt)(fn_node* fn);
784
785/* from threads.c */
786void CLG_(init_threads)(void);
787thread_info** CLG_(get_threads)(void);
788thread_info* CLG_(get_current_thread)(void);
789void CLG_(switch_thread)(ThreadId tid);
790void CLG_(forall_threads)(void (*func)(thread_info*));
791void CLG_(run_thread)(ThreadId tid);
792
793void CLG_(init_exec_state)(exec_state* es);
794void CLG_(init_exec_stack)(exec_stack*);
795void CLG_(copy_current_exec_stack)(exec_stack*);
796void CLG_(set_current_exec_stack)(exec_stack*);
797void CLG_(pre_signal)(ThreadId tid, Int sigNum, Bool alt_stack);
798void CLG_(post_signal)(ThreadId tid, Int sigNum);
799void CLG_(run_post_signal_on_call_stack_bottom)(void);
800
801/* from dump.c */
802extern FullCost CLG_(total_cost);
weidendo4ce5e792006-09-20 21:29:39 +0000803void CLG_(init_dumps)(void);
weidendocbf4e192007-11-27 01:27:12 +0000804Char* CLG_(get_out_file)(void);
805Char* CLG_(get_out_directory)(void);
weidendoa17f2a32006-03-20 10:27:30 +0000806
807/*------------------------------------------------------------*/
808/*--- Exported global variables ---*/
809/*------------------------------------------------------------*/
810
811extern CommandLineOptions CLG_(clo);
812extern Statistics CLG_(stat);
813extern EventMapping* CLG_(dumpmap);
814
815/* Function active counter array, indexed by function number */
816extern UInt* CLG_(fn_active_array);
817extern Bool CLG_(instrument_state);
818
819extern call_stack CLG_(current_call_stack);
820extern fn_stack CLG_(current_fn_stack);
821extern exec_state CLG_(current_state);
822extern ThreadId CLG_(current_tid);
823
824
825/*------------------------------------------------------------*/
826/*--- Debug output ---*/
827/*------------------------------------------------------------*/
828
829#if CLG_ENABLE_DEBUG
830
831#define CLG_DEBUGIF(x) \
832 if ( (CLG_(clo).verbose >x) && \
833 (CLG_(stat).bb_executions >= CLG_(clo).verbose_start))
834
835#define CLG_DEBUG(x,format,args...) \
836 CLG_DEBUGIF(x) { \
837 CLG_(print_bbno)(); \
838 VG_(printf)(format,##args); \
839 }
840
841#define CLG_ASSERT(cond) \
842 if (!(cond)) { \
843 CLG_(print_context)(); \
844 CLG_(print_bbno)(); \
845 tl_assert(cond); \
846 }
847
848#else
849#define CLG_DEBUGIF(x) if (0)
850#define CLG_DEBUG(x...) {}
851#define CLG_ASSERT(cond) tl_assert(cond);
852#endif
853
854/* from debug.c */
855void CLG_(print_bbno)(void);
856void CLG_(print_context)(void);
857void CLG_(print_jcc)(int s, jCC* jcc);
weidendo09ee78e2009-02-24 12:26:53 +0000858void CLG_(print_bbcc)(int s, BBCC* bbcc);
weidendoa17f2a32006-03-20 10:27:30 +0000859void CLG_(print_bbcc_fn)(BBCC* bbcc);
860void CLG_(print_execstate)(int s, exec_state* es);
861void CLG_(print_eventset)(int s, EventSet* es);
862void CLG_(print_cost)(int s, EventSet*, ULong* cost);
863void CLG_(print_bb)(int s, BB* bb);
864void CLG_(print_bbcc_cost)(int s, BBCC*);
865void CLG_(print_cxt)(int s, Context* cxt, int rec_index);
866void CLG_(print_short_jcc)(jCC* jcc);
867void CLG_(print_stackentry)(int s, int sp);
868void CLG_(print_addr)(Addr addr);
869void CLG_(print_addr_ln)(Addr addr);
870
sewardj9c606bd2008-09-18 18:12:50 +0000871void* CLG_(malloc)(HChar* cc, UWord s, char* f);
weidendoa17f2a32006-03-20 10:27:30 +0000872void* CLG_(free)(void* p, char* f);
873#if 0
sewardj9c606bd2008-09-18 18:12:50 +0000874#define CLG_MALLOC(_cc,x) CLG_(malloc)((_cc),x,__FUNCTION__)
875#define CLG_FREE(p) CLG_(free)(p,__FUNCTION__)
weidendoa17f2a32006-03-20 10:27:30 +0000876#else
sewardj9c606bd2008-09-18 18:12:50 +0000877#define CLG_MALLOC(_cc,x) VG_(malloc)((_cc),x)
878#define CLG_FREE(p) VG_(free)(p)
weidendoa17f2a32006-03-20 10:27:30 +0000879#endif
880
881#endif /* CLG_GLOBAL */