2 /*--------------------------------------------------------------------*/
3 /*--- Command line options. m_options.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2013 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)
42 // See pub_{core,tool}_options.h for explanations of all these.
45 /* Define, and set defaults. */
46 VexControl
VG_(clo_vex_control
);
47 Bool
VG_(clo_error_limit
) = True
;
48 Int
VG_(clo_error_exitcode
) = 0;
50 #if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \
51 || defined(VGPV_mips32_linux_android) \
52 || defined(VGP_arm64_linux) // temporarily disabled on arm64-linux
53 VgVgdb
VG_(clo_vgdb
) = Vg_VgdbNo
; // currently disabled on Android
55 VgVgdb
VG_(clo_vgdb
) = Vg_VgdbYes
;
57 Int
VG_(clo_vgdb_poll
) = 5000;
58 Int
VG_(clo_vgdb_error
) = 999999999;
59 UInt
VG_(clo_vgdb_stop_at
) = 0;
60 const HChar
*VG_(clo_vgdb_prefix
) = NULL
;
61 const HChar
*VG_(arg_vgdb_prefix
) = NULL
;
62 Bool
VG_(clo_vgdb_shadow_registers
) = False
;
64 Bool
VG_(clo_db_attach
) = False
;
65 const HChar
* VG_(clo_db_command
) = GDB_PATH
" -nw %f %p";
66 Int
VG_(clo_gen_suppressions
) = 0;
67 Int
VG_(clo_sanity_level
) = 1;
68 Int
VG_(clo_verbosity
) = 1;
69 Bool
VG_(clo_stats
) = False
;
70 Bool
VG_(clo_xml
) = False
;
71 const HChar
* VG_(clo_xml_user_comment
) = NULL
;
72 Bool
VG_(clo_demangle
) = True
;
73 const HChar
* VG_(clo_soname_synonyms
) = NULL
;
74 Bool
VG_(clo_trace_children
) = False
;
75 const HChar
* VG_(clo_trace_children_skip
) = NULL
;
76 const HChar
* VG_(clo_trace_children_skip_by_arg
) = NULL
;
77 Bool
VG_(clo_child_silent_after_fork
) = False
;
78 HChar
* VG_(clo_log_fname_expanded
) = NULL
;
79 HChar
* VG_(clo_xml_fname_expanded
) = NULL
;
80 Bool
VG_(clo_time_stamp
) = False
;
81 Int
VG_(clo_input_fd
) = 0; /* stdin */
82 Int
VG_(clo_n_suppressions
) = 0;
83 const HChar
* VG_(clo_suppressions
)[VG_CLO_MAX_SFILES
];
84 Int
VG_(clo_n_fullpath_after
) = 0;
85 const HChar
* VG_(clo_fullpath_after
)[VG_CLO_MAX_FULLPATH_AFTER
];
86 const HChar
* VG_(clo_extra_debuginfo_path
) = NULL
;
87 const HChar
* VG_(clo_debuginfo_server
) = NULL
;
88 Bool
VG_(clo_allow_mismatched_debuginfo
) = False
;
89 UChar
VG_(clo_trace_flags
) = 0; // 00000000b
90 Bool
VG_(clo_profyle_sbs
) = False
;
91 UChar
VG_(clo_profyle_flags
) = 0; // 00000000b
92 ULong
VG_(clo_profyle_interval
) = 0;
93 Int
VG_(clo_trace_notbelow
) = -1; // unspecified
94 Int
VG_(clo_trace_notabove
) = -1; // unspecified
95 Bool
VG_(clo_trace_syscalls
) = False
;
96 Bool
VG_(clo_trace_signals
) = False
;
97 Bool
VG_(clo_trace_symtab
) = False
;
98 const HChar
* VG_(clo_trace_symtab_patt
) = "*";
99 Bool
VG_(clo_trace_cfi
) = False
;
100 Bool
VG_(clo_debug_dump_syms
) = False
;
101 Bool
VG_(clo_debug_dump_line
) = False
;
102 Bool
VG_(clo_debug_dump_frames
) = False
;
103 Bool
VG_(clo_trace_redir
) = False
;
105 VG_(clo_fair_sched
) = disable_fair_sched
;
106 Bool
VG_(clo_trace_sched
) = False
;
107 Bool
VG_(clo_profile_heap
) = False
;
108 Int
VG_(clo_core_redzone_size
) = CORE_REDZONE_DEFAULT_SZB
;
109 // A value != -1 overrides the tool-specific value
110 // VG_(needs_malloc_replacement).tool_client_redzone_szB
111 Int
VG_(clo_redzone_size
) = -1;
112 Int
VG_(clo_dump_error
) = 0;
113 Int
VG_(clo_backtrace_size
) = 12;
114 Int
VG_(clo_merge_recursive_frames
) = 0; // default value: no merge
115 const HChar
* VG_(clo_sim_hints
) = NULL
;
116 Bool
VG_(clo_sym_offsets
) = False
;
117 Bool
VG_(clo_read_var_info
) = False
;
118 Int
VG_(clo_n_req_tsyms
) = 0;
119 const HChar
* VG_(clo_req_tsyms
)[VG_CLO_MAX_REQ_TSYMS
];
120 HChar
* VG_(clo_require_text_symbol
) = NULL
;
121 Bool
VG_(clo_run_libc_freeres
) = True
;
122 Bool
VG_(clo_track_fds
) = False
;
123 Bool
VG_(clo_show_below_main
)= False
;
124 Bool
VG_(clo_show_emwarns
) = False
;
125 Word
VG_(clo_max_stackframe
) = 2000000;
126 Word
VG_(clo_main_stacksize
) = 0; /* use client's rlimit.stack */
127 Bool
VG_(clo_wait_for_gdb
) = False
;
128 VgSmc
VG_(clo_smc_check
) = Vg_SmcStack
;
129 const HChar
* VG_(clo_kernel_variant
) = NULL
;
130 Bool
VG_(clo_dsymutil
) = False
;
131 Bool
VG_(clo_sigill_diag
) = True
;
132 UInt
VG_(clo_unw_stack_scan_thresh
) = 0; /* disabled by default */
133 UInt
VG_(clo_unw_stack_scan_frames
) = 5;
136 /*====================================================================*/
137 /*=== File expansion ===*/
138 /*====================================================================*/
140 // Copies the string, prepending it with the startup working directory, and
141 // expanding %p and %q entries. Returns a new, malloc'd string.
142 HChar
* VG_(expand_file_name
)(const HChar
* option_name
, const HChar
* format
)
144 static HChar base_dir
[VKI_PATH_MAX
];
145 Int len
, i
= 0, j
= 0;
148 Bool ok
= VG_(get_startup_wd
)(base_dir
, VKI_PATH_MAX
);
151 if (VG_STREQ(format
, "")) {
153 VG_(fmsg
)("%s: filename is empty", option_name
);
157 // If 'format' starts with a '~', abort -- the user probably expected the
158 // shell to expand but it didn't (see bug 195268 for details). This means
159 // that we don't allow a legitimate filename beginning with '~' but that
160 // seems very unlikely.
161 if (format
[0] == '~') {
163 "%s: filename begins with '~'\n"
164 "You probably expected the shell to expand the '~', but it\n"
165 "didn't. The rules for '~'-expansion vary from shell to shell.\n"
166 "You might have more luck using $HOME instead.\n",
172 len
= VG_(strlen
)(format
) + 1;
173 out
= VG_(malloc
)( "options.efn.1", len
);
175 #define ENSURE_THIS_MUCH_SPACE(x) \
176 if (j + x >= len) { \
178 out = VG_(realloc)("options.efn.2(multiple)", out, len); \
182 if (format
[i
] != '%') {
183 ENSURE_THIS_MUCH_SPACE(1);
184 out
[j
++] = format
[i
++];
187 // We saw a '%'. What's next...
189 if ('%' == format
[i
]) {
190 // Replace '%%' with '%'.
191 ENSURE_THIS_MUCH_SPACE(1);
192 out
[j
++] = format
[i
++];
194 else if ('p' == format
[i
]) {
195 // Print the PID. Assume that it's not longer than 10 chars --
196 // reasonable since 'pid' is an Int (ie. 32 bits).
197 Int pid
= VG_(getpid
)();
198 ENSURE_THIS_MUCH_SPACE(10);
199 j
+= VG_(sprintf
)(&out
[j
], "%d", pid
);
202 else if ('q' == format
[i
]) {
204 if ('{' == format
[i
]) {
205 // Get the env var name, print its contents.
206 const HChar
* qualname
;
209 qualname
= &format
[i
];
211 if (0 == format
[i
]) {
212 VG_(fmsg
)("%s: malformed %%q specifier\n", option_name
);
214 } else if ('}' == format
[i
]) {
215 // Temporarily replace the '}' with NUL to extract var
217 // FIXME: this is not safe as FORMAT is sometimes a
218 // string literal which may reside in read-only memory
219 ((HChar
*)format
)[i
] = 0;
220 qual
= VG_(getenv
)(qualname
);
222 VG_(fmsg
)("%s: environment variable %s is not set\n",
223 option_name
, qualname
);
224 // FIXME: this is not safe as FORMAT is sometimes a
225 // string literal which may reside in read-only memory
226 ((HChar
*)format
)[i
] = '}'; // Put the '}' back.
229 // FIXME: this is not safe as FORMAT is sometimes a
230 // string literal which may reside in read-only memory
231 ((HChar
*)format
)[i
] = '}'; // Put the '}' back.
237 ENSURE_THIS_MUCH_SPACE(VG_(strlen
)(qual
));
238 j
+= VG_(sprintf
)(&out
[j
], "%s", qual
);
240 VG_(fmsg
)("%s: expected '{' after '%%q'\n", option_name
);
245 // Something else, abort.
246 VG_(fmsg
)("%s: expected 'p' or 'q' or '%%' after '%%'\n",
252 ENSURE_THIS_MUCH_SPACE(1);
255 // If 'out' is not an absolute path name, prefix it with the startup dir.
257 len
= VG_(strlen
)(base_dir
) + 1 + VG_(strlen
)(out
) + 1;
259 HChar
*absout
= VG_(malloc
)("options.efn.4", len
);
260 VG_(strcpy
)(absout
, base_dir
);
261 VG_(strcat
)(absout
, "/");
262 VG_(strcat
)(absout
, out
);
270 HChar
* opt
= // 2: 1 for the '=', 1 for the NUL.
271 VG_(malloc
)( "options.efn.3",
272 VG_(strlen
)(option_name
) + VG_(strlen
)(format
) + 2 );
273 VG_(strcpy
)(opt
, option_name
);
274 VG_(strcat
)(opt
, "=");
275 VG_(strcat
)(opt
, format
);
276 VG_(fmsg_bad_option
)(opt
, "");
280 /*====================================================================*/
281 /*=== --trace-children= support ===*/
282 /*====================================================================*/
284 static HChar
const* consume_commas ( HChar
const* c
) {
285 while (*c
&& *c
== ',') {
291 static HChar
const* consume_field ( HChar
const* c
) {
292 while (*c
&& *c
!= ',') {
298 /* Should we trace into this child executable (across execve etc) ?
299 This involves considering --trace-children=,
300 --trace-children-skip=, --trace-children-skip-by-arg=, and the name
301 of the executable. 'child_argv' must not include the name of the
302 executable itself; iow child_argv[0] must be the first arg, if any,
304 Bool
VG_(should_we_trace_this_child
) ( HChar
* child_exe_name
,
307 // child_exe_name is pulled out of the guest's space. We
308 // should be at least marginally cautious with it, lest it
309 // explode or burst into flames unexpectedly.
310 if (child_exe_name
== NULL
|| VG_(strlen
)(child_exe_name
) == 0)
311 return VG_(clo_trace_children
); // we know narfink
313 // If --trace-children=no, the answer is simply NO.
314 if (! VG_(clo_trace_children
))
317 // Otherwise, look for other reasons to say NO. First,
318 // see if the exe name matches any of the patterns specified
319 // by --trace-children-skip=.
320 if (VG_(clo_trace_children_skip
)) {
321 HChar
const* last
= VG_(clo_trace_children_skip
);
322 HChar
const* name
= child_exe_name
;
326 HChar
const* first
= consume_commas(last
);
327 last
= consume_field(first
);
330 vg_assert(last
> first
);
331 /* copy the candidate string into a temporary malloc'd block
332 so we can use VG_(string_match) on it. */
333 patt
= VG_(calloc
)("m_options.swttc.1", last
- first
+ 1, 1);
334 VG_(memcpy
)(patt
, first
, last
- first
);
335 vg_assert(patt
[last
-first
] == 0);
336 matches
= VG_(string_match
)(patt
, name
);
343 // Check if any of the args match any of the patterns specified
344 // by --trace-children-skip-by-arg=.
345 if (VG_(clo_trace_children_skip_by_arg
) && child_argv
!= NULL
) {
346 HChar
const* last
= VG_(clo_trace_children_skip_by_arg
);
351 HChar
const* first
= consume_commas(last
);
352 last
= consume_field(first
);
355 vg_assert(last
> first
);
356 /* copy the candidate string into a temporary malloc'd block
357 so we can use VG_(string_match) on it. */
358 patt
= VG_(calloc
)("m_options.swttc.1", last
- first
+ 1, 1);
359 VG_(memcpy
)(patt
, first
, last
- first
);
360 vg_assert(patt
[last
-first
] == 0);
361 for (i
= 0; child_argv
[i
]; i
++) {
362 matches
= VG_(string_match
)(patt
, child_argv
[i
]);
372 // --trace-children=yes, and this particular executable isn't
378 /*--------------------------------------------------------------------*/
380 /*--------------------------------------------------------------------*/