• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-2012 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. */
46 VexControl VG_(clo_vex_control);
47 Bool   VG_(clo_error_limit)    = True;
48 Int    VG_(clo_error_exitcode) = 0;
49 
50 #if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android)
51 VgVgdb VG_(clo_vgdb)           = Vg_VgdbNo; // currently disabled on Android
52 #else
53 VgVgdb VG_(clo_vgdb)           = Vg_VgdbYes;
54 #endif
55 Int    VG_(clo_vgdb_poll)      = 5000;
56 Int    VG_(clo_vgdb_error)     = 999999999;
57 HChar* VG_(clo_vgdb_prefix)    = NULL;
58 Bool   VG_(clo_vgdb_shadow_registers) = False;
59 
60 Bool   VG_(clo_db_attach)      = False;
61 Char*  VG_(clo_db_command)     = GDB_PATH " -nw %f %p";
62 Int    VG_(clo_gen_suppressions) = 0;
63 Int    VG_(clo_sanity_level)   = 1;
64 Int    VG_(clo_verbosity)      = 1;
65 Bool   VG_(clo_stats)          = False;
66 Bool   VG_(clo_xml)            = False;
67 HChar* VG_(clo_xml_user_comment) = NULL;
68 Bool   VG_(clo_demangle)       = True;
69 HChar* VG_(clo_soname_synonyms)    = NULL;
70 Bool   VG_(clo_trace_children) = False;
71 HChar* VG_(clo_trace_children_skip) = NULL;
72 HChar* VG_(clo_trace_children_skip_by_arg) = NULL;
73 Bool   VG_(clo_child_silent_after_fork) = False;
74 Char*  VG_(clo_log_fname_expanded) = NULL;
75 Char*  VG_(clo_xml_fname_expanded) = NULL;
76 Bool   VG_(clo_time_stamp)     = False;
77 Int    VG_(clo_input_fd)       = 0; /* stdin */
78 Int    VG_(clo_n_suppressions) = 0;
79 Char*  VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
80 Int    VG_(clo_n_fullpath_after) = 0;
81 Char*  VG_(clo_fullpath_after)[VG_CLO_MAX_FULLPATH_AFTER];
82 UChar  VG_(clo_trace_flags)    = 0; // 00000000b
83 UChar  VG_(clo_profile_flags)  = 0; // 00000000b
84 Int    VG_(clo_trace_notbelow) = -1;  // unspecified
85 Int    VG_(clo_trace_notabove) = -1;  // unspecified
86 Bool   VG_(clo_trace_syscalls) = False;
87 Bool   VG_(clo_trace_signals)  = False;
88 Bool   VG_(clo_trace_symtab)   = False;
89 HChar* VG_(clo_trace_symtab_patt) = "*";
90 Bool   VG_(clo_trace_cfi)      = False;
91 Bool   VG_(clo_debug_dump_syms) = False;
92 Bool   VG_(clo_debug_dump_line) = False;
93 Bool   VG_(clo_debug_dump_frames) = False;
94 Bool   VG_(clo_trace_redir)    = False;
95 enum FairSchedType
96        VG_(clo_fair_sched)     = disable_fair_sched;
97 Bool   VG_(clo_trace_sched)    = False;
98 Bool   VG_(clo_profile_heap)   = False;
99 Int    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
102 Int    VG_(clo_redzone_size)   = -1;
103 Int    VG_(clo_dump_error)     = 0;
104 Int    VG_(clo_backtrace_size) = 12;
105 Char*  VG_(clo_sim_hints)      = NULL;
106 Bool   VG_(clo_sym_offsets)    = False;
107 Bool   VG_(clo_read_var_info)  = False;
108 Int    VG_(clo_n_req_tsyms)    = 0;
109 HChar* VG_(clo_req_tsyms)[VG_CLO_MAX_REQ_TSYMS];
110 HChar* VG_(clo_require_text_symbol) = NULL;
111 Bool   VG_(clo_run_libc_freeres) = True;
112 Bool   VG_(clo_track_fds)      = False;
113 Bool   VG_(clo_show_below_main)= False;
114 Bool   VG_(clo_show_emwarns)   = False;
115 Word   VG_(clo_max_stackframe) = 2000000;
116 Word   VG_(clo_main_stacksize) = 0; /* use client's rlimit.stack */
117 Bool   VG_(clo_wait_for_gdb)   = False;
118 VgSmc  VG_(clo_smc_check)      = Vg_SmcStack;
119 HChar* VG_(clo_kernel_variant) = NULL;
120 Bool   VG_(clo_dsymutil)       = False;
121 
122 
123 /*====================================================================*/
124 /*=== File expansion                                               ===*/
125 /*====================================================================*/
126 
127 // Copies the string, prepending it with the startup working directory, and
128 // expanding %p and %q entries.  Returns a new, malloc'd string.
VG_(expand_file_name)129 Char* 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.
140       VG_(fmsg)("%s: filename is empty", option_name);
141       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] == '~') {
149       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       );
156       goto bad;
157    }
158 
159    // If 'format' starts with a '/', do not prefix with startup dir.
160    if (format[0] != '/') {
161       j += VG_(strlen)(base_dir);
162    }
163 
164    // The 10 is slop, it should be enough in most cases.
165    len = j + VG_(strlen)(format) + 10;
166    out = VG_(malloc)( "options.efn.1", len );
167    if (format[0] != '/') {
168       VG_(strcpy)(out, base_dir);
169       out[j++] = '/';
170    }
171 
172 #define ENSURE_THIS_MUCH_SPACE(x) \
173    if (j + x >= len) { \
174       len += (10 + x); \
175       out = VG_(realloc)("options.efn.2(multiple)", out, len); \
176    }
177 
178    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          }
199          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;
205                i++;
206                qualname = &format[i];
207                while (True) {
208                   if (0 == format[i]) {
209                      VG_(fmsg)("%s: malformed %%q specifier\n", option_name);
210                      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) {
217                         VG_(fmsg)("%s: environment variable %s is not set\n",
218                                   option_name, qualname);
219                         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 {
231                VG_(fmsg)("%s: expected '{' after '%%q'\n", option_name);
232                goto bad;
233             }
234          }
235          else {
236             // Something else, abort.
237             VG_(fmsg)("%s: expected 'p' or 'q' or '%%' after '%%'\n",
238                       option_name);
239             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.
250       VG_(malloc)( "options.efn.3",
251                    VG_(strlen)(option_name) + VG_(strlen)(format) + 2 );
252    VG_(strcpy)(opt, option_name);
253    VG_(strcat)(opt, "=");
254    VG_(strcat)(opt, format);
255    VG_(fmsg_bad_option)(opt, "");
256   }
257 }
258 
259 /*====================================================================*/
260 /*=== --trace-children= support                                    ===*/
261 /*====================================================================*/
262 
consume_commas(HChar const * c)263 static HChar const* consume_commas ( HChar const* c ) {
264    while (*c && *c == ',') {
265       ++c;
266    }
267    return c;
268 }
269 
consume_field(HChar const * c)270 static 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) ?
278    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. */
VG_(should_we_trace_this_child)283 Bool VG_(should_we_trace_this_child) ( HChar* child_exe_name,
284                                        HChar** child_argv )
285 {
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 
292    // If --trace-children=no, the answer is simply NO.
293    if (! VG_(clo_trace_children))
294       return False;
295 
296    // 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=.
299    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    }
321 
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 
351    // --trace-children=yes, and this particular executable isn't
352    // excluded
353    return True;
354 }
355 
356 
357 /*--------------------------------------------------------------------*/
358 /*--- end                                                          ---*/
359 /*--------------------------------------------------------------------*/
360