blob: 54851a0f1d54476eb0b11a63157b1875674273df [file] [log] [blame]
sewardj32824022005-05-17 13:45:04 +00001
2/*--------------------------------------------------------------------*/
njnb1cc5d62010-07-06 04:05:23 +00003/*--- Command line options. m_options.c ---*/
sewardj32824022005-05-17 13:45:04 +00004/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj03f8d3f2012-08-05 15:46:46 +000010 Copyright (C) 2000-2012 Nicholas Nethercote
sewardj32824022005-05-17 13:45:04 +000011 njn@valgrind.org
12
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.
17
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.
22
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
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
njn374a36d2007-11-23 01:41:32 +000032#include "pub_core_vki.h"
njna3506d32005-05-18 04:36:01 +000033#include "pub_core_options.h"
sewardj6893d652006-10-15 01:25:13 +000034#include "pub_core_libcassert.h"
njn374a36d2007-11-23 01:41:32 +000035#include "pub_core_libcbase.h"
36#include "pub_core_libcfile.h"
sewardj6893d652006-10-15 01:25:13 +000037#include "pub_core_libcprint.h"
njn374a36d2007-11-23 01:41:32 +000038#include "pub_core_libcproc.h"
39#include "pub_core_mallocfree.h"
sewardj06421272009-11-05 08:55:13 +000040#include "pub_core_seqmatch.h" // VG_(string_match)
sewardj32824022005-05-17 13:45:04 +000041
njna3506d32005-05-18 04:36:01 +000042// See pub_{core,tool}_options.h for explanations of all these.
43
44
45/* Define, and set defaults. */
46VexControl VG_(clo_vex_control);
47Bool VG_(clo_error_limit) = True;
sewardjb9779082006-05-12 23:50:15 +000048Int VG_(clo_error_exitcode) = 0;
sewardj0ba37c92011-07-12 11:46:24 +000049
philippe5d5dd8e2012-08-05 00:08:25 +000050#if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android)
sewardjdeddfdf2011-10-26 15:07:56 +000051VgVgdb VG_(clo_vgdb) = Vg_VgdbNo; // currently disabled on Android
52#else
sewardj0ba37c92011-07-12 11:46:24 +000053VgVgdb VG_(clo_vgdb) = Vg_VgdbYes;
sewardjdeddfdf2011-10-26 15:07:56 +000054#endif
sewardj3b290482011-05-06 21:02:55 +000055Int VG_(clo_vgdb_poll) = 5000;
56Int VG_(clo_vgdb_error) = 999999999;
florian19f91bb2012-11-10 22:29:54 +000057const HChar* VG_(clo_vgdb_prefix) = NULL;
sewardj3b290482011-05-06 21:02:55 +000058Bool VG_(clo_vgdb_shadow_registers) = False;
sewardj0ba37c92011-07-12 11:46:24 +000059
njna3506d32005-05-18 04:36:01 +000060Bool VG_(clo_db_attach) = False;
florian19f91bb2012-11-10 22:29:54 +000061const HChar* VG_(clo_db_command) = GDB_PATH " -nw %f %p";
njna3506d32005-05-18 04:36:01 +000062Int VG_(clo_gen_suppressions) = 0;
63Int VG_(clo_sanity_level) = 1;
64Int VG_(clo_verbosity) = 1;
sewardj2d9e8742009-08-07 15:46:56 +000065Bool VG_(clo_stats) = False;
sewardj71bc3cb2005-05-19 00:25:45 +000066Bool VG_(clo_xml) = False;
florian19f91bb2012-11-10 22:29:54 +000067const HChar* VG_(clo_xml_user_comment) = NULL;
njna3506d32005-05-18 04:36:01 +000068Bool VG_(clo_demangle) = True;
florian19f91bb2012-11-10 22:29:54 +000069const HChar* VG_(clo_soname_synonyms) = NULL;
njna3506d32005-05-18 04:36:01 +000070Bool VG_(clo_trace_children) = False;
florian19f91bb2012-11-10 22:29:54 +000071const HChar* VG_(clo_trace_children_skip) = NULL;
72const HChar* VG_(clo_trace_children_skip_by_arg) = NULL;
sewardj6e31f802007-11-17 22:29:25 +000073Bool VG_(clo_child_silent_after_fork) = False;
florian19f91bb2012-11-10 22:29:54 +000074HChar* VG_(clo_log_fname_expanded) = NULL;
75HChar* VG_(clo_xml_fname_expanded) = NULL;
njna3506d32005-05-18 04:36:01 +000076Bool VG_(clo_time_stamp) = False;
77Int VG_(clo_input_fd) = 0; /* stdin */
78Int VG_(clo_n_suppressions) = 0;
florian19f91bb2012-11-10 22:29:54 +000079const HChar* VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
sewardj14cdbf82010-10-12 00:44:05 +000080Int VG_(clo_n_fullpath_after) = 0;
florian19f91bb2012-11-10 22:29:54 +000081const HChar* VG_(clo_fullpath_after)[VG_CLO_MAX_FULLPATH_AFTER];
sewardj71826f72012-12-06 18:23:20 +000082const HChar* VG_(clo_extra_debuginfo_path) = NULL;
njna3506d32005-05-18 04:36:01 +000083UChar VG_(clo_trace_flags) = 0; // 00000000b
84UChar VG_(clo_profile_flags) = 0; // 00000000b
florian29e022d2012-07-02 21:13:34 +000085Int VG_(clo_trace_notbelow) = -1; // unspecified
86Int VG_(clo_trace_notabove) = -1; // unspecified
njna3506d32005-05-18 04:36:01 +000087Bool VG_(clo_trace_syscalls) = False;
88Bool VG_(clo_trace_signals) = False;
89Bool VG_(clo_trace_symtab) = False;
florian19f91bb2012-11-10 22:29:54 +000090const HChar* VG_(clo_trace_symtab_patt) = "*";
njna3506d32005-05-18 04:36:01 +000091Bool VG_(clo_trace_cfi) = False;
sewardjf767d962007-02-12 17:47:14 +000092Bool VG_(clo_debug_dump_syms) = False;
93Bool VG_(clo_debug_dump_line) = False;
94Bool VG_(clo_debug_dump_frames) = False;
njna3506d32005-05-18 04:36:01 +000095Bool VG_(clo_trace_redir) = False;
bart78bfc712011-12-08 16:14:59 +000096enum FairSchedType
97 VG_(clo_fair_sched) = disable_fair_sched;
njna3506d32005-05-18 04:36:01 +000098Bool VG_(clo_trace_sched) = False;
sewardj9c606bd2008-09-18 18:12:50 +000099Bool VG_(clo_profile_heap) = False;
philipped99c26a2012-07-31 22:17:28 +0000100Int VG_(clo_core_redzone_size) = CORE_REDZONE_DEFAULT_SZB;
101// A value != -1 overrides the tool-specific value
102// VG_(needs_malloc_replacement).tool_client_redzone_szB
103Int VG_(clo_redzone_size) = -1;
njna3506d32005-05-18 04:36:01 +0000104Int VG_(clo_dump_error) = 0;
105Int VG_(clo_backtrace_size) = 12;
florian19f91bb2012-11-10 22:29:54 +0000106const HChar* VG_(clo_sim_hints) = NULL;
sewardj41ded2c2006-10-17 01:34:57 +0000107Bool VG_(clo_sym_offsets) = False;
sewardjb8b79ad2008-03-03 01:35:41 +0000108Bool VG_(clo_read_var_info) = False;
sewardjf9ebc392010-05-09 22:30:43 +0000109Int VG_(clo_n_req_tsyms) = 0;
florian19f91bb2012-11-10 22:29:54 +0000110const HChar* VG_(clo_req_tsyms)[VG_CLO_MAX_REQ_TSYMS];
sewardjf9ebc392010-05-09 22:30:43 +0000111HChar* VG_(clo_require_text_symbol) = NULL;
njna3506d32005-05-18 04:36:01 +0000112Bool VG_(clo_run_libc_freeres) = True;
113Bool VG_(clo_track_fds) = False;
114Bool VG_(clo_show_below_main)= False;
njna3506d32005-05-18 04:36:01 +0000115Bool VG_(clo_show_emwarns) = False;
sewardj91b470c2007-08-28 17:03:01 +0000116Word VG_(clo_max_stackframe) = 2000000;
sewardj95d86c02007-12-18 01:49:23 +0000117Word VG_(clo_main_stacksize) = 0; /* use client's rlimit.stack */
njna3506d32005-05-18 04:36:01 +0000118Bool VG_(clo_wait_for_gdb) = False;
sewardj6c3a2192005-07-24 07:00:45 +0000119VgSmc VG_(clo_smc_check) = Vg_SmcStack;
florian19f91bb2012-11-10 22:29:54 +0000120const HChar* VG_(clo_kernel_variant) = NULL;
njn97db7612009-08-04 02:32:55 +0000121Bool VG_(clo_dsymutil) = False;
sewardjc30cd9b2012-12-06 18:08:54 +0000122Bool VG_(clo_sigill_diag) = True;
sewardj32824022005-05-17 13:45:04 +0000123
sewardj6893d652006-10-15 01:25:13 +0000124/*====================================================================*/
njnb1cc5d62010-07-06 04:05:23 +0000125/*=== File expansion ===*/
sewardj6893d652006-10-15 01:25:13 +0000126/*====================================================================*/
127
njn374a36d2007-11-23 01:41:32 +0000128// Copies the string, prepending it with the startup working directory, and
129// expanding %p and %q entries. Returns a new, malloc'd string.
florian19f91bb2012-11-10 22:29:54 +0000130HChar* VG_(expand_file_name)(const HChar* option_name, const HChar* format)
njn374a36d2007-11-23 01:41:32 +0000131{
florian19f91bb2012-11-10 22:29:54 +0000132 static HChar base_dir[VKI_PATH_MAX];
njn374a36d2007-11-23 01:41:32 +0000133 Int len, i = 0, j = 0;
florian19f91bb2012-11-10 22:29:54 +0000134 HChar* out;
njn374a36d2007-11-23 01:41:32 +0000135
136 Bool ok = VG_(get_startup_wd)(base_dir, VKI_PATH_MAX);
137 tl_assert(ok);
138
139 if (VG_STREQ(format, "")) {
140 // Empty name, bad.
njnb1cc5d62010-07-06 04:05:23 +0000141 VG_(fmsg)("%s: filename is empty", option_name);
njn5542d8f2009-06-09 00:50:45 +0000142 goto bad;
143 }
144
145 // If 'format' starts with a '~', abort -- the user probably expected the
146 // shell to expand but it didn't (see bug 195268 for details). This means
147 // that we don't allow a legitimate filename beginning with '~' but that
148 // seems very unlikely.
149 if (format[0] == '~') {
njnb1cc5d62010-07-06 04:05:23 +0000150 VG_(fmsg)(
151 "%s: filename begins with '~'\n"
152 "You probably expected the shell to expand the '~', but it\n"
153 "didn't. The rules for '~'-expansion vary from shell to shell.\n"
154 "You might have more luck using $HOME instead.\n",
155 option_name
156 );
njn374a36d2007-11-23 01:41:32 +0000157 goto bad;
158 }
159
njn2dd08f52007-11-23 22:37:35 +0000160 // If 'format' starts with a '/', do not prefix with startup dir.
161 if (format[0] != '/') {
162 j += VG_(strlen)(base_dir);
163 }
164
njn374a36d2007-11-23 01:41:32 +0000165 // The 10 is slop, it should be enough in most cases.
njn374a36d2007-11-23 01:41:32 +0000166 len = j + VG_(strlen)(format) + 10;
sewardj9c606bd2008-09-18 18:12:50 +0000167 out = VG_(malloc)( "options.efn.1", len );
njn2dd08f52007-11-23 22:37:35 +0000168 if (format[0] != '/') {
169 VG_(strcpy)(out, base_dir);
170 out[j++] = '/';
171 }
njn374a36d2007-11-23 01:41:32 +0000172
173#define ENSURE_THIS_MUCH_SPACE(x) \
174 if (j + x >= len) { \
175 len += (10 + x); \
sewardj9c606bd2008-09-18 18:12:50 +0000176 out = VG_(realloc)("options.efn.2(multiple)", out, len); \
njn374a36d2007-11-23 01:41:32 +0000177 }
178
njn374a36d2007-11-23 01:41:32 +0000179 while (format[i]) {
180 if (format[i] != '%') {
181 ENSURE_THIS_MUCH_SPACE(1);
182 out[j++] = format[i++];
183
184 } else {
185 // We saw a '%'. What's next...
186 i++;
187 if ('%' == format[i]) {
188 // Replace '%%' with '%'.
189 ENSURE_THIS_MUCH_SPACE(1);
190 out[j++] = format[i++];
191 }
192 else if ('p' == format[i]) {
193 // Print the PID. Assume that it's not longer than 10 chars --
194 // reasonable since 'pid' is an Int (ie. 32 bits).
195 Int pid = VG_(getpid)();
196 ENSURE_THIS_MUCH_SPACE(10);
197 j += VG_(sprintf)(&out[j], "%d", pid);
198 i++;
199 }
njn2dd08f52007-11-23 22:37:35 +0000200 else if ('q' == format[i]) {
201 i++;
202 if ('{' == format[i]) {
203 // Get the env var name, print its contents.
florian19f91bb2012-11-10 22:29:54 +0000204 const HChar* qualname;
205 HChar* qual;
njn374a36d2007-11-23 01:41:32 +0000206 i++;
njn2dd08f52007-11-23 22:37:35 +0000207 qualname = &format[i];
208 while (True) {
209 if (0 == format[i]) {
njnb1cc5d62010-07-06 04:05:23 +0000210 VG_(fmsg)("%s: malformed %%q specifier\n", option_name);
njn2dd08f52007-11-23 22:37:35 +0000211 goto bad;
212 } else if ('}' == format[i]) {
213 // Temporarily replace the '}' with NUL to extract var
214 // name.
floriancfea7842012-10-20 19:52:14 +0000215 // FIXME: this is not safe as FORMAT is sometimes a
216 // string literal which may reside in read-only memory
florian19f91bb2012-11-10 22:29:54 +0000217 ((HChar *)format)[i] = 0;
njn2dd08f52007-11-23 22:37:35 +0000218 qual = VG_(getenv)(qualname);
219 if (NULL == qual) {
njnb1cc5d62010-07-06 04:05:23 +0000220 VG_(fmsg)("%s: environment variable %s is not set\n",
221 option_name, qualname);
florian19f91bb2012-11-10 22:29:54 +0000222 // FIXME: this is not safe as FORMAT is sometimes a
223 // string literal which may reside in read-only memory
224 ((HChar *)format)[i] = '}'; // Put the '}' back.
njn2dd08f52007-11-23 22:37:35 +0000225 goto bad;
226 }
florian19f91bb2012-11-10 22:29:54 +0000227 // FIXME: this is not safe as FORMAT is sometimes a
228 // string literal which may reside in read-only memory
229 ((HChar *)format)[i] = '}'; // Put the '}' back.
njn2dd08f52007-11-23 22:37:35 +0000230 i++;
231 break;
232 }
233 i++;
234 }
235 ENSURE_THIS_MUCH_SPACE(VG_(strlen)(qual));
236 j += VG_(sprintf)(&out[j], "%s", qual);
237 } else {
njnb1cc5d62010-07-06 04:05:23 +0000238 VG_(fmsg)("%s: expected '{' after '%%q'\n", option_name);
njn2dd08f52007-11-23 22:37:35 +0000239 goto bad;
njn374a36d2007-11-23 01:41:32 +0000240 }
njn374a36d2007-11-23 01:41:32 +0000241 }
242 else {
243 // Something else, abort.
njnb1cc5d62010-07-06 04:05:23 +0000244 VG_(fmsg)("%s: expected 'p' or 'q' or '%%' after '%%'\n",
245 option_name);
njn374a36d2007-11-23 01:41:32 +0000246 goto bad;
247 }
248 }
249 }
250 ENSURE_THIS_MUCH_SPACE(1);
251 out[j++] = 0;
252
253 return out;
254
255 bad: {
florian19f91bb2012-11-10 22:29:54 +0000256 HChar* opt = // 2: 1 for the '=', 1 for the NUL.
sewardj9c606bd2008-09-18 18:12:50 +0000257 VG_(malloc)( "options.efn.3",
258 VG_(strlen)(option_name) + VG_(strlen)(format) + 2 );
njn374a36d2007-11-23 01:41:32 +0000259 VG_(strcpy)(opt, option_name);
260 VG_(strcat)(opt, "=");
261 VG_(strcat)(opt, format);
njnb1cc5d62010-07-06 04:05:23 +0000262 VG_(fmsg_bad_option)(opt, "");
njn374a36d2007-11-23 01:41:32 +0000263 }
264}
265
sewardj06421272009-11-05 08:55:13 +0000266/*====================================================================*/
267/*=== --trace-children= support ===*/
268/*====================================================================*/
269
270static HChar const* consume_commas ( HChar const* c ) {
271 while (*c && *c == ',') {
272 ++c;
273 }
274 return c;
275}
276
277static HChar const* consume_field ( HChar const* c ) {
278 while (*c && *c != ',') {
279 ++c;
280 }
281 return c;
282}
283
284/* Should we trace into this child executable (across execve etc) ?
sewardj9ab64a42010-12-06 11:40:04 +0000285 This involves considering --trace-children=,
286 --trace-children-skip=, --trace-children-skip-by-arg=, and the name
287 of the executable. 'child_argv' must not include the name of the
288 executable itself; iow child_argv[0] must be the first arg, if any,
289 for the child. */
290Bool VG_(should_we_trace_this_child) ( HChar* child_exe_name,
291 HChar** child_argv )
sewardj06421272009-11-05 08:55:13 +0000292{
293 // child_exe_name is pulled out of the guest's space. We
294 // should be at least marginally cautious with it, lest it
295 // explode or burst into flames unexpectedly.
296 if (child_exe_name == NULL || VG_(strlen)(child_exe_name) == 0)
297 return VG_(clo_trace_children); // we know narfink
298
sewardj06421272009-11-05 08:55:13 +0000299 // If --trace-children=no, the answer is simply NO.
300 if (! VG_(clo_trace_children))
301 return False;
302
sewardj9ab64a42010-12-06 11:40:04 +0000303 // Otherwise, look for other reasons to say NO. First,
304 // see if the exe name matches any of the patterns specified
305 // by --trace-children-skip=.
sewardj06421272009-11-05 08:55:13 +0000306 if (VG_(clo_trace_children_skip)) {
307 HChar const* last = VG_(clo_trace_children_skip);
florian19f91bb2012-11-10 22:29:54 +0000308 HChar const* name = child_exe_name;
sewardj06421272009-11-05 08:55:13 +0000309 while (*last) {
310 Bool matches;
311 HChar* patt;
312 HChar const* first = consume_commas(last);
313 last = consume_field(first);
314 if (first == last)
315 break;
316 vg_assert(last > first);
317 /* copy the candidate string into a temporary malloc'd block
318 so we can use VG_(string_match) on it. */
319 patt = VG_(calloc)("m_options.swttc.1", last - first + 1, 1);
320 VG_(memcpy)(patt, first, last - first);
321 vg_assert(patt[last-first] == 0);
322 matches = VG_(string_match)(patt, name);
323 VG_(free)(patt);
324 if (matches)
325 return False;
326 }
327 }
sewardj9ab64a42010-12-06 11:40:04 +0000328
329 // Check if any of the args match any of the patterns specified
330 // by --trace-children-skip-by-arg=.
331 if (VG_(clo_trace_children_skip_by_arg) && child_argv != NULL) {
332 HChar const* last = VG_(clo_trace_children_skip_by_arg);
333 while (*last) {
334 Int i;
335 Bool matches;
336 HChar* patt;
337 HChar const* first = consume_commas(last);
338 last = consume_field(first);
339 if (first == last)
340 break;
341 vg_assert(last > first);
342 /* copy the candidate string into a temporary malloc'd block
343 so we can use VG_(string_match) on it. */
344 patt = VG_(calloc)("m_options.swttc.1", last - first + 1, 1);
345 VG_(memcpy)(patt, first, last - first);
346 vg_assert(patt[last-first] == 0);
347 for (i = 0; child_argv[i]; i++) {
348 matches = VG_(string_match)(patt, child_argv[i]);
349 if (matches) {
350 VG_(free)(patt);
351 return False;
352 }
353 }
354 VG_(free)(patt);
355 }
356 }
357
sewardj06421272009-11-05 08:55:13 +0000358 // --trace-children=yes, and this particular executable isn't
359 // excluded
360 return True;
361}
njn374a36d2007-11-23 01:41:32 +0000362
sewardj6893d652006-10-15 01:25:13 +0000363
sewardj32824022005-05-17 13:45:04 +0000364/*--------------------------------------------------------------------*/
njnb1cc5d62010-07-06 04:05:23 +0000365/*--- end ---*/
sewardj32824022005-05-17 13:45:04 +0000366/*--------------------------------------------------------------------*/