blob: 7c5a59ce9efcc8bb5f2e4ee1c2f683f66d50e7d5 [file] [log] [blame]
weidendoa17f2a32006-03-20 10:27:30 +00001/*--------------------------------------------------------------------*/
weidendof247f492014-08-22 08:38:40 +00002/*--- Callgrind data structures, functions. global.h ---*/
weidendoa17f2a32006-03-20 10:27:30 +00003/*--------------------------------------------------------------------*/
4
weidendof247f492014-08-22 08:38:40 +00005/*
6 This file is part of Valgrind, a dynamic binary instrumentation
7 framework.
8
Elliott Hughesed398002017-06-21 14:41:24 -07009 Copyright (C) 2004-2017 Josef Weidendorfer
weidendof247f492014-08-22 08:38:40 +000010 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
weidendoa17f2a32006-03-20 10:27:30 +000030#ifndef CLG_GLOBAL
31#define CLG_GLOBAL
32
33#include "pub_tool_basics.h"
sewardj4cfea4f2006-10-14 19:26:10 +000034#include "pub_tool_vki.h"
weidendoa17f2a32006-03-20 10:27:30 +000035#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"
sewardj14c7cc52007-02-25 15:08:24 +000045#include "pub_tool_xarray.h"
weidendoa17f2a32006-03-20 10:27:30 +000046#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/*------------------------------------------------------------*/
weidendof247f492014-08-22 08:38:40 +000054/*--- Callgrind compile options --- */
weidendoa17f2a32006-03-20 10:27:30 +000055/*------------------------------------------------------------*/
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
weidendoa17f2a32006-03-20 10:27:30 +000067
68
69/*------------------------------------------------------------*/
70/*--- Command line options ---*/
71/*------------------------------------------------------------*/
72
weidendocbf4e192007-11-27 01:27:12 +000073#define DEFAULT_OUTFORMAT "callgrind.out.%p"
weidendoa17f2a32006-03-20 10:27:30 +000074
75typedef struct _CommandLineOptions CommandLineOptions;
76struct _CommandLineOptions {
77
78 /* Dump format options */
florian25f6c572012-10-21 02:55:56 +000079 const HChar* out_format; /* Format string for callgrind output file name */
80 Bool combine_dumps; /* Dump trace parts into same file? */
weidendoa17f2a32006-03-20 10:27:30 +000081 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 */
weidendo9e326b72006-03-31 13:16:15 +000092 ULong dump_every_bb; /* Dump every xxx BBs. */
weidendoa17f2a32006-03-20 10:27:30 +000093
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
weidendoaeb86222010-06-09 22:33:02 +0000107 Bool collect_bus; /* Collect global bus events */
108
weidendoa17f2a32006-03-20 10:27:30 +0000109 /* Instrument options */
110 Bool instrument_atstart; /* Instrument at start? */
111 Bool simulate_cache; /* Call into cache simulator ? */
weidendo320705f2010-07-02 19:56:23 +0000112 Bool simulate_branch; /* Call into branch prediction simulator ? */
weidendoa17f2a32006-03-20 10:27:30 +0000113
weidendoa762b0f2006-05-01 00:55:54 +0000114 /* Call graph generation */
115 Bool pop_on_jump; /* Handle a jump between functions as ret+call */
116
weidendoa17f2a32006-03-20 10:27:30 +0000117#if CLG_ENABLE_DEBUG
118 Int verbose;
119 ULong verbose_start;
120#endif
121};
122
123/*------------------------------------------------------------*/
124/*--- Constants ---*/
125/*------------------------------------------------------------*/
126
weidendoa17f2a32006-03-20 10:27:30 +0000127/* Minimum cache line size allowed */
128#define MIN_LINE_SIZE 16
129
weidendoa17f2a32006-03-20 10:27:30 +0000130
weidendoa17f2a32006-03-20 10:27:30 +0000131/*------------------------------------------------------------*/
132/*--- Statistics ---*/
133/*------------------------------------------------------------*/
134
135typedef struct _Statistics Statistics;
136struct _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
180typedef struct _Context Context;
181typedef struct _CC CC;
182typedef struct _BB BB;
weidendoa17f2a32006-03-20 10:27:30 +0000183typedef struct _BBCC BBCC;
184typedef struct _jCC jCC;
185typedef struct _fCC fCC;
186typedef struct _fn_node fn_node;
187typedef struct _file_node file_node;
188typedef struct _obj_node obj_node;
189typedef struct _fn_config fn_config;
190typedef struct _call_entry call_entry;
191typedef struct _thread_info thread_info;
192
193/* Costs of event sets. Aliases to arrays of 64-bit values */
194typedef ULong* SimCost; /* All events the simulator can produce */
195typedef ULong* UserCost;
196typedef ULong* FullCost; /* Simulator + User */
197
198
weidendo28a23c02011-11-14 21:16:25 +0000199/* The types of control flow changes that can happen between
200 * execution of two BBs in a thread.
201 */
202typedef 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
weidendoa17f2a32006-03-20 10:27:30 +0000211/* 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 */
weidendoa17f2a32006-03-20 10:27:30 +0000230
231struct _jCC {
weidendo28a23c02011-11-14 21:16:25 +0000232 ClgJumpKind jmpkind; /* jk_Call, jk_Jump, jk_CondJump */
weidendoa17f2a32006-03-20 10:27:30 +0000233 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 */
247typedef struct _InstrInfo InstrInfo;
248struct _InstrInfo {
249 UInt instr_offset;
250 UInt instr_size;
weidendoa17f2a32006-03-20 10:27:30 +0000251 UInt cost_offset;
252 EventSet* eventset;
253};
254
255
weidendo28a23c02011-11-14 21:16:25 +0000256
weidendoa17f2a32006-03-20 10:27:30 +0000257/*
weidendo28a23c02011-11-14 21:16:25 +0000258 * Info for a side exit in a BB
weidendoa17f2a32006-03-20 10:27:30 +0000259 */
260typedef struct _CJmpInfo CJmpInfo;
261struct _CJmpInfo {
weidendo28a23c02011-11-14 21:16:25 +0000262 UInt instr; /* instruction index for BB.instr array */
263 ClgJumpKind jmpkind; /* jump kind when leaving BB at this side exit */
weidendoa17f2a32006-03-20 10:27:30 +0000264};
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 */
283struct _BB {
284 obj_node* obj; /* ELF object of BB */
njnc4431bf2009-01-15 21:29:24 +0000285 PtrdiffT offset; /* offset of BB in ELF object file */
weidendoa17f2a32006-03-20 10:27:30 +0000286 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 */
weidendo28a23c02011-11-14 21:16:25 +0000300 UInt cjmp_count; /* number of side exits */
weidendoa17f2a32006-03-20 10:27:30 +0000301 CJmpInfo* jmp; /* array of info for condition jumps,
302 * allocated directly after this struct */
weidendo28a23c02011-11-14 21:16:25 +0000303 Bool cjmp_inverted; /* is last side exit actually fall through? */
weidendoa17f2a32006-03-20 10:27:30 +0000304
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 */
327struct _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/*
weidendo28a23c02011-11-14 21:16:25 +0000337 * Cost info for a side exits from a BB
weidendoa17f2a32006-03-20 10:27:30 +0000338 */
339typedef struct _JmpData JmpData;
340struct _JmpData {
341 ULong ecounter; /* number of times the BB was left at this exit */
weidendo28a23c02011-11-14 21:16:25 +0000342 jCC* jcc_list; /* JCCs used for this exit */
weidendoa17f2a32006-03-20 10:27:30 +0000343};
344
345
346/*
347 * Basic Block Cost Center
348 *
349 * On demand, multiple BBCCs will be created for the same BB
Elliott Hughesed398002017-06-21 14:41:24 -0700350 * dependent on command line options and:
weidendoa17f2a32006-03-20 10:27:30 +0000351 * - 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 */
361struct _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
391struct _fn_node {
florian19f91bb2012-11-10 22:29:54 +0000392 HChar* name;
weidendoa17f2a32006-03-20 10:27:30 +0000393 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
weidendoa17f2a32006-03-20 10:27:30 +0000423
424struct _file_node {
florian19f91bb2012-11-10 22:29:54 +0000425 HChar* name;
weidendoa17f2a32006-03-20 10:27:30 +0000426 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 */
436struct _obj_node {
florian25f6c572012-10-21 02:55:56 +0000437 const HChar* name;
weidendoa17f2a32006-03-20 10:27:30 +0000438 UInt last_slash_pos;
439
440 Addr start; /* Start address of text segment mapping */
441 SizeT size; /* Length of mapping */
njnc4431bf2009-01-15 21:29:24 +0000442 PtrdiffT offset; /* Offset between symbol address and file offset */
weidendoa17f2a32006-03-20 10:27:30 +0000443
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 */
456struct _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 */
479typedef struct _exec_state exec_state;
480struct _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
weidendod7341d82013-11-12 15:32:58 +0000493 /* number of conditional jumps passed in last BB */
494 Int jmps_passed;
weidendoa17f2a32006-03-20 10:27:30 +0000495 BBCC* bbcc; /* last BB executed */
496 BBCC* nonskipped;
497
498 Int call_stack_bottom; /* Index into fn_stack */
499};
500
501/* Global state structures */
502typedef struct _bb_hash bb_hash;
503struct _bb_hash {
504 UInt size, entries;
505 BB** table;
506};
507
508typedef struct _cxt_hash cxt_hash;
509struct _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 */
518typedef struct _bbcc_hash bbcc_hash;
519struct _bbcc_hash {
520 UInt size, entries;
521 BBCC** table;
522};
523
524typedef struct _jcc_hash jcc_hash;
525struct _jcc_hash {
526 UInt size, entries;
527 jCC** table;
528 jCC* spontaneous;
529};
530
531typedef struct _fn_array fn_array;
532struct _fn_array {
533 UInt size;
534 UInt* array;
535};
536
537typedef struct _call_stack call_stack;
538struct _call_stack {
539 UInt size;
540 Int sp;
541 call_entry* entry;
542};
543
544typedef struct _fn_stack fn_stack;
545struct _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
555typedef struct _exec_stack exec_stack;
556struct _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 */
570struct _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 */
594typedef struct _AddrPos AddrPos;
595struct _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 */
603typedef struct _AddrCost AddrCost;
604struct _AddrCost {
605 AddrPos p;
606 SimCost cost;
607};
608
609/* A function in an execution context */
610typedef struct _FnPos FnPos;
611struct _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
624struct cachesim_if
625{
626 void (*print_opts)(void);
florian19f91bb2012-11-10 22:29:54 +0000627 Bool (*parse_opt)(const HChar* arg);
weidendoa17f2a32006-03-20 10:27:30 +0000628 void (*post_clo_init)(void);
629 void (*clear)(void);
floriancc9480f2014-11-11 20:46:34 +0000630 void (*dump_desc)(VgFile *fp);
weidendo320705f2010-07-02 19:56:23 +0000631 void (*printstat)(Int,Int,Int);
weidendoa17f2a32006-03-20 10:27:30 +0000632 void (*add_icost)(SimCost, BBCC*, InstrInfo*, ULong);
weidendoa17f2a32006-03-20 10:27:30 +0000633 void (*finish)(void);
634
635 void (*log_1I0D)(InstrInfo*) VG_REGPARM(1);
weidendo0a1951d2009-06-15 00:16:36 +0000636 void (*log_2I0D)(InstrInfo*, InstrInfo*) VG_REGPARM(2);
637 void (*log_3I0D)(InstrInfo*, InstrInfo*, InstrInfo*) VG_REGPARM(3);
weidendoa17f2a32006-03-20 10:27:30 +0000638
weidendo0a1951d2009-06-15 00:16:36 +0000639 void (*log_1I1Dr)(InstrInfo*, Addr, Word) VG_REGPARM(3);
640 void (*log_1I1Dw)(InstrInfo*, Addr, Word) VG_REGPARM(3);
weidendoa17f2a32006-03-20 10:27:30 +0000641
weidendo0a1951d2009-06-15 00:16:36 +0000642 void (*log_0I1Dr)(InstrInfo*, Addr, Word) VG_REGPARM(3);
643 void (*log_0I1Dw)(InstrInfo*, Addr, Word) VG_REGPARM(3);
weidendoa17f2a32006-03-20 10:27:30 +0000644
645 // function names of helpers (for debugging generated code)
florian25f6c572012-10-21 02:55:56 +0000646 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;
weidendoa17f2a32006-03-20 10:27:30 +0000649};
650
weidendo5bba5252010-06-09 22:32:53 +0000651// Event groups
652#define EG_USE 0
653#define EG_IR 1
654#define EG_DR 2
655#define EG_DW 3
weidendo320705f2010-07-02 19:56:23 +0000656#define EG_BC 4
657#define EG_BI 5
658#define EG_BUS 6
659#define EG_ALLOC 7
660#define EG_SYS 8
weidendo5bba5252010-06-09 22:32:53 +0000661
662struct event_sets {
663 EventSet *base, *full;
664};
weidendo5bba5252010-06-09 22:32:53 +0000665
666#define fullOffset(group) (CLG_(sets).full->offset[group])
667
weidendoa17f2a32006-03-20 10:27:30 +0000668
669/*------------------------------------------------------------*/
670/*--- Functions ---*/
671/*------------------------------------------------------------*/
672
673/* from clo.c */
674
675void CLG_(set_clo_defaults)(void);
676void CLG_(update_fn_config)(fn_node*);
florian19f91bb2012-11-10 22:29:54 +0000677Bool CLG_(process_cmd_line_option)(const HChar*);
weidendoa17f2a32006-03-20 10:27:30 +0000678void CLG_(print_usage)(void);
679void CLG_(print_debug_usage)(void);
680
681/* from sim.c */
weidendo5bba5252010-06-09 22:32:53 +0000682void CLG_(init_eventsets)(void);
weidendoa17f2a32006-03-20 10:27:30 +0000683
684/* from main.c */
florian10ef7252014-10-27 12:06:35 +0000685Bool CLG_(get_debug_info)(Addr, const HChar **dirname,
686 const HChar **filename,
florian536e3d72014-10-26 19:16:14 +0000687 const HChar **fn_name, UInt*, DebugInfo**);
sewardj0b9d74a2006-12-24 02:24:11 +0000688void CLG_(collectBlockInfo)(IRSB* bbIn, UInt*, UInt*, Bool*);
florian25f6c572012-10-21 02:55:56 +0000689void CLG_(set_instrument_state)(const HChar*,Bool);
690void CLG_(dump_profile)(const HChar* trigger,Bool only_current_thread);
weidendoa17f2a32006-03-20 10:27:30 +0000691void CLG_(zero_all_cost)(Bool only_current_thread);
692Int CLG_(get_dump_counter)(void);
693void CLG_(fini)(Int exitcode);
694
weidendoa17f2a32006-03-20 10:27:30 +0000695/* from bb.c */
696void CLG_(init_bb_hash)(void);
697bb_hash* CLG_(get_bb_hash)(void);
sewardj0b9d74a2006-12-24 02:24:11 +0000698BB* CLG_(get_bb)(Addr addr, IRSB* bb_in, Bool *seen_before);
weidendoa17f2a32006-03-20 10:27:30 +0000699void CLG_(delete_bb)(Addr addr);
700
701static __inline__ Addr bb_addr(BB* bb)
702 { return bb->offset + bb->obj->offset; }
703static __inline__ Addr bb_jmpaddr(BB* bb)
weidendo90741fb2006-09-12 19:10:08 +0000704 { UInt off = (bb->instr_count > 0) ? bb->instr[bb->instr_count-1].instr_offset : 0;
705 return off + bb->offset + bb->obj->offset; }
weidendoa17f2a32006-03-20 10:27:30 +0000706
707/* from fn.c */
708void CLG_(init_fn_array)(fn_array*);
709void CLG_(copy_current_fn_array)(fn_array* dst);
710fn_array* CLG_(get_current_fn_array)(void);
711void CLG_(set_current_fn_array)(fn_array*);
712UInt* CLG_(get_fn_entry)(Int n);
713
714void CLG_(init_obj_table)(void);
sewardjb8b79ad2008-03-03 01:35:41 +0000715obj_node* CLG_(get_obj_node)(DebugInfo* si);
florian536e3d72014-10-26 19:16:14 +0000716file_node* CLG_(get_file_node)(obj_node*, const HChar *dirname,
717 const HChar* filename);
weidendoa17f2a32006-03-20 10:27:30 +0000718fn_node* CLG_(get_fn_node)(BB* bb);
719
720/* from bbcc.c */
721void CLG_(init_bbcc_hash)(bbcc_hash* bbccs);
722void CLG_(copy_current_bbcc_hash)(bbcc_hash* dst);
723bbcc_hash* CLG_(get_current_bbcc_hash)(void);
724void CLG_(set_current_bbcc_hash)(bbcc_hash*);
725void CLG_(forall_bbccs)(void (*func)(BBCC*));
726void CLG_(zero_bbcc)(BBCC* bbcc);
727BBCC* CLG_(get_bbcc)(BB* bb);
728BBCC* CLG_(clone_bbcc)(BBCC* orig, Context* cxt, Int rec_index);
729void CLG_(setup_bbcc)(BB* bb) VG_REGPARM(1);
730
731
732/* from jumps.c */
733void CLG_(init_jcc_hash)(jcc_hash*);
734void CLG_(copy_current_jcc_hash)(jcc_hash* dst);
weidendoa17f2a32006-03-20 10:27:30 +0000735void CLG_(set_current_jcc_hash)(jcc_hash*);
736jCC* CLG_(get_jcc)(BBCC* from, UInt, BBCC* to);
737
738/* from callstack.c */
739void CLG_(init_call_stack)(call_stack*);
740void CLG_(copy_current_call_stack)(call_stack* dst);
741void CLG_(set_current_call_stack)(call_stack*);
742call_entry* CLG_(get_call_entry)(Int n);
743
744void CLG_(push_call_stack)(BBCC* from, UInt jmp, BBCC* to, Addr sp, Bool skip);
745void CLG_(pop_call_stack)(void);
weidendoc63ca372011-02-04 20:50:58 +0000746Int CLG_(unwind_call_stack)(Addr sp, Int);
weidendoa17f2a32006-03-20 10:27:30 +0000747
748/* from context.c */
749void CLG_(init_fn_stack)(fn_stack*);
750void CLG_(copy_current_fn_stack)(fn_stack*);
weidendoa17f2a32006-03-20 10:27:30 +0000751void CLG_(set_current_fn_stack)(fn_stack*);
752
753void CLG_(init_cxt_table)(void);
weidendoa17f2a32006-03-20 10:27:30 +0000754Context* CLG_(get_cxt)(fn_node** fn);
755void CLG_(push_cxt)(fn_node* fn);
756
757/* from threads.c */
758void CLG_(init_threads)(void);
759thread_info** CLG_(get_threads)(void);
760thread_info* CLG_(get_current_thread)(void);
761void CLG_(switch_thread)(ThreadId tid);
762void CLG_(forall_threads)(void (*func)(thread_info*));
763void CLG_(run_thread)(ThreadId tid);
764
765void CLG_(init_exec_state)(exec_state* es);
766void CLG_(init_exec_stack)(exec_stack*);
767void CLG_(copy_current_exec_stack)(exec_stack*);
768void CLG_(set_current_exec_stack)(exec_stack*);
769void CLG_(pre_signal)(ThreadId tid, Int sigNum, Bool alt_stack);
770void CLG_(post_signal)(ThreadId tid, Int sigNum);
771void CLG_(run_post_signal_on_call_stack_bottom)(void);
772
773/* from dump.c */
weidendo4ce5e792006-09-20 21:29:39 +0000774void CLG_(init_dumps)(void);
weidendoa17f2a32006-03-20 10:27:30 +0000775
776/*------------------------------------------------------------*/
777/*--- Exported global variables ---*/
778/*------------------------------------------------------------*/
779
780extern CommandLineOptions CLG_(clo);
781extern Statistics CLG_(stat);
782extern EventMapping* CLG_(dumpmap);
783
784/* Function active counter array, indexed by function number */
785extern UInt* CLG_(fn_active_array);
786extern Bool CLG_(instrument_state);
sewardjc4ebde32012-06-03 23:10:55 +0000787 /* min of L1 and LL cache line sizes */
788extern Int CLG_(min_line_size);
weidendoa17f2a32006-03-20 10:27:30 +0000789extern call_stack CLG_(current_call_stack);
790extern fn_stack CLG_(current_fn_stack);
791extern exec_state CLG_(current_state);
792extern ThreadId CLG_(current_tid);
florian2f938d02014-08-18 15:36:29 +0000793extern FullCost CLG_(total_cost);
794extern struct cachesim_if CLG_(cachesim);
795extern struct event_sets CLG_(sets);
796
797// set by setup_bbcc at start of every BB, and needed by log_* helpers
798extern Addr CLG_(bb_base);
799extern ULong* CLG_(cost_base);
weidendoa17f2a32006-03-20 10:27:30 +0000800
801
802/*------------------------------------------------------------*/
803/*--- Debug output ---*/
804/*------------------------------------------------------------*/
805
806#if CLG_ENABLE_DEBUG
807
808#define CLG_DEBUGIF(x) \
philippe1fda5fe2012-09-02 20:26:23 +0000809 if (UNLIKELY( (CLG_(clo).verbose >x) && \
810 (CLG_(stat).bb_executions >= CLG_(clo).verbose_start)))
weidendoa17f2a32006-03-20 10:27:30 +0000811
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) \
philippe1fda5fe2012-09-02 20:26:23 +0000819 if (UNLIKELY(!(cond))) { \
weidendoa17f2a32006-03-20 10:27:30 +0000820 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 */
832void CLG_(print_bbno)(void);
833void CLG_(print_context)(void);
834void CLG_(print_jcc)(int s, jCC* jcc);
weidendo09ee78e2009-02-24 12:26:53 +0000835void CLG_(print_bbcc)(int s, BBCC* bbcc);
weidendoa17f2a32006-03-20 10:27:30 +0000836void CLG_(print_bbcc_fn)(BBCC* bbcc);
837void CLG_(print_execstate)(int s, exec_state* es);
838void CLG_(print_eventset)(int s, EventSet* es);
839void CLG_(print_cost)(int s, EventSet*, ULong* cost);
840void CLG_(print_bb)(int s, BB* bb);
841void CLG_(print_bbcc_cost)(int s, BBCC*);
842void CLG_(print_cxt)(int s, Context* cxt, int rec_index);
843void CLG_(print_short_jcc)(jCC* jcc);
844void CLG_(print_stackentry)(int s, int sp);
845void CLG_(print_addr)(Addr addr);
846void CLG_(print_addr_ln)(Addr addr);
847
florian19f91bb2012-11-10 22:29:54 +0000848void* CLG_(malloc)(const HChar* cc, UWord s, const HChar* f);
849void* CLG_(free)(void* p, const HChar* f);
weidendoa17f2a32006-03-20 10:27:30 +0000850#if 0
sewardj9c606bd2008-09-18 18:12:50 +0000851#define CLG_MALLOC(_cc,x) CLG_(malloc)((_cc),x,__FUNCTION__)
852#define CLG_FREE(p) CLG_(free)(p,__FUNCTION__)
weidendoa17f2a32006-03-20 10:27:30 +0000853#else
sewardj9c606bd2008-09-18 18:12:50 +0000854#define CLG_MALLOC(_cc,x) VG_(malloc)((_cc),x)
855#define CLG_FREE(p) VG_(free)(p)
weidendoa17f2a32006-03-20 10:27:30 +0000856#endif
857
858#endif /* CLG_GLOBAL */