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
};
56 #if defined(VGPV_arm_linux_android) \
57 || defined(VGPV_x86_linux_android) \
58 || defined(VGPV_mips32_linux_android) \
59 || defined(VGPV_arm64_linux_android)
60 VgVgdb
VG_(clo_vgdb
) = Vg_VgdbNo
; // currently disabled on Android
62 VgVgdb
VG_(clo_vgdb
) = Vg_VgdbYes
;
64 Int
VG_(clo_vgdb_poll
) = 5000;
65 Int
VG_(clo_vgdb_error
) = 999999999;
66 UInt
VG_(clo_vgdb_stop_at
) = 0;
67 const HChar
*VG_(clo_vgdb_prefix
) = NULL
;
68 const HChar
*VG_(arg_vgdb_prefix
) = NULL
;
69 Bool
VG_(clo_vgdb_shadow_registers
) = False
;
71 Int
VG_(clo_gen_suppressions
) = 0;
72 Int
VG_(clo_sanity_level
) = 1;
73 Int
VG_(clo_verbosity
) = 1;
74 Bool
VG_(clo_stats
) = False
;
75 Bool
VG_(clo_xml
) = False
;
76 const HChar
* VG_(clo_xml_user_comment
) = NULL
;
77 Bool
VG_(clo_demangle
) = True
;
78 const HChar
* VG_(clo_soname_synonyms
) = NULL
;
79 Bool
VG_(clo_trace_children
) = False
;
80 const HChar
* VG_(clo_trace_children_skip
) = NULL
;
81 const HChar
* VG_(clo_trace_children_skip_by_arg
) = NULL
;
82 Bool
VG_(clo_child_silent_after_fork
) = False
;
83 const HChar
*VG_(clo_log_fname_unexpanded
) = NULL
;
84 const HChar
*VG_(clo_xml_fname_unexpanded
) = NULL
;
85 Bool
VG_(clo_time_stamp
) = False
;
86 Int
VG_(clo_input_fd
) = 0; /* stdin */
87 Bool
VG_(clo_default_supp
) = True
;
88 XArray
*VG_(clo_suppressions
); // array of strings
89 XArray
*VG_(clo_fullpath_after
); // array of strings
90 const HChar
* VG_(clo_extra_debuginfo_path
) = NULL
;
91 const HChar
* VG_(clo_debuginfo_server
) = NULL
;
92 Bool
VG_(clo_allow_mismatched_debuginfo
) = False
;
93 UChar
VG_(clo_trace_flags
) = 0; // 00000000b
94 Bool
VG_(clo_profyle_sbs
) = False
;
95 UChar
VG_(clo_profyle_flags
) = 0; // 00000000b
96 ULong
VG_(clo_profyle_interval
) = 0;
97 Int
VG_(clo_trace_notbelow
) = -1; // unspecified
98 Int
VG_(clo_trace_notabove
) = -1; // unspecified
99 Bool
VG_(clo_trace_syscalls
) = False
;
100 Bool
VG_(clo_trace_signals
) = False
;
101 Bool
VG_(clo_trace_symtab
) = False
;
102 const HChar
* VG_(clo_trace_symtab_patt
) = "*";
103 Bool
VG_(clo_trace_cfi
) = False
;
104 Bool
VG_(clo_debug_dump_syms
) = False
;
105 Bool
VG_(clo_debug_dump_line
) = False
;
106 Bool
VG_(clo_debug_dump_frames
) = False
;
107 Bool
VG_(clo_trace_redir
) = False
;
109 VG_(clo_fair_sched
) = disable_fair_sched
;
110 Bool
VG_(clo_trace_sched
) = False
;
111 Bool
VG_(clo_profile_heap
) = False
;
112 Int
VG_(clo_core_redzone_size
) = CORE_REDZONE_DEFAULT_SZB
;
113 // A value != -1 overrides the tool-specific value
114 // VG_(needs_malloc_replacement).tool_client_redzone_szB
115 Int
VG_(clo_redzone_size
) = -1;
116 VgXTMemory
VG_(clo_xtree_memory
) = Vg_XTMemory_None
;
117 const HChar
* VG_(clo_xtree_memory_file
) = "xtmemory.kcg.%p";
118 Bool
VG_(clo_xtree_compress_strings
) = True
;
120 Int
VG_(clo_dump_error
) = 0;
121 Int
VG_(clo_backtrace_size
) = 12;
122 Int
VG_(clo_merge_recursive_frames
) = 0; // default value: no merge
123 UInt
VG_(clo_sim_hints
) = 0;
124 Bool
VG_(clo_sym_offsets
) = False
;
125 Bool
VG_(clo_read_inline_info
) = False
; // Or should be put it to True by default ???
126 Bool
VG_(clo_read_var_info
) = False
;
127 XArray
*VG_(clo_req_tsyms
); // array of strings
128 Bool
VG_(clo_run_libc_freeres
) = True
;
129 Bool
VG_(clo_run_cxx_freeres
) = True
;
130 Bool
VG_(clo_track_fds
) = False
;
131 Bool
VG_(clo_show_below_main
)= False
;
132 Bool
VG_(clo_show_emwarns
) = False
;
133 Word
VG_(clo_max_stackframe
) = 2000000;
134 UInt
VG_(clo_max_threads
) = MAX_THREADS_DEFAULT
;
135 Word
VG_(clo_main_stacksize
) = 0; /* use client's rlimit.stack */
136 Word
VG_(clo_valgrind_stacksize
) = VG_DEFAULT_STACK_ACTIVE_SZB
;
137 Bool
VG_(clo_wait_for_gdb
) = False
;
138 UInt
VG_(clo_kernel_variant
) = 0;
139 Bool
VG_(clo_dsymutil
) = True
;
140 Bool
VG_(clo_sigill_diag
) = True
;
141 UInt
VG_(clo_unw_stack_scan_thresh
) = 0; /* disabled by default */
142 UInt
VG_(clo_unw_stack_scan_frames
) = 5;
144 // Set clo_smc_check so that it provides transparent self modifying
145 // code support for "correct" programs at the smallest achievable
146 // expense for this arch.
147 #if defined(VGA_x86) || defined(VGA_amd64) || defined(VGA_s390x)
148 VgSmc
VG_(clo_smc_check
) = Vg_SmcAllNonFile
;
149 #elif defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le) \
150 || defined(VGA_arm) || defined(VGA_arm64) \
151 || defined(VGA_mips32) || defined(VGA_mips64)
152 VgSmc
VG_(clo_smc_check
) = Vg_SmcStack
;
154 # error "Unknown arch"
157 #if defined(VGO_darwin)
158 UInt
VG_(clo_resync_filter
) = 1; /* enabled, but quiet */
160 UInt
VG_(clo_resync_filter
) = 0; /* disabled */
164 /*====================================================================*/
165 /*=== File expansion ===*/
166 /*====================================================================*/
168 // Copies the string, prepending it with the startup working directory, and
169 // expanding %p and %q entries. Returns a new, malloc'd string.
170 HChar
* VG_(expand_file_name
)(const HChar
* option_name
, const HChar
* format
)
172 const HChar
*base_dir
;
173 Int len
, i
= 0, j
= 0;
175 const HChar
*message
= NULL
;
177 base_dir
= VG_(get_startup_wd
)();
179 if (VG_STREQ(format
, "")) {
181 message
= "No filename given\n";
185 // If 'format' starts with a '~', abort -- the user probably expected the
186 // shell to expand but it didn't (see bug 195268 for details). This means
187 // that we don't allow a legitimate filename beginning with '~' but that
188 // seems very unlikely.
189 if (format
[0] == '~') {
191 "Filename begins with '~'\n"
192 "You probably expected the shell to expand the '~', but it\n"
193 "didn't. The rules for '~'-expansion vary from shell to shell.\n"
194 "You might have more luck using $HOME instead.\n";
198 len
= VG_(strlen
)(format
) + 1;
199 out
= VG_(malloc
)( "options.efn.1", len
);
201 #define ENSURE_THIS_MUCH_SPACE(x) \
202 if (j + x >= len) { \
204 out = VG_(realloc)("options.efn.2(multiple)", out, len); \
208 if (format
[i
] != '%') {
209 ENSURE_THIS_MUCH_SPACE(1);
210 out
[j
++] = format
[i
++];
213 // We saw a '%'. What's next...
215 if ('%' == format
[i
]) {
216 // Replace '%%' with '%'.
217 ENSURE_THIS_MUCH_SPACE(1);
218 out
[j
++] = format
[i
++];
220 else if ('p' == format
[i
]) {
221 // Print the PID. Assume that it's not longer than 10 chars --
222 // reasonable since 'pid' is an Int (ie. 32 bits).
223 Int pid
= VG_(getpid
)();
224 ENSURE_THIS_MUCH_SPACE(10);
225 j
+= VG_(sprintf
)(&out
[j
], "%d", pid
);
228 else if ('n' == format
[i
]) {
232 Int pid
= VG_(getpid
)();
237 ENSURE_THIS_MUCH_SPACE(10);
238 j
+= VG_(sprintf
)(&out
[j
], "%d", seq_nr
);
241 else if ('q' == format
[i
]) {
243 if ('{' == format
[i
]) {
244 // Get the env var name, print its contents.
246 Int begin_qualname
= ++i
;
248 if (0 == format
[i
]) {
249 message
= "Missing '}' in %q specifier\n";
251 } else if ('}' == format
[i
]) {
252 Int qualname_len
= i
- begin_qualname
;
253 HChar qualname
[qualname_len
+ 1];
254 VG_(strncpy
)(qualname
, format
+ begin_qualname
,
256 qualname
[qualname_len
] = '\0';
257 qual
= VG_(getenv
)(qualname
);
259 // This memory will leak, But we don't care because
260 // VG_(fmsg_bad_option) will terminate the process.
261 HChar
*str
= VG_(malloc
)("options.efn.3",
264 "Environment variable '%s' is not set\n",
274 ENSURE_THIS_MUCH_SPACE(VG_(strlen
)(qual
));
275 j
+= VG_(sprintf
)(&out
[j
], "%s", qual
);
277 message
= "Expected '{' after '%q'\n";
282 // Something else, abort.
283 message
= "Expected 'p' or 'q' or '%' after '%'\n";
288 ENSURE_THIS_MUCH_SPACE(1);
291 // If 'out' is not an absolute path name, prefix it with the startup dir.
293 if (base_dir
== NULL
) {
294 message
= "Current working dir doesn't exist, use absolute path\n";
297 len
= VG_(strlen
)(base_dir
) + 1 + VG_(strlen
)(out
) + 1;
299 HChar
*absout
= VG_(malloc
)("options.efn.4", len
);
300 VG_(strcpy
)(absout
, base_dir
);
301 VG_(strcat
)(absout
, "/");
302 VG_(strcat
)(absout
, out
);
310 vg_assert(message
!= NULL
);
311 // 2: 1 for the '=', 1 for the NUL.
312 HChar opt
[VG_(strlen
)(option_name
) + VG_(strlen
)(format
) + 2];
313 VG_(sprintf
)(opt
, "%s=%s", option_name
, format
);
314 VG_(fmsg_bad_option
)(opt
, "%s", message
);
318 /*====================================================================*/
319 /*=== --trace-children= support ===*/
320 /*====================================================================*/
322 static HChar
const* consume_commas ( HChar
const* c
) {
323 while (*c
&& *c
== ',') {
329 static HChar
const* consume_field ( HChar
const* c
) {
330 while (*c
&& *c
!= ',') {
336 /* Should we trace into this child executable (across execve, spawn etc) ?
337 This involves considering --trace-children=,
338 --trace-children-skip=, --trace-children-skip-by-arg=, and the name
339 of the executable. 'child_argv' must not include the name of the
340 executable itself; iow child_argv[0] must be the first arg, if any,
342 Bool
VG_(should_we_trace_this_child
) ( const HChar
* child_exe_name
,
343 const HChar
** child_argv
)
345 // child_exe_name is pulled out of the guest's space. We
346 // should be at least marginally cautious with it, lest it
347 // explode or burst into flames unexpectedly.
348 if (child_exe_name
== NULL
|| VG_(strlen
)(child_exe_name
) == 0)
349 return VG_(clo_trace_children
); // we know narfink
351 // If --trace-children=no, the answer is simply NO.
352 if (! VG_(clo_trace_children
))
355 // Otherwise, look for other reasons to say NO. First,
356 // see if the exe name matches any of the patterns specified
357 // by --trace-children-skip=.
358 if (VG_(clo_trace_children_skip
)) {
359 HChar
const* last
= VG_(clo_trace_children_skip
);
360 HChar
const* name
= child_exe_name
;
364 HChar
const* first
= consume_commas(last
);
365 last
= consume_field(first
);
368 vg_assert(last
> first
);
369 /* copy the candidate string into a temporary malloc'd block
370 so we can use VG_(string_match) on it. */
371 patt
= VG_(calloc
)("m_options.swttc.1", last
- first
+ 1, 1);
372 VG_(memcpy
)(patt
, first
, last
- first
);
373 vg_assert(patt
[last
-first
] == 0);
374 matches
= VG_(string_match
)(patt
, name
);
381 // Check if any of the args match any of the patterns specified
382 // by --trace-children-skip-by-arg=.
383 if (VG_(clo_trace_children_skip_by_arg
) && child_argv
!= NULL
) {
384 HChar
const* last
= VG_(clo_trace_children_skip_by_arg
);
389 HChar
const* first
= consume_commas(last
);
390 last
= consume_field(first
);
393 vg_assert(last
> first
);
394 /* copy the candidate string into a temporary malloc'd block
395 so we can use VG_(string_match) on it. */
396 patt
= VG_(calloc
)("m_options.swttc.1", last
- first
+ 1, 1);
397 VG_(memcpy
)(patt
, first
, last
- first
);
398 vg_assert(patt
[last
-first
] == 0);
399 for (i
= 0; child_argv
[i
]; i
++) {
400 matches
= VG_(string_match
)(patt
, child_argv
[i
]);
410 // --trace-children=yes, and this particular executable isn't
416 /*--------------------------------------------------------------------*/
418 /*--------------------------------------------------------------------*/