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