ftmemsim-valgrind/include/pub_tool_options.h
Nicholas Nethercote da695aa41a atoll() is a terrible function -- you can't do any error checking with it.
Some of our option processing code uses it.  This means that eg.
'--log-fd=9xxx' logs to fd 9, and '--log-fd=blahblahblah' logs to 0 (because
atoll() returns 0 if the string doesn't contain a number!)

It turns out that most of our option processing uses VG_(strtoll*) instead
of VG_(atoll).  The reason that not all of it does is that the
option-processing macros are underpowered -- they currently work well if you
just want to assign the value to a variable, eg:

        VG_BOOL_CLO(arg, "--heap",   clo_heap)
   else VG_BOOL_CLO(arg, "--stacks", clo_stacks)

   else VG_NUM_CLO(arg, "--heap-admin", clo_heap_admin)
   else VG_NUM_CLO(arg, "--depth",      clo_depth)

(This works because they are actually an if-statement, but it looks odd.)

VG_NUM_CLO uses VG_(stroll10).  But if you want to do any checking or
processing, you can't use those macros, leading to code like this:

      else if (VG_CLO_STREQN(9,  arg, "--log-fd=")) {
         log_to            = VgLogTo_Fd;
         VG_(clo_log_name) = NULL;
         tmp_log_fd        = (Int)VG_(atoll)(&arg[9]);
      }

So this commit:
- Improves the *_CLO_* macros so that they can be used in all circumstances.
  They're now just expressions (albeit ones with side-effects, setting the
  named variable appropriately).  Thus they can be used as if-conditions,
  and any post-checking or processing can occur in the then-statement.  And
  malformed numeric arguments (eg. --log-fd=foo) aren't accepted.  This also
  means you don't have to specify the lengths of any option strings anywhere
  (eg.  the 9 in the --log-fd example above).  The use of a wrong number
  caused at least one bug, in Massif.
- Updates all places where the macros were used.
- Updates Helgrind to use the *_CLO_* macros (it didn't use them).
- Updates Callgrind to use the *_CLO_* macros (it didn't use them), except
  for the more esoteric option names (those with numbers in the option
  name).  This allowed getUInt() and getUWord() to be removed.
- Improves the cache option parsing in Cachegrind and Callgrind -- now uses
  VG_(strtoll10)(), detects overflow, and is shorter.
- Uses INT instead of NUM in the macro names, to distinguish better vs. the
  DBL macro.
- Removes VG_(atoll*) and the few remaining uses -- they're wretched
  functions and VG_(strtoll*) should be used instead.
- Adds the VG_STREQN macro.
- Changes VG_BINT_CLO and VG_BHEX_CLO to abort if the given value is outside
  the range -- the current silent truncation is likely to cause confusion as
  much as anything.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@9255
2009-02-25 01:01:05 +00:00

199 lines
7.4 KiB
C

/*--------------------------------------------------------------------*/
/*--- Command line options. pub_tool_options.h ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Valgrind, a dynamic binary instrumentation
framework.
Copyright (C) 2000-2008 Julian Seward
jseward@acm.org
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#ifndef __PUB_TOOL_OPTIONS_H
#define __PUB_TOOL_OPTIONS_H
#include "libvex.h" // for VexControl
// Higher-level command-line option recognisers; use in if/else chains.
// Note that they assign a value to the 'qq_var' argument. So often they
// can be used like this:
//
// if VG_STR_CLO(arg, "--foo", clo_foo) { }
//
// But if you want to do further checking or processing, you can do this:
//
// if VG_STR_CLO(arg, "--foo", clo_foo) { <further checking or processing> }
//
// They use GNU statement expressions to do the qq_var assignment within a
// conditional expression.
// String argument, eg. --foo=yes or --foo=no
#define VG_BOOL_CLO(qq_arg, qq_option, qq_var) \
(VG_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=") && \
({ \
Char* val = &(qq_arg)[ VG_(strlen)(qq_option)+1 ]; \
if VG_STREQ(val, "yes") (qq_var) = True; \
else if VG_STREQ(val, "no") (qq_var) = False; \
True; \
}) \
)
// String argument, eg. --foo=bar
#define VG_STR_CLO(qq_arg, qq_option, qq_var) \
(VG_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=") && \
({ \
Char* val = &(qq_arg)[ VG_(strlen)(qq_option)+1 ]; \
(qq_var) = val; \
True; \
}) \
)
// Unbounded integer arg, eg. --foo=10
#define VG_INT_CLO(qq_arg, qq_option, qq_var) \
(VG_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=") && \
({ \
Char* val = &(qq_arg)[ VG_(strlen)(qq_option)+1 ]; \
Char* s; \
Long n = VG_(strtoll10)( val, &s ); \
(qq_var) = n; \
/* Check for non-numeralness, or overflow. */ \
if ('\0' != s[0] || (qq_var) != n) VG_(err_bad_option)(qq_arg); \
True; \
}) \
)
// Bounded integer arg, eg. --foo=10 ; if the value exceeds the bounds it
// causes an abort. 'qq_base' can be 10 or 16.
#define VG_BINTN_CLO(qq_base, qq_arg, qq_option, qq_var, qq_lo, qq_hi) \
(VG_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=") && \
({ \
Char* val = &(qq_arg)[ VG_(strlen)(qq_option)+1 ]; \
Char* s; \
Long n = VG_(strtoll##qq_base)( val, &s ); \
(qq_var) = n; \
/* Check for non-numeralness, or overflow. */ \
/* Nb: it will overflow if qq_var is unsigned and qq_val is negative! */ \
if ('\0' != s[0] || (qq_var) != n) VG_(err_bad_option)(qq_arg); \
/* Check bounds. */ \
if ((qq_var) < (qq_lo) || (qq_var) > (qq_hi)) { \
VG_(message)(Vg_UserMsg, \
"'%s' argument must be between %lld and %lld", \
(qq_option), (Long)(qq_lo), (Long)(qq_hi)); \
VG_(err_bad_option)(qq_arg); \
} \
True; \
}) \
)
// Bounded decimal integer arg, eg. --foo=100
#define VG_BINT_CLO(qq_arg, qq_option, qq_var, qq_lo, qq_hi) \
VG_BINTN_CLO(10, (qq_arg), qq_option, (qq_var), (qq_lo), (qq_hi))
// Bounded hexadecimal integer arg, eg. --foo=0x1fa8
#define VG_BHEX_CLO(qq_arg, qq_option, qq_var, qq_lo, qq_hi) \
VG_BINTN_CLO(16, (qq_arg), qq_option, (qq_var), (qq_lo), (qq_hi))
// Double (decimal) arg, eg. --foo=4.6
// XXX: there's not VG_BDBL_CLO because we don't have a good way of printing
// floats at the moment!
#define VG_DBL_CLO(qq_arg, qq_option, qq_var) \
(VG_STREQN(VG_(strlen)(qq_option)+1, qq_arg, qq_option"=") && \
({ \
Char* val = &(qq_arg)[ VG_(strlen)(qq_option)+1 ]; \
Char* s; \
double n = VG_(strtod)( val, &s ); \
(qq_var) = n; \
/* Check for non-numeralness */ \
if ('\0' != s[0]) VG_(err_bad_option)(qq_arg); \
True; \
}) \
)
// Arg whose value is denoted by the exact presence of the given string;
// if it matches, qq_var is assigned the value in qq_val.
#define VG_XACT_CLO(qq_arg, qq_option, qq_var, qq_val) \
(VG_STREQ((qq_arg), (qq_option)) && \
({ \
(qq_var) = (qq_val); \
True; \
}) \
)
/* Verbosity level: 0 = silent, 1 (default), > 1 = more verbose. */
extern Int VG_(clo_verbosity);
/* Emit all messages as XML? default: NO */
/* If clo_xml is set, various other options are set in a non-default
way. See vg_main.c and mc_main.c. */
extern Bool VG_(clo_xml);
/* An arbitrary user-supplied string which is copied into the
XML output, in between <usercomment> tags. */
extern HChar* VG_(clo_xml_user_comment);
/* Vex iropt control. Tool-visible so tools can make Vex optimise
less aggressively if that is needed (callgrind needs this). */
extern VexControl VG_(clo_vex_control);
/* Number of parents of a backtrace. Default: 8. */
extern Int VG_(clo_backtrace_size);
/* Continue stack traces below main()? Default: NO */
extern Bool VG_(clo_show_below_main);
/* Call this if a recognised option was bad for some reason. Note:
don't use it just because an option was unrecognised -- return
'False' from VG_(tdict).tool_process_cmd_line_option) to indicate that --
use it if eg. an option was given an inappropriate argument.
This function prints an error message, then shuts down the entire system.
It returns a Bool so it can be used in the _CLO_ macros. */
__attribute__((noreturn))
extern void VG_(err_bad_option) ( Char* opt );
/* Used to expand file names. "option_name" is the option name, eg.
"--log-file". 'format' is what follows, eg. "cachegrind.out.%p". In
'format':
- "%p" is replaced with PID.
- "%q{QUAL}" is replaced with the environment variable $QUAL. If $QUAL
isn't set, we abort. If the "{QUAL}" part is malformed, we abort.
- "%%" is replaced with "%".
Anything else after '%' causes an abort.
If the format specifies a relative file name, it's put in the program's
initial working directory. If it specifies an absolute file name (ie.
starts with '/') then it is put there.
Note that "option_name" has no effect on the returned string: the
returned string depends only on "format" and the PIDs and
environment variables that it references (if any). "option_name" is
merely used in printing error messages, if an error message needs
to be printed due to malformedness of the "format" argument.
*/
extern Char* VG_(expand_file_name)(Char* option_name, Char* format);
#endif // __PUB_TOOL_OPTIONS_H
/*--------------------------------------------------------------------*/
/*--- end ---*/
/*--------------------------------------------------------------------*/