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-2010 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;
49Bool   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;
54Bool   VG_(clo_stats)          = False;
55Bool   VG_(clo_xml)            = False;
56HChar* VG_(clo_xml_user_comment) = NULL;
57Bool   VG_(clo_demangle)       = True;
58Bool   VG_(clo_trace_children) = False;
59HChar* VG_(clo_trace_children_skip) = NULL;
60Bool   VG_(clo_child_silent_after_fork) = False;
61Char*  VG_(clo_log_fname_expanded) = NULL;
62Char*  VG_(clo_xml_fname_expanded) = NULL;
63Bool   VG_(clo_time_stamp)     = False;
64Int    VG_(clo_input_fd)       = 0; /* stdin */
65Int    VG_(clo_n_suppressions) = 0;
66Char*  VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
67Int    VG_(clo_n_fullpath_after) = 0;
68Char*  VG_(clo_fullpath_after)[VG_CLO_MAX_FULLPATH_AFTER];
69UChar  VG_(clo_trace_flags)    = 0; // 00000000b
70UChar  VG_(clo_profile_flags)  = 0; // 00000000b
71Int    VG_(clo_trace_notbelow) = 999999999;
72Bool   VG_(clo_trace_syscalls) = False;
73Bool   VG_(clo_trace_signals)  = False;
74Bool   VG_(clo_trace_symtab)   = False;
75HChar* VG_(clo_trace_symtab_patt) = "*";
76Bool   VG_(clo_trace_cfi)      = False;
77Bool   VG_(clo_debug_dump_syms) = False;
78Bool   VG_(clo_debug_dump_line) = False;
79Bool   VG_(clo_debug_dump_frames) = False;
80Bool   VG_(clo_trace_redir)    = False;
81Bool   VG_(clo_trace_sched)    = False;
82Bool   VG_(clo_profile_heap)   = False;
83Int    VG_(clo_dump_error)     = 0;
84Int    VG_(clo_backtrace_size) = 12;
85Char*  VG_(clo_sim_hints)      = NULL;
86Bool   VG_(clo_sym_offsets)    = False;
87Bool   VG_(clo_read_var_info)  = False;
88Int    VG_(clo_n_req_tsyms)    = 0;
89HChar* VG_(clo_req_tsyms)[VG_CLO_MAX_REQ_TSYMS];
90HChar* VG_(clo_require_text_symbol) = NULL;
91Bool   VG_(clo_run_libc_freeres) = True;
92Bool   VG_(clo_track_fds)      = False;
93Bool   VG_(clo_show_below_main)= False;
94Bool   VG_(clo_show_emwarns)   = False;
95Word   VG_(clo_max_stackframe) = 2000000;
96Word   VG_(clo_main_stacksize) = 0; /* use client's rlimit.stack */
97Bool   VG_(clo_wait_for_gdb)   = False;
98VgSmc  VG_(clo_smc_check)      = Vg_SmcStack;
99HChar* VG_(clo_kernel_variant) = NULL;
100Bool   VG_(clo_dsymutil)       = False;
101Char*  VG_(clo_memfs_malloc_path) = NULL;
102Int    VG_(clo_memfs_page_size)  = 2048;  /* 2M */
103
104
105/*====================================================================*/
106/*=== File expansion                                               ===*/
107/*====================================================================*/
108
109// Copies the string, prepending it with the startup working directory, and
110// expanding %p and %q entries.  Returns a new, malloc'd string.
111Char* VG_(expand_file_name)(Char* option_name, Char* format)
112{
113   static Char base_dir[VKI_PATH_MAX];
114   Int len, i = 0, j = 0;
115   Char* out;
116
117   Bool ok = VG_(get_startup_wd)(base_dir, VKI_PATH_MAX);
118   tl_assert(ok);
119
120   if (VG_STREQ(format, "")) {
121      // Empty name, bad.
122      VG_(fmsg)("%s: filename is empty", option_name);
123      goto bad;
124   }
125
126   // If 'format' starts with a '~', abort -- the user probably expected the
127   // shell to expand but it didn't (see bug 195268 for details).  This means
128   // that we don't allow a legitimate filename beginning with '~' but that
129   // seems very unlikely.
130   if (format[0] == '~') {
131      VG_(fmsg)(
132         "%s: filename begins with '~'\n"
133         "You probably expected the shell to expand the '~', but it\n"
134         "didn't.  The rules for '~'-expansion vary from shell to shell.\n"
135         "You might have more luck using $HOME instead.\n",
136         option_name
137      );
138      goto bad;
139   }
140
141   // If 'format' starts with a '/', do not prefix with startup dir.
142   if (format[0] != '/') {
143      j += VG_(strlen)(base_dir);
144   }
145
146   // The 10 is slop, it should be enough in most cases.
147   len = j + VG_(strlen)(format) + 10;
148   out = VG_(malloc)( "options.efn.1", len );
149   if (format[0] != '/') {
150      VG_(strcpy)(out, base_dir);
151      out[j++] = '/';
152   }
153
154#define ENSURE_THIS_MUCH_SPACE(x) \
155   if (j + x >= len) { \
156      len += (10 + x); \
157      out = VG_(realloc)("options.efn.2(multiple)", out, len); \
158   }
159
160   while (format[i]) {
161      if (format[i] != '%') {
162         ENSURE_THIS_MUCH_SPACE(1);
163         out[j++] = format[i++];
164
165      } else {
166         // We saw a '%'.  What's next...
167         i++;
168         if      ('%' == format[i]) {
169            // Replace '%%' with '%'.
170            ENSURE_THIS_MUCH_SPACE(1);
171            out[j++] = format[i++];
172         }
173         else if ('p' == format[i]) {
174            // Print the PID.  Assume that it's not longer than 10 chars --
175            // reasonable since 'pid' is an Int (ie. 32 bits).
176            Int pid = VG_(getpid)();
177            ENSURE_THIS_MUCH_SPACE(10);
178            j += VG_(sprintf)(&out[j], "%d", pid);
179            i++;
180         }
181         else if ('q' == format[i]) {
182            i++;
183            if ('{' == format[i]) {
184               // Get the env var name, print its contents.
185               Char* qualname;
186               Char* qual;
187               i++;
188               qualname = &format[i];
189               while (True) {
190                  if (0 == format[i]) {
191                     VG_(fmsg)("%s: malformed %%q specifier\n", option_name);
192                     goto bad;
193                  } else if ('}' == format[i]) {
194                     // Temporarily replace the '}' with NUL to extract var
195                     // name.
196                     format[i] = 0;
197                     qual = VG_(getenv)(qualname);
198                     if (NULL == qual) {
199                        VG_(fmsg)("%s: environment variable %s is not set\n",
200                                  option_name, qualname);
201                        format[i] = '}';  // Put the '}' back.
202                        goto bad;
203                     }
204                     format[i] = '}';     // Put the '}' back.
205                     i++;
206                     break;
207                  }
208                  i++;
209               }
210               ENSURE_THIS_MUCH_SPACE(VG_(strlen)(qual));
211               j += VG_(sprintf)(&out[j], "%s", qual);
212            } else {
213               VG_(fmsg)("%s: expected '{' after '%%q'\n", option_name);
214               goto bad;
215            }
216         }
217         else {
218            // Something else, abort.
219            VG_(fmsg)("%s: expected 'p' or 'q' or '%%' after '%%'\n",
220                      option_name);
221            goto bad;
222         }
223      }
224   }
225   ENSURE_THIS_MUCH_SPACE(1);
226   out[j++] = 0;
227
228   return out;
229
230  bad: {
231   Char* opt =    // 2:  1 for the '=', 1 for the NUL.
232      VG_(malloc)( "options.efn.3",
233                   VG_(strlen)(option_name) + VG_(strlen)(format) + 2 );
234   VG_(strcpy)(opt, option_name);
235   VG_(strcat)(opt, "=");
236   VG_(strcat)(opt, format);
237   VG_(fmsg_bad_option)(opt, "");
238  }
239}
240
241/*====================================================================*/
242/*=== --trace-children= support                                    ===*/
243/*====================================================================*/
244
245static HChar const* consume_commas ( HChar const* c ) {
246   while (*c && *c == ',') {
247      ++c;
248   }
249   return c;
250}
251
252static HChar const* consume_field ( HChar const* c ) {
253   while (*c && *c != ',') {
254      ++c;
255   }
256   return c;
257}
258
259/* Should we trace into this child executable (across execve etc) ?
260   This involves considering --trace-children=, --trace-children-skip=
261   and the name of the executable. */
262Bool VG_(should_we_trace_this_child) ( HChar* child_exe_name )
263{
264   // child_exe_name is pulled out of the guest's space.  We
265   // should be at least marginally cautious with it, lest it
266   // explode or burst into flames unexpectedly.
267   if (child_exe_name == NULL || VG_(strlen)(child_exe_name) == 0)
268      return VG_(clo_trace_children);  // we know narfink
269
270   // the main logic
271   // If --trace-children=no, the answer is simply NO.
272   if (! VG_(clo_trace_children))
273      return False;
274
275   // otherwise, return True, unless the exe name matches any of the
276   // patterns specified by --trace-children-skip=.
277   if (VG_(clo_trace_children_skip)) {
278      HChar const* last = VG_(clo_trace_children_skip);
279      HChar const* name = (HChar const*)child_exe_name;
280      while (*last) {
281         Bool   matches;
282         HChar* patt;
283         HChar const* first = consume_commas(last);
284         last = consume_field(first);
285         if (first == last)
286            break;
287         vg_assert(last > first);
288         /* copy the candidate string into a temporary malloc'd block
289            so we can use VG_(string_match) on it. */
290         patt = VG_(calloc)("m_options.swttc.1", last - first + 1, 1);
291         VG_(memcpy)(patt, first, last - first);
292         vg_assert(patt[last-first] == 0);
293         matches = VG_(string_match)(patt, name);
294         VG_(free)(patt);
295         if (matches)
296            return False;
297      }
298   }
299
300   // --trace-children=yes, and this particular executable isn't
301   // excluded
302   return True;
303}
304
305
306/*--------------------------------------------------------------------*/
307/*--- end                                                          ---*/
308/*--------------------------------------------------------------------*/
309