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