2 This file is part of Callgrind, a Valgrind tool for call graph
5 Copyright (C) 2002-2010, Josef Weidendorfer (Josef.Weidendorfer@gmx.de)
7 This tool is derived from and contains lot of code from Cachegrind
8 Copyright (C) 2002 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
25 The GNU General Public License is contained in the file COPYING.
28 #include "config.h" // for VG_PREFIX
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
45 /* Logging configuration for a function */
52 Int skip; /* Handle CALL to this function as JMP (= Skip)? */
53 Int group; /* don't change caller dependency inside group !=0 */
56 Int separate_callers; /* separate logging dependent on caller */
57 Int separate_recursions; /* separate logging of rec. levels */
60 Int verbosity; /* Change debug verbosity level while in function */
64 /* Configurations for function name prefix patterns.
65 * Currently, only very limit patterns are possible:
66 * Exact prefix patterns and "*::" are allowed.
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;
87 config_node* sub_node[NODE_DEGREE];
89 config_node* wild_star;
90 config_node* wild_char;
96 static config_node* fn_configs = 0;
99 fn_config* new_fnc(void)
101 fn_config* fnc = (fn_config*) CLG_MALLOC("cl.clo.nf.1",
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;
115 fnc->verbosity = CONFIG_DEFAULT;
122 static config_node* new_config(Char* name, int length)
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];
134 node->length = length;
136 for(i=0;i<NODE_DEGREE;i++)
137 node->sub_node[i] = 0;
142 CLG_DEBUG(3, " new_config('%s', len %d)\n", node->name, length);
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, Char* name)
163 config_node *new_sub, *n, *nprev;
166 CLG_DEBUG(3, " get_fnc2(%p, '%s')\n", node, name);
169 if (!node->config) node->config = new_fnc();
173 if (is_wild(*name)) {
175 while(name[1] == '*') name++;
176 new_sub = node->wild_star;
179 new_sub = node->wild_char;
182 new_sub = new_config(name, 1);
184 node->wild_star = new_sub;
186 node->wild_char = new_sub;
189 return get_fnc2( new_sub, name+1);
192 n = node->sub_node[ name[0]%NODE_DEGREE ];
196 for(len=0; name[len] == n->name[len]; len++);
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;
224 new_node = new_config(n->name, len);
226 nprev->next = new_node;
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];
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);
258 /* name and node name are the same */
259 if (!n->config) n->config = new_fnc();
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)
278 if (node != fn_configs) {
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];
291 print_config_node(depth+1, i, n);
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(Char* name)
304 CLG_DEBUG(3, " +get_fnc(%s)\n", name);
306 fn_configs = new_config(name, 0);
307 fnc = get_fnc2(fn_configs, name);
310 CLG_DEBUG(3, " -get_fnc(%s):\n", name);
311 print_config_node(3, -1, fn_configs);
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;
348 if (fnc->verbosity != CONFIG_DEFAULT)
349 fn->verbosity = fnc->verbosity;
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, Char* name, config_node* node)
361 CLG_DEBUG(3, " update_fn_config2('%s', node '%s'): \n",
363 if ((*name == 0) && node->config) {
364 CLG_DEBUG(3, " found!\n");
365 update_fn_config1(fn, node->config);
369 n = node->sub_node[ name[0]%NODE_DEGREE ];
371 if (VG_(strncmp)(name, n->name, n->length)==0) break;
375 CLG_DEBUG(3, " '%s' matching at hash %d\n",
376 n->name, name[0]%NODE_DEGREE);
377 update_fn_config2(fn, name+n->length, n);
380 if (node->wild_char) {
381 CLG_DEBUG(3, " skip '%c' for wildcard '?'\n", *name);
382 update_fn_config2(fn, name+1, node->wild_char);
385 if (node->wild_star) {
386 CLG_DEBUG(3, " wildcard '*'\n");
388 update_fn_config2(fn, name, node->wild_star);
391 update_fn_config2(fn, name, node->wild_star);
395 /* Update function config according to configs of name prefixes */
396 void CLG_(update_fn_config)(fn_node* fn)
398 CLG_DEBUG(3, " update_fn_config('%s')\n", fn->name);
400 update_fn_config2(fn, fn->name, fn_configs);
404 /*--------------------------------------------------------------------*/
405 /*--- Command line processing ---*/
406 /*--------------------------------------------------------------------*/
408 Bool CLG_(process_cmd_line_option)(Char* arg)
412 if VG_BOOL_CLO(arg, "--skip-plt", CLG_(clo).skip_plt) {}
414 else if VG_BOOL_CLO(arg, "--collect-jumps", CLG_(clo).collect_jumps) {}
415 /* compatibility alias, deprecated option */
416 else if VG_BOOL_CLO(arg, "--trace-jump", CLG_(clo).collect_jumps) {}
418 else if VG_BOOL_CLO(arg, "--combine-dumps", CLG_(clo).combine_dumps) {}
420 else if VG_BOOL_CLO(arg, "--collect-atstart", CLG_(clo).collect_atstart) {}
422 else if VG_BOOL_CLO(arg, "--instr-atstart", CLG_(clo).instrument_atstart) {}
424 else if VG_BOOL_CLO(arg, "--separate-threads", CLG_(clo).separate_threads) {}
426 else if VG_BOOL_CLO(arg, "--compress-strings", CLG_(clo).compress_strings) {}
427 else if VG_BOOL_CLO(arg, "--compress-mangled", CLG_(clo).compress_mangled) {}
428 else if VG_BOOL_CLO(arg, "--compress-pos", CLG_(clo).compress_pos) {}
430 else if VG_STR_CLO(arg, "--fn-skip", tmp_str) {
431 fn_config* fnc = get_fnc(tmp_str);
432 fnc->skip = CONFIG_TRUE;
435 else if VG_STR_CLO(arg, "--dump-before", tmp_str) {
436 fn_config* fnc = get_fnc(tmp_str);
437 fnc->dump_before = CONFIG_TRUE;
440 else if VG_STR_CLO(arg, "--zero-before", tmp_str) {
441 fn_config* fnc = get_fnc(tmp_str);
442 fnc->zero_before = CONFIG_TRUE;
445 else if VG_STR_CLO(arg, "--dump-after", tmp_str) {
446 fn_config* fnc = get_fnc(tmp_str);
447 fnc->dump_after = CONFIG_TRUE;
450 else if VG_STR_CLO(arg, "--toggle-collect", tmp_str) {
451 fn_config* fnc = get_fnc(tmp_str);
452 fnc->toggle_collect = CONFIG_TRUE;
453 /* defaults to initial collection off */
454 CLG_(clo).collect_atstart = False;
457 else if VG_INT_CLO(arg, "--separate-recs", CLG_(clo).separate_recursions) {}
459 /* change handling of a jump between functions to ret+call */
460 else if VG_XACT_CLO(arg, "--pop-on-jump", CLG_(clo).pop_on_jump, True) {}
461 else if VG_STR_CLO( arg, "--pop-on-jump", tmp_str) {
462 fn_config* fnc = get_fnc(tmp_str);
463 fnc->pop_on_jump = CONFIG_TRUE;
467 else if VG_INT_CLO(arg, "--ct-verbose", CLG_(clo).verbose) {}
468 else if VG_INT_CLO(arg, "--ct-vstart", CLG_(clo).verbose_start) {}
470 else if VG_STREQN(12, arg, "--ct-verbose") {
473 UInt n = VG_(strtoll10)(arg+12, &s);
474 if ((n <= 0) || *s != '=') return False;
480 else if VG_XACT_CLO(arg, "--separate-callers=auto",
481 CLG_(clo).separate_callers, CONFIG_AUTO) {}
482 else if VG_INT_CLO( arg, "--separate-callers",
483 CLG_(clo).separate_callers) {}
485 else if VG_STREQN(10, arg, "--fn-group") {
488 UInt n = VG_(strtoll10)(arg+10, &s);
489 if ((n <= 0) || *s != '=') return False;
494 else if VG_STREQN(18, arg, "--separate-callers") {
497 UInt n = VG_(strtoll10)(arg+18, &s);
498 if ((n <= 0) || *s != '=') return False;
500 fnc->separate_callers = n;
503 else if VG_STREQN(15, arg, "--separate-recs") {
506 UInt n = VG_(strtoll10)(arg+15, &s);
507 if ((n <= 0) || *s != '=') return False;
509 fnc->separate_recursions = n;
512 else if VG_STR_CLO(arg, "--callgrind-out-file", CLG_(clo).out_format) {}
514 else if VG_BOOL_CLO(arg, "--mangle-names", CLG_(clo).mangle_names) {}
516 else if VG_BOOL_CLO(arg, "--skip-direct-rec",
517 CLG_(clo).skip_direct_recursion) {}
519 else if VG_BOOL_CLO(arg, "--dump-bbs", CLG_(clo).dump_bbs) {}
520 else if VG_BOOL_CLO(arg, "--dump-line", CLG_(clo).dump_line) {}
521 else if VG_BOOL_CLO(arg, "--dump-instr", CLG_(clo).dump_instr) {}
522 else if VG_BOOL_CLO(arg, "--dump-bb", CLG_(clo).dump_bb) {}
524 else if VG_INT_CLO( arg, "--dump-every-bb", CLG_(clo).dump_every_bb) {}
526 else if VG_BOOL_CLO(arg, "--collect-alloc", CLG_(clo).collect_alloc) {}
527 else if VG_BOOL_CLO(arg, "--collect-systime", CLG_(clo).collect_systime) {}
528 else if VG_BOOL_CLO(arg, "--simulate-cache", CLG_(clo).simulate_cache) {}
531 Bool isCachesimOption = (*CLG_(cachesim).parse_opt)(arg);
533 /* cache simulator is used if a simulator option is given */
534 if (isCachesimOption)
535 CLG_(clo).simulate_cache = True;
537 return isCachesimOption;
543 void CLG_(print_usage)(void)
546 "\n dump creation options:\n"
547 " --callgrind-out-file=<f> Output file name [callgrind.out.%%p]\n"
548 " --dump-line=no|yes Dump source lines of costs? [yes]\n"
549 " --dump-instr=no|yes Dump instruction address of costs? [no]\n"
550 " --compress-strings=no|yes Compress strings in profile dump? [yes]\n"
551 " --compress-pos=no|yes Compress positions in profile dump? [yes]\n"
552 " --combine-dumps=no|yes Concat all dumps into same file [no]\n"
554 " --compress-events=no|yes Compress events in profile dump? [no]\n"
555 " --dump-bb=no|yes Dump basic block address of costs? [no]\n"
556 " --dump-bbs=no|yes Dump basic block info? [no]\n"
557 " --dump-skipped=no|yes Dump info on skipped functions in calls? [no]\n"
558 " --mangle-names=no|yes Mangle separation into names? [yes]\n"
561 "\n activity options (for interactivity use callgrind_control):\n"
562 " --dump-every-bb=<count> Dump every <count> basic blocks [0=never]\n"
563 " --dump-before=<func> Dump when entering function\n"
564 " --zero-before=<func> Zero all costs when entering function\n"
565 " --dump-after=<func> Dump when leaving function\n"
567 " --dump-objs=no|yes Dump static object information [no]\n"
570 "\n data collection options:\n"
571 " --instr-atstart=no|yes Do instrumentation at callgrind start [yes]\n"
572 " --collect-atstart=no|yes Collect at process/thread start [yes]\n"
573 " --toggle-collect=<func> Toggle collection on enter/leave function\n"
574 " --collect-jumps=no|yes Collect jumps? [no]\n"
576 " --collect-alloc=no|yes Collect memory allocation info? [no]\n"
578 " --collect-systime=no|yes Collect system call time info? [no]\n"
580 "\n cost entity separation options:\n"
581 " --separate-threads=no|yes Separate data per thread [no]\n"
582 " --separate-callers=<n> Separate functions by call chain length [0]\n"
583 " --separate-callers<n>=<f> Separate <n> callers for function <f>\n"
584 " --separate-recs=<n> Separate function recursions up to level [2]\n"
585 " --separate-recs<n>=<f> Separate <n> recursions for function <f>\n"
586 " --skip-plt=no|yes Ignore calls to/from PLT sections? [yes]\n"
587 " --skip-direct-rec=no|yes Ignore direct recursions? [yes]\n"
588 " --fn-skip=<function> Ignore calls to/from function?\n"
590 " --fn-group<no>=<func> Put function into separation group <no>\n"
594 (*CLG_(cachesim).print_opts)();
597 // " For full callgrind documentation, see\n"
598 // " "VG_PREFIX"/share/doc/callgrind/html/callgrind.html\n\n");
601 void CLG_(print_debug_usage)(void)
606 " --ct-verbose=<level> Verbosity of standard debug output [0]\n"
607 " --ct-vstart=<BB number> Only be verbose after basic block [0]\n"
608 " --ct-verbose<level>=<func> Verbosity while in <func>\n"
617 void CLG_(set_clo_defaults)(void)
619 /* Default values for command line arguments */
622 CLG_(clo).out_format = 0;
623 CLG_(clo).combine_dumps = False;
624 CLG_(clo).compress_strings = True;
625 CLG_(clo).compress_mangled = False;
626 CLG_(clo).compress_events = False;
627 CLG_(clo).compress_pos = True;
628 CLG_(clo).mangle_names = True;
629 CLG_(clo).dump_line = True;
630 CLG_(clo).dump_instr = False;
631 CLG_(clo).dump_bb = False;
632 CLG_(clo).dump_bbs = False;
634 CLG_(clo).dump_every_bb = 0;
637 CLG_(clo).separate_threads = False;
638 CLG_(clo).collect_atstart = True;
639 CLG_(clo).collect_jumps = False;
640 CLG_(clo).collect_alloc = False;
641 CLG_(clo).collect_systime = False;
643 CLG_(clo).skip_plt = True;
644 CLG_(clo).separate_callers = 0;
645 CLG_(clo).separate_recursions = 2;
646 CLG_(clo).skip_direct_recursion = False;
648 /* Instrumentation */
649 CLG_(clo).instrument_atstart = True;
650 CLG_(clo).simulate_cache = False;
653 CLG_(clo).pop_on_jump = False;
656 CLG_(clo).verbose = 0;
657 CLG_(clo).verbose_start = 0;