2 /*--------------------------------------------------------------------*/
3 /*--- Command line options. m_options.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2017 Nicholas Nethercote
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics.h"
32 #include "pub_core_vki.h"
33 #include "pub_core_options.h"
34 #include "pub_core_libcassert.h"
35 #include "pub_core_libcbase.h"
36 #include "pub_core_libcfile.h"
37 #include "pub_core_libcprint.h"
38 #include "pub_core_libcproc.h"
39 #include "pub_core_mallocfree.h"
40 #include "pub_core_seqmatch.h" // VG_(string_match)
41 #include "pub_core_aspacemgr.h"
43 // See pub_{core,tool}_options.h for explanations of all these.
46 /* Define, and set defaults. */
48 const HChar
*VG_(clo_toolname
) = "memcheck"; // default to Memcheck
49 VexControl
VG_(clo_vex_control
);
50 VexRegisterUpdates
VG_(clo_px_file_backed
) = VexRegUpd_INVALID
;
52 Bool
VG_(clo_error_limit
) = True
;
53 Int
VG_(clo_error_exitcode
) = 0;
54 HChar
*VG_(clo_error_markers
)[2] = {NULL
, NULL
};
55 Bool
VG_(clo_exit_on_first_error
) = False
;
57 Bool
VG_(clo_show_error_list
) = False
;
59 #if defined(VGPV_arm_linux_android) \
60 || defined(VGPV_x86_linux_android) \
61 || defined(VGPV_mips32_linux_android) \
62 || defined(VGPV_arm64_linux_android)
63 VgVgdb
VG_(clo_vgdb
) = Vg_VgdbNo
; // currently disabled on Android
65 VgVgdb
VG_(clo_vgdb
) = Vg_VgdbYes
;
67 Int
VG_(clo_vgdb_poll
) = 5000;
68 Int
VG_(clo_vgdb_error
) = 999999999;
69 UInt
VG_(clo_vgdb_stop_at
) = 0;
70 const HChar
*VG_(clo_vgdb_prefix
) = NULL
;
71 const HChar
*VG_(arg_vgdb_prefix
) = NULL
;
72 Bool
VG_(clo_vgdb_shadow_registers
) = False
;
74 Int
VG_(clo_gen_suppressions
) = 0;
75 Int
VG_(clo_sanity_level
) = 1;
76 Int
VG_(clo_verbosity
) = 1;
77 Bool
VG_(clo_stats
) = False
;
78 Bool
VG_(clo_xml
) = False
;
79 const HChar
* VG_(clo_xml_user_comment
) = NULL
;
80 Bool
VG_(clo_demangle
) = True
;
81 const HChar
* VG_(clo_soname_synonyms
) = NULL
;
82 Bool
VG_(clo_trace_children
) = False
;
83 const HChar
* VG_(clo_trace_children_skip
) = NULL
;
84 const HChar
* VG_(clo_trace_children_skip_by_arg
) = NULL
;
85 Bool
VG_(clo_child_silent_after_fork
) = False
;
86 const HChar
*VG_(clo_log_fname_unexpanded
) = NULL
;
87 const HChar
*VG_(clo_xml_fname_unexpanded
) = NULL
;
88 Bool
VG_(clo_time_stamp
) = False
;
89 Int
VG_(clo_input_fd
) = 0; /* stdin */
90 Bool
VG_(clo_default_supp
) = True
;
91 XArray
*VG_(clo_suppressions
); // array of strings
92 XArray
*VG_(clo_fullpath_after
); // array of strings
93 const HChar
* VG_(clo_extra_debuginfo_path
) = NULL
;
94 const HChar
* VG_(clo_debuginfo_server
) = NULL
;
95 Bool
VG_(clo_allow_mismatched_debuginfo
) = False
;
96 UChar
VG_(clo_trace_flags
) = 0; // 00000000b
97 Bool
VG_(clo_profyle_sbs
) = False
;
98 UChar
VG_(clo_profyle_flags
) = 0; // 00000000b
99 ULong
VG_(clo_profyle_interval
) = 0;
100 Int
VG_(clo_trace_notbelow
) = -1; // unspecified
101 Int
VG_(clo_trace_notabove
) = -1; // unspecified
102 Bool
VG_(clo_trace_syscalls
) = False
;
103 Bool
VG_(clo_trace_signals
) = False
;
104 Bool
VG_(clo_trace_symtab
) = False
;
105 const HChar
* VG_(clo_trace_symtab_patt
) = "*";
106 Bool
VG_(clo_trace_cfi
) = False
;
107 Bool
VG_(clo_debug_dump_syms
) = False
;
108 Bool
VG_(clo_debug_dump_line
) = False
;
109 Bool
VG_(clo_debug_dump_frames
) = False
;
110 Bool
VG_(clo_trace_redir
) = False
;
112 VG_(clo_fair_sched
) = disable_fair_sched
;
113 Bool
VG_(clo_trace_sched
) = False
;
114 Bool
VG_(clo_profile_heap
) = False
;
115 UInt
VG_(clo_progress_interval
) = 0; /* in seconds, 1 .. 3600,
117 Int
VG_(clo_core_redzone_size
) = CORE_REDZONE_DEFAULT_SZB
;
118 // A value != -1 overrides the tool-specific value
119 // VG_(needs_malloc_replacement).tool_client_redzone_szB
120 Int
VG_(clo_redzone_size
) = -1;
121 VgXTMemory
VG_(clo_xtree_memory
) = Vg_XTMemory_None
;
122 const HChar
* VG_(clo_xtree_memory_file
) = "xtmemory.kcg.%p";
123 Bool
VG_(clo_xtree_compress_strings
) = True
;
125 Int
VG_(clo_dump_error
) = 0;
126 Int
VG_(clo_backtrace_size
) = 12;
127 Int
VG_(clo_merge_recursive_frames
) = 0; // default value: no merge
128 UInt
VG_(clo_sim_hints
) = 0;
129 Bool
VG_(clo_sym_offsets
) = False
;
130 Bool
VG_(clo_read_inline_info
) = False
; // Or should be put it to True by default ???
131 Bool
VG_(clo_read_var_info
) = False
;
132 XArray
*VG_(clo_req_tsyms
); // array of strings
133 Bool
VG_(clo_run_libc_freeres
) = True
;
134 Bool
VG_(clo_run_cxx_freeres
) = True
;
135 Bool
VG_(clo_track_fds
) = False
;
136 Bool
VG_(clo_show_below_main
)= False
;
137 Bool
VG_(clo_keep_debuginfo
) = False
;
138 Bool
VG_(clo_show_emwarns
) = False
;
139 Word
VG_(clo_max_stackframe
) = 2000000;
140 UInt
VG_(clo_max_threads
) = MAX_THREADS_DEFAULT
;
141 Word
VG_(clo_main_stacksize
) = 0; /* use client's rlimit.stack */
142 Word
VG_(clo_valgrind_stacksize
) = VG_DEFAULT_STACK_ACTIVE_SZB
;
143 Bool
VG_(clo_wait_for_gdb
) = False
;
144 UInt
VG_(clo_kernel_variant
) = 0;
145 Bool
VG_(clo_dsymutil
) = True
;
146 Bool
VG_(clo_sigill_diag
) = True
;
147 UInt
VG_(clo_unw_stack_scan_thresh
) = 0; /* disabled by default */
148 UInt
VG_(clo_unw_stack_scan_frames
) = 5;
150 // Set clo_smc_check so that it provides transparent self modifying
151 // code support for "correct" programs at the smallest achievable
152 // expense for this arch.
153 #if defined(VGA_x86) || defined(VGA_amd64) || defined(VGA_s390x)
154 VgSmc
VG_(clo_smc_check
) = Vg_SmcAllNonFile
;
155 #elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le) \
156 || defined(VGA_arm) || defined(VGA_arm64) \
157 || defined(VGA_mips32) || defined(VGA_mips64)
158 VgSmc
VG_(clo_smc_check
) = Vg_SmcStack
;
160 # error "Unknown arch"
163 #if defined(VGO_darwin)
164 UInt
VG_(clo_resync_filter
) = 1; /* enabled, but quiet */
166 UInt
VG_(clo_resync_filter
) = 0; /* disabled */
170 /*====================================================================*/
171 /*=== File expansion ===*/
172 /*====================================================================*/
174 // Copies the string, prepending it with the startup working directory, and
175 // expanding %p and %q entries. Returns a new, malloc'd string.
176 HChar
* VG_(expand_file_name
)(const HChar
* option_name
, const HChar
* format
)
178 const HChar
*base_dir
;
179 Int len
, i
= 0, j
= 0;
181 const HChar
*message
= NULL
;
183 base_dir
= VG_(get_startup_wd
)();
185 if (VG_STREQ(format
, "")) {
187 message
= "No filename given\n";
191 // If 'format' starts with a '~', abort -- the user probably expected the
192 // shell to expand but it didn't (see bug 195268 for details). This means
193 // that we don't allow a legitimate filename beginning with '~' but that
194 // seems very unlikely.
195 if (format
[0] == '~') {
197 "Filename begins with '~'\n"
198 "You probably expected the shell to expand the '~', but it\n"
199 "didn't. The rules for '~'-expansion vary from shell to shell.\n"
200 "You might have more luck using $HOME instead.\n";
204 len
= VG_(strlen
)(format
) + 1;
205 out
= VG_(malloc
)( "options.efn.1", len
);
207 #define ENSURE_THIS_MUCH_SPACE(x) \
208 if (j + x >= len) { \
210 out = VG_(realloc)("options.efn.2(multiple)", out, len); \
214 if (format
[i
] != '%') {
215 ENSURE_THIS_MUCH_SPACE(1);
216 out
[j
++] = format
[i
++];
219 // We saw a '%'. What's next...
221 if ('%' == format
[i
]) {
222 // Replace '%%' with '%'.
223 ENSURE_THIS_MUCH_SPACE(1);
224 out
[j
++] = format
[i
++];
226 else if ('p' == format
[i
]) {
227 // Print the PID. Assume that it's not longer than 10 chars --
228 // reasonable since 'pid' is an Int (ie. 32 bits).
229 Int pid
= VG_(getpid
)();
230 ENSURE_THIS_MUCH_SPACE(10);
231 j
+= VG_(sprintf
)(&out
[j
], "%d", pid
);
234 else if ('n' == format
[i
]) {
238 Int pid
= VG_(getpid
)();
243 ENSURE_THIS_MUCH_SPACE(10);
244 j
+= VG_(sprintf
)(&out
[j
], "%d", seq_nr
);
247 else if ('q' == format
[i
]) {
249 if ('{' == format
[i
]) {
250 // Get the env var name, print its contents.
252 Int begin_qualname
= ++i
;
254 if (0 == format
[i
]) {
255 message
= "Missing '}' in %q specifier\n";
257 } else if ('}' == format
[i
]) {
258 Int qualname_len
= i
- begin_qualname
;
259 HChar qualname
[qualname_len
+ 1];
260 VG_(strncpy
)(qualname
, format
+ begin_qualname
,
262 qualname
[qualname_len
] = '\0';
263 qual
= VG_(getenv
)(qualname
);
265 // This memory will leak, But we don't care because
266 // VG_(fmsg_bad_option) will terminate the process.
267 HChar
*str
= VG_(malloc
)("options.efn.3",
270 "Environment variable '%s' is not set\n",
280 ENSURE_THIS_MUCH_SPACE(VG_(strlen
)(qual
));
281 j
+= VG_(sprintf
)(&out
[j
], "%s", qual
);
283 message
= "Expected '{' after '%q'\n";
288 // Something else, abort.
289 message
= "Expected 'p' or 'q' or '%' after '%'\n";
294 ENSURE_THIS_MUCH_SPACE(1);
297 // If 'out' is not an absolute path name, prefix it with the startup dir.
299 if (base_dir
== NULL
) {
300 message
= "Current working dir doesn't exist, use absolute path\n";
303 len
= VG_(strlen
)(base_dir
) + 1 + VG_(strlen
)(out
) + 1;
305 HChar
*absout
= VG_(malloc
)("options.efn.4", len
);
306 VG_(strcpy
)(absout
, base_dir
);
307 VG_(strcat
)(absout
, "/");
308 VG_(strcat
)(absout
, out
);
316 vg_assert(message
!= NULL
);
317 // 2: 1 for the '=', 1 for the NUL.
318 HChar opt
[VG_(strlen
)(option_name
) + VG_(strlen
)(format
) + 2];
319 VG_(sprintf
)(opt
, "%s=%s", option_name
, format
);
320 VG_(fmsg_bad_option
)(opt
, "%s", message
);
324 /*====================================================================*/
325 /*=== --trace-children= support ===*/
326 /*====================================================================*/
328 static HChar
const* consume_commas ( HChar
const* c
) {
329 while (*c
&& *c
== ',') {
335 static HChar
const* consume_field ( HChar
const* c
) {
336 while (*c
&& *c
!= ',') {
342 /* Should we trace into this child executable (across execve, spawn etc) ?
343 This involves considering --trace-children=,
344 --trace-children-skip=, --trace-children-skip-by-arg=, and the name
345 of the executable. 'child_argv' must not include the name of the
346 executable itself; iow child_argv[0] must be the first arg, if any,
348 Bool
VG_(should_we_trace_this_child
) ( const HChar
* child_exe_name
,
349 const HChar
** child_argv
)
351 // child_exe_name is pulled out of the guest's space. We
352 // should be at least marginally cautious with it, lest it
353 // explode or burst into flames unexpectedly.
354 if (child_exe_name
== NULL
|| VG_(strlen
)(child_exe_name
) == 0)
355 return VG_(clo_trace_children
); // we know narfink
357 // If --trace-children=no, the answer is simply NO.
358 if (! VG_(clo_trace_children
))
361 // Otherwise, look for other reasons to say NO. First,
362 // see if the exe name matches any of the patterns specified
363 // by --trace-children-skip=.
364 if (VG_(clo_trace_children_skip
)) {
365 HChar
const* last
= VG_(clo_trace_children_skip
);
366 HChar
const* name
= child_exe_name
;
370 HChar
const* first
= consume_commas(last
);
371 last
= consume_field(first
);
374 vg_assert(last
> first
);
375 /* copy the candidate string into a temporary malloc'd block
376 so we can use VG_(string_match) on it. */
377 patt
= VG_(calloc
)("m_options.swttc.1", last
- first
+ 1, 1);
378 VG_(memcpy
)(patt
, first
, last
- first
);
379 vg_assert(patt
[last
-first
] == 0);
380 matches
= VG_(string_match
)(patt
, name
);
387 // Check if any of the args match any of the patterns specified
388 // by --trace-children-skip-by-arg=.
389 if (VG_(clo_trace_children_skip_by_arg
) && child_argv
!= NULL
) {
390 HChar
const* last
= VG_(clo_trace_children_skip_by_arg
);
395 HChar
const* first
= consume_commas(last
);
396 last
= consume_field(first
);
399 vg_assert(last
> first
);
400 /* copy the candidate string into a temporary malloc'd block
401 so we can use VG_(string_match) on it. */
402 patt
= VG_(calloc
)("m_options.swttc.1", last
- first
+ 1, 1);
403 VG_(memcpy
)(patt
, first
, last
- first
);
404 vg_assert(patt
[last
-first
] == 0);
405 for (i
= 0; child_argv
[i
]; i
++) {
406 matches
= VG_(string_match
)(patt
, child_argv
[i
]);
416 // --trace-children=yes, and this particular executable isn't
422 /*--------------------------------------------------------------------*/
424 /*--------------------------------------------------------------------*/