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, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
29 #include "pub_core_basics.h"
30 #include "pub_core_vki.h"
31 #include "pub_core_options.h"
32 #include "pub_core_libcassert.h"
33 #include "pub_core_libcbase.h"
34 #include "pub_core_libcfile.h"
35 #include "pub_core_libcprint.h"
36 #include "pub_core_libcproc.h"
37 #include "pub_core_mallocfree.h"
38 #include "pub_core_seqmatch.h" // VG_(string_match)
39 #include "pub_core_aspacemgr.h"
41 // See pub_{core,tool}_options.h for explanations of all these.
43 static Clo_Mode clo_mode
= cloE
;
44 static Bool clo_recognised
= False
;
46 void VG_(set_Clo_Mode
) (Clo_Mode mode
)
49 clo_recognised
= False
;
51 Clo_Mode
VG_(Clo_Mode
) (void)
56 void VG_(set_Clo_Recognised
) (void)
58 clo_recognised
= True
;
61 Bool
VG_(Clo_Recognised
) (void)
63 return clo_recognised
;
66 Bool
VG_(check_clom
) (Clo_Mode modes
, const HChar
* arg
, const HChar
* option
,
69 Bool res
= recognised
&& (modes
& VG_(Clo_Mode
)());
70 Bool dynamic
= cloD
== VG_(Clo_Mode
)();
73 VG_(set_Clo_Recognised
) ();
75 VG_(umsg
)("Cannot change %s option dynamically\n", option
);
76 else if (dynamic
&& VG_(clo_verbosity
) >= 1)
77 VG_(umsg
)("Handling new value %s for option %s\n", arg
, option
);
79 if (cloH
== VG_(Clo_Mode
)() && (cloD
& modes
))
80 VG_(list_clo
)(option
);
85 /* Define, and set defaults. */
87 const HChar
*VG_(clo_toolname
) = "memcheck"; // default to Memcheck
88 VexControl
VG_(clo_vex_control
);
89 VexRegisterUpdates
VG_(clo_px_file_backed
) = VexRegUpd_INVALID
;
91 Bool
VG_(clo_error_limit
) = True
;
92 Int
VG_(clo_error_exitcode
) = 0;
93 HChar
*VG_(clo_error_markers
)[2] = {NULL
, NULL
};
94 Bool
VG_(clo_exit_on_first_error
) = False
;
96 Bool
VG_(clo_show_error_list
) = False
;
98 #if defined(VGPV_arm_linux_android) \
99 || defined(VGPV_x86_linux_android) \
100 || defined(VGPV_mips32_linux_android) \
101 || defined(VGPV_arm64_linux_android) \
102 || defined(VGP_nanomips_linux)
103 VgVgdb
VG_(clo_vgdb
) = Vg_VgdbNo
; // currently disabled on Android
105 VgVgdb
VG_(clo_vgdb
) = Vg_VgdbYes
;
107 Int
VG_(clo_vgdb_poll
) = 5000;
108 Int
VG_(clo_vgdb_error
) = 999999999;
109 UInt
VG_(clo_vgdb_stop_at
) = 0;
110 const HChar
*VG_(clo_vgdb_prefix
) = NULL
;
111 const HChar
*VG_(arg_vgdb_prefix
) = NULL
;
112 Bool
VG_(clo_vgdb_shadow_registers
) = False
;
114 Int
VG_(clo_gen_suppressions
) = 0;
115 Int
VG_(clo_sanity_level
) = 1;
116 Int
VG_(clo_verbosity
) = 1;
117 Bool
VG_(clo_stats
) = False
;
118 Bool
VG_(clo_xml
) = False
;
119 const HChar
* VG_(clo_xml_user_comment
) = NULL
;
120 Bool
VG_(clo_demangle
) = True
;
121 const HChar
* VG_(clo_soname_synonyms
) = NULL
;
122 Bool
VG_(clo_trace_children
) = False
;
123 const HChar
* VG_(clo_trace_children_skip
) = NULL
;
124 const HChar
* VG_(clo_trace_children_skip_by_arg
) = NULL
;
125 Bool
VG_(clo_child_silent_after_fork
) = False
;
126 const HChar
*VG_(clo_log_fname_unexpanded
) = NULL
;
127 const HChar
*VG_(clo_xml_fname_unexpanded
) = NULL
;
128 Bool
VG_(clo_time_stamp
) = False
;
129 Int
VG_(clo_input_fd
) = 0; /* stdin */
130 Bool
VG_(clo_default_supp
) = True
;
131 XArray
*VG_(clo_suppressions
); // array of strings
132 XArray
*VG_(clo_fullpath_after
); // array of strings
133 const HChar
* VG_(clo_extra_debuginfo_path
) = NULL
;
134 const HChar
* VG_(clo_debuginfo_server
) = NULL
;
135 Bool
VG_(clo_allow_mismatched_debuginfo
) = False
;
136 UChar
VG_(clo_trace_flags
) = 0; // 00000000b
137 Bool
VG_(clo_profyle_sbs
) = False
;
138 UChar
VG_(clo_profyle_flags
) = 0; // 00000000b
139 ULong
VG_(clo_profyle_interval
) = 0;
140 Int
VG_(clo_trace_notbelow
) = -1; // unspecified
141 Int
VG_(clo_trace_notabove
) = -1; // unspecified
142 Bool
VG_(clo_trace_syscalls
) = False
;
143 Bool
VG_(clo_trace_signals
) = False
;
144 Bool
VG_(clo_trace_symtab
) = False
;
145 const HChar
* VG_(clo_trace_symtab_patt
) = "*";
146 Bool
VG_(clo_trace_cfi
) = False
;
147 Bool
VG_(clo_debug_dump_syms
) = False
;
148 Bool
VG_(clo_debug_dump_line
) = False
;
149 Bool
VG_(clo_debug_dump_frames
) = False
;
150 Bool
VG_(clo_trace_redir
) = False
;
152 VG_(clo_fair_sched
) = disable_fair_sched
;
153 Bool
VG_(clo_trace_sched
) = False
;
154 Bool
VG_(clo_profile_heap
) = False
;
155 UInt
VG_(clo_progress_interval
) = 0; /* in seconds, 1 .. 3600,
157 Int
VG_(clo_core_redzone_size
) = CORE_REDZONE_DEFAULT_SZB
;
158 // A value != -1 overrides the tool-specific value
159 // VG_(needs_malloc_replacement).tool_client_redzone_szB
160 Int
VG_(clo_redzone_size
) = -1;
161 VgXTMemory
VG_(clo_xtree_memory
) = Vg_XTMemory_None
;
162 const HChar
* VG_(clo_xtree_memory_file
) = "xtmemory.kcg.%p";
163 Bool
VG_(clo_xtree_compress_strings
) = True
;
165 Int
VG_(clo_dump_error
) = 0;
166 Int
VG_(clo_backtrace_size
) = 12;
167 Int
VG_(clo_merge_recursive_frames
) = 0; // default value: no merge
168 UInt
VG_(clo_sim_hints
) = 0;
169 Bool
VG_(clo_sym_offsets
) = False
;
170 Bool
VG_(clo_read_inline_info
) = False
; // Or should be put it to True by default ???
171 Bool
VG_(clo_read_var_info
) = False
;
172 XArray
*VG_(clo_req_tsyms
); // array of strings
173 Bool
VG_(clo_run_libc_freeres
) = True
;
174 Bool
VG_(clo_run_cxx_freeres
) = True
;
175 UInt
VG_(clo_track_fds
) = 0;
176 Bool
VG_(clo_show_below_main
)= False
;
177 Bool
VG_(clo_keep_debuginfo
) = False
;
178 Bool
VG_(clo_show_emwarns
) = False
;
179 Word
VG_(clo_max_stackframe
) = 2000000;
180 UInt
VG_(clo_max_threads
) = MAX_THREADS_DEFAULT
;
181 Word
VG_(clo_main_stacksize
) = 0; /* use client's rlimit.stack */
182 Word
VG_(clo_valgrind_stacksize
) = VG_DEFAULT_STACK_ACTIVE_SZB
;
183 Bool
VG_(clo_wait_for_gdb
) = False
;
184 UInt
VG_(clo_kernel_variant
) = 0;
185 Bool
VG_(clo_dsymutil
) = True
;
186 Bool
VG_(clo_sigill_diag
) = True
;
187 UInt
VG_(clo_unw_stack_scan_thresh
) = 0; /* disabled by default */
188 UInt
VG_(clo_unw_stack_scan_frames
) = 5;
190 // Set clo_smc_check so that it provides transparent self modifying
191 // code support for "correct" programs at the smallest achievable
192 // expense for this arch.
193 #if defined(VGA_x86) || defined(VGA_amd64) || defined(VGA_s390x)
194 VgSmc
VG_(clo_smc_check
) = Vg_SmcAllNonFile
;
195 #elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le) \
196 || defined(VGA_arm) || defined(VGA_arm64) \
197 || defined(VGA_mips32) || defined(VGA_mips64) || defined(VGA_nanomips)
198 VgSmc
VG_(clo_smc_check
) = Vg_SmcStack
;
200 # error "Unknown arch"
203 #if defined(VGO_darwin)
204 UInt
VG_(clo_resync_filter
) = 1; /* enabled, but quiet */
206 UInt
VG_(clo_resync_filter
) = 0; /* disabled */
210 /*====================================================================*/
211 /*=== File expansion ===*/
212 /*====================================================================*/
214 // Copies the string, prepending it with the startup working directory, and
215 // expanding %p and %q entries. Returns a new, malloc'd string.
216 HChar
* VG_(expand_file_name
)(const HChar
* option_name
, const HChar
* format
)
218 const HChar
*base_dir
;
219 Int len
, i
= 0, j
= 0;
221 const HChar
*message
= NULL
;
223 base_dir
= VG_(get_startup_wd
)();
225 if (VG_STREQ(format
, "")) {
227 message
= "No filename given\n";
231 // If 'format' starts with a '~', abort -- the user probably expected the
232 // shell to expand but it didn't (see bug 195268 for details). This means
233 // that we don't allow a legitimate filename beginning with '~' but that
234 // seems very unlikely.
235 if (format
[0] == '~') {
237 "Filename begins with '~'\n"
238 "You probably expected the shell to expand the '~', but it\n"
239 "didn't. The rules for '~'-expansion vary from shell to shell.\n"
240 "You might have more luck using $HOME instead.\n";
244 len
= VG_(strlen
)(format
) + 1;
245 out
= VG_(malloc
)( "options.efn.1", len
);
247 #define ENSURE_THIS_MUCH_SPACE(x) \
248 if (j + x >= len) { \
250 out = VG_(realloc)("options.efn.2(multiple)", out, len); \
254 if (format
[i
] != '%') {
255 ENSURE_THIS_MUCH_SPACE(1);
256 out
[j
++] = format
[i
++];
259 // We saw a '%'. What's next...
261 if ('%' == format
[i
]) {
262 // Replace '%%' with '%'.
263 ENSURE_THIS_MUCH_SPACE(1);
264 out
[j
++] = format
[i
++];
266 else if ('p' == format
[i
]) {
267 // Print the PID. Assume that it's not longer than 10 chars --
268 // reasonable since 'pid' is an Int (ie. 32 bits).
269 Int pid
= VG_(getpid
)();
270 ENSURE_THIS_MUCH_SPACE(10);
271 j
+= VG_(sprintf
)(&out
[j
], "%d", pid
);
274 else if ('n' == format
[i
]) {
278 Int pid
= VG_(getpid
)();
283 ENSURE_THIS_MUCH_SPACE(10);
284 j
+= VG_(sprintf
)(&out
[j
], "%d", seq_nr
);
287 else if ('q' == format
[i
]) {
289 if ('{' == format
[i
]) {
290 // Get the env var name, print its contents.
292 Int begin_qualname
= ++i
;
294 if (0 == format
[i
]) {
295 message
= "Missing '}' in %q specifier\n";
297 } else if ('}' == format
[i
]) {
298 Int qualname_len
= i
- begin_qualname
;
299 HChar qualname
[qualname_len
+ 1];
300 VG_(strncpy
)(qualname
, format
+ begin_qualname
,
302 qualname
[qualname_len
] = '\0';
303 qual
= VG_(getenv
)(qualname
);
305 // This memory will leak, But we don't care because
306 // VG_(fmsg_bad_option) will terminate the process.
307 HChar
*str
= VG_(malloc
)("options.efn.3",
310 "Environment variable '%s' is not set\n",
320 ENSURE_THIS_MUCH_SPACE(VG_(strlen
)(qual
));
321 j
+= VG_(sprintf
)(&out
[j
], "%s", qual
);
323 message
= "Expected '{' after '%q'\n";
328 // Something else, abort.
329 message
= "Expected 'p' or 'q' or '%' after '%'\n";
334 ENSURE_THIS_MUCH_SPACE(1);
337 // If 'out' is not an absolute path name, prefix it with the startup dir.
339 if (base_dir
== NULL
) {
340 message
= "Current working dir doesn't exist, use absolute path\n";
343 len
= VG_(strlen
)(base_dir
) + 1 + VG_(strlen
)(out
) + 1;
345 HChar
*absout
= VG_(malloc
)("options.efn.4", len
);
346 VG_(strcpy
)(absout
, base_dir
);
347 VG_(strcat
)(absout
, "/");
348 VG_(strcat
)(absout
, out
);
356 vg_assert(message
!= NULL
);
357 // 2: 1 for the '=', 1 for the NUL.
358 HChar opt
[VG_(strlen
)(option_name
) + VG_(strlen
)(format
) + 2];
359 VG_(sprintf
)(opt
, "%s=%s", option_name
, format
);
360 VG_(fmsg_bad_option
)(opt
, "%s", message
);
361 VG_(exit
)(1); // Cannot continue
367 void VG_(list_clo
)(const HChar
*qq_option
)
369 int len
= VG_(strlen
)(qq_option
);
370 if (col
+ len
+ 1 > 80) {
382 VG_(printf
)("%s", qq_option
);
385 void VG_(list_dynamic_options
) (void)
389 VG_(sprintf
)(dummy
, "%s", "<dummy option to trigger help>");
390 VG_(printf
)(" dynamically changeable options:\n");
391 VG_(process_dynamic_option
) (cloH
, dummy
);
398 /*====================================================================*/
399 /*=== --trace-children= support ===*/
400 /*====================================================================*/
402 static HChar
const* consume_commas ( HChar
const* c
) {
403 while (*c
&& *c
== ',') {
409 static HChar
const* consume_field ( HChar
const* c
) {
410 while (*c
&& *c
!= ',') {
416 /* Should we trace into this child executable (across execve, spawn etc) ?
417 This involves considering --trace-children=,
418 --trace-children-skip=, --trace-children-skip-by-arg=, and the name
419 of the executable. 'child_argv' must not include the name of the
420 executable itself; iow child_argv[0] must be the first arg, if any,
422 Bool
VG_(should_we_trace_this_child
) ( const HChar
* child_exe_name
,
423 const HChar
** child_argv
)
425 // child_exe_name is pulled out of the guest's space. We
426 // should be at least marginally cautious with it, lest it
427 // explode or burst into flames unexpectedly.
428 if (child_exe_name
== NULL
|| VG_(strlen
)(child_exe_name
) == 0)
429 return VG_(clo_trace_children
); // we know narfink
431 // If --trace-children=no, the answer is simply NO.
432 if (! VG_(clo_trace_children
))
435 // Otherwise, look for other reasons to say NO. First,
436 // see if the exe name matches any of the patterns specified
437 // by --trace-children-skip=.
438 if (VG_(clo_trace_children_skip
)) {
439 HChar
const* last
= VG_(clo_trace_children_skip
);
440 HChar
const* name
= child_exe_name
;
444 HChar
const* first
= consume_commas(last
);
445 last
= consume_field(first
);
448 vg_assert(last
> first
);
449 /* copy the candidate string into a temporary malloc'd block
450 so we can use VG_(string_match) on it. */
451 patt
= VG_(calloc
)("m_options.swttc.1", last
- first
+ 1, 1);
452 VG_(memcpy
)(patt
, first
, last
- first
);
453 vg_assert(patt
[last
-first
] == 0);
454 matches
= VG_(string_match
)(patt
, name
);
461 // Check if any of the args match any of the patterns specified
462 // by --trace-children-skip-by-arg=.
463 if (VG_(clo_trace_children_skip_by_arg
) && child_argv
!= NULL
) {
464 HChar
const* last
= VG_(clo_trace_children_skip_by_arg
);
469 HChar
const* first
= consume_commas(last
);
470 last
= consume_field(first
);
473 vg_assert(last
> first
);
474 /* copy the candidate string into a temporary malloc'd block
475 so we can use VG_(string_match) on it. */
476 patt
= VG_(calloc
)("m_options.swttc.1", last
- first
+ 1, 1);
477 VG_(memcpy
)(patt
, first
, last
- first
);
478 vg_assert(patt
[last
-first
] == 0);
479 for (i
= 0; child_argv
[i
]; i
++) {
480 matches
= VG_(string_match
)(patt
, child_argv
[i
]);
490 // --trace-children=yes, and this particular executable isn't
496 /*--------------------------------------------------------------------*/
498 /*--------------------------------------------------------------------*/