blob: a1de2491803f5a761b164a57d1e643b6c70afc58 [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;
florianb985e2d2011-09-29 03:03:45 +000057HChar* 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;
61Char* VG_(clo_db_command) = GDB_PATH " -nw %f %p";
62Int 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;
sewardj768db0e2005-07-19 14:18:56 +000067HChar* VG_(clo_xml_user_comment) = NULL;
njna3506d32005-05-18 04:36:01 +000068Bool VG_(clo_demangle) = True;
philippe1e470b52012-05-11 19:33:46 +000069HChar* VG_(clo_soname_synonyms) = NULL;
njna3506d32005-05-18 04:36:01 +000070Bool VG_(clo_trace_children) = False;
sewardj06421272009-11-05 08:55:13 +000071HChar* VG_(clo_trace_children_skip) = NULL;
sewardj9ab64a42010-12-06 11:40:04 +000072HChar* VG_(clo_trace_children_skip_by_arg) = NULL;
sewardj6e31f802007-11-17 22:29:25 +000073Bool VG_(clo_child_silent_after_fork) = False;
sewardj738856f2009-07-15 14:48:32 +000074Char* VG_(clo_log_fname_expanded) = NULL;
75Char* 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;
79Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
sewardj14cdbf82010-10-12 00:44:05 +000080Int VG_(clo_n_fullpath_after) = 0;
81Char* VG_(clo_fullpath_after)[VG_CLO_MAX_FULLPATH_AFTER];
njna3506d32005-05-18 04:36:01 +000082UChar VG_(clo_trace_flags) = 0; // 00000000b
83UChar VG_(clo_profile_flags) = 0; // 00000000b
florian29e022d2012-07-02 21:13:34 +000084Int VG_(clo_trace_notbelow) = -1; // unspecified
85Int VG_(clo_trace_notabove) = -1; // unspecified
njna3506d32005-05-18 04:36:01 +000086Bool VG_(clo_trace_syscalls) = False;
87Bool VG_(clo_trace_signals) = False;
88Bool VG_(clo_trace_symtab) = False;
sewardjf767d962007-02-12 17:47:14 +000089HChar* VG_(clo_trace_symtab_patt) = "*";
njna3506d32005-05-18 04:36:01 +000090Bool VG_(clo_trace_cfi) = False;
sewardjf767d962007-02-12 17:47:14 +000091Bool VG_(clo_debug_dump_syms) = False;
92Bool VG_(clo_debug_dump_line) = False;
93Bool VG_(clo_debug_dump_frames) = False;
njna3506d32005-05-18 04:36:01 +000094Bool VG_(clo_trace_redir) = False;
bart78bfc712011-12-08 16:14:59 +000095enum FairSchedType
96 VG_(clo_fair_sched) = disable_fair_sched;
njna3506d32005-05-18 04:36:01 +000097Bool VG_(clo_trace_sched) = False;
sewardj9c606bd2008-09-18 18:12:50 +000098Bool VG_(clo_profile_heap) = False;
philipped99c26a2012-07-31 22:17:28 +000099Int VG_(clo_core_redzone_size) = CORE_REDZONE_DEFAULT_SZB;
100// A value != -1 overrides the tool-specific value
101// VG_(needs_malloc_replacement).tool_client_redzone_szB
102Int VG_(clo_redzone_size) = -1;
njna3506d32005-05-18 04:36:01 +0000103Int VG_(clo_dump_error) = 0;
104Int VG_(clo_backtrace_size) = 12;
njn628add62005-11-12 18:21:40 +0000105Char* VG_(clo_sim_hints) = NULL;
sewardj41ded2c2006-10-17 01:34:57 +0000106Bool VG_(clo_sym_offsets) = False;
sewardjb8b79ad2008-03-03 01:35:41 +0000107Bool VG_(clo_read_var_info) = False;
sewardjf9ebc392010-05-09 22:30:43 +0000108Int VG_(clo_n_req_tsyms) = 0;
109HChar* VG_(clo_req_tsyms)[VG_CLO_MAX_REQ_TSYMS];
110HChar* VG_(clo_require_text_symbol) = NULL;
njna3506d32005-05-18 04:36:01 +0000111Bool VG_(clo_run_libc_freeres) = True;
112Bool VG_(clo_track_fds) = False;
113Bool VG_(clo_show_below_main)= False;
njna3506d32005-05-18 04:36:01 +0000114Bool VG_(clo_show_emwarns) = False;
sewardj91b470c2007-08-28 17:03:01 +0000115Word VG_(clo_max_stackframe) = 2000000;
sewardj95d86c02007-12-18 01:49:23 +0000116Word VG_(clo_main_stacksize) = 0; /* use client's rlimit.stack */
njna3506d32005-05-18 04:36:01 +0000117Bool VG_(clo_wait_for_gdb) = False;
sewardj6c3a2192005-07-24 07:00:45 +0000118VgSmc VG_(clo_smc_check) = Vg_SmcStack;
sewardjce5a5662005-10-06 03:19:49 +0000119HChar* VG_(clo_kernel_variant) = NULL;
njn97db7612009-08-04 02:32:55 +0000120Bool VG_(clo_dsymutil) = False;
sewardj26412bd2005-07-07 10:05:05 +0000121
sewardj32824022005-05-17 13:45:04 +0000122
sewardj6893d652006-10-15 01:25:13 +0000123/*====================================================================*/
njnb1cc5d62010-07-06 04:05:23 +0000124/*=== File expansion ===*/
sewardj6893d652006-10-15 01:25:13 +0000125/*====================================================================*/
126
njn374a36d2007-11-23 01:41:32 +0000127// Copies the string, prepending it with the startup working directory, and
128// expanding %p and %q entries. Returns a new, malloc'd string.
129Char* VG_(expand_file_name)(Char* option_name, Char* format)
130{
131 static Char base_dir[VKI_PATH_MAX];
132 Int len, i = 0, j = 0;
133 Char* out;
134
135 Bool ok = VG_(get_startup_wd)(base_dir, VKI_PATH_MAX);
136 tl_assert(ok);
137
138 if (VG_STREQ(format, "")) {
139 // Empty name, bad.
njnb1cc5d62010-07-06 04:05:23 +0000140 VG_(fmsg)("%s: filename is empty", option_name);
njn5542d8f2009-06-09 00:50:45 +0000141 goto bad;
142 }
143
144 // If 'format' starts with a '~', abort -- the user probably expected the
145 // shell to expand but it didn't (see bug 195268 for details). This means
146 // that we don't allow a legitimate filename beginning with '~' but that
147 // seems very unlikely.
148 if (format[0] == '~') {
njnb1cc5d62010-07-06 04:05:23 +0000149 VG_(fmsg)(
150 "%s: filename begins with '~'\n"
151 "You probably expected the shell to expand the '~', but it\n"
152 "didn't. The rules for '~'-expansion vary from shell to shell.\n"
153 "You might have more luck using $HOME instead.\n",
154 option_name
155 );
njn374a36d2007-11-23 01:41:32 +0000156 goto bad;
157 }
158
njn2dd08f52007-11-23 22:37:35 +0000159 // If 'format' starts with a '/', do not prefix with startup dir.
160 if (format[0] != '/') {
161 j += VG_(strlen)(base_dir);
162 }
163
njn374a36d2007-11-23 01:41:32 +0000164 // The 10 is slop, it should be enough in most cases.
njn374a36d2007-11-23 01:41:32 +0000165 len = j + VG_(strlen)(format) + 10;
sewardj9c606bd2008-09-18 18:12:50 +0000166 out = VG_(malloc)( "options.efn.1", len );
njn2dd08f52007-11-23 22:37:35 +0000167 if (format[0] != '/') {
168 VG_(strcpy)(out, base_dir);
169 out[j++] = '/';
170 }
njn374a36d2007-11-23 01:41:32 +0000171
172#define ENSURE_THIS_MUCH_SPACE(x) \
173 if (j + x >= len) { \
174 len += (10 + x); \
sewardj9c606bd2008-09-18 18:12:50 +0000175 out = VG_(realloc)("options.efn.2(multiple)", out, len); \
njn374a36d2007-11-23 01:41:32 +0000176 }
177
njn374a36d2007-11-23 01:41:32 +0000178 while (format[i]) {
179 if (format[i] != '%') {
180 ENSURE_THIS_MUCH_SPACE(1);
181 out[j++] = format[i++];
182
183 } else {
184 // We saw a '%'. What's next...
185 i++;
186 if ('%' == format[i]) {
187 // Replace '%%' with '%'.
188 ENSURE_THIS_MUCH_SPACE(1);
189 out[j++] = format[i++];
190 }
191 else if ('p' == format[i]) {
192 // Print the PID. Assume that it's not longer than 10 chars --
193 // reasonable since 'pid' is an Int (ie. 32 bits).
194 Int pid = VG_(getpid)();
195 ENSURE_THIS_MUCH_SPACE(10);
196 j += VG_(sprintf)(&out[j], "%d", pid);
197 i++;
198 }
njn2dd08f52007-11-23 22:37:35 +0000199 else if ('q' == format[i]) {
200 i++;
201 if ('{' == format[i]) {
202 // Get the env var name, print its contents.
203 Char* qualname;
204 Char* qual;
njn374a36d2007-11-23 01:41:32 +0000205 i++;
njn2dd08f52007-11-23 22:37:35 +0000206 qualname = &format[i];
207 while (True) {
208 if (0 == format[i]) {
njnb1cc5d62010-07-06 04:05:23 +0000209 VG_(fmsg)("%s: malformed %%q specifier\n", option_name);
njn2dd08f52007-11-23 22:37:35 +0000210 goto bad;
211 } else if ('}' == format[i]) {
212 // Temporarily replace the '}' with NUL to extract var
213 // name.
214 format[i] = 0;
215 qual = VG_(getenv)(qualname);
216 if (NULL == qual) {
njnb1cc5d62010-07-06 04:05:23 +0000217 VG_(fmsg)("%s: environment variable %s is not set\n",
218 option_name, qualname);
njn2dd08f52007-11-23 22:37:35 +0000219 format[i] = '}'; // Put the '}' back.
220 goto bad;
221 }
222 format[i] = '}'; // Put the '}' back.
223 i++;
224 break;
225 }
226 i++;
227 }
228 ENSURE_THIS_MUCH_SPACE(VG_(strlen)(qual));
229 j += VG_(sprintf)(&out[j], "%s", qual);
230 } else {
njnb1cc5d62010-07-06 04:05:23 +0000231 VG_(fmsg)("%s: expected '{' after '%%q'\n", option_name);
njn2dd08f52007-11-23 22:37:35 +0000232 goto bad;
njn374a36d2007-11-23 01:41:32 +0000233 }
njn374a36d2007-11-23 01:41:32 +0000234 }
235 else {
236 // Something else, abort.
njnb1cc5d62010-07-06 04:05:23 +0000237 VG_(fmsg)("%s: expected 'p' or 'q' or '%%' after '%%'\n",
238 option_name);
njn374a36d2007-11-23 01:41:32 +0000239 goto bad;
240 }
241 }
242 }
243 ENSURE_THIS_MUCH_SPACE(1);
244 out[j++] = 0;
245
246 return out;
247
248 bad: {
249 Char* opt = // 2: 1 for the '=', 1 for the NUL.
sewardj9c606bd2008-09-18 18:12:50 +0000250 VG_(malloc)( "options.efn.3",
251 VG_(strlen)(option_name) + VG_(strlen)(format) + 2 );
njn374a36d2007-11-23 01:41:32 +0000252 VG_(strcpy)(opt, option_name);
253 VG_(strcat)(opt, "=");
254 VG_(strcat)(opt, format);
njnb1cc5d62010-07-06 04:05:23 +0000255 VG_(fmsg_bad_option)(opt, "");
njn374a36d2007-11-23 01:41:32 +0000256 }
257}
258
sewardj06421272009-11-05 08:55:13 +0000259/*====================================================================*/
260/*=== --trace-children= support ===*/
261/*====================================================================*/
262
263static HChar const* consume_commas ( HChar const* c ) {
264 while (*c && *c == ',') {
265 ++c;
266 }
267 return c;
268}
269
270static HChar const* consume_field ( HChar const* c ) {
271 while (*c && *c != ',') {
272 ++c;
273 }
274 return c;
275}
276
277/* Should we trace into this child executable (across execve etc) ?
sewardj9ab64a42010-12-06 11:40:04 +0000278 This involves considering --trace-children=,
279 --trace-children-skip=, --trace-children-skip-by-arg=, and the name
280 of the executable. 'child_argv' must not include the name of the
281 executable itself; iow child_argv[0] must be the first arg, if any,
282 for the child. */
283Bool VG_(should_we_trace_this_child) ( HChar* child_exe_name,
284 HChar** child_argv )
sewardj06421272009-11-05 08:55:13 +0000285{
286 // child_exe_name is pulled out of the guest's space. We
287 // should be at least marginally cautious with it, lest it
288 // explode or burst into flames unexpectedly.
289 if (child_exe_name == NULL || VG_(strlen)(child_exe_name) == 0)
290 return VG_(clo_trace_children); // we know narfink
291
sewardj06421272009-11-05 08:55:13 +0000292 // If --trace-children=no, the answer is simply NO.
293 if (! VG_(clo_trace_children))
294 return False;
295
sewardj9ab64a42010-12-06 11:40:04 +0000296 // Otherwise, look for other reasons to say NO. First,
297 // see if the exe name matches any of the patterns specified
298 // by --trace-children-skip=.
sewardj06421272009-11-05 08:55:13 +0000299 if (VG_(clo_trace_children_skip)) {
300 HChar const* last = VG_(clo_trace_children_skip);
301 HChar const* name = (HChar const*)child_exe_name;
302 while (*last) {
303 Bool matches;
304 HChar* patt;
305 HChar const* first = consume_commas(last);
306 last = consume_field(first);
307 if (first == last)
308 break;
309 vg_assert(last > first);
310 /* copy the candidate string into a temporary malloc'd block
311 so we can use VG_(string_match) on it. */
312 patt = VG_(calloc)("m_options.swttc.1", last - first + 1, 1);
313 VG_(memcpy)(patt, first, last - first);
314 vg_assert(patt[last-first] == 0);
315 matches = VG_(string_match)(patt, name);
316 VG_(free)(patt);
317 if (matches)
318 return False;
319 }
320 }
sewardj9ab64a42010-12-06 11:40:04 +0000321
322 // Check if any of the args match any of the patterns specified
323 // by --trace-children-skip-by-arg=.
324 if (VG_(clo_trace_children_skip_by_arg) && child_argv != NULL) {
325 HChar const* last = VG_(clo_trace_children_skip_by_arg);
326 while (*last) {
327 Int i;
328 Bool matches;
329 HChar* patt;
330 HChar const* first = consume_commas(last);
331 last = consume_field(first);
332 if (first == last)
333 break;
334 vg_assert(last > first);
335 /* copy the candidate string into a temporary malloc'd block
336 so we can use VG_(string_match) on it. */
337 patt = VG_(calloc)("m_options.swttc.1", last - first + 1, 1);
338 VG_(memcpy)(patt, first, last - first);
339 vg_assert(patt[last-first] == 0);
340 for (i = 0; child_argv[i]; i++) {
341 matches = VG_(string_match)(patt, child_argv[i]);
342 if (matches) {
343 VG_(free)(patt);
344 return False;
345 }
346 }
347 VG_(free)(patt);
348 }
349 }
350
sewardj06421272009-11-05 08:55:13 +0000351 // --trace-children=yes, and this particular executable isn't
352 // excluded
353 return True;
354}
njn374a36d2007-11-23 01:41:32 +0000355
sewardj6893d652006-10-15 01:25:13 +0000356
sewardj32824022005-05-17 13:45:04 +0000357/*--------------------------------------------------------------------*/
njnb1cc5d62010-07-06 04:05:23 +0000358/*--- end ---*/
sewardj32824022005-05-17 13:45:04 +0000359/*--------------------------------------------------------------------*/