• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*--------------------------------------------------------------------*/
3 /*--- Command line options.                     pub_tool_options.h ---*/
4 /*--------------------------------------------------------------------*/
5 
6 /*
7    This file is part of Valgrind, a dynamic binary instrumentation
8    framework.
9 
10    Copyright (C) 2000-2013 Julian Seward
11       jseward@acm.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 #ifndef __PUB_TOOL_OPTIONS_H
32 #define __PUB_TOOL_OPTIONS_H
33 
34 #include "pub_tool_basics.h"     // for VG_ macro
35 #include "libvex.h"              // for VexControl
36 
37 
38 // Higher-level command-line option recognisers;  use in if/else chains.
39 // Note that they assign a value to the 'qq_var' argument.  So often they
40 // can be used like this:
41 //
42 //   if VG_STR_CLO(arg, "--foo", clo_foo) { }
43 //
44 // But if you want to do further checking or processing, you can do this:
45 //
46 //   if VG_STR_CLO(arg, "--foo", clo_foo) { <further checking or processing> }
47 //
48 // They use GNU statement expressions to do the qq_var assignment within a
49 // conditional expression.
50 
51 // String argument, eg. --foo=yes or --foo=no
52 #define VG_BOOL_CLO(qq_arg, qq_option, qq_var) \
53    (VG_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=") && \
54     ({ \
55       const HChar* val = &(qq_arg)[ VG_(strlen)(qq_option)+1 ]; \
56       if      VG_STREQ(val, "yes") (qq_var) = True; \
57       else if VG_STREQ(val, "no")  (qq_var) = False; \
58       else VG_(fmsg_bad_option)(qq_arg, "Invalid boolean value '%s'" \
59                                 " (should be 'yes' or 'no')\n", val);    \
60       True; \
61     }) \
62    )
63 
64 // String argument, eg. --foo=bar
65 #define VG_STR_CLO(qq_arg, qq_option, qq_var) \
66    (VG_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=") && \
67     ({ \
68       const HChar* val = &(qq_arg)[ VG_(strlen)(qq_option)+1 ]; \
69       (qq_var) = val; \
70       True; \
71     }) \
72    )
73 
74 // Unbounded integer arg, eg. --foo=10
75 #define VG_INT_CLO(qq_arg, qq_option, qq_var) \
76    (VG_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=") && \
77     ({ \
78       const HChar* val = &(qq_arg)[ VG_(strlen)(qq_option)+1 ]; \
79       HChar* s; \
80       Long n = VG_(strtoll10)( val, &s ); \
81       (qq_var) = n; \
82       /* Check for non-numeralness, or overflow. */ \
83       if ('\0' != s[0] || (qq_var) != n) VG_(fmsg_bad_option)(qq_arg, ""); \
84       True; \
85      }) \
86     )
87 
88 // Bounded integer arg, eg. --foo=10 ;  if the value exceeds the bounds it
89 // causes an abort.  'qq_base' can be 10 or 16.
90 #define VG_BINTN_CLO(qq_base, qq_arg, qq_option, qq_var, qq_lo, qq_hi) \
91    (VG_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=") && \
92     ({ \
93       const HChar* val = &(qq_arg)[ VG_(strlen)(qq_option)+1 ]; \
94       HChar* s; \
95       Long n = VG_(strtoll##qq_base)( val, &s ); \
96       (qq_var) = n; \
97       /* MMM: separate the two cases, and explain the problem;  likewise */ \
98       /* for all the other macros in this file. */ \
99       /* Check for non-numeralness, or overflow. */ \
100       /* Nb: it will overflow if qq_var is unsigned and qq_val is negative! */ \
101       if ('\0' != s[0] || (qq_var) != n) VG_(fmsg_bad_option)(qq_arg, ""); \
102       /* Check bounds. */ \
103       if ((qq_var) < (qq_lo) || (qq_var) > (qq_hi)) { \
104          VG_(fmsg_bad_option)(qq_arg, \
105             "'%s' argument must be between %lld and %lld\n", \
106             (qq_option), (Long)(qq_lo), (Long)(qq_hi)); \
107       } \
108       True; \
109      }) \
110     )
111 
112 // Bounded decimal integer arg, eg. --foo=100
113 #define VG_BINT_CLO(qq_arg, qq_option, qq_var, qq_lo, qq_hi) \
114    VG_BINTN_CLO(10, (qq_arg), qq_option, (qq_var), (qq_lo), (qq_hi))
115 
116 // Bounded hexadecimal integer arg, eg. --foo=0x1fa8
117 #define VG_BHEX_CLO(qq_arg, qq_option, qq_var, qq_lo, qq_hi) \
118    VG_BINTN_CLO(16, (qq_arg), qq_option, (qq_var), (qq_lo), (qq_hi))
119 
120 // Double (decimal) arg, eg. --foo=4.6
121 // XXX: there's not VG_BDBL_CLO because we don't have a good way of printing
122 // floats at the moment!
123 #define VG_DBL_CLO(qq_arg, qq_option, qq_var) \
124    (VG_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=") && \
125     ({ \
126       const HChar* val = &(qq_arg)[ VG_(strlen)(qq_option)+1 ]; \
127       HChar* s; \
128       double n = VG_(strtod)( val, &s ); \
129       (qq_var) = n; \
130       /* Check for non-numeralness */ \
131       if ('\0' != s[0]) VG_(fmsg_bad_option)(qq_arg, ""); \
132       True; \
133      }) \
134     )
135 
136 // Arg whose value is denoted by the exact presence of the given string;
137 // if it matches, qq_var is assigned the value in qq_val.
138 #define VG_XACT_CLO(qq_arg, qq_option, qq_var, qq_val) \
139    (VG_STREQ((qq_arg), (qq_option)) && \
140     ({ \
141       (qq_var) = (qq_val); \
142       True; \
143     }) \
144    )
145 
146 /* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
147 extern Int  VG_(clo_verbosity);
148 
149 /* Show tool and core statistics */
150 extern Bool VG_(clo_stats);
151 
152 /* wait for vgdb/gdb after reporting that amount of error.
153    Note that this is the initial value provided from the command line.
154    The real value is maintained in VG_(dyn_vgdb_error) and
155    can be changed dynamically.*/
156 extern Int VG_(clo_vgdb_error);
157 
158 /* If user has provided the --vgdb-prefix command line option,
159    VG_(arg_vgdb_prefix) points at the provided argument (including the
160    '--vgdb-prefix=' string).
161    Otherwise, it is NULL.
162    Typically, this is used by tools to produce user message with the
163    expected vgdb prefix argument, if the user has changed the default. */
164 extern const HChar *VG_(arg_vgdb_prefix);
165 
166 /* Emit all messages as XML? default: NO */
167 /* If clo_xml is set, various other options are set in a non-default
168    way.  See vg_main.c and mc_main.c. */
169 extern Bool VG_(clo_xml);
170 
171 /* An arbitrary user-supplied string which is copied into the
172    XML output, in between <usercomment> tags. */
173 extern const HChar* VG_(clo_xml_user_comment);
174 
175 /* Vex iropt control.  Tool-visible so tools can make Vex optimise
176    less aggressively if that is needed (callgrind needs this). */
177 extern VexControl VG_(clo_vex_control);
178 
179 /* Number of parents of a backtrace.  Default: 8.  */
180 extern Int   VG_(clo_backtrace_size);
181 
182 /* Continue stack traces below main()?  Default: NO */
183 extern Bool VG_(clo_show_below_main);
184 
185 
186 /* Used to expand file names.  "option_name" is the option name, eg.
187    "--log-file".  'format' is what follows, eg. "cachegrind.out.%p".  In
188    'format':
189    - "%p" is replaced with PID.
190    - "%q{QUAL}" is replaced with the environment variable $QUAL.  If $QUAL
191      isn't set, we abort.  If the "{QUAL}" part is malformed, we abort.
192    - "%%" is replaced with "%".
193    Anything else after '%' causes an abort.
194    If the format specifies a relative file name, it's put in the program's
195    initial working directory.  If it specifies an absolute file name (ie.
196    starts with '/') then it is put there.
197 
198    Note that "option_name" has no effect on the returned string: the
199    returned string depends only on "format" and the PIDs and
200    environment variables that it references (if any). "option_name" is
201    merely used in printing error messages, if an error message needs
202    to be printed due to malformedness of the "format" argument.
203 */
204 extern HChar* VG_(expand_file_name)(const HChar* option_name,
205                                     const HChar* format);
206 
207 #endif   // __PUB_TOOL_OPTIONS_H
208 
209 /*--------------------------------------------------------------------*/
210 /*--- end                                                          ---*/
211 /*--------------------------------------------------------------------*/
212