1 2/*--------------------------------------------------------------------*/ 3/*--- Command line options. m_options.c ---*/ 4/*--------------------------------------------------------------------*/ 5 6/* 7 This file is part of Valgrind, a dynamic binary instrumentation 8 framework. 9 10 Copyright (C) 2000-2013 Nicholas Nethercote 11 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 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 42// 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; 48Int VG_(clo_error_exitcode) = 0; 49 50#if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \ 51 || defined(VGPV_mips32_linux_android) 52VgVgdb VG_(clo_vgdb) = Vg_VgdbNo; // currently disabled on Android 53#else 54VgVgdb VG_(clo_vgdb) = Vg_VgdbYes; 55#endif 56Int VG_(clo_vgdb_poll) = 5000; 57Int VG_(clo_vgdb_error) = 999999999; 58UInt VG_(clo_vgdb_stop_at) = 0; 59const HChar *VG_(clo_vgdb_prefix) = NULL; 60const HChar *VG_(arg_vgdb_prefix) = NULL; 61Bool VG_(clo_vgdb_shadow_registers) = False; 62 63Bool VG_(clo_db_attach) = False; 64const HChar* VG_(clo_db_command) = GDB_PATH " -nw %f %p"; 65Int VG_(clo_gen_suppressions) = 0; 66Int VG_(clo_sanity_level) = 1; 67Int VG_(clo_verbosity) = 1; 68Bool VG_(clo_stats) = False; 69Bool VG_(clo_xml) = False; 70const HChar* VG_(clo_xml_user_comment) = NULL; 71Bool VG_(clo_demangle) = True; 72const HChar* VG_(clo_soname_synonyms) = NULL; 73Bool VG_(clo_trace_children) = False; 74const HChar* VG_(clo_trace_children_skip) = NULL; 75const HChar* VG_(clo_trace_children_skip_by_arg) = NULL; 76Bool VG_(clo_child_silent_after_fork) = False; 77HChar* VG_(clo_log_fname_expanded) = NULL; 78HChar* VG_(clo_xml_fname_expanded) = NULL; 79Bool VG_(clo_time_stamp) = False; 80Int VG_(clo_input_fd) = 0; /* stdin */ 81Int VG_(clo_n_suppressions) = 0; 82const HChar* VG_(clo_suppressions)[VG_CLO_MAX_SFILES]; 83Int VG_(clo_n_fullpath_after) = 0; 84const HChar* VG_(clo_fullpath_after)[VG_CLO_MAX_FULLPATH_AFTER]; 85const HChar* VG_(clo_extra_debuginfo_path) = NULL; 86const HChar* VG_(clo_debuginfo_server) = NULL; 87Bool VG_(clo_allow_mismatched_debuginfo) = False; 88UChar VG_(clo_trace_flags) = 0; // 00000000b 89Bool VG_(clo_profyle_sbs) = False; 90UChar VG_(clo_profyle_flags) = 0; // 00000000b 91ULong VG_(clo_profyle_interval) = 0; 92Int VG_(clo_trace_notbelow) = -1; // unspecified 93Int VG_(clo_trace_notabove) = -1; // unspecified 94Bool VG_(clo_trace_syscalls) = False; 95Bool VG_(clo_trace_signals) = False; 96Bool VG_(clo_trace_symtab) = False; 97const HChar* VG_(clo_trace_symtab_patt) = "*"; 98Bool VG_(clo_trace_cfi) = False; 99Bool VG_(clo_debug_dump_syms) = False; 100Bool VG_(clo_debug_dump_line) = False; 101Bool VG_(clo_debug_dump_frames) = False; 102Bool VG_(clo_trace_redir) = False; 103enum FairSchedType 104 VG_(clo_fair_sched) = disable_fair_sched; 105Bool VG_(clo_trace_sched) = False; 106Bool VG_(clo_profile_heap) = False; 107Int VG_(clo_core_redzone_size) = CORE_REDZONE_DEFAULT_SZB; 108// A value != -1 overrides the tool-specific value 109// VG_(needs_malloc_replacement).tool_client_redzone_szB 110Int VG_(clo_redzone_size) = -1; 111Int VG_(clo_dump_error) = 0; 112Int VG_(clo_backtrace_size) = 12; 113Int VG_(clo_merge_recursive_frames) = 0; // default value: no merge 114const HChar* VG_(clo_sim_hints) = NULL; 115Bool VG_(clo_sym_offsets) = False; 116Bool VG_(clo_read_var_info) = False; 117Int VG_(clo_n_req_tsyms) = 0; 118const HChar* VG_(clo_req_tsyms)[VG_CLO_MAX_REQ_TSYMS]; 119HChar* VG_(clo_require_text_symbol) = NULL; 120Bool VG_(clo_run_libc_freeres) = True; 121Bool VG_(clo_track_fds) = False; 122Bool VG_(clo_show_below_main)= False; 123Bool VG_(clo_show_emwarns) = False; 124Word VG_(clo_max_stackframe) = 2000000; 125Word VG_(clo_main_stacksize) = 0; /* use client's rlimit.stack */ 126Bool VG_(clo_wait_for_gdb) = False; 127VgSmc VG_(clo_smc_check) = Vg_SmcStack; 128const HChar* VG_(clo_kernel_variant) = NULL; 129Bool VG_(clo_dsymutil) = False; 130Bool VG_(clo_sigill_diag) = True; 131UInt VG_(clo_unw_stack_scan_thresh) = 0; /* disabled by default */ 132UInt VG_(clo_unw_stack_scan_frames) = 5; 133 134 135/*====================================================================*/ 136/*=== File expansion ===*/ 137/*====================================================================*/ 138 139// Copies the string, prepending it with the startup working directory, and 140// expanding %p and %q entries. Returns a new, malloc'd string. 141HChar* VG_(expand_file_name)(const HChar* option_name, const HChar* format) 142{ 143 static HChar base_dir[VKI_PATH_MAX]; 144 Int len, i = 0, j = 0; 145 HChar* out; 146 147 Bool ok = VG_(get_startup_wd)(base_dir, VKI_PATH_MAX); 148 tl_assert(ok); 149 150 if (VG_STREQ(format, "")) { 151 // Empty name, bad. 152 VG_(fmsg)("%s: filename is empty", option_name); 153 goto bad; 154 } 155 156 // If 'format' starts with a '~', abort -- the user probably expected the 157 // shell to expand but it didn't (see bug 195268 for details). This means 158 // that we don't allow a legitimate filename beginning with '~' but that 159 // seems very unlikely. 160 if (format[0] == '~') { 161 VG_(fmsg)( 162 "%s: filename begins with '~'\n" 163 "You probably expected the shell to expand the '~', but it\n" 164 "didn't. The rules for '~'-expansion vary from shell to shell.\n" 165 "You might have more luck using $HOME instead.\n", 166 option_name 167 ); 168 goto bad; 169 } 170 171 len = VG_(strlen)(format) + 1; 172 out = VG_(malloc)( "options.efn.1", len ); 173 174#define ENSURE_THIS_MUCH_SPACE(x) \ 175 if (j + x >= len) { \ 176 len += (10 + x); \ 177 out = VG_(realloc)("options.efn.2(multiple)", out, len); \ 178 } 179 180 while (format[i]) { 181 if (format[i] != '%') { 182 ENSURE_THIS_MUCH_SPACE(1); 183 out[j++] = format[i++]; 184 185 } else { 186 // We saw a '%'. What's next... 187 i++; 188 if ('%' == format[i]) { 189 // Replace '%%' with '%'. 190 ENSURE_THIS_MUCH_SPACE(1); 191 out[j++] = format[i++]; 192 } 193 else if ('p' == format[i]) { 194 // Print the PID. Assume that it's not longer than 10 chars -- 195 // reasonable since 'pid' is an Int (ie. 32 bits). 196 Int pid = VG_(getpid)(); 197 ENSURE_THIS_MUCH_SPACE(10); 198 j += VG_(sprintf)(&out[j], "%d", pid); 199 i++; 200 } 201 else if ('q' == format[i]) { 202 i++; 203 if ('{' == format[i]) { 204 // Get the env var name, print its contents. 205 const HChar* qualname; 206 HChar* qual; 207 i++; 208 qualname = &format[i]; 209 while (True) { 210 if (0 == format[i]) { 211 VG_(fmsg)("%s: malformed %%q specifier\n", option_name); 212 goto bad; 213 } else if ('}' == format[i]) { 214 // Temporarily replace the '}' with NUL to extract var 215 // name. 216 // FIXME: this is not safe as FORMAT is sometimes a 217 // string literal which may reside in read-only memory 218 ((HChar *)format)[i] = 0; 219 qual = VG_(getenv)(qualname); 220 if (NULL == qual) { 221 VG_(fmsg)("%s: environment variable %s is not set\n", 222 option_name, qualname); 223 // FIXME: this is not safe as FORMAT is sometimes a 224 // string literal which may reside in read-only memory 225 ((HChar *)format)[i] = '}'; // Put the '}' back. 226 goto bad; 227 } 228 // FIXME: this is not safe as FORMAT is sometimes a 229 // string literal which may reside in read-only memory 230 ((HChar *)format)[i] = '}'; // Put the '}' back. 231 i++; 232 break; 233 } 234 i++; 235 } 236 ENSURE_THIS_MUCH_SPACE(VG_(strlen)(qual)); 237 j += VG_(sprintf)(&out[j], "%s", qual); 238 } else { 239 VG_(fmsg)("%s: expected '{' after '%%q'\n", option_name); 240 goto bad; 241 } 242 } 243 else { 244 // Something else, abort. 245 VG_(fmsg)("%s: expected 'p' or 'q' or '%%' after '%%'\n", 246 option_name); 247 goto bad; 248 } 249 } 250 } 251 ENSURE_THIS_MUCH_SPACE(1); 252 out[j++] = 0; 253 254 // If 'out' is not an absolute path name, prefix it with the startup dir. 255 if (out[0] != '/') { 256 len = VG_(strlen)(base_dir) + 1 + VG_(strlen)(out) + 1; 257 258 HChar *absout = VG_(malloc)("options.efn.4", len); 259 VG_(strcpy)(absout, base_dir); 260 VG_(strcat)(absout, "/"); 261 VG_(strcat)(absout, out); 262 VG_(free)(out); 263 out = absout; 264 } 265 266 return out; 267 268 bad: { 269 HChar* opt = // 2: 1 for the '=', 1 for the NUL. 270 VG_(malloc)( "options.efn.3", 271 VG_(strlen)(option_name) + VG_(strlen)(format) + 2 ); 272 VG_(strcpy)(opt, option_name); 273 VG_(strcat)(opt, "="); 274 VG_(strcat)(opt, format); 275 VG_(fmsg_bad_option)(opt, ""); 276 } 277} 278 279/*====================================================================*/ 280/*=== --trace-children= support ===*/ 281/*====================================================================*/ 282 283static HChar const* consume_commas ( HChar const* c ) { 284 while (*c && *c == ',') { 285 ++c; 286 } 287 return c; 288} 289 290static HChar const* consume_field ( HChar const* c ) { 291 while (*c && *c != ',') { 292 ++c; 293 } 294 return c; 295} 296 297/* Should we trace into this child executable (across execve etc) ? 298 This involves considering --trace-children=, 299 --trace-children-skip=, --trace-children-skip-by-arg=, and the name 300 of the executable. 'child_argv' must not include the name of the 301 executable itself; iow child_argv[0] must be the first arg, if any, 302 for the child. */ 303Bool VG_(should_we_trace_this_child) ( HChar* child_exe_name, 304 HChar** child_argv ) 305{ 306 // child_exe_name is pulled out of the guest's space. We 307 // should be at least marginally cautious with it, lest it 308 // explode or burst into flames unexpectedly. 309 if (child_exe_name == NULL || VG_(strlen)(child_exe_name) == 0) 310 return VG_(clo_trace_children); // we know narfink 311 312 // If --trace-children=no, the answer is simply NO. 313 if (! VG_(clo_trace_children)) 314 return False; 315 316 // Otherwise, look for other reasons to say NO. First, 317 // see if the exe name matches any of the patterns specified 318 // by --trace-children-skip=. 319 if (VG_(clo_trace_children_skip)) { 320 HChar const* last = VG_(clo_trace_children_skip); 321 HChar const* name = child_exe_name; 322 while (*last) { 323 Bool matches; 324 HChar* patt; 325 HChar const* first = consume_commas(last); 326 last = consume_field(first); 327 if (first == last) 328 break; 329 vg_assert(last > first); 330 /* copy the candidate string into a temporary malloc'd block 331 so we can use VG_(string_match) on it. */ 332 patt = VG_(calloc)("m_options.swttc.1", last - first + 1, 1); 333 VG_(memcpy)(patt, first, last - first); 334 vg_assert(patt[last-first] == 0); 335 matches = VG_(string_match)(patt, name); 336 VG_(free)(patt); 337 if (matches) 338 return False; 339 } 340 } 341 342 // Check if any of the args match any of the patterns specified 343 // by --trace-children-skip-by-arg=. 344 if (VG_(clo_trace_children_skip_by_arg) && child_argv != NULL) { 345 HChar const* last = VG_(clo_trace_children_skip_by_arg); 346 while (*last) { 347 Int i; 348 Bool matches; 349 HChar* patt; 350 HChar const* first = consume_commas(last); 351 last = consume_field(first); 352 if (first == last) 353 break; 354 vg_assert(last > first); 355 /* copy the candidate string into a temporary malloc'd block 356 so we can use VG_(string_match) on it. */ 357 patt = VG_(calloc)("m_options.swttc.1", last - first + 1, 1); 358 VG_(memcpy)(patt, first, last - first); 359 vg_assert(patt[last-first] == 0); 360 for (i = 0; child_argv[i]; i++) { 361 matches = VG_(string_match)(patt, child_argv[i]); 362 if (matches) { 363 VG_(free)(patt); 364 return False; 365 } 366 } 367 VG_(free)(patt); 368 } 369 } 370 371 // --trace-children=yes, and this particular executable isn't 372 // excluded 373 return True; 374} 375 376 377/*--------------------------------------------------------------------*/ 378/*--- end ---*/ 379/*--------------------------------------------------------------------*/ 380