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 #if defined(VGPV_arm_linux_android) \
58 || defined(VGPV_x86_linux_android) \
59 || defined(VGPV_mips32_linux_android) \
60 || defined(VGPV_arm64_linux_android)
61 VgVgdb
VG_(clo_vgdb
) = Vg_VgdbNo
; // currently disabled on Android
63 VgVgdb
VG_(clo_vgdb
) = Vg_VgdbYes
;
65 Int
VG_(clo_vgdb_poll
) = 5000;
66 Int
VG_(clo_vgdb_error
) = 999999999;
67 UInt
VG_(clo_vgdb_stop_at
) = 0;
68 const HChar
*VG_(clo_vgdb_prefix
) = NULL
;
69 const HChar
*VG_(arg_vgdb_prefix
) = NULL
;
70 Bool
VG_(clo_vgdb_shadow_registers
) = False
;
72 Int
VG_(clo_gen_suppressions
) = 0;
73 Int
VG_(clo_sanity_level
) = 1;
74 Int
VG_(clo_verbosity
) = 1;
75 Bool
VG_(clo_stats
) = False
;
76 Bool
VG_(clo_xml
) = False
;
77 const HChar
* VG_(clo_xml_user_comment
) = NULL
;
78 Bool
VG_(clo_demangle
) = True
;
79 const HChar
* VG_(clo_soname_synonyms
) = NULL
;
80 Bool
VG_(clo_trace_children
) = False
;
81 const HChar
* VG_(clo_trace_children_skip
) = NULL
;
82 const HChar
* VG_(clo_trace_children_skip_by_arg
) = NULL
;
83 Bool
VG_(clo_child_silent_after_fork
) = False
;
84 const HChar
*VG_(clo_log_fname_unexpanded
) = NULL
;
85 const HChar
*VG_(clo_xml_fname_unexpanded
) = NULL
;
86 Bool
VG_(clo_time_stamp
) = False
;
87 Int
VG_(clo_input_fd
) = 0; /* stdin */
88 Bool
VG_(clo_default_supp
) = True
;
89 XArray
*VG_(clo_suppressions
); // array of strings
90 XArray
*VG_(clo_fullpath_after
); // array of strings
91 const HChar
* VG_(clo_extra_debuginfo_path
) = NULL
;
92 const HChar
* VG_(clo_debuginfo_server
) = NULL
;
93 Bool
VG_(clo_allow_mismatched_debuginfo
) = False
;
94 UChar
VG_(clo_trace_flags
) = 0; // 00000000b
95 Bool
VG_(clo_profyle_sbs
) = False
;
96 UChar
VG_(clo_profyle_flags
) = 0; // 00000000b
97 ULong
VG_(clo_profyle_interval
) = 0;
98 Int
VG_(clo_trace_notbelow
) = -1; // unspecified
99 Int
VG_(clo_trace_notabove
) = -1; // unspecified
100 Bool
VG_(clo_trace_syscalls
) = False
;
101 Bool
VG_(clo_trace_signals
) = False
;
102 Bool
VG_(clo_trace_symtab
) = False
;
103 const HChar
* VG_(clo_trace_symtab_patt
) = "*";
104 Bool
VG_(clo_trace_cfi
) = False
;
105 Bool
VG_(clo_debug_dump_syms
) = False
;
106 Bool
VG_(clo_debug_dump_line
) = False
;
107 Bool
VG_(clo_debug_dump_frames
) = False
;
108 Bool
VG_(clo_trace_redir
) = False
;
110 VG_(clo_fair_sched
) = disable_fair_sched
;
111 Bool
VG_(clo_trace_sched
) = False
;
112 Bool
VG_(clo_profile_heap
) = False
;
113 UInt
VG_(clo_progress_interval
) = 0; /* in seconds, 1 .. 3600,
115 Int
VG_(clo_core_redzone_size
) = CORE_REDZONE_DEFAULT_SZB
;
116 // A value != -1 overrides the tool-specific value
117 // VG_(needs_malloc_replacement).tool_client_redzone_szB
118 Int
VG_(clo_redzone_size
) = -1;
119 VgXTMemory
VG_(clo_xtree_memory
) = Vg_XTMemory_None
;
120 const HChar
* VG_(clo_xtree_memory_file
) = "xtmemory.kcg.%p";
121 Bool
VG_(clo_xtree_compress_strings
) = True
;
123 Int
VG_(clo_dump_error
) = 0;
124 Int
VG_(clo_backtrace_size
) = 12;
125 Int
VG_(clo_merge_recursive_frames
) = 0; // default value: no merge
126 UInt
VG_(clo_sim_hints
) = 0;
127 Bool
VG_(clo_sym_offsets
) = False
;
128 Bool
VG_(clo_read_inline_info
) = False
; // Or should be put it to True by default ???
129 Bool
VG_(clo_read_var_info
) = False
;
130 XArray
*VG_(clo_req_tsyms
); // array of strings
131 Bool
VG_(clo_run_libc_freeres
) = True
;
132 Bool
VG_(clo_run_cxx_freeres
) = True
;
133 Bool
VG_(clo_track_fds
) = False
;
134 Bool
VG_(clo_show_below_main
)= False
;
135 Bool
VG_(clo_keep_debuginfo
) = False
;
136 Bool
VG_(clo_show_emwarns
) = False
;
137 Word
VG_(clo_max_stackframe
) = 2000000;
138 UInt
VG_(clo_max_threads
) = MAX_THREADS_DEFAULT
;
139 Word
VG_(clo_main_stacksize
) = 0; /* use client's rlimit.stack */
140 Word
VG_(clo_valgrind_stacksize
) = VG_DEFAULT_STACK_ACTIVE_SZB
;
141 Bool
VG_(clo_wait_for_gdb
) = False
;
142 UInt
VG_(clo_kernel_variant
) = 0;
143 Bool
VG_(clo_dsymutil
) = True
;
144 Bool
VG_(clo_sigill_diag
) = True
;
145 UInt
VG_(clo_unw_stack_scan_thresh
) = 0; /* disabled by default */
146 UInt
VG_(clo_unw_stack_scan_frames
) = 5;
148 // Set clo_smc_check so that it provides transparent self modifying
149 // code support for "correct" programs at the smallest achievable
150 // expense for this arch.
151 #if defined(VGA_x86) || defined(VGA_amd64) || defined(VGA_s390x)
152 VgSmc
VG_(clo_smc_check
) = Vg_SmcAllNonFile
;
153 #elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le) \
154 || defined(VGA_arm) || defined(VGA_arm64) \
155 || defined(VGA_mips32) || defined(VGA_mips64)
156 VgSmc
VG_(clo_smc_check
) = Vg_SmcStack
;
158 # error "Unknown arch"
161 #if defined(VGO_darwin)
162 UInt
VG_(clo_resync_filter
) = 1; /* enabled, but quiet */
164 UInt
VG_(clo_resync_filter
) = 0; /* disabled */
168 /*====================================================================*/
169 /*=== File expansion ===*/
170 /*====================================================================*/
172 // Copies the string, prepending it with the startup working directory, and
173 // expanding %p and %q entries. Returns a new, malloc'd string.
174 HChar
* VG_(expand_file_name
)(const HChar
* option_name
, const HChar
* format
)
176 const HChar
*base_dir
;
177 Int len
, i
= 0, j
= 0;
179 const HChar
*message
= NULL
;
181 base_dir
= VG_(get_startup_wd
)();
183 if (VG_STREQ(format
, "")) {
185 message
= "No filename given\n";
189 // If 'format' starts with a '~', abort -- the user probably expected the
190 // shell to expand but it didn't (see bug 195268 for details). This means
191 // that we don't allow a legitimate filename beginning with '~' but that
192 // seems very unlikely.
193 if (format
[0] == '~') {
195 "Filename begins with '~'\n"
196 "You probably expected the shell to expand the '~', but it\n"
197 "didn't. The rules for '~'-expansion vary from shell to shell.\n"
198 "You might have more luck using $HOME instead.\n";
202 len
= VG_(strlen
)(format
) + 1;
203 out
= VG_(malloc
)( "options.efn.1", len
);
205 #define ENSURE_THIS_MUCH_SPACE(x) \
206 if (j + x >= len) { \
208 out = VG_(realloc)("options.efn.2(multiple)", out, len); \
212 if (format
[i
] != '%') {
213 ENSURE_THIS_MUCH_SPACE(1);
214 out
[j
++] = format
[i
++];
217 // We saw a '%'. What's next...
219 if ('%' == format
[i
]) {
220 // Replace '%%' with '%'.
221 ENSURE_THIS_MUCH_SPACE(1);
222 out
[j
++] = format
[i
++];
224 else if ('p' == format
[i
]) {
225 // Print the PID. Assume that it's not longer than 10 chars --
226 // reasonable since 'pid' is an Int (ie. 32 bits).
227 Int pid
= VG_(getpid
)();
228 ENSURE_THIS_MUCH_SPACE(10);
229 j
+= VG_(sprintf
)(&out
[j
], "%d", pid
);
232 else if ('n' == format
[i
]) {
236 Int pid
= VG_(getpid
)();
241 ENSURE_THIS_MUCH_SPACE(10);
242 j
+= VG_(sprintf
)(&out
[j
], "%d", seq_nr
);
245 else if ('q' == format
[i
]) {
247 if ('{' == format
[i
]) {
248 // Get the env var name, print its contents.
250 Int begin_qualname
= ++i
;
252 if (0 == format
[i
]) {
253 message
= "Missing '}' in %q specifier\n";
255 } else if ('}' == format
[i
]) {
256 Int qualname_len
= i
- begin_qualname
;
257 HChar qualname
[qualname_len
+ 1];
258 VG_(strncpy
)(qualname
, format
+ begin_qualname
,
260 qualname
[qualname_len
] = '\0';
261 qual
= VG_(getenv
)(qualname
);
263 // This memory will leak, But we don't care because
264 // VG_(fmsg_bad_option) will terminate the process.
265 HChar
*str
= VG_(malloc
)("options.efn.3",
268 "Environment variable '%s' is not set\n",
278 ENSURE_THIS_MUCH_SPACE(VG_(strlen
)(qual
));
279 j
+= VG_(sprintf
)(&out
[j
], "%s", qual
);
281 message
= "Expected '{' after '%q'\n";
286 // Something else, abort.
287 message
= "Expected 'p' or 'q' or '%' after '%'\n";
292 ENSURE_THIS_MUCH_SPACE(1);
295 // If 'out' is not an absolute path name, prefix it with the startup dir.
297 if (base_dir
== NULL
) {
298 message
= "Current working dir doesn't exist, use absolute path\n";
301 len
= VG_(strlen
)(base_dir
) + 1 + VG_(strlen
)(out
) + 1;
303 HChar
*absout
= VG_(malloc
)("options.efn.4", len
);
304 VG_(strcpy
)(absout
, base_dir
);
305 VG_(strcat
)(absout
, "/");
306 VG_(strcat
)(absout
, out
);
314 vg_assert(message
!= NULL
);
315 // 2: 1 for the '=', 1 for the NUL.
316 HChar opt
[VG_(strlen
)(option_name
) + VG_(strlen
)(format
) + 2];
317 VG_(sprintf
)(opt
, "%s=%s", option_name
, format
);
318 VG_(fmsg_bad_option
)(opt
, "%s", message
);
322 /*====================================================================*/
323 /*=== --trace-children= support ===*/
324 /*====================================================================*/
326 static HChar
const* consume_commas ( HChar
const* c
) {
327 while (*c
&& *c
== ',') {
333 static HChar
const* consume_field ( HChar
const* c
) {
334 while (*c
&& *c
!= ',') {
340 /* Should we trace into this child executable (across execve, spawn etc) ?
341 This involves considering --trace-children=,
342 --trace-children-skip=, --trace-children-skip-by-arg=, and the name
343 of the executable. 'child_argv' must not include the name of the
344 executable itself; iow child_argv[0] must be the first arg, if any,
346 Bool
VG_(should_we_trace_this_child
) ( const HChar
* child_exe_name
,
347 const HChar
** child_argv
)
349 // child_exe_name is pulled out of the guest's space. We
350 // should be at least marginally cautious with it, lest it
351 // explode or burst into flames unexpectedly.
352 if (child_exe_name
== NULL
|| VG_(strlen
)(child_exe_name
) == 0)
353 return VG_(clo_trace_children
); // we know narfink
355 // If --trace-children=no, the answer is simply NO.
356 if (! VG_(clo_trace_children
))
359 // Otherwise, look for other reasons to say NO. First,
360 // see if the exe name matches any of the patterns specified
361 // by --trace-children-skip=.
362 if (VG_(clo_trace_children_skip
)) {
363 HChar
const* last
= VG_(clo_trace_children_skip
);
364 HChar
const* name
= child_exe_name
;
368 HChar
const* first
= consume_commas(last
);
369 last
= consume_field(first
);
372 vg_assert(last
> first
);
373 /* copy the candidate string into a temporary malloc'd block
374 so we can use VG_(string_match) on it. */
375 patt
= VG_(calloc
)("m_options.swttc.1", last
- first
+ 1, 1);
376 VG_(memcpy
)(patt
, first
, last
- first
);
377 vg_assert(patt
[last
-first
] == 0);
378 matches
= VG_(string_match
)(patt
, name
);
385 // Check if any of the args match any of the patterns specified
386 // by --trace-children-skip-by-arg=.
387 if (VG_(clo_trace_children_skip_by_arg
) && child_argv
!= NULL
) {
388 HChar
const* last
= VG_(clo_trace_children_skip_by_arg
);
393 HChar
const* first
= consume_commas(last
);
394 last
= consume_field(first
);
397 vg_assert(last
> first
);
398 /* copy the candidate string into a temporary malloc'd block
399 so we can use VG_(string_match) on it. */
400 patt
= VG_(calloc
)("m_options.swttc.1", last
- first
+ 1, 1);
401 VG_(memcpy
)(patt
, first
, last
- first
);
402 vg_assert(patt
[last
-first
] == 0);
403 for (i
= 0; child_argv
[i
]; i
++) {
404 matches
= VG_(string_match
)(patt
, child_argv
[i
]);
414 // --trace-children=yes, and this particular executable isn't
420 /*--------------------------------------------------------------------*/
422 /*--------------------------------------------------------------------*/