drd: Add a consistency check
[valgrind.git] / callgrind / clo.c
blob5ee62e6ce2906c692650161445d706e70dc7d109
1 /*
2 This file is part of Callgrind, a Valgrind tool for call graph
3 profiling programs.
5 Copyright (C) 2002-2013, Josef Weidendorfer (Josef.Weidendorfer@gmx.de)
7 This tool is derived from and contains lot of code from Cachegrind
8 Copyright (C) 2002-2013 Nicholas Nethercote (njn@valgrind.org)
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 02111-1307, USA.
25 The GNU General Public License is contained in the file COPYING.
28 #include "config.h" // for VG_PREFIX
30 #include "global.h"
34 /*------------------------------------------------------------*/
35 /*--- Function specific configuration options ---*/
36 /*------------------------------------------------------------*/
38 /* Special value for separate_callers: automatic = adaptive */
39 #define CONFIG_AUTO -1
41 #define CONFIG_DEFAULT -1
42 #define CONFIG_FALSE 0
43 #define CONFIG_TRUE 1
45 /* Logging configuration for a function */
46 struct _fn_config {
47 Int dump_before;
48 Int dump_after;
49 Int zero_before;
50 Int toggle_collect;
52 Int skip; /* Handle CALL to this function as JMP (= Skip)? */
53 Int group; /* don't change caller dependency inside group !=0 */
54 Int pop_on_jump;
56 Int separate_callers; /* separate logging dependent on caller */
57 Int separate_recursions; /* separate logging of rec. levels */
59 #if CLG_ENABLE_DEBUG
60 Int verbosity; /* Change debug verbosity level while in function */
61 #endif
64 /* Configurations for function name prefix patterns.
65 * Currently, only very limit patterns are possible:
66 * Exact prefix patterns and "*::" are allowed.
67 * E.g.
68 * - "abc" matches all functions starting with "abc".
69 * - "abc*::def" matches all functions starting with "abc" and
70 * starting with "def" after the first "::" separator.
71 * - "*::print(" matches C++ methods "print" in all classes
72 * without namespace. I.e. "*" doesn't match a "::".
74 * We build a trie from patterns, and for a given function, we
75 * go down the tree and apply all non-default configurations.
79 #define NODE_DEGREE 30
81 /* node of compressed trie search structure */
82 typedef struct _config_node config_node;
83 struct _config_node {
84 Int length;
86 fn_config* config;
87 config_node* sub_node[NODE_DEGREE];
88 config_node* next;
89 config_node* wild_star;
90 config_node* wild_char;
92 HChar name[1];
95 /* root of trie */
96 static config_node* fn_configs = 0;
98 static __inline__
99 fn_config* new_fnc(void)
101 fn_config* fnc = (fn_config*) CLG_MALLOC("cl.clo.nf.1",
102 sizeof(fn_config));
104 fnc->dump_before = CONFIG_DEFAULT;
105 fnc->dump_after = CONFIG_DEFAULT;
106 fnc->zero_before = CONFIG_DEFAULT;
107 fnc->toggle_collect = CONFIG_DEFAULT;
108 fnc->skip = CONFIG_DEFAULT;
109 fnc->pop_on_jump = CONFIG_DEFAULT;
110 fnc->group = CONFIG_DEFAULT;
111 fnc->separate_callers = CONFIG_DEFAULT;
112 fnc->separate_recursions = CONFIG_DEFAULT;
114 #if CLG_ENABLE_DEBUG
115 fnc->verbosity = CONFIG_DEFAULT;
116 #endif
118 return fnc;
122 static config_node* new_config(const HChar* name, int length)
124 int i;
125 config_node* node = (config_node*) CLG_MALLOC("cl.clo.nc.1",
126 sizeof(config_node) + length);
128 for(i=0;i<length;i++) {
129 if (name[i] == 0) break;
130 node->name[i] = name[i];
132 node->name[i] = 0;
134 node->length = length;
135 node->config = 0;
136 for(i=0;i<NODE_DEGREE;i++)
137 node->sub_node[i] = 0;
138 node->next = 0;
139 node->wild_char = 0;
140 node->wild_star = 0;
142 CLG_DEBUG(3, " new_config('%s', len %d)\n", node->name, length);
144 return node;
147 static __inline__
148 Bool is_wild(HChar n)
150 return (n == '*') || (n == '?');
153 /* Recursively build up function matching tree (prefix tree).
154 * Returns function config object for pattern <name>
155 * and starting at tree node <*pnode>.
157 * Tree nodes (config_node) are created as needed,
158 * tree root is stored into <*pnode>, and the created
159 * leaf (fn_config) for the given pattern is returned.
161 static fn_config* get_fnc2(config_node* node, const HChar* name)
163 config_node *new_sub, *n, *nprev;
164 int offset, len;
166 CLG_DEBUG(3, " get_fnc2(%p, '%s')\n", node, name);
168 if (name[0] == 0) {
169 if (!node->config) node->config = new_fnc();
170 return node->config;
173 if (is_wild(*name)) {
174 if (*name == '*') {
175 while(name[1] == '*') name++;
176 new_sub = node->wild_star;
178 else
179 new_sub = node->wild_char;
181 if (!new_sub) {
182 new_sub = new_config(name, 1);
183 if (*name == '*')
184 node->wild_star = new_sub;
185 else
186 node->wild_char = new_sub;
189 return get_fnc2( new_sub, name+1);
192 n = node->sub_node[ name[0]%NODE_DEGREE ];
193 nprev = 0;
194 len = 0;
195 while(n) {
196 for(len=0; name[len] == n->name[len]; len++);
197 if (len>0) break;
198 nprev = n;
199 n = n->next;
202 if (!n) {
203 len = 1;
204 while(name[len] && (!is_wild(name[len]))) len++;
205 new_sub = new_config(name, len);
206 new_sub->next = node->sub_node[ name[0]%NODE_DEGREE ];
207 node->sub_node[ name[0]%NODE_DEGREE ] = new_sub;
209 if (name[len] == 0) {
210 new_sub->config = new_fnc();
211 return new_sub->config;
214 /* recurse on wildcard */
215 return get_fnc2( new_sub, name+len);
218 if (len < n->length) {
220 /* split up the subnode <n> */
221 config_node *new_node;
222 int i;
224 new_node = new_config(n->name, len);
225 if (nprev)
226 nprev->next = new_node;
227 else
228 node->sub_node[ n->name[0]%NODE_DEGREE ] = new_node;
229 new_node->next = n->next;
231 new_node->sub_node[ n->name[len]%NODE_DEGREE ] = n;
233 for(i=0, offset=len; offset < n->length; i++, offset++)
234 n->name[i] = n->name[offset];
235 n->name[i] = 0;
236 n->length = i;
238 name += len;
239 offset = 0;
240 while(name[offset] && (!is_wild(name[offset]))) offset++;
241 new_sub = new_config(name, offset);
242 /* this sub_node of new_node could already be set: chain! */
243 new_sub->next = new_node->sub_node[ name[0]%NODE_DEGREE ];
244 new_node->sub_node[ name[0]%NODE_DEGREE ] = new_sub;
246 if (name[offset]==0) {
247 new_sub->config = new_fnc();
248 return new_sub->config;
251 /* recurse on wildcard */
252 return get_fnc2( new_sub, name+offset);
255 name += n->length;
257 if (name[0] == 0) {
258 /* name and node name are the same */
259 if (!n->config) n->config = new_fnc();
260 return n->config;
263 offset = 1;
264 while(name[offset] && (!is_wild(name[offset]))) offset++;
266 new_sub = new_config(name, offset);
267 new_sub->next = n->sub_node[ name[0]%NODE_DEGREE ];
268 n->sub_node[ name[0]%NODE_DEGREE ] = new_sub;
270 return get_fnc2(new_sub, name+offset);
273 static void print_config_node(int depth, int hash, config_node* node)
275 config_node* n;
276 int i;
278 if (node != fn_configs) {
279 const HChar sp[] = " ";
281 if (depth>40) depth=40;
282 VG_(printf)("%s", sp+40-depth);
283 if (hash >=0) VG_(printf)(" [hash %2d]", hash);
284 else if (hash == -2) VG_(printf)(" [wildc ?]");
285 else if (hash == -3) VG_(printf)(" [wildc *]");
286 VG_(printf)(" '%s' (len %d)\n", node->name, node->length);
288 for(i=0;i<NODE_DEGREE;i++) {
289 n = node->sub_node[i];
290 while(n) {
291 print_config_node(depth+1, i, n);
292 n = n->next;
295 if (node->wild_char) print_config_node(depth+1, -2, node->wild_char);
296 if (node->wild_star) print_config_node(depth+1, -3, node->wild_star);
299 /* get a function config for a name pattern (from command line) */
300 static fn_config* get_fnc(const HChar* name)
302 fn_config* fnc;
304 CLG_DEBUG(3, " +get_fnc(%s)\n", name);
305 if (fn_configs == 0)
306 fn_configs = new_config(name, 0);
307 fnc = get_fnc2(fn_configs, name);
309 CLG_DEBUGIF(3) {
310 CLG_DEBUG(3, " -get_fnc(%s):\n", name);
311 print_config_node(3, -1, fn_configs);
313 return fnc;
318 static void update_fn_config1(fn_node* fn, fn_config* fnc)
320 if (fnc->dump_before != CONFIG_DEFAULT)
321 fn->dump_before = (fnc->dump_before == CONFIG_TRUE);
323 if (fnc->dump_after != CONFIG_DEFAULT)
324 fn->dump_after = (fnc->dump_after == CONFIG_TRUE);
326 if (fnc->zero_before != CONFIG_DEFAULT)
327 fn->zero_before = (fnc->zero_before == CONFIG_TRUE);
329 if (fnc->toggle_collect != CONFIG_DEFAULT)
330 fn->toggle_collect = (fnc->toggle_collect == CONFIG_TRUE);
332 if (fnc->skip != CONFIG_DEFAULT)
333 fn->skip = (fnc->skip == CONFIG_TRUE);
335 if (fnc->pop_on_jump != CONFIG_DEFAULT)
336 fn->pop_on_jump = (fnc->pop_on_jump == CONFIG_TRUE);
338 if (fnc->group != CONFIG_DEFAULT)
339 fn->group = fnc->group;
341 if (fnc->separate_callers != CONFIG_DEFAULT)
342 fn->separate_callers = fnc->separate_callers;
344 if (fnc->separate_recursions != CONFIG_DEFAULT)
345 fn->separate_recursions = fnc->separate_recursions;
347 #if CLG_ENABLE_DEBUG
348 if (fnc->verbosity != CONFIG_DEFAULT)
349 fn->verbosity = fnc->verbosity;
350 #endif
353 /* Recursively go down the function matching tree,
354 * looking for a match to <name>. For every matching leaf,
355 * <fn> is updated with the pattern config.
357 static void update_fn_config2(fn_node* fn, const HChar* name,
358 config_node* node)
360 config_node* n;
362 CLG_DEBUG(3, " update_fn_config2('%s', node '%s'): \n",
363 name, node->name);
364 if ((*name == 0) && node->config) {
365 CLG_DEBUG(3, " found!\n");
366 update_fn_config1(fn, node->config);
367 return;
370 n = node->sub_node[ name[0]%NODE_DEGREE ];
371 while(n) {
372 if (VG_(strncmp)(name, n->name, n->length)==0) break;
373 n = n->next;
375 if (n) {
376 CLG_DEBUG(3, " '%s' matching at hash %d\n",
377 n->name, name[0]%NODE_DEGREE);
378 update_fn_config2(fn, name+n->length, n);
381 if (node->wild_char) {
382 CLG_DEBUG(3, " skip '%c' for wildcard '?'\n", *name);
383 update_fn_config2(fn, name+1, node->wild_char);
386 if (node->wild_star) {
387 CLG_DEBUG(3, " wildcard '*'\n");
388 while(*name) {
389 update_fn_config2(fn, name, node->wild_star);
390 name++;
392 update_fn_config2(fn, name, node->wild_star);
396 /* Update function config according to configs of name prefixes */
397 void CLG_(update_fn_config)(fn_node* fn)
399 CLG_DEBUG(3, " update_fn_config('%s')\n", fn->name);
400 if (fn_configs)
401 update_fn_config2(fn, fn->name, fn_configs);
405 /*--------------------------------------------------------------------*/
406 /*--- Command line processing ---*/
407 /*--------------------------------------------------------------------*/
409 Bool CLG_(process_cmd_line_option)(const HChar* arg)
411 const HChar* tmp_str;
413 if VG_BOOL_CLO(arg, "--skip-plt", CLG_(clo).skip_plt) {}
415 else if VG_BOOL_CLO(arg, "--collect-jumps", CLG_(clo).collect_jumps) {}
416 /* compatibility alias, deprecated option */
417 else if VG_BOOL_CLO(arg, "--trace-jump", CLG_(clo).collect_jumps) {}
419 else if VG_BOOL_CLO(arg, "--combine-dumps", CLG_(clo).combine_dumps) {}
421 else if VG_BOOL_CLO(arg, "--collect-atstart", CLG_(clo).collect_atstart) {}
423 else if VG_BOOL_CLO(arg, "--instr-atstart", CLG_(clo).instrument_atstart) {}
425 else if VG_BOOL_CLO(arg, "--separate-threads", CLG_(clo).separate_threads) {}
427 else if VG_BOOL_CLO(arg, "--compress-strings", CLG_(clo).compress_strings) {}
428 else if VG_BOOL_CLO(arg, "--compress-mangled", CLG_(clo).compress_mangled) {}
429 else if VG_BOOL_CLO(arg, "--compress-pos", CLG_(clo).compress_pos) {}
431 else if VG_STR_CLO(arg, "--fn-skip", tmp_str) {
432 fn_config* fnc = get_fnc(tmp_str);
433 fnc->skip = CONFIG_TRUE;
436 else if VG_STR_CLO(arg, "--dump-before", tmp_str) {
437 fn_config* fnc = get_fnc(tmp_str);
438 fnc->dump_before = CONFIG_TRUE;
441 else if VG_STR_CLO(arg, "--zero-before", tmp_str) {
442 fn_config* fnc = get_fnc(tmp_str);
443 fnc->zero_before = CONFIG_TRUE;
446 else if VG_STR_CLO(arg, "--dump-after", tmp_str) {
447 fn_config* fnc = get_fnc(tmp_str);
448 fnc->dump_after = CONFIG_TRUE;
451 else if VG_STR_CLO(arg, "--toggle-collect", tmp_str) {
452 fn_config* fnc = get_fnc(tmp_str);
453 fnc->toggle_collect = CONFIG_TRUE;
454 /* defaults to initial collection off */
455 CLG_(clo).collect_atstart = False;
458 else if VG_INT_CLO(arg, "--separate-recs", CLG_(clo).separate_recursions) {}
460 /* change handling of a jump between functions to ret+call */
461 else if VG_XACT_CLO(arg, "--pop-on-jump", CLG_(clo).pop_on_jump, True) {}
462 else if VG_STR_CLO( arg, "--pop-on-jump", tmp_str) {
463 fn_config* fnc = get_fnc(tmp_str);
464 fnc->pop_on_jump = CONFIG_TRUE;
467 #if CLG_ENABLE_DEBUG
468 else if VG_INT_CLO(arg, "--ct-verbose", CLG_(clo).verbose) {}
469 else if VG_INT_CLO(arg, "--ct-vstart", CLG_(clo).verbose_start) {}
471 else if VG_STREQN(12, arg, "--ct-verbose") {
472 fn_config* fnc;
473 HChar* s;
474 UInt n = VG_(strtoll10)(arg+12, &s);
475 if ((n <= 0) || *s != '=') return False;
476 fnc = get_fnc(s+1);
477 fnc->verbosity = n;
479 #endif
481 else if VG_XACT_CLO(arg, "--separate-callers=auto",
482 CLG_(clo).separate_callers, CONFIG_AUTO) {}
483 else if VG_INT_CLO( arg, "--separate-callers",
484 CLG_(clo).separate_callers) {}
486 else if VG_STREQN(10, arg, "--fn-group") {
487 fn_config* fnc;
488 HChar* s;
489 UInt n = VG_(strtoll10)(arg+10, &s);
490 if ((n <= 0) || *s != '=') return False;
491 fnc = get_fnc(s+1);
492 fnc->group = n;
495 else if VG_STREQN(18, arg, "--separate-callers") {
496 fn_config* fnc;
497 HChar* s;
498 UInt n = VG_(strtoll10)(arg+18, &s);
499 if ((n <= 0) || *s != '=') return False;
500 fnc = get_fnc(s+1);
501 fnc->separate_callers = n;
504 else if VG_STREQN(15, arg, "--separate-recs") {
505 fn_config* fnc;
506 HChar* s;
507 UInt n = VG_(strtoll10)(arg+15, &s);
508 if ((n <= 0) || *s != '=') return False;
509 fnc = get_fnc(s+1);
510 fnc->separate_recursions = n;
513 else if VG_STR_CLO(arg, "--callgrind-out-file", CLG_(clo).out_format) {}
515 else if VG_BOOL_CLO(arg, "--mangle-names", CLG_(clo).mangle_names) {}
517 else if VG_BOOL_CLO(arg, "--skip-direct-rec",
518 CLG_(clo).skip_direct_recursion) {}
520 else if VG_BOOL_CLO(arg, "--dump-bbs", CLG_(clo).dump_bbs) {}
521 else if VG_BOOL_CLO(arg, "--dump-line", CLG_(clo).dump_line) {}
522 else if VG_BOOL_CLO(arg, "--dump-instr", CLG_(clo).dump_instr) {}
523 else if VG_BOOL_CLO(arg, "--dump-bb", CLG_(clo).dump_bb) {}
525 else if VG_INT_CLO( arg, "--dump-every-bb", CLG_(clo).dump_every_bb) {}
527 else if VG_BOOL_CLO(arg, "--collect-alloc", CLG_(clo).collect_alloc) {}
528 else if VG_BOOL_CLO(arg, "--collect-systime", CLG_(clo).collect_systime) {}
529 else if VG_BOOL_CLO(arg, "--collect-bus", CLG_(clo).collect_bus) {}
530 /* for option compatibility with cachegrind */
531 else if VG_BOOL_CLO(arg, "--cache-sim", CLG_(clo).simulate_cache) {}
532 /* compatibility alias, deprecated option */
533 else if VG_BOOL_CLO(arg, "--simulate-cache", CLG_(clo).simulate_cache) {}
534 /* for option compatibility with cachegrind */
535 else if VG_BOOL_CLO(arg, "--branch-sim", CLG_(clo).simulate_branch) {}
536 else {
537 Bool isCachesimOption = (*CLG_(cachesim).parse_opt)(arg);
539 /* cache simulator is used if a simulator option is given */
540 if (isCachesimOption)
541 CLG_(clo).simulate_cache = True;
543 return isCachesimOption;
546 return True;
549 void CLG_(print_usage)(void)
551 VG_(printf)(
552 "\n dump creation options:\n"
553 " --callgrind-out-file=<f> Output file name [callgrind.out.%%p]\n"
554 " --dump-line=no|yes Dump source lines of costs? [yes]\n"
555 " --dump-instr=no|yes Dump instruction address of costs? [no]\n"
556 " --compress-strings=no|yes Compress strings in profile dump? [yes]\n"
557 " --compress-pos=no|yes Compress positions in profile dump? [yes]\n"
558 " --combine-dumps=no|yes Concat all dumps into same file [no]\n"
559 #if CLG_EXPERIMENTAL
560 " --compress-events=no|yes Compress events in profile dump? [no]\n"
561 " --dump-bb=no|yes Dump basic block address of costs? [no]\n"
562 " --dump-bbs=no|yes Dump basic block info? [no]\n"
563 " --dump-skipped=no|yes Dump info on skipped functions in calls? [no]\n"
564 " --mangle-names=no|yes Mangle separation into names? [yes]\n"
565 #endif
567 "\n activity options (for interactivity use callgrind_control):\n"
568 " --dump-every-bb=<count> Dump every <count> basic blocks [0=never]\n"
569 " --dump-before=<func> Dump when entering function\n"
570 " --zero-before=<func> Zero all costs when entering function\n"
571 " --dump-after=<func> Dump when leaving function\n"
572 #if CLG_EXPERIMENTAL
573 " --dump-objs=no|yes Dump static object information [no]\n"
574 #endif
576 "\n data collection options:\n"
577 " --instr-atstart=no|yes Do instrumentation at callgrind start [yes]\n"
578 " --collect-atstart=no|yes Collect at process/thread start [yes]\n"
579 " --toggle-collect=<func> Toggle collection on enter/leave function\n"
580 " --collect-jumps=no|yes Collect jumps? [no]\n"
581 " --collect-bus=no|yes Collect global bus events? [no]\n"
582 #if CLG_EXPERIMENTAL
583 " --collect-alloc=no|yes Collect memory allocation info? [no]\n"
584 #endif
585 " --collect-systime=no|yes Collect system call time info? [no]\n"
587 "\n cost entity separation options:\n"
588 " --separate-threads=no|yes Separate data per thread [no]\n"
589 " --separate-callers=<n> Separate functions by call chain length [0]\n"
590 " --separate-callers<n>=<f> Separate <n> callers for function <f>\n"
591 " --separate-recs=<n> Separate function recursions up to level [2]\n"
592 " --separate-recs<n>=<f> Separate <n> recursions for function <f>\n"
593 " --skip-plt=no|yes Ignore calls to/from PLT sections? [yes]\n"
594 " --skip-direct-rec=no|yes Ignore direct recursions? [yes]\n"
595 " --fn-skip=<function> Ignore calls to/from function?\n"
596 #if CLG_EXPERIMENTAL
597 " --fn-group<no>=<func> Put function into separation group <no>\n"
598 #endif
599 "\n simulation options:\n"
600 " --branch-sim=no|yes Do branch prediction simulation [no]\n"
601 " --cache-sim=no|yes Do cache simulation [no]\n"
604 (*CLG_(cachesim).print_opts)();
606 // VG_(printf)("\n"
607 // " For full callgrind documentation, see\n"
608 // " "VG_PREFIX"/share/doc/callgrind/html/callgrind.html\n\n");
611 void CLG_(print_debug_usage)(void)
613 VG_(printf)(
615 #if CLG_ENABLE_DEBUG
616 " --ct-verbose=<level> Verbosity of standard debug output [0]\n"
617 " --ct-vstart=<BB number> Only be verbose after basic block [0]\n"
618 " --ct-verbose<level>=<func> Verbosity while in <func>\n"
619 #else
620 " (none)\n"
621 #endif
627 void CLG_(set_clo_defaults)(void)
629 /* Default values for command line arguments */
631 /* dump options */
632 CLG_(clo).out_format = 0;
633 CLG_(clo).combine_dumps = False;
634 CLG_(clo).compress_strings = True;
635 CLG_(clo).compress_mangled = False;
636 CLG_(clo).compress_events = False;
637 CLG_(clo).compress_pos = True;
638 CLG_(clo).mangle_names = True;
639 CLG_(clo).dump_line = True;
640 CLG_(clo).dump_instr = False;
641 CLG_(clo).dump_bb = False;
642 CLG_(clo).dump_bbs = False;
644 CLG_(clo).dump_every_bb = 0;
646 /* Collection */
647 CLG_(clo).separate_threads = False;
648 CLG_(clo).collect_atstart = True;
649 CLG_(clo).collect_jumps = False;
650 CLG_(clo).collect_alloc = False;
651 CLG_(clo).collect_systime = False;
652 CLG_(clo).collect_bus = False;
654 CLG_(clo).skip_plt = True;
655 CLG_(clo).separate_callers = 0;
656 CLG_(clo).separate_recursions = 2;
657 CLG_(clo).skip_direct_recursion = False;
659 /* Instrumentation */
660 CLG_(clo).instrument_atstart = True;
661 CLG_(clo).simulate_cache = False;
662 CLG_(clo).simulate_branch = False;
664 /* Call graph */
665 CLG_(clo).pop_on_jump = False;
667 #if CLG_ENABLE_DEBUG
668 CLG_(clo).verbose = 0;
669 CLG_(clo).verbose_start = 0;
670 #endif