blob: 888e2c782d28a0968963bea6e0ac4989c0ca320a [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
sewardj9eecbbb2010-05-03 21:37:12 +000010 Copyright (C) 2000-2010 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;
njna3506d32005-05-18 04:36:01 +000049Bool VG_(clo_db_attach) = False;
50Char* VG_(clo_db_command) = GDB_PATH " -nw %f %p";
51Int VG_(clo_gen_suppressions) = 0;
52Int VG_(clo_sanity_level) = 1;
53Int VG_(clo_verbosity) = 1;
sewardj2d9e8742009-08-07 15:46:56 +000054Bool VG_(clo_stats) = False;
sewardj71bc3cb2005-05-19 00:25:45 +000055Bool VG_(clo_xml) = False;
sewardj768db0e2005-07-19 14:18:56 +000056HChar* VG_(clo_xml_user_comment) = NULL;
njna3506d32005-05-18 04:36:01 +000057Bool VG_(clo_demangle) = True;
58Bool VG_(clo_trace_children) = False;
sewardj06421272009-11-05 08:55:13 +000059HChar* VG_(clo_trace_children_skip) = NULL;
sewardj9ab64a42010-12-06 11:40:04 +000060HChar* VG_(clo_trace_children_skip_by_arg) = NULL;
sewardj6e31f802007-11-17 22:29:25 +000061Bool VG_(clo_child_silent_after_fork) = False;
sewardj738856f2009-07-15 14:48:32 +000062Char* VG_(clo_log_fname_expanded) = NULL;
63Char* VG_(clo_xml_fname_expanded) = NULL;
njna3506d32005-05-18 04:36:01 +000064Bool VG_(clo_time_stamp) = False;
65Int VG_(clo_input_fd) = 0; /* stdin */
66Int VG_(clo_n_suppressions) = 0;
67Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
sewardj14cdbf82010-10-12 00:44:05 +000068Int VG_(clo_n_fullpath_after) = 0;
69Char* VG_(clo_fullpath_after)[VG_CLO_MAX_FULLPATH_AFTER];
njna3506d32005-05-18 04:36:01 +000070UChar VG_(clo_trace_flags) = 0; // 00000000b
71UChar VG_(clo_profile_flags) = 0; // 00000000b
sewardj33afdb52006-01-17 02:36:40 +000072Int VG_(clo_trace_notbelow) = 999999999;
njna3506d32005-05-18 04:36:01 +000073Bool VG_(clo_trace_syscalls) = False;
74Bool VG_(clo_trace_signals) = False;
75Bool VG_(clo_trace_symtab) = False;
sewardjf767d962007-02-12 17:47:14 +000076HChar* VG_(clo_trace_symtab_patt) = "*";
njna3506d32005-05-18 04:36:01 +000077Bool VG_(clo_trace_cfi) = False;
sewardjf767d962007-02-12 17:47:14 +000078Bool VG_(clo_debug_dump_syms) = False;
79Bool VG_(clo_debug_dump_line) = False;
80Bool VG_(clo_debug_dump_frames) = False;
njna3506d32005-05-18 04:36:01 +000081Bool VG_(clo_trace_redir) = False;
82Bool VG_(clo_trace_sched) = False;
sewardj9c606bd2008-09-18 18:12:50 +000083Bool VG_(clo_profile_heap) = False;
njna3506d32005-05-18 04:36:01 +000084Int VG_(clo_dump_error) = 0;
85Int VG_(clo_backtrace_size) = 12;
njn628add62005-11-12 18:21:40 +000086Char* VG_(clo_sim_hints) = NULL;
sewardj41ded2c2006-10-17 01:34:57 +000087Bool VG_(clo_sym_offsets) = False;
sewardjb8b79ad2008-03-03 01:35:41 +000088Bool VG_(clo_read_var_info) = False;
sewardjf9ebc392010-05-09 22:30:43 +000089Int VG_(clo_n_req_tsyms) = 0;
90HChar* VG_(clo_req_tsyms)[VG_CLO_MAX_REQ_TSYMS];
91HChar* VG_(clo_require_text_symbol) = NULL;
njna3506d32005-05-18 04:36:01 +000092Bool VG_(clo_run_libc_freeres) = True;
93Bool VG_(clo_track_fds) = False;
94Bool VG_(clo_show_below_main)= False;
njna3506d32005-05-18 04:36:01 +000095Bool VG_(clo_show_emwarns) = False;
sewardj91b470c2007-08-28 17:03:01 +000096Word VG_(clo_max_stackframe) = 2000000;
sewardj95d86c02007-12-18 01:49:23 +000097Word VG_(clo_main_stacksize) = 0; /* use client's rlimit.stack */
njna3506d32005-05-18 04:36:01 +000098Bool VG_(clo_wait_for_gdb) = False;
sewardj6c3a2192005-07-24 07:00:45 +000099VgSmc VG_(clo_smc_check) = Vg_SmcStack;
sewardjce5a5662005-10-06 03:19:49 +0000100HChar* VG_(clo_kernel_variant) = NULL;
njn97db7612009-08-04 02:32:55 +0000101Bool VG_(clo_dsymutil) = False;
sewardj26412bd2005-07-07 10:05:05 +0000102
sewardj32824022005-05-17 13:45:04 +0000103
sewardj6893d652006-10-15 01:25:13 +0000104/*====================================================================*/
njnb1cc5d62010-07-06 04:05:23 +0000105/*=== File expansion ===*/
sewardj6893d652006-10-15 01:25:13 +0000106/*====================================================================*/
107
njn374a36d2007-11-23 01:41:32 +0000108// Copies the string, prepending it with the startup working directory, and
109// expanding %p and %q entries. Returns a new, malloc'd string.
110Char* VG_(expand_file_name)(Char* option_name, Char* format)
111{
112 static Char base_dir[VKI_PATH_MAX];
113 Int len, i = 0, j = 0;
114 Char* out;
115
116 Bool ok = VG_(get_startup_wd)(base_dir, VKI_PATH_MAX);
117 tl_assert(ok);
118
119 if (VG_STREQ(format, "")) {
120 // Empty name, bad.
njnb1cc5d62010-07-06 04:05:23 +0000121 VG_(fmsg)("%s: filename is empty", option_name);
njn5542d8f2009-06-09 00:50:45 +0000122 goto bad;
123 }
124
125 // If 'format' starts with a '~', abort -- the user probably expected the
126 // shell to expand but it didn't (see bug 195268 for details). This means
127 // that we don't allow a legitimate filename beginning with '~' but that
128 // seems very unlikely.
129 if (format[0] == '~') {
njnb1cc5d62010-07-06 04:05:23 +0000130 VG_(fmsg)(
131 "%s: filename begins with '~'\n"
132 "You probably expected the shell to expand the '~', but it\n"
133 "didn't. The rules for '~'-expansion vary from shell to shell.\n"
134 "You might have more luck using $HOME instead.\n",
135 option_name
136 );
njn374a36d2007-11-23 01:41:32 +0000137 goto bad;
138 }
139
njn2dd08f52007-11-23 22:37:35 +0000140 // If 'format' starts with a '/', do not prefix with startup dir.
141 if (format[0] != '/') {
142 j += VG_(strlen)(base_dir);
143 }
144
njn374a36d2007-11-23 01:41:32 +0000145 // The 10 is slop, it should be enough in most cases.
njn374a36d2007-11-23 01:41:32 +0000146 len = j + VG_(strlen)(format) + 10;
sewardj9c606bd2008-09-18 18:12:50 +0000147 out = VG_(malloc)( "options.efn.1", len );
njn2dd08f52007-11-23 22:37:35 +0000148 if (format[0] != '/') {
149 VG_(strcpy)(out, base_dir);
150 out[j++] = '/';
151 }
njn374a36d2007-11-23 01:41:32 +0000152
153#define ENSURE_THIS_MUCH_SPACE(x) \
154 if (j + x >= len) { \
155 len += (10 + x); \
sewardj9c606bd2008-09-18 18:12:50 +0000156 out = VG_(realloc)("options.efn.2(multiple)", out, len); \
njn374a36d2007-11-23 01:41:32 +0000157 }
158
njn374a36d2007-11-23 01:41:32 +0000159 while (format[i]) {
160 if (format[i] != '%') {
161 ENSURE_THIS_MUCH_SPACE(1);
162 out[j++] = format[i++];
163
164 } else {
165 // We saw a '%'. What's next...
166 i++;
167 if ('%' == format[i]) {
168 // Replace '%%' with '%'.
169 ENSURE_THIS_MUCH_SPACE(1);
170 out[j++] = format[i++];
171 }
172 else if ('p' == format[i]) {
173 // Print the PID. Assume that it's not longer than 10 chars --
174 // reasonable since 'pid' is an Int (ie. 32 bits).
175 Int pid = VG_(getpid)();
176 ENSURE_THIS_MUCH_SPACE(10);
177 j += VG_(sprintf)(&out[j], "%d", pid);
178 i++;
179 }
njn2dd08f52007-11-23 22:37:35 +0000180 else if ('q' == format[i]) {
181 i++;
182 if ('{' == format[i]) {
183 // Get the env var name, print its contents.
184 Char* qualname;
185 Char* qual;
njn374a36d2007-11-23 01:41:32 +0000186 i++;
njn2dd08f52007-11-23 22:37:35 +0000187 qualname = &format[i];
188 while (True) {
189 if (0 == format[i]) {
njnb1cc5d62010-07-06 04:05:23 +0000190 VG_(fmsg)("%s: malformed %%q specifier\n", option_name);
njn2dd08f52007-11-23 22:37:35 +0000191 goto bad;
192 } else if ('}' == format[i]) {
193 // Temporarily replace the '}' with NUL to extract var
194 // name.
195 format[i] = 0;
196 qual = VG_(getenv)(qualname);
197 if (NULL == qual) {
njnb1cc5d62010-07-06 04:05:23 +0000198 VG_(fmsg)("%s: environment variable %s is not set\n",
199 option_name, qualname);
njn2dd08f52007-11-23 22:37:35 +0000200 format[i] = '}'; // Put the '}' back.
201 goto bad;
202 }
203 format[i] = '}'; // Put the '}' back.
204 i++;
205 break;
206 }
207 i++;
208 }
209 ENSURE_THIS_MUCH_SPACE(VG_(strlen)(qual));
210 j += VG_(sprintf)(&out[j], "%s", qual);
211 } else {
njnb1cc5d62010-07-06 04:05:23 +0000212 VG_(fmsg)("%s: expected '{' after '%%q'\n", option_name);
njn2dd08f52007-11-23 22:37:35 +0000213 goto bad;
njn374a36d2007-11-23 01:41:32 +0000214 }
njn374a36d2007-11-23 01:41:32 +0000215 }
216 else {
217 // Something else, abort.
njnb1cc5d62010-07-06 04:05:23 +0000218 VG_(fmsg)("%s: expected 'p' or 'q' or '%%' after '%%'\n",
219 option_name);
njn374a36d2007-11-23 01:41:32 +0000220 goto bad;
221 }
222 }
223 }
224 ENSURE_THIS_MUCH_SPACE(1);
225 out[j++] = 0;
226
227 return out;
228
229 bad: {
230 Char* opt = // 2: 1 for the '=', 1 for the NUL.
sewardj9c606bd2008-09-18 18:12:50 +0000231 VG_(malloc)( "options.efn.3",
232 VG_(strlen)(option_name) + VG_(strlen)(format) + 2 );
njn374a36d2007-11-23 01:41:32 +0000233 VG_(strcpy)(opt, option_name);
234 VG_(strcat)(opt, "=");
235 VG_(strcat)(opt, format);
njnb1cc5d62010-07-06 04:05:23 +0000236 VG_(fmsg_bad_option)(opt, "");
njn374a36d2007-11-23 01:41:32 +0000237 }
238}
239
sewardj06421272009-11-05 08:55:13 +0000240/*====================================================================*/
241/*=== --trace-children= support ===*/
242/*====================================================================*/
243
244static HChar const* consume_commas ( HChar const* c ) {
245 while (*c && *c == ',') {
246 ++c;
247 }
248 return c;
249}
250
251static HChar const* consume_field ( HChar const* c ) {
252 while (*c && *c != ',') {
253 ++c;
254 }
255 return c;
256}
257
258/* Should we trace into this child executable (across execve etc) ?
sewardj9ab64a42010-12-06 11:40:04 +0000259 This involves considering --trace-children=,
260 --trace-children-skip=, --trace-children-skip-by-arg=, and the name
261 of the executable. 'child_argv' must not include the name of the
262 executable itself; iow child_argv[0] must be the first arg, if any,
263 for the child. */
264Bool VG_(should_we_trace_this_child) ( HChar* child_exe_name,
265 HChar** child_argv )
sewardj06421272009-11-05 08:55:13 +0000266{
267 // child_exe_name is pulled out of the guest's space. We
268 // should be at least marginally cautious with it, lest it
269 // explode or burst into flames unexpectedly.
270 if (child_exe_name == NULL || VG_(strlen)(child_exe_name) == 0)
271 return VG_(clo_trace_children); // we know narfink
272
sewardj06421272009-11-05 08:55:13 +0000273 // If --trace-children=no, the answer is simply NO.
274 if (! VG_(clo_trace_children))
275 return False;
276
sewardj9ab64a42010-12-06 11:40:04 +0000277 // Otherwise, look for other reasons to say NO. First,
278 // see if the exe name matches any of the patterns specified
279 // by --trace-children-skip=.
sewardj06421272009-11-05 08:55:13 +0000280 if (VG_(clo_trace_children_skip)) {
281 HChar const* last = VG_(clo_trace_children_skip);
282 HChar const* name = (HChar const*)child_exe_name;
283 while (*last) {
284 Bool matches;
285 HChar* patt;
286 HChar const* first = consume_commas(last);
287 last = consume_field(first);
288 if (first == last)
289 break;
290 vg_assert(last > first);
291 /* copy the candidate string into a temporary malloc'd block
292 so we can use VG_(string_match) on it. */
293 patt = VG_(calloc)("m_options.swttc.1", last - first + 1, 1);
294 VG_(memcpy)(patt, first, last - first);
295 vg_assert(patt[last-first] == 0);
296 matches = VG_(string_match)(patt, name);
297 VG_(free)(patt);
298 if (matches)
299 return False;
300 }
301 }
sewardj9ab64a42010-12-06 11:40:04 +0000302
303 // Check if any of the args match any of the patterns specified
304 // by --trace-children-skip-by-arg=.
305 if (VG_(clo_trace_children_skip_by_arg) && child_argv != NULL) {
306 HChar const* last = VG_(clo_trace_children_skip_by_arg);
307 while (*last) {
308 Int i;
309 Bool matches;
310 HChar* patt;
311 HChar const* first = consume_commas(last);
312 last = consume_field(first);
313 if (first == last)
314 break;
315 vg_assert(last > first);
316 /* copy the candidate string into a temporary malloc'd block
317 so we can use VG_(string_match) on it. */
318 patt = VG_(calloc)("m_options.swttc.1", last - first + 1, 1);
319 VG_(memcpy)(patt, first, last - first);
320 vg_assert(patt[last-first] == 0);
321 for (i = 0; child_argv[i]; i++) {
322 matches = VG_(string_match)(patt, child_argv[i]);
323 if (matches) {
324 VG_(free)(patt);
325 return False;
326 }
327 }
328 VG_(free)(patt);
329 }
330 }
331
sewardj06421272009-11-05 08:55:13 +0000332 // --trace-children=yes, and this particular executable isn't
333 // excluded
334 return True;
335}
njn374a36d2007-11-23 01:41:32 +0000336
sewardj6893d652006-10-15 01:25:13 +0000337
sewardj32824022005-05-17 13:45:04 +0000338/*--------------------------------------------------------------------*/
njnb1cc5d62010-07-06 04:05:23 +0000339/*--- end ---*/
sewardj32824022005-05-17 13:45:04 +0000340/*--------------------------------------------------------------------*/