Home | History | Annotate | Download | only in coregrind
      1 
      2 /*--------------------------------------------------------------------*/
      3 /*--- Startup: the real stuff                             m_main.c ---*/
      4 /*--------------------------------------------------------------------*/
      5 
      6 /*
      7    This file is part of Valgrind, a dynamic binary instrumentation
      8    framework.
      9 
     10    Copyright (C) 2000-2012 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 #include "pub_core_basics.h"
     32 #include "pub_core_vki.h"
     33 #include "pub_core_vkiscnums.h"
     34 #include "pub_core_libcsetjmp.h"    // to keep _threadstate.h happy
     35 #include "pub_core_threadstate.h"
     36 #include "pub_core_xarray.h"
     37 #include "pub_core_clientstate.h"
     38 #include "pub_core_aspacemgr.h"
     39 #include "pub_core_aspacehl.h"
     40 #include "pub_core_commandline.h"
     41 #include "pub_core_debuglog.h"
     42 #include "pub_core_errormgr.h"
     43 #include "pub_core_execontext.h"
     44 #include "pub_core_gdbserver.h"
     45 #include "pub_core_initimg.h"
     46 #include "pub_core_libcbase.h"
     47 #include "pub_core_libcassert.h"
     48 #include "pub_core_libcfile.h"
     49 #include "pub_core_libcprint.h"
     50 #include "pub_core_libcproc.h"
     51 #include "pub_core_libcsignal.h"
     52 #include "pub_core_syscall.h"       // VG_(strerror)
     53 #include "pub_core_mach.h"
     54 #include "pub_core_machine.h"
     55 #include "pub_core_mallocfree.h"
     56 #include "pub_core_options.h"
     57 #include "pub_core_debuginfo.h"
     58 #include "pub_core_redir.h"
     59 #include "pub_core_scheduler.h"
     60 #include "pub_core_seqmatch.h"      // For VG_(string_match)
     61 #include "pub_core_signals.h"
     62 #include "pub_core_stacks.h"        // For VG_(register_stack)
     63 #include "pub_core_syswrap.h"
     64 #include "pub_core_tooliface.h"
     65 #include "pub_core_translate.h"     // For VG_(translate)
     66 #include "pub_core_trampoline.h"
     67 #include "pub_core_transtab.h"
     68 #include "pub_tool_inner.h"
     69 #if defined(ENABLE_INNER_CLIENT_REQUEST)
     70 #include "valgrind.h"
     71 #endif
     72 
     73 
     74 /*====================================================================*/
     75 /*=== Counters, for profiling purposes only                        ===*/
     76 /*====================================================================*/
     77 
     78 static void print_all_stats ( void )
     79 {
     80    VG_(print_translation_stats)();
     81    VG_(print_tt_tc_stats)();
     82    VG_(print_scheduler_stats)();
     83    VG_(print_ExeContext_stats)();
     84    VG_(print_errormgr_stats)();
     85 
     86    // Memory stats
     87    if (VG_(clo_verbosity) > 2) {
     88       VG_(message)(Vg_DebugMsg, "\n");
     89       VG_(message)(Vg_DebugMsg,
     90          "------ Valgrind's internal memory use stats follow ------\n" );
     91       VG_(sanity_check_malloc_all)();
     92       VG_(message)(Vg_DebugMsg, "------\n" );
     93       VG_(print_all_arena_stats)();
     94       VG_(message)(Vg_DebugMsg, "\n");
     95    }
     96 }
     97 
     98 
     99 /*====================================================================*/
    100 /*=== Command-line: variables, processing, etc                     ===*/
    101 /*====================================================================*/
    102 
    103 // See pub_{core,tool}_options.h for explanations of all these.
    104 
    105 static void usage_NORETURN ( Bool debug_help )
    106 {
    107    /* 'usage1' contains a %s
    108       - for the name of the GDB executable
    109       - for the name of vgdb's path prefix
    110       which must be supplied when they are VG_(printf)'d. */
    111    Char* usage1 =
    112 "usage: valgrind [options] prog-and-args\n"
    113 "\n"
    114 "  tool-selection option, with default in [ ]:\n"
    115 "    --tool=<name>             use the Valgrind tool named <name> [memcheck]\n"
    116 "\n"
    117 "  basic user options for all Valgrind tools, with defaults in [ ]:\n"
    118 "    -h --help                 show this message\n"
    119 "    --help-debug              show this message, plus debugging options\n"
    120 "    --version                 show version\n"
    121 "    -q --quiet                run silently; only print error msgs\n"
    122 "    -v --verbose              be more verbose -- show misc extra info\n"
    123 "    --trace-children=no|yes   Valgrind-ise child processes (follow execve)? [no]\n"
    124 "    --trace-children-skip=patt1,patt2,...    specifies a list of executables\n"
    125 "                              that --trace-children=yes should not trace into\n"
    126 "    --trace-children-skip-by-arg=patt1,patt2,...   same as --trace-children-skip=\n"
    127 "                              but check the argv[] entries for children, rather\n"
    128 "                              than the exe name, to make a follow/no-follow decision\n"
    129 "    --child-silent-after-fork=no|yes omit child output between fork & exec? [no]\n"
    130 "    --vgdb=no|yes|full        activate gdbserver? [yes]\n"
    131 "                              full is slower but provides precise watchpoint/step\n"
    132 "    --vgdb-error=<number>     invoke gdbserver after <number> errors [%d]\n"
    133 "                              to get started quickly, use --vgdb-error=0\n"
    134 "                              and follow the on-screen directions\n"
    135 "    --track-fds=no|yes        track open file descriptors? [no]\n"
    136 "    --time-stamp=no|yes       add timestamps to log messages? [no]\n"
    137 "    --log-fd=<number>         log messages to file descriptor [2=stderr]\n"
    138 "    --log-file=<file>         log messages to <file>\n"
    139 "    --log-socket=ipaddr:port  log messages to socket ipaddr:port\n"
    140 "\n"
    141 "  user options for Valgrind tools that report errors:\n"
    142 "    --xml=yes                 emit error output in XML (some tools only)\n"
    143 "    --xml-fd=<number>         XML output to file descriptor\n"
    144 "    --xml-file=<file>         XML output to <file>\n"
    145 "    --xml-socket=ipaddr:port  XML output to socket ipaddr:port\n"
    146 "    --xml-user-comment=STR    copy STR verbatim into XML output\n"
    147 "    --demangle=no|yes         automatically demangle C++ names? [yes]\n"
    148 "    --num-callers=<number>    show <number> callers in stack traces [12]\n"
    149 "    --error-limit=no|yes      stop showing new errors if too many? [yes]\n"
    150 "    --error-exitcode=<number> exit code to return if errors found [0=disable]\n"
    151 "    --show-below-main=no|yes  continue stack traces below main() [no]\n"
    152 "    --suppressions=<filename> suppress errors described in <filename>\n"
    153 "    --gen-suppressions=no|yes|all    print suppressions for errors? [no]\n"
    154 "    --db-attach=no|yes        start debugger when errors detected? [no]\n"
    155 "    --db-command=<command>    command to start debugger [%s -nw %%f %%p]\n"
    156 "    --input-fd=<number>       file descriptor for input [0=stdin]\n"
    157 "    --dsymutil=no|yes         run dsymutil on Mac OS X when helpful? [no]\n"
    158 "    --max-stackframe=<number> assume stack switch for SP changes larger\n"
    159 "                              than <number> bytes [2000000]\n"
    160 "    --main-stacksize=<number> set size of main thread's stack (in bytes)\n"
    161 "                              [use current 'ulimit' value]\n"
    162 "\n"
    163 "  user options for Valgrind tools that replace malloc:\n"
    164 "    --alignment=<number>      set minimum alignment of heap allocations [%s]\n"
    165 "    --redzone-size=<number>   set minimum size of redzones added before/after\n"
    166 "                              heap blocks (in bytes). [%s]\n"
    167 "\n"
    168 "  uncommon user options for all Valgrind tools:\n"
    169 "    --fullpath-after=         (with nothing after the '=')\n"
    170 "                              show full source paths in call stacks\n"
    171 "    --fullpath-after=string   like --fullpath-after=, but only show the\n"
    172 "                              part of the path after 'string'.  Allows removal\n"
    173 "                              of path prefixes.  Use this flag multiple times\n"
    174 "                              to specify a set of prefixes to remove.\n"
    175 "    --smc-check=none|stack|all|all-non-file [stack]\n"
    176 "                              checks for self-modifying code: none, only for\n"
    177 "                              code found in stacks, for all code, or for all\n"
    178 "                              code except that from file-backed mappings\n"
    179 "    --read-var-info=yes|no    read debug info on stack and global variables\n"
    180 "                              and use it to print better error messages in\n"
    181 "                              tools that make use of it (Memcheck, Helgrind,\n"
    182 "                              DRD) [no]\n"
    183 "    --vgdb-poll=<number>      gdbserver poll max every <number> basic blocks [%d] \n"
    184 "    --vgdb-shadow-registers=no|yes   let gdb see the shadow registers [no]\n"
    185 "    --vgdb-prefix=<prefix>    prefix for vgdb FIFOs [%s]\n"
    186 "    --run-libc-freeres=no|yes free up glibc memory at exit on Linux? [yes]\n"
    187 "    --sim-hints=hint1,hint2,...  known hints:\n"
    188 "                                 lax-ioctls, enable-outer, fuse-compatible [none]\n"
    189 "    --fair-sched=no|yes|try   schedule threads fairly on multicore systems [no]\n"
    190 "    --kernel-variant=variant1,variant2,...  known variants: bproc [none]\n"
    191 "                              handle non-standard kernel variants\n"
    192 "    --show-emwarns=no|yes     show warnings about emulation limits? [no]\n"
    193 "    --require-text-symbol=:sonamepattern:symbolpattern    abort run if the\n"
    194 "                              stated shared object doesn't have the stated\n"
    195 "                              text symbol.  Patterns can contain ? and *.\n"
    196 "    --soname-synonyms=syn1=pattern1,syn2=pattern2,... synonym soname\n"
    197 "              specify patterns for function wrapping or replacement.\n"
    198 "              To use a non-libc malloc library that is\n"
    199 "                  in the main exe:  --soname-synonyms=somalloc=NONE\n"
    200 "                  in libxyzzy.so:   --soname-synonyms=somalloc=libxyzzy.so\n"
    201 "\n";
    202 
    203    Char* usage2 =
    204 "\n"
    205 "  debugging options for all Valgrind tools:\n"
    206 "    -d                        show verbose debugging output\n"
    207 "    --stats=no|yes            show tool and core statistics [no]\n"
    208 "    --sanity-level=<number>   level of sanity checking to do [1]\n"
    209 "    --trace-flags=<XXXXXXXX>   show generated code? (X = 0|1) [00000000]\n"
    210 "    --profile-flags=<XXXXXXXX> ditto, but for profiling (X = 0|1) [00000000]\n"
    211 "    --trace-notbelow=<number> only show BBs above <number> [999999999]\n"
    212 "    --trace-notabove=<number> only show BBs below <number> [0]\n"
    213 "    --trace-syscalls=no|yes   show all system calls? [no]\n"
    214 "    --trace-signals=no|yes    show signal handling details? [no]\n"
    215 "    --trace-symtab=no|yes     show symbol table details? [no]\n"
    216 "    --trace-symtab-patt=<patt> limit debuginfo tracing to obj name <patt>\n"
    217 "    --trace-cfi=no|yes        show call-frame-info details? [no]\n"
    218 "    --debug-dump=syms         mimic /usr/bin/readelf --syms\n"
    219 "    --debug-dump=line         mimic /usr/bin/readelf --debug-dump=line\n"
    220 "    --debug-dump=frames       mimic /usr/bin/readelf --debug-dump=frames\n"
    221 "    --trace-redir=no|yes      show redirection details? [no]\n"
    222 "    --trace-sched=no|yes      show thread scheduler details? [no]\n"
    223 "    --profile-heap=no|yes     profile Valgrind's own space use\n"
    224 "    --core-redzone=<number>   set minimum size of redzones added before/after\n"
    225 "                              heap blocks allocated for Valgrind internal use (in bytes) [4]\n"
    226 "    --wait-for-gdb=yes|no     pause on startup to wait for gdb attach\n"
    227 "    --sym-offsets=yes|no      show syms in form 'name+offset' ? [no]\n"
    228 "    --command-line-only=no|yes  only use command line options [no]\n"
    229 "\n"
    230 "  Vex options for all Valgrind tools:\n"
    231 "    --vex-iropt-verbosity=<0..9>           [0]\n"
    232 "    --vex-iropt-level=<0..2>               [2]\n"
    233 "    --vex-iropt-register-updates=unwindregs-at-mem-access\n"
    234 "                                |allregs-at-mem-access\n"
    235 "                                |allregs-at-each-insn  [unwindregs-at-mem-access]\n"
    236 "    --vex-iropt-unroll-thresh=<0..400>     [120]\n"
    237 "    --vex-guest-max-insns=<1..100>         [50]\n"
    238 "    --vex-guest-chase-thresh=<0..99>       [10]\n"
    239 "    --vex-guest-chase-cond=no|yes          [no]\n"
    240 "    --trace-flags and --profile-flags values (omit the middle space):\n"
    241 "       1000 0000   show conversion into IR\n"
    242 "       0100 0000   show after initial opt\n"
    243 "       0010 0000   show after instrumentation\n"
    244 "       0001 0000   show after second opt\n"
    245 "       0000 1000   show after tree building\n"
    246 "       0000 0100   show selecting insns\n"
    247 "       0000 0010   show after reg-alloc\n"
    248 "       0000 0001   show final assembly\n"
    249 "      (Nb: you need --trace-notbelow and/or --trace-notabove with --trace-flags for full details)\n"
    250 "\n"
    251 "  debugging options for Valgrind tools that report errors\n"
    252 "    --dump-error=<number>     show translation for basic block associated\n"
    253 "                              with <number>'th error context [0=show none]\n"
    254 "\n"
    255 "  debugging options for Valgrind tools that replace malloc:\n"
    256 "    --trace-malloc=no|yes     show client malloc details? [no]\n"
    257 "\n";
    258 
    259    Char* usage3 =
    260 "\n"
    261 "  Extra options read from ~/.valgrindrc, $VALGRIND_OPTS, ./.valgrindrc\n"
    262 "\n"
    263 "  %s is %s\n"
    264 "  Valgrind is Copyright (C) 2000-2012, and GNU GPL'd, by Julian Seward et al.\n"
    265 "  LibVEX is Copyright (C) 2004-2012, and GNU GPL'd, by OpenWorks LLP et al.\n"
    266 "\n"
    267 "  Bug reports, feedback, admiration, abuse, etc, to: %s.\n"
    268 "\n";
    269 
    270    Char* gdb_path = GDB_PATH;
    271    Char default_alignment[30];
    272    Char default_redzone_size[30];
    273 
    274    // Ensure the message goes to stdout
    275    VG_(log_output_sink).fd = 1;
    276    VG_(log_output_sink).is_socket = False;
    277 
    278    if (VG_(needs).malloc_replacement) {
    279       VG_(sprintf)(default_alignment,    "%d",  VG_MIN_MALLOC_SZB);
    280       VG_(sprintf)(default_redzone_size, "%lu", VG_(tdict).tool_client_redzone_szB);
    281    } else {
    282       VG_(strcpy)(default_alignment,    "not used by this tool");
    283       VG_(strcpy)(default_redzone_size, "not used by this tool");
    284    }
    285    /* 'usage1' a type as described after each arg. */
    286    VG_(printf)(usage1,
    287                VG_(clo_vgdb_error)        /* int */,
    288                gdb_path                   /* char* */,
    289                default_alignment          /* char* */,
    290                default_redzone_size       /* char* */,
    291                VG_(clo_vgdb_poll)         /* int */,
    292                VG_(vgdb_prefix_default)() /* char* */
    293                );
    294    if (VG_(details).name) {
    295       VG_(printf)("  user options for %s:\n", VG_(details).name);
    296       if (VG_(needs).command_line_options)
    297 	 VG_TDICT_CALL(tool_print_usage);
    298       else
    299 	 VG_(printf)("    (none)\n");
    300    }
    301    if (debug_help) {
    302       VG_(printf)("%s", usage2);
    303 
    304       if (VG_(details).name) {
    305          VG_(printf)("  debugging options for %s:\n", VG_(details).name);
    306 
    307          if (VG_(needs).command_line_options)
    308             VG_TDICT_CALL(tool_print_debug_usage);
    309          else
    310             VG_(printf)("    (none)\n");
    311       }
    312    }
    313    VG_(printf)(usage3, VG_(details).name, VG_(details).copyright_author,
    314                VG_BUGS_TO);
    315    VG_(exit)(0);
    316 }
    317 
    318 
    319 /* Peer at previously set up VG_(args_for_valgrind) and do some
    320    minimal command line processing that must happen early on:
    321 
    322    - show the version string, if requested (-v)
    323    - extract any request for help (--help, -h, --help-debug)
    324    - get the toolname (--tool=)
    325    - set VG_(clo_max_stackframe) (--max-stackframe=)
    326    - set VG_(clo_main_stacksize) (--main-stacksize=)
    327    - set VG_(clo_sim_hints) (--sim-hints=)
    328 
    329    That's all it does.  The main command line processing is done below
    330    by main_process_cmd_line_options.  Note that
    331    main_process_cmd_line_options has to handle but ignore the ones we
    332    have handled here.
    333 */
    334 static void early_process_cmd_line_options ( /*OUT*/Int* need_help,
    335                                              /*OUT*/HChar** tool )
    336 {
    337    UInt   i;
    338    HChar* str;
    339 
    340    vg_assert( VG_(args_for_valgrind) );
    341 
    342    /* parse the options we have (only the options we care about now) */
    343    for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) {
    344 
    345       str = * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i );
    346       vg_assert(str);
    347 
    348       // Nb: the version string goes to stdout.
    349       if VG_XACT_CLO(str, "--version", VG_(log_output_sink).fd, 1) {
    350          VG_(log_output_sink).is_socket = False;
    351          VG_(printf)("valgrind-" VERSION "\n");
    352          VG_(exit)(0);
    353       }
    354       else if VG_XACT_CLO(str, "--help", *need_help, *need_help+1) {}
    355       else if VG_XACT_CLO(str, "-h",     *need_help, *need_help+1) {}
    356 
    357       else if VG_XACT_CLO(str, "--help-debug", *need_help, *need_help+2) {}
    358 
    359       // The tool has already been determined, but we need to know the name
    360       // here.
    361       else if VG_STR_CLO(str, "--tool", *tool) {}
    362 
    363       // Set up VG_(clo_max_stackframe) and VG_(clo_main_stacksize).
    364       // These are needed by VG_(ii_create_image), which happens
    365       // before main_process_cmd_line_options().
    366       else if VG_INT_CLO(str, "--max-stackframe", VG_(clo_max_stackframe)) {}
    367       else if VG_INT_CLO(str, "--main-stacksize", VG_(clo_main_stacksize)) {}
    368 
    369       // Set up VG_(clo_sim_hints). This is needed a.o. for an inner
    370       // running in an outer, to have "no-inner-prefix" enabled
    371       // as early as possible.
    372       else if VG_STR_CLO (str, "--sim-hints",     VG_(clo_sim_hints)) {}
    373    }
    374 }
    375 
    376 /* The main processing for command line options.  See comments above
    377    on early_process_cmd_line_options.
    378 
    379    Comments on how the logging options are handled:
    380 
    381    User can specify:
    382       --log-fd=      for a fd to write to (default setting, fd = 2)
    383       --log-file=    for a file name to write to
    384       --log-socket=  for a socket to write to
    385 
    386    As a result of examining these and doing relevant socket/file
    387    opening, a final fd is established.  This is stored in
    388    VG_(log_output_sink) in m_libcprint.  Also, if --log-file=STR was
    389    specified, then STR, after expansion of %p and %q templates within
    390    it, is stored in VG_(clo_log_fname_expanded), in m_options, just in
    391    case anybody wants to know what it is.
    392 
    393    When printing, VG_(log_output_sink) is consulted to find the
    394    fd to send output to.
    395 
    396    Exactly analogous actions are undertaken for the XML output
    397    channel, with the one difference that the default fd is -1, meaning
    398    the channel is disabled by default.
    399 */
    400 static
    401 void main_process_cmd_line_options ( /*OUT*/Bool* logging_to_fd,
    402                                      /*OUT*/Char** xml_fname_unexpanded,
    403                                      const HChar* toolname )
    404 {
    405    // VG_(clo_log_fd) is used by all the messaging.  It starts as 2 (stderr)
    406    // and we cannot change it until we know what we are changing it to is
    407    // ok.  So we have tmp_log_fd to hold the tmp fd prior to that point.
    408    SysRes sres;
    409    Int    i, tmp_log_fd, tmp_xml_fd;
    410    Int    toolname_len = VG_(strlen)(toolname);
    411    Char*  tmp_str;         // Used in a couple of places.
    412    enum {
    413       VgLogTo_Fd,
    414       VgLogTo_File,
    415       VgLogTo_Socket
    416    } log_to = VgLogTo_Fd,   // Where is logging output to be sent?
    417      xml_to = VgLogTo_Fd;   // Where is XML output to be sent?
    418 
    419    /* Temporarily holds the string STR specified with
    420       --{log,xml}-{name,socket}=STR.  'fs' stands for
    421       file-or-socket. */
    422    Char* log_fsname_unexpanded = NULL;
    423    Char* xml_fsname_unexpanded = NULL;
    424 
    425    /* Log to stderr by default, but usage message goes to stdout.  XML
    426       output is initially disabled. */
    427    tmp_log_fd = 2;
    428    tmp_xml_fd = -1;
    429 
    430    /* Check for sane path in ./configure --prefix=... */
    431    if (VG_LIBDIR[0] != '/')
    432       VG_(err_config_error)("Please use absolute paths in "
    433                             "./configure --prefix=... or --libdir=...\n");
    434 
    435    vg_assert( VG_(args_for_valgrind) );
    436 
    437    /* BEGIN command-line processing loop */
    438 
    439    for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) {
    440 
    441       HChar* arg   = * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i );
    442       HChar* colon = arg;
    443 
    444       // Look for a colon in the option name.
    445       while (*colon && *colon != ':' && *colon != '=')
    446          colon++;
    447 
    448       // Does it have the form "--toolname:foo"?  We have to do it at the start
    449       // in case someone has combined a prefix with a core-specific option,
    450       // eg.  "--memcheck:verbose".
    451       if (*colon == ':') {
    452          if (VG_STREQN(2,            arg,                "--") &&
    453              VG_STREQN(toolname_len, arg+2,              toolname) &&
    454              VG_STREQN(1,            arg+2+toolname_len, ":"))
    455          {
    456             // Prefix matches, convert "--toolname:foo" to "--foo".
    457             // Two things to note:
    458             // - We cannot modify the option in-place.  If we did, and then
    459             //   a child was spawned with --trace-children=yes, the
    460             //   now-non-prefixed option would be passed and could screw up
    461             //   the child.
    462             // - We create copies, and never free them.  Why?  Non-prefixed
    463             //   options hang around forever, so tools need not make copies
    464             //   of strings within them.  We need to have the same behaviour
    465             //   for prefixed options.  The pointer to the copy will be lost
    466             //   once we leave this function (although a tool may keep a
    467             //   pointer into it), but the space wasted is insignificant.
    468             //   (In bug #142197, the copies were being freed, which caused
    469             //   problems for tools that reasonably assumed that arguments
    470             //   wouldn't disappear on them.)
    471             if (0)
    472                VG_(printf)("tool-specific arg: %s\n", arg);
    473             arg = VG_(strdup)("main.mpclo.1", arg + toolname_len + 1);
    474             arg[0] = '-';
    475             arg[1] = '-';
    476 
    477          } else {
    478             // prefix doesn't match, skip to next arg
    479             continue;
    480          }
    481       }
    482 
    483       /* Ignore these options - they've already been handled */
    484       if      VG_STREQN( 7, arg, "--tool=")              {}
    485       else if VG_STREQN(20, arg, "--command-line-only=") {}
    486       else if VG_STREQ(     arg, "--")                   {}
    487       else if VG_STREQ(     arg, "-d")                   {}
    488       else if VG_STREQN(16, arg, "--max-stackframe")     {}
    489       else if VG_STREQN(16, arg, "--main-stacksize")     {}
    490       else if VG_STREQN(11, arg,  "--sim-hints")         {}
    491       else if VG_STREQN(14, arg, "--profile-heap")       {}
    492       else if VG_STREQN(14, arg, "--core-redzone-size")  {}
    493       else if VG_STREQN(14, arg, "--redzone-size")       {}
    494 
    495       /* Obsolete options. Report an error and exit */
    496       else if VG_STREQN(34, arg, "--vex-iropt-precise-memory-exns=no") {
    497          VG_(fmsg_bad_option)
    498             (arg,
    499              "--vex-iropt-precise-memory-exns is obsolete\n"
    500              "Use --vex-iropt-register-updates=unwindregs-at-mem-access instead\n");
    501       }
    502       else if VG_STREQN(35, arg, "--vex-iropt-precise-memory-exns=yes") {
    503          VG_(fmsg_bad_option)
    504             (arg,
    505              "--vex-iropt-precise-memory-exns is obsolete\n"
    506              "Use --vex-iropt-register-updates=allregs-at-mem-access instead\n"
    507              " (or --vex-iropt-register-updates=allregs-at-each-insn)\n");
    508       }
    509 
    510       // These options are new.
    511       else if (VG_STREQ(arg, "-v") ||
    512                VG_STREQ(arg, "--verbose"))
    513          VG_(clo_verbosity)++;
    514 
    515       else if (VG_STREQ(arg, "-q") ||
    516                VG_STREQ(arg, "--quiet"))
    517          VG_(clo_verbosity)--;
    518 
    519       else if VG_BOOL_CLO(arg, "--stats",          VG_(clo_stats)) {}
    520       else if VG_BOOL_CLO(arg, "--xml",            VG_(clo_xml))
    521          VG_(debugLog_setXml)(VG_(clo_xml));
    522 
    523       else if VG_XACT_CLO(arg, "--vgdb=no",        VG_(clo_vgdb), Vg_VgdbNo) {}
    524       else if VG_XACT_CLO(arg, "--vgdb=yes",       VG_(clo_vgdb), Vg_VgdbYes) {}
    525       else if VG_XACT_CLO(arg, "--vgdb=full",      VG_(clo_vgdb), Vg_VgdbFull) {
    526          /* automatically updates register values at each insn
    527             with --vgdb=full */
    528          VG_(clo_vex_control).iropt_register_updates
    529             = VexRegUpdAllregsAtEachInsn;
    530       }
    531       else if VG_INT_CLO (arg, "--vgdb-poll",      VG_(clo_vgdb_poll)) {}
    532       else if VG_INT_CLO (arg, "--vgdb-error",     VG_(clo_vgdb_error)) {}
    533       else if VG_STR_CLO (arg, "--vgdb-prefix",    VG_(clo_vgdb_prefix)) {}
    534       else if VG_BOOL_CLO(arg, "--vgdb-shadow-registers",
    535                             VG_(clo_vgdb_shadow_registers)) {}
    536       else if VG_BOOL_CLO(arg, "--db-attach",      VG_(clo_db_attach)) {}
    537       else if VG_BOOL_CLO(arg, "--demangle",       VG_(clo_demangle)) {}
    538       else if VG_STR_CLO (arg, "--soname-synonyms",VG_(clo_soname_synonyms)) {}
    539       else if VG_BOOL_CLO(arg, "--error-limit",    VG_(clo_error_limit)) {}
    540       else if VG_INT_CLO (arg, "--error-exitcode", VG_(clo_error_exitcode)) {}
    541       else if VG_BOOL_CLO(arg, "--show-emwarns",   VG_(clo_show_emwarns)) {}
    542 
    543       else if VG_BOOL_CLO(arg, "--run-libc-freeres", VG_(clo_run_libc_freeres)) {}
    544       else if VG_BOOL_CLO(arg, "--show-below-main",  VG_(clo_show_below_main)) {}
    545       else if VG_BOOL_CLO(arg, "--time-stamp",       VG_(clo_time_stamp)) {}
    546       else if VG_BOOL_CLO(arg, "--track-fds",        VG_(clo_track_fds)) {}
    547       else if VG_BOOL_CLO(arg, "--trace-children",   VG_(clo_trace_children)) {}
    548       else if VG_BOOL_CLO(arg, "--child-silent-after-fork",
    549                             VG_(clo_child_silent_after_fork)) {}
    550       else if VG_STR_CLO(arg, "--fair-sched",        tmp_str) {
    551          if (VG_(strcmp)(tmp_str, "yes") == 0)
    552             VG_(clo_fair_sched) = enable_fair_sched;
    553          else if (VG_(strcmp)(tmp_str, "try") == 0)
    554             VG_(clo_fair_sched) = try_fair_sched;
    555          else if (VG_(strcmp)(tmp_str, "no") == 0)
    556             VG_(clo_fair_sched) = disable_fair_sched;
    557          else
    558             VG_(fmsg_bad_option)(arg, "");
    559 
    560       }
    561       else if VG_BOOL_CLO(arg, "--trace-sched",      VG_(clo_trace_sched)) {}
    562       else if VG_BOOL_CLO(arg, "--trace-signals",    VG_(clo_trace_signals)) {}
    563       else if VG_BOOL_CLO(arg, "--trace-symtab",     VG_(clo_trace_symtab)) {}
    564       else if VG_STR_CLO (arg, "--trace-symtab-patt", VG_(clo_trace_symtab_patt)) {}
    565       else if VG_BOOL_CLO(arg, "--trace-cfi",        VG_(clo_trace_cfi)) {}
    566       else if VG_XACT_CLO(arg, "--debug-dump=syms",  VG_(clo_debug_dump_syms),
    567                                                      True) {}
    568       else if VG_XACT_CLO(arg, "--debug-dump=line",  VG_(clo_debug_dump_line),
    569                                                      True) {}
    570       else if VG_XACT_CLO(arg, "--debug-dump=frames",
    571                                VG_(clo_debug_dump_frames), True) {}
    572       else if VG_BOOL_CLO(arg, "--trace-redir",      VG_(clo_trace_redir)) {}
    573 
    574       else if VG_BOOL_CLO(arg, "--trace-syscalls",   VG_(clo_trace_syscalls)) {}
    575       else if VG_BOOL_CLO(arg, "--wait-for-gdb",     VG_(clo_wait_for_gdb)) {}
    576       else if VG_STR_CLO (arg, "--db-command",       VG_(clo_db_command)) {}
    577       else if VG_BOOL_CLO(arg, "--sym-offsets",      VG_(clo_sym_offsets)) {}
    578       else if VG_BOOL_CLO(arg, "--read-var-info",    VG_(clo_read_var_info)) {}
    579 
    580       else if VG_INT_CLO (arg, "--dump-error",       VG_(clo_dump_error))   {}
    581       else if VG_INT_CLO (arg, "--input-fd",         VG_(clo_input_fd))     {}
    582       else if VG_INT_CLO (arg, "--sanity-level",     VG_(clo_sanity_level)) {}
    583       else if VG_BINT_CLO(arg, "--num-callers",      VG_(clo_backtrace_size), 1,
    584                                                      VG_DEEPEST_BACKTRACE) {}
    585 
    586       else if VG_XACT_CLO(arg, "--smc-check=none",  VG_(clo_smc_check),
    587                                                     Vg_SmcNone);
    588       else if VG_XACT_CLO(arg, "--smc-check=stack", VG_(clo_smc_check),
    589                                                     Vg_SmcStack);
    590       else if VG_XACT_CLO(arg, "--smc-check=all",   VG_(clo_smc_check),
    591                                                     Vg_SmcAll);
    592       else if VG_XACT_CLO(arg, "--smc-check=all-non-file",
    593                                                     VG_(clo_smc_check),
    594                                                     Vg_SmcAllNonFile);
    595 
    596       else if VG_STR_CLO (arg, "--kernel-variant",  VG_(clo_kernel_variant)) {}
    597 
    598       else if VG_BOOL_CLO(arg, "--dsymutil",        VG_(clo_dsymutil)) {}
    599 
    600       else if VG_STR_CLO (arg, "--trace-children-skip",
    601                                VG_(clo_trace_children_skip)) {}
    602       else if VG_STR_CLO (arg, "--trace-children-skip-by-arg",
    603                                VG_(clo_trace_children_skip_by_arg)) {}
    604 
    605       else if VG_BINT_CLO(arg, "--vex-iropt-verbosity",
    606                        VG_(clo_vex_control).iropt_verbosity, 0, 10) {}
    607       else if VG_BINT_CLO(arg, "--vex-iropt-level",
    608                        VG_(clo_vex_control).iropt_level, 0, 2) {}
    609       else if VG_XACT_CLO(arg,
    610                        "--vex-iropt-register-updates=unwindregs-at-mem-access",
    611                        VG_(clo_vex_control).iropt_register_updates,
    612                        VexRegUpdUnwindregsAtMemAccess);
    613       else if VG_XACT_CLO(arg,
    614                        "--vex-iropt-register-updates=allregs-at-mem-access",
    615                        VG_(clo_vex_control).iropt_register_updates,
    616                        VexRegUpdAllregsAtMemAccess);
    617       else if VG_XACT_CLO(arg,
    618                        "--vex-iropt-register-updates=allregs-at-each-insn",
    619                        VG_(clo_vex_control).iropt_register_updates,
    620                        VexRegUpdAllregsAtEachInsn);
    621       else if VG_BINT_CLO(arg, "--vex-iropt-unroll-thresh",
    622                        VG_(clo_vex_control).iropt_unroll_thresh, 0, 400) {}
    623       else if VG_BINT_CLO(arg, "--vex-guest-max-insns",
    624                        VG_(clo_vex_control).guest_max_insns, 1, 100) {}
    625       else if VG_BINT_CLO(arg, "--vex-guest-chase-thresh",
    626                        VG_(clo_vex_control).guest_chase_thresh, 0, 99) {}
    627       else if VG_BOOL_CLO(arg, "--vex-guest-chase-cond",
    628                        VG_(clo_vex_control).guest_chase_cond) {}
    629 
    630       else if VG_INT_CLO(arg, "--log-fd", tmp_log_fd) {
    631          log_to = VgLogTo_Fd;
    632          log_fsname_unexpanded = NULL;
    633       }
    634       else if VG_INT_CLO(arg, "--xml-fd", tmp_xml_fd) {
    635          xml_to = VgLogTo_Fd;
    636          xml_fsname_unexpanded = NULL;
    637       }
    638 
    639       else if VG_STR_CLO(arg, "--log-file", log_fsname_unexpanded) {
    640          log_to = VgLogTo_File;
    641       }
    642       else if VG_STR_CLO(arg, "--xml-file", xml_fsname_unexpanded) {
    643          xml_to = VgLogTo_File;
    644       }
    645 
    646       else if VG_STR_CLO(arg, "--log-socket", log_fsname_unexpanded) {
    647          log_to = VgLogTo_Socket;
    648       }
    649       else if VG_STR_CLO(arg, "--xml-socket", xml_fsname_unexpanded) {
    650          xml_to = VgLogTo_Socket;
    651       }
    652 
    653       else if VG_STR_CLO(arg, "--xml-user-comment",
    654                               VG_(clo_xml_user_comment)) {}
    655 
    656       else if VG_STR_CLO(arg, "--suppressions", tmp_str) {
    657          if (VG_(clo_n_suppressions) >= VG_CLO_MAX_SFILES) {
    658             VG_(fmsg_bad_option)(arg,
    659                "Too many suppression files specified.\n"
    660                "Increase VG_CLO_MAX_SFILES and recompile.\n");
    661          }
    662          VG_(clo_suppressions)[VG_(clo_n_suppressions)] = tmp_str;
    663          VG_(clo_n_suppressions)++;
    664       }
    665 
    666       else if VG_STR_CLO (arg, "--fullpath-after", tmp_str) {
    667          if (VG_(clo_n_fullpath_after) >= VG_CLO_MAX_FULLPATH_AFTER) {
    668             VG_(fmsg_bad_option)(arg,
    669                "Too many --fullpath-after= specifications.\n"
    670                "Increase VG_CLO_MAX_FULLPATH_AFTER and recompile.\n");
    671          }
    672          VG_(clo_fullpath_after)[VG_(clo_n_fullpath_after)] = tmp_str;
    673          VG_(clo_n_fullpath_after)++;
    674       }
    675 
    676       else if VG_STR_CLO(arg, "--require-text-symbol", tmp_str) {
    677          if (VG_(clo_n_req_tsyms) >= VG_CLO_MAX_REQ_TSYMS) {
    678             VG_(fmsg_bad_option)(arg,
    679                "Too many --require-text-symbol= specifications.\n"
    680                "Increase VG_CLO_MAX_REQ_TSYMS and recompile.\n");
    681          }
    682          /* String needs to be of the form C?*C?*, where C is any
    683             character, but is the same both times.  Having it in this
    684             form facilitates finding the boundary between the sopatt
    685             and the fnpatt just by looking for the second occurrence
    686             of C, without hardwiring any assumption about what C
    687             is. */
    688          Char patt[7];
    689          Bool ok = True;
    690          ok = tmp_str && VG_(strlen)(tmp_str) > 0;
    691          if (ok) {
    692            patt[0] = patt[3] = tmp_str[0];
    693            patt[1] = patt[4] = '?';
    694            patt[2] = patt[5] = '*';
    695            patt[6] = 0;
    696            ok = VG_(string_match)(patt, tmp_str);
    697          }
    698          if (!ok) {
    699             VG_(fmsg_bad_option)(arg,
    700                "Invalid --require-text-symbol= specification.\n");
    701          }
    702          VG_(clo_req_tsyms)[VG_(clo_n_req_tsyms)] = tmp_str;
    703          VG_(clo_n_req_tsyms)++;
    704       }
    705 
    706       /* "stuvwxyz" --> stuvwxyz (binary) */
    707       else if VG_STR_CLO(arg, "--trace-flags", tmp_str) {
    708          Int j;
    709 
    710          if (8 != VG_(strlen)(tmp_str)) {
    711             VG_(fmsg_bad_option)(arg,
    712                "--trace-flags argument must have 8 digits\n");
    713          }
    714          for (j = 0; j < 8; j++) {
    715             if      ('0' == tmp_str[j]) { /* do nothing */ }
    716             else if ('1' == tmp_str[j]) VG_(clo_trace_flags) |= (1 << (7-j));
    717             else {
    718                VG_(fmsg_bad_option)(arg,
    719                   "--trace-flags argument can only contain 0s and 1s\n");
    720             }
    721          }
    722       }
    723 
    724       /* "stuvwxyz" --> stuvwxyz (binary) */
    725       else if VG_STR_CLO(arg, "--profile-flags", tmp_str) {
    726          Int j;
    727 
    728          if (8 != VG_(strlen)(tmp_str)) {
    729             VG_(fmsg_bad_option)(arg,
    730                "--profile-flags argument must have 8 digits\n");
    731          }
    732          for (j = 0; j < 8; j++) {
    733             if      ('0' == tmp_str[j]) { /* do nothing */ }
    734             else if ('1' == tmp_str[j]) VG_(clo_profile_flags) |= (1 << (7-j));
    735             else {
    736                VG_(fmsg_bad_option)(arg,
    737                   "--profile-flags argument can only contain 0s and 1s\n");
    738             }
    739          }
    740       }
    741 
    742       else if VG_INT_CLO (arg, "--trace-notbelow", VG_(clo_trace_notbelow)) {}
    743 
    744       else if VG_INT_CLO (arg, "--trace-notabove", VG_(clo_trace_notabove)) {}
    745 
    746       else if VG_XACT_CLO(arg, "--gen-suppressions=no",
    747                                VG_(clo_gen_suppressions), 0) {}
    748       else if VG_XACT_CLO(arg, "--gen-suppressions=yes",
    749                                VG_(clo_gen_suppressions), 1) {}
    750       else if VG_XACT_CLO(arg, "--gen-suppressions=all",
    751                                VG_(clo_gen_suppressions), 2) {}
    752 
    753       else if ( ! VG_(needs).command_line_options
    754              || ! VG_TDICT_CALL(tool_process_cmd_line_option, arg) ) {
    755          VG_(fmsg_bad_option)(arg, "");
    756       }
    757    }
    758 
    759    /* END command-line processing loop */
    760 
    761    /* Determine the path prefix for vgdb */
    762    if (VG_(clo_vgdb_prefix) == NULL)
    763      VG_(clo_vgdb_prefix) = VG_(vgdb_prefix_default)();
    764 
    765    /* Make VEX control parameters sane */
    766 
    767    if (VG_(clo_vex_control).guest_chase_thresh
    768        >= VG_(clo_vex_control).guest_max_insns)
    769       VG_(clo_vex_control).guest_chase_thresh
    770          = VG_(clo_vex_control).guest_max_insns - 1;
    771 
    772    if (VG_(clo_vex_control).guest_chase_thresh < 0)
    773       VG_(clo_vex_control).guest_chase_thresh = 0;
    774 
    775    /* Check various option values */
    776 
    777    if (VG_(clo_verbosity) < 0)
    778       VG_(clo_verbosity) = 0;
    779 
    780    if (VG_(clo_trace_notbelow) == -1) {
    781      if (VG_(clo_trace_notabove) == -1) {
    782        /* [] */
    783        VG_(clo_trace_notbelow) = 2147483647;
    784        VG_(clo_trace_notabove) = 0;
    785      } else {
    786        /* [0 .. notabove] */
    787        VG_(clo_trace_notbelow) = 0;
    788      }
    789    } else {
    790      if (VG_(clo_trace_notabove) == -1) {
    791        /* [notbelow .. ]  */
    792        VG_(clo_trace_notabove) = 2147483647;
    793      } else {
    794        /* [notbelow .. notabove]  */
    795      }
    796    }
    797 
    798    VG_(dyn_vgdb_error) = VG_(clo_vgdb_error);
    799 
    800    if (VG_(clo_gen_suppressions) > 0 &&
    801        !VG_(needs).core_errors && !VG_(needs).tool_errors) {
    802       VG_(fmsg_bad_option)("--gen-suppressions=yes",
    803          "Can't use --gen-suppressions= with %s\n"
    804          "because it doesn't generate errors.\n", VG_(details).name);
    805    }
    806 
    807    /* If XML output is requested, check that the tool actually
    808       supports it. */
    809    if (VG_(clo_xml) && !VG_(needs).xml_output) {
    810       VG_(clo_xml) = False;
    811       VG_(fmsg_bad_option)("--xml=yes",
    812          "%s does not support XML output.\n", VG_(details).name);
    813       /*NOTREACHED*/
    814    }
    815 
    816    vg_assert( VG_(clo_gen_suppressions) >= 0 );
    817    vg_assert( VG_(clo_gen_suppressions) <= 2 );
    818 
    819    /* If we've been asked to emit XML, mash around various other
    820       options so as to constrain the output somewhat, and to remove
    821       any need for user input during the run.
    822    */
    823    if (VG_(clo_xml)) {
    824 
    825       /* We can't allow --gen-suppressions=yes, since that requires us
    826          to print the error and then ask the user if she wants a
    827          suppression for it, but in XML mode we won't print it until
    828          we know whether we also need to print a suppression.  Hence a
    829          circular dependency.  So disallow this.
    830          (--gen-suppressions=all is still OK since we don't need any
    831          user interaction in this case.) */
    832       if (VG_(clo_gen_suppressions) == 1) {
    833          VG_(fmsg_bad_option)(
    834             "--xml=yes together with --gen-suppressions=yes",
    835             "When --xml=yes is specified, --gen-suppressions=no\n"
    836             "or --gen-suppressions=all is allowed, but not "
    837             "--gen-suppressions=yes.\n");
    838       }
    839 
    840       /* We can't allow DB attaching (or we maybe could, but results
    841          could be chaotic ..) since it requires user input.  Hence
    842          disallow. */
    843       if (VG_(clo_db_attach)) {
    844          VG_(fmsg_bad_option)(
    845             "--xml=yes together with --db-attach=yes",
    846             "--db-attach=yes is not allowed with --xml=yes\n"
    847             "because it would require user input.\n");
    848       }
    849 
    850       /* Disallow dump_error in XML mode; sounds like a recipe for
    851          chaos.  No big deal; dump_error is a flag for debugging V
    852          itself. */
    853       if (VG_(clo_dump_error) > 0) {
    854          VG_(fmsg_bad_option)("--xml=yes together with --dump-error", "");
    855       }
    856 
    857       /* Disable error limits (this might be a bad idea!) */
    858       VG_(clo_error_limit) = False;
    859       /* Disable emulation warnings */
    860 
    861       /* Also, we want to set options for the leak checker, but that
    862          will have to be done in Memcheck's flag-handling code, not
    863          here. */
    864    }
    865 
    866    /* All non-logging-related options have been checked.  If the logging
    867       option specified is ok, we can switch to it, as we know we won't
    868       have to generate any other command-line-related error messages.
    869       (So far we should be still attached to stderr, so we can show on
    870       the terminal any problems to do with processing command line
    871       opts.)
    872 
    873       So set up logging now.  After this is done, VG_(log_output_sink)
    874       and (if relevant) VG_(xml_output_sink) should be connected to
    875       whatever sink has been selected, and we indiscriminately chuck
    876       stuff into it without worrying what the nature of it is.  Oh the
    877       wonder of Unix streams. */
    878 
    879    vg_assert(VG_(log_output_sink).fd == 2 /* stderr */);
    880    vg_assert(VG_(log_output_sink).is_socket == False);
    881    vg_assert(VG_(clo_log_fname_expanded) == NULL);
    882 
    883    vg_assert(VG_(xml_output_sink).fd == -1 /* disabled */);
    884    vg_assert(VG_(xml_output_sink).is_socket == False);
    885    vg_assert(VG_(clo_xml_fname_expanded) == NULL);
    886 
    887    /* --- set up the normal text output channel --- */
    888 
    889    switch (log_to) {
    890 
    891       case VgLogTo_Fd:
    892          vg_assert(log_fsname_unexpanded == NULL);
    893          break;
    894 
    895       case VgLogTo_File: {
    896          Char* logfilename;
    897 
    898          vg_assert(log_fsname_unexpanded != NULL);
    899          vg_assert(VG_(strlen)(log_fsname_unexpanded) <= 900); /* paranoia */
    900 
    901          // Nb: we overwrite an existing file of this name without asking
    902          // any questions.
    903          logfilename = VG_(expand_file_name)("--log-file",
    904                                              log_fsname_unexpanded);
    905          sres = VG_(open)(logfilename,
    906                           VKI_O_CREAT|VKI_O_WRONLY|VKI_O_TRUNC,
    907                           VKI_S_IRUSR|VKI_S_IWUSR);
    908          if (!sr_isError(sres)) {
    909             tmp_log_fd = sr_Res(sres);
    910             VG_(clo_log_fname_expanded) = logfilename;
    911          } else {
    912             VG_(fmsg)("can't create log file '%s': %s\n",
    913                       logfilename, VG_(strerror)(sr_Err(sres)));
    914             VG_(exit)(1);
    915             /*NOTREACHED*/
    916          }
    917          break;
    918       }
    919 
    920       case VgLogTo_Socket: {
    921          vg_assert(log_fsname_unexpanded != NULL);
    922          vg_assert(VG_(strlen)(log_fsname_unexpanded) <= 900); /* paranoia */
    923          tmp_log_fd = VG_(connect_via_socket)( log_fsname_unexpanded );
    924          if (tmp_log_fd == -1) {
    925             VG_(fmsg)("Invalid --log-socket spec of '%s'\n",
    926                       log_fsname_unexpanded);
    927             VG_(exit)(1);
    928             /*NOTREACHED*/
    929 	 }
    930          if (tmp_log_fd == -2) {
    931             VG_(umsg)("failed to connect to logging server '%s'.\n"
    932                       "Log messages will sent to stderr instead.\n",
    933                       log_fsname_unexpanded );
    934 
    935             /* We don't change anything here. */
    936             vg_assert(VG_(log_output_sink).fd == 2);
    937             tmp_log_fd = 2;
    938 	 } else {
    939             vg_assert(tmp_log_fd > 0);
    940             VG_(log_output_sink).is_socket = True;
    941          }
    942          break;
    943       }
    944    }
    945 
    946    /* --- set up the XML output channel --- */
    947 
    948    switch (xml_to) {
    949 
    950       case VgLogTo_Fd:
    951          vg_assert(xml_fsname_unexpanded == NULL);
    952          break;
    953 
    954       case VgLogTo_File: {
    955          Char* xmlfilename;
    956 
    957          vg_assert(xml_fsname_unexpanded != NULL);
    958          vg_assert(VG_(strlen)(xml_fsname_unexpanded) <= 900); /* paranoia */
    959 
    960          // Nb: we overwrite an existing file of this name without asking
    961          // any questions.
    962          xmlfilename = VG_(expand_file_name)("--xml-file",
    963                                              xml_fsname_unexpanded);
    964          sres = VG_(open)(xmlfilename,
    965                           VKI_O_CREAT|VKI_O_WRONLY|VKI_O_TRUNC,
    966                           VKI_S_IRUSR|VKI_S_IWUSR);
    967          if (!sr_isError(sres)) {
    968             tmp_xml_fd = sr_Res(sres);
    969             VG_(clo_xml_fname_expanded) = xmlfilename;
    970             /* strdup here is probably paranoid overkill, but ... */
    971             *xml_fname_unexpanded = VG_(strdup)( "main.mpclo.2",
    972                                                  xml_fsname_unexpanded );
    973          } else {
    974             VG_(fmsg)("can't create XML file '%s': %s\n",
    975                       xmlfilename, VG_(strerror)(sr_Err(sres)));
    976             VG_(exit)(1);
    977             /*NOTREACHED*/
    978          }
    979          break;
    980       }
    981 
    982       case VgLogTo_Socket: {
    983          vg_assert(xml_fsname_unexpanded != NULL);
    984          vg_assert(VG_(strlen)(xml_fsname_unexpanded) <= 900); /* paranoia */
    985          tmp_xml_fd = VG_(connect_via_socket)( xml_fsname_unexpanded );
    986          if (tmp_xml_fd == -1) {
    987             VG_(fmsg)("Invalid --xml-socket spec of '%s'\n",
    988                       xml_fsname_unexpanded );
    989             VG_(exit)(1);
    990             /*NOTREACHED*/
    991 	 }
    992          if (tmp_xml_fd == -2) {
    993             VG_(umsg)("failed to connect to XML logging server '%s'.\n"
    994                       "XML output will sent to stderr instead.\n",
    995                       xml_fsname_unexpanded);
    996             /* We don't change anything here. */
    997             vg_assert(VG_(xml_output_sink).fd == 2);
    998             tmp_xml_fd = 2;
    999 	 } else {
   1000             vg_assert(tmp_xml_fd > 0);
   1001             VG_(xml_output_sink).is_socket = True;
   1002          }
   1003          break;
   1004       }
   1005    }
   1006 
   1007    /* If we've got this far, and XML mode was requested, but no XML
   1008       output channel appears to have been specified, just stop.  We
   1009       could continue, and XML output will simply vanish into nowhere,
   1010       but that is likely to confuse the hell out of users, which is
   1011       distinctly Ungood. */
   1012    if (VG_(clo_xml) && tmp_xml_fd == -1) {
   1013       VG_(fmsg_bad_option)(
   1014           "--xml=yes, but no XML destination specified",
   1015           "--xml=yes has been specified, but there is no XML output\n"
   1016           "destination.  You must specify an XML output destination\n"
   1017           "using --xml-fd, --xml-file or --xml-socket.\n"
   1018       );
   1019    }
   1020 
   1021    // Finalise the output fds: the log fd ..
   1022 
   1023    if (tmp_log_fd >= 0) {
   1024       // Move log_fd into the safe range, so it doesn't conflict with
   1025       // any app fds.
   1026       tmp_log_fd = VG_(fcntl)(tmp_log_fd, VKI_F_DUPFD, VG_(fd_hard_limit));
   1027       if (tmp_log_fd < 0) {
   1028          VG_(message)(Vg_UserMsg, "valgrind: failed to move logfile fd "
   1029                                   "into safe range, using stderr\n");
   1030          VG_(log_output_sink).fd = 2;   // stderr
   1031          VG_(log_output_sink).is_socket = False;
   1032       } else {
   1033          VG_(log_output_sink).fd = tmp_log_fd;
   1034          VG_(fcntl)(VG_(log_output_sink).fd, VKI_F_SETFD, VKI_FD_CLOEXEC);
   1035       }
   1036    } else {
   1037       // If they said --log-fd=-1, don't print anything.  Plausible for use in
   1038       // regression testing suites that use client requests to count errors.
   1039       VG_(log_output_sink).fd = -1;
   1040       VG_(log_output_sink).is_socket = False;
   1041    }
   1042 
   1043    // Finalise the output fds: and the XML fd ..
   1044 
   1045    if (tmp_xml_fd >= 0) {
   1046       // Move xml_fd into the safe range, so it doesn't conflict with
   1047       // any app fds.
   1048       tmp_xml_fd = VG_(fcntl)(tmp_xml_fd, VKI_F_DUPFD, VG_(fd_hard_limit));
   1049       if (tmp_xml_fd < 0) {
   1050          VG_(message)(Vg_UserMsg, "valgrind: failed to move XML file fd "
   1051                                   "into safe range, using stderr\n");
   1052          VG_(xml_output_sink).fd = 2;   // stderr
   1053          VG_(xml_output_sink).is_socket = False;
   1054       } else {
   1055          VG_(xml_output_sink).fd = tmp_xml_fd;
   1056          VG_(fcntl)(VG_(xml_output_sink).fd, VKI_F_SETFD, VKI_FD_CLOEXEC);
   1057       }
   1058    } else {
   1059       // If they said --xml-fd=-1, don't print anything.  Plausible for use in
   1060       // regression testing suites that use client requests to count errors.
   1061       VG_(xml_output_sink).fd = -1;
   1062       VG_(xml_output_sink).is_socket = False;
   1063    }
   1064 
   1065    // Suppressions related stuff
   1066 
   1067    if (VG_(clo_n_suppressions) < VG_CLO_MAX_SFILES-1 &&
   1068        (VG_(needs).core_errors || VG_(needs).tool_errors)) {
   1069       /* If we haven't reached the max number of suppressions, load
   1070          the default one. */
   1071       static const Char default_supp[] = "default.supp";
   1072       Int len = VG_(strlen)(VG_(libdir)) + 1 + sizeof(default_supp);
   1073       Char *buf = VG_(arena_malloc)(VG_AR_CORE, "main.mpclo.3", len);
   1074       VG_(sprintf)(buf, "%s/%s", VG_(libdir), default_supp);
   1075       VG_(clo_suppressions)[VG_(clo_n_suppressions)] = buf;
   1076       VG_(clo_n_suppressions)++;
   1077    }
   1078 
   1079    *logging_to_fd = log_to == VgLogTo_Fd || log_to == VgLogTo_Socket;
   1080 }
   1081 
   1082 // Write the name and value of log file qualifiers to the xml file.
   1083 static void print_file_vars(Char* format)
   1084 {
   1085    Int i = 0;
   1086 
   1087    while (format[i]) {
   1088       if (format[i] == '%') {
   1089          // We saw a '%'.  What's next...
   1090          i++;
   1091 	 if ('q' == format[i]) {
   1092             i++;
   1093             if ('{' == format[i]) {
   1094 	       // Get the env var name, print its contents.
   1095 	       Char* qualname;
   1096                Char* qual;
   1097                i++;
   1098                qualname = &format[i];
   1099                while (True) {
   1100 		  if ('}' == format[i]) {
   1101                      // Temporarily replace the '}' with NUL to extract var
   1102                      // name.
   1103 		     format[i] = 0;
   1104                      qual = VG_(getenv)(qualname);
   1105 		     break;
   1106                   }
   1107                   i++;
   1108                }
   1109 
   1110                VG_(printf_xml)(
   1111                   "<logfilequalifier> <var>%pS</var> "
   1112                   "<value>%pS</value> </logfilequalifier>\n",
   1113                   qualname,qual
   1114                );
   1115 	       format[i] = '}';
   1116 	       i++;
   1117 	    }
   1118          }
   1119       } else {
   1120 	 i++;
   1121       }
   1122    }
   1123 }
   1124 
   1125 
   1126 /*====================================================================*/
   1127 /*=== Printing the preamble                                        ===*/
   1128 /*====================================================================*/
   1129 
   1130 // Print the argument, escaping any chars that require it.
   1131 static void umsg_arg(const Char* arg)
   1132 {
   1133    SizeT len = VG_(strlen)(arg);
   1134    Char* special = " \\<>";
   1135    Int i;
   1136    for (i = 0; i < len; i++) {
   1137       if (VG_(strchr)(special, arg[i])) {
   1138          VG_(umsg)("\\");   // escape with a backslash if necessary
   1139       }
   1140       VG_(umsg)("%c", arg[i]);
   1141    }
   1142 }
   1143 
   1144 // Send output to the XML-stream and escape any XML meta-characters.
   1145 static void xml_arg(const Char* arg)
   1146 {
   1147    VG_(printf_xml)("%pS", arg);
   1148 }
   1149 
   1150 /* Ok, the logging sink is running now.  Print a suitable preamble.
   1151    If logging to file or a socket, write details of parent PID and
   1152    command line args, to help people trying to interpret the
   1153    results of a run which encompasses multiple processes. */
   1154 static void print_preamble ( Bool logging_to_fd,
   1155                              Char* xml_fname_unexpanded,
   1156                              const HChar* toolname )
   1157 {
   1158    Int    i;
   1159    HChar* xpre  = VG_(clo_xml) ? "  <line>" : "";
   1160    HChar* xpost = VG_(clo_xml) ? "</line>" : "";
   1161    UInt (*umsg_or_xml)( const HChar*, ... )
   1162       = VG_(clo_xml) ? VG_(printf_xml) : VG_(umsg);
   1163 
   1164    void (*umsg_or_xml_arg)( const Char* )
   1165       = VG_(clo_xml) ? xml_arg : umsg_arg;
   1166 
   1167    vg_assert( VG_(args_for_client) );
   1168    vg_assert( VG_(args_for_valgrind) );
   1169    vg_assert( toolname );
   1170 
   1171    if (VG_(clo_xml)) {
   1172       VG_(printf_xml)("<?xml version=\"1.0\"?>\n");
   1173       VG_(printf_xml)("\n");
   1174       VG_(printf_xml)("<valgrindoutput>\n");
   1175       VG_(printf_xml)("\n");
   1176       VG_(printf_xml)("<protocolversion>4</protocolversion>\n");
   1177       VG_(printf_xml)("<protocoltool>%s</protocoltool>\n", toolname);
   1178       VG_(printf_xml)("\n");
   1179    }
   1180 
   1181    if (VG_(clo_xml) || VG_(clo_verbosity > 0)) {
   1182 
   1183       if (VG_(clo_xml))
   1184          VG_(printf_xml)("<preamble>\n");
   1185 
   1186       /* Tool details */
   1187       umsg_or_xml( VG_(clo_xml) ? "%s%pS%pS%pS, %pS%s\n" : "%s%s%s%s, %s%s\n",
   1188                    xpre,
   1189                    VG_(details).name,
   1190                    NULL == VG_(details).version ? "" : "-",
   1191                    NULL == VG_(details).version
   1192                       ? (Char*)"" : VG_(details).version,
   1193                    VG_(details).description,
   1194                    xpost );
   1195 
   1196       if (VG_(strlen)(toolname) >= 4 && VG_STREQN(4, toolname, "exp-")) {
   1197          umsg_or_xml(
   1198             "%sNOTE: This is an Experimental-Class Valgrind Tool%s\n",
   1199             xpre, xpost
   1200          );
   1201       }
   1202 
   1203       umsg_or_xml( VG_(clo_xml) ? "%s%pS%s\n" : "%s%s%s\n",
   1204                    xpre, VG_(details).copyright_author, xpost );
   1205 
   1206       /* Core details */
   1207       umsg_or_xml(
   1208          "%sUsing Valgrind-%s and LibVEX; rerun with -h for copyright info%s\n",
   1209          xpre, VERSION, xpost
   1210       );
   1211 
   1212       // Print the command line.  At one point we wrapped at 80 chars and
   1213       // printed a '\' as a line joiner, but that makes it hard to cut and
   1214       // paste the command line (because of the "==pid==" prefixes), so we now
   1215       // favour utility and simplicity over aesthetics.
   1216       umsg_or_xml("%sCommand: ", xpre);
   1217       if (VG_(args_the_exename))
   1218          umsg_or_xml_arg(VG_(args_the_exename));
   1219 
   1220       for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
   1221          HChar* s = *(HChar**)VG_(indexXA)( VG_(args_for_client), i );
   1222          umsg_or_xml(" ");
   1223          umsg_or_xml_arg(s);
   1224       }
   1225       umsg_or_xml("%s\n", xpost);
   1226 
   1227       if (VG_(clo_xml))
   1228          VG_(printf_xml)("</preamble>\n");
   1229    }
   1230 
   1231    // Print the parent PID, and other stuff, if necessary.
   1232    if (!VG_(clo_xml) && VG_(clo_verbosity) > 0 && !logging_to_fd) {
   1233       VG_(umsg)("Parent PID: %d\n", VG_(getppid)());
   1234    }
   1235    else
   1236    if (VG_(clo_xml)) {
   1237       VG_(printf_xml)("\n");
   1238       VG_(printf_xml)("<pid>%d</pid>\n", VG_(getpid)());
   1239       VG_(printf_xml)("<ppid>%d</ppid>\n", VG_(getppid)());
   1240       VG_(printf_xml)("<tool>%pS</tool>\n", toolname);
   1241       if (xml_fname_unexpanded)
   1242          print_file_vars(xml_fname_unexpanded);
   1243       if (VG_(clo_xml_user_comment)) {
   1244          /* Note: the user comment itself is XML and is therefore to
   1245             be passed through verbatim (%s) rather than escaped
   1246             (%pS). */
   1247          VG_(printf_xml)("<usercomment>%s</usercomment>\n",
   1248                          VG_(clo_xml_user_comment));
   1249       }
   1250       VG_(printf_xml)("\n");
   1251       VG_(printf_xml)("<args>\n");
   1252 
   1253       VG_(printf_xml)("  <vargv>\n");
   1254       if (VG_(name_of_launcher))
   1255          VG_(printf_xml)("    <exe>%pS</exe>\n",
   1256                                 VG_(name_of_launcher));
   1257       else
   1258          VG_(printf_xml)("    <exe>%pS</exe>\n",
   1259                                 "(launcher name unknown)");
   1260       for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) {
   1261          VG_(printf_xml)(
   1262             "    <arg>%pS</arg>\n",
   1263             * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i )
   1264          );
   1265       }
   1266       VG_(printf_xml)("  </vargv>\n");
   1267 
   1268       VG_(printf_xml)("  <argv>\n");
   1269       if (VG_(args_the_exename))
   1270          VG_(printf_xml)("    <exe>%pS</exe>\n",
   1271                                 VG_(args_the_exename));
   1272       for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
   1273          VG_(printf_xml)(
   1274             "    <arg>%pS</arg>\n",
   1275             * (HChar**) VG_(indexXA)( VG_(args_for_client), i )
   1276          );
   1277       }
   1278       VG_(printf_xml)("  </argv>\n");
   1279 
   1280       VG_(printf_xml)("</args>\n");
   1281    }
   1282 
   1283    // Last thing in the preamble is a blank line.
   1284    if (VG_(clo_xml))
   1285       VG_(printf_xml)("\n");
   1286    else if (VG_(clo_verbosity) > 0)
   1287       VG_(umsg)("\n");
   1288 
   1289 #  if defined(VGO_darwin) && DARWIN_VERS == DARWIN_10_8
   1290    /* Uh, this doesn't play nice with XML output. */
   1291    umsg_or_xml( "WARNING: Support on MacOS 10.8 is experimental and mostly broken.\n");
   1292    umsg_or_xml( "WARNING: Expect incorrect results, assertions and crashes.\n");
   1293    umsg_or_xml( "WARNING: In particular, Memcheck on 32-bit programs will fail to\n");
   1294    umsg_or_xml( "WARNING: detect any errors associated with heap-allocated data.\n");
   1295    umsg_or_xml( "\n" );
   1296 #  endif
   1297 
   1298    if (VG_(clo_verbosity) > 1) {
   1299       SysRes fd;
   1300       VexArch vex_arch;
   1301       VexArchInfo vex_archinfo;
   1302       if (!logging_to_fd)
   1303          VG_(message)(Vg_DebugMsg, "\n");
   1304       VG_(message)(Vg_DebugMsg, "Valgrind options:\n");
   1305       for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) {
   1306          VG_(message)(Vg_DebugMsg,
   1307                      "   %s\n",
   1308                      * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i ));
   1309       }
   1310 
   1311       VG_(message)(Vg_DebugMsg, "Contents of /proc/version:\n");
   1312       fd = VG_(open) ( "/proc/version", VKI_O_RDONLY, 0 );
   1313       if (sr_isError(fd)) {
   1314          VG_(message)(Vg_DebugMsg, "  can't open /proc/version\n");
   1315       } else {
   1316 #        define BUF_LEN    256
   1317          Char version_buf[BUF_LEN];
   1318          Int n = VG_(read) ( sr_Res(fd), version_buf, BUF_LEN );
   1319          vg_assert(n <= BUF_LEN);
   1320          if (n > 0) {
   1321             version_buf[n-1] = '\0';
   1322             VG_(message)(Vg_DebugMsg, "  %s\n", version_buf);
   1323          } else {
   1324             VG_(message)(Vg_DebugMsg, "  (empty?)\n");
   1325          }
   1326          VG_(close)(sr_Res(fd));
   1327 #        undef BUF_LEN
   1328       }
   1329 
   1330       VG_(machine_get_VexArchInfo)( &vex_arch, &vex_archinfo );
   1331       VG_(message)(
   1332          Vg_DebugMsg,
   1333          "Arch and hwcaps: %s, %s\n",
   1334          LibVEX_ppVexArch   ( vex_arch ),
   1335          LibVEX_ppVexHwCaps ( vex_arch, vex_archinfo.hwcaps )
   1336       );
   1337       VG_(message)(
   1338          Vg_DebugMsg,
   1339          "Page sizes: currently %d, max supported %d\n",
   1340          (Int)VKI_PAGE_SIZE, (Int)VKI_MAX_PAGE_SIZE
   1341       );
   1342       VG_(message)(Vg_DebugMsg,
   1343                    "Valgrind library directory: %s\n", VG_(libdir));
   1344    }
   1345 }
   1346 
   1347 
   1348 /*====================================================================*/
   1349 /*=== File descriptor setup                                        ===*/
   1350 /*====================================================================*/
   1351 
   1352 /* Number of file descriptors that Valgrind tries to reserve for
   1353    it's own use - just a small constant. */
   1354 #define N_RESERVED_FDS (10)
   1355 
   1356 static void setup_file_descriptors(void)
   1357 {
   1358    struct vki_rlimit rl;
   1359    Bool show = False;
   1360 
   1361    /* Get the current file descriptor limits. */
   1362    if (VG_(getrlimit)(VKI_RLIMIT_NOFILE, &rl) < 0) {
   1363       rl.rlim_cur = 1024;
   1364       rl.rlim_max = 1024;
   1365    }
   1366 
   1367 #  if defined(VGO_darwin)
   1368    /* Darwin lies. It reports file max as RLIM_INFINITY but
   1369       silently disallows anything bigger than 10240. */
   1370    if (rl.rlim_cur >= 10240  &&  rl.rlim_max == 0x7fffffffffffffffULL) {
   1371       rl.rlim_max = 10240;
   1372    }
   1373 #  endif
   1374 
   1375    if (show)
   1376       VG_(printf)("fd limits: host, before: cur %lu max %lu\n",
   1377                   (UWord)rl.rlim_cur, (UWord)rl.rlim_max);
   1378 
   1379    /* Work out where to move the soft limit to. */
   1380    if (rl.rlim_cur + N_RESERVED_FDS <= rl.rlim_max) {
   1381       rl.rlim_cur = rl.rlim_cur + N_RESERVED_FDS;
   1382    } else {
   1383       rl.rlim_cur = rl.rlim_max;
   1384    }
   1385 
   1386    /* Reserve some file descriptors for our use. */
   1387    VG_(fd_soft_limit) = rl.rlim_cur - N_RESERVED_FDS;
   1388    VG_(fd_hard_limit) = rl.rlim_cur - N_RESERVED_FDS;
   1389 
   1390    /* Update the soft limit. */
   1391    VG_(setrlimit)(VKI_RLIMIT_NOFILE, &rl);
   1392 
   1393    if (show) {
   1394       VG_(printf)("fd limits: host,  after: cur %lu max %lu\n",
   1395                   (UWord)rl.rlim_cur, (UWord)rl.rlim_max);
   1396       VG_(printf)("fd limits: guest       : cur %u max %u\n",
   1397                   VG_(fd_soft_limit), VG_(fd_hard_limit));
   1398    }
   1399 
   1400    if (VG_(cl_exec_fd) != -1)
   1401       VG_(cl_exec_fd) = VG_(safe_fd)( VG_(cl_exec_fd) );
   1402 }
   1403 
   1404 
   1405 /*====================================================================*/
   1406 /*=== BB profiling                                                 ===*/
   1407 /*====================================================================*/
   1408 
   1409 static
   1410 void show_BB_profile ( BBProfEntry tops[], UInt n_tops, ULong score_total )
   1411 {
   1412    ULong score_cumul,   score_here;
   1413    Char  buf_cumul[10], buf_here[10];
   1414    Char  name[64];
   1415    Int   r;
   1416 
   1417    VG_(printf)("\n");
   1418    VG_(printf)("-----------------------------------------------------------\n");
   1419    VG_(printf)("--- BEGIN BB Profile (summary of scores)                ---\n");
   1420    VG_(printf)("-----------------------------------------------------------\n");
   1421    VG_(printf)("\n");
   1422 
   1423    VG_(printf)("Total score = %lld\n\n", score_total);
   1424 
   1425    score_cumul = 0;
   1426    for (r = 0; r < n_tops; r++) {
   1427       if (tops[r].addr == 0)
   1428          continue;
   1429       name[0] = 0;
   1430       VG_(get_fnname_w_offset)(tops[r].addr, name, 64);
   1431       name[63] = 0;
   1432       score_here = tops[r].score;
   1433       score_cumul += score_here;
   1434       VG_(percentify)(score_cumul, score_total, 2, 6, buf_cumul);
   1435       VG_(percentify)(score_here,  score_total, 2, 6, buf_here);
   1436       VG_(printf)("%3d: (%9lld %s)   %9lld %s      0x%llx %s\n",
   1437                   r,
   1438                   score_cumul, buf_cumul,
   1439                   score_here,  buf_here, tops[r].addr, name );
   1440    }
   1441 
   1442    VG_(printf)("\n");
   1443    VG_(printf)("-----------------------------------------------------------\n");
   1444    VG_(printf)("--- BB Profile (BB details)                             ---\n");
   1445    VG_(printf)("-----------------------------------------------------------\n");
   1446    VG_(printf)("\n");
   1447 
   1448    score_cumul = 0;
   1449    for (r = 0; r < n_tops; r++) {
   1450       if (tops[r].addr == 0)
   1451          continue;
   1452       name[0] = 0;
   1453       VG_(get_fnname_w_offset)(tops[r].addr, name, 64);
   1454       name[63] = 0;
   1455       score_here = tops[r].score;
   1456       score_cumul += score_here;
   1457       VG_(percentify)(score_cumul, score_total, 2, 6, buf_cumul);
   1458       VG_(percentify)(score_here,  score_total, 2, 6, buf_here);
   1459       VG_(printf)("\n");
   1460       VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= begin BB rank %d "
   1461                   "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r);
   1462       VG_(printf)("%3d: (%9lld %s)   %9lld %s      0x%llx %s\n",
   1463                   r,
   1464                   score_cumul, buf_cumul,
   1465                   score_here,  buf_here, tops[r].addr, name );
   1466       VG_(printf)("\n");
   1467       VG_(discard_translations)(tops[r].addr, 1, "bb profile");
   1468       VG_(translate)(0, tops[r].addr, True, VG_(clo_profile_flags), 0, True);
   1469       VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-=  end BB rank %d  "
   1470                   "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r);
   1471    }
   1472 
   1473    VG_(printf)("\n");
   1474    VG_(printf)("-----------------------------------------------------------\n");
   1475    VG_(printf)("--- END BB Profile                                      ---\n");
   1476    VG_(printf)("-----------------------------------------------------------\n");
   1477    VG_(printf)("\n");
   1478 }
   1479 
   1480 
   1481 /*====================================================================*/
   1482 /*=== main()                                                       ===*/
   1483 /*====================================================================*/
   1484 
   1485 /* When main() is entered, we should be on the following stack, not
   1486    the one the kernel gave us.  We will run on this stack until
   1487    simulation of the root thread is started, at which point a transfer
   1488    is made to a dynamically allocated stack.  This is for the sake of
   1489    uniform overflow detection for all Valgrind threads.  This is
   1490    marked global even though it isn't, because assembly code below
   1491    needs to reference the name. */
   1492 
   1493 /*static*/ VgStack VG_(interim_stack);
   1494 
   1495 /* These are the structures used to hold info for creating the initial
   1496    client image.
   1497 
   1498    'iicii' mostly holds important register state present at system
   1499    startup (_start_valgrind).  valgrind_main() then fills in the rest
   1500    of it and passes it to VG_(ii_create_image)().  That produces
   1501    'iifii', which is later handed to VG_(ii_finalise_image). */
   1502 
   1503 /* In all OS-instantiations, the_iicii has a field .sp_at_startup.
   1504    This should get some address inside the stack on which we gained
   1505    control (eg, it could be the SP at startup).  It doesn't matter
   1506    exactly where in the stack it is.  This value is passed to the
   1507    address space manager at startup.  On Linux, aspacem then uses it
   1508    to identify the initial stack segment and hence the upper end of
   1509    the usable address space. */
   1510 
   1511 static IICreateImageInfo   the_iicii;
   1512 static IIFinaliseImageInfo the_iifii;
   1513 
   1514 
   1515 /* A simple pair structure, used for conveying debuginfo handles to
   1516    calls to VG_TRACK(new_mem_startup, ...). */
   1517 typedef  struct { Addr a; ULong ull; }  Addr_n_ULong;
   1518 
   1519 
   1520 /* --- Forwards decls to do with shutdown --- */
   1521 
   1522 static void final_tidyup(ThreadId tid);
   1523 
   1524 /* Do everything which needs doing when the last thread exits */
   1525 static
   1526 void shutdown_actions_NORETURN( ThreadId tid,
   1527                                 VgSchedReturnCode tids_schedretcode );
   1528 
   1529 /* --- end of Forwards decls to do with shutdown --- */
   1530 
   1531 
   1532 /* By the time we get to valgrind_main, the_iicii should already have
   1533    been filled in with any important details as required by whatever
   1534    OS we have been built for.
   1535 */
   1536 static
   1537 Int valgrind_main ( Int argc, HChar **argv, HChar **envp )
   1538 {
   1539    HChar*  toolname           = "memcheck";    // default to Memcheck
   1540    Int     need_help          = 0; // 0 = no, 1 = --help, 2 = --help-debug
   1541    ThreadId tid_main          = VG_INVALID_THREADID;
   1542    Bool    logging_to_fd      = False;
   1543    Char* xml_fname_unexpanded = NULL;
   1544    Int     loglevel, i;
   1545    struct vki_rlimit zero = { 0, 0 };
   1546    XArray* addr2dihandle = NULL;
   1547 
   1548    // For an inner Valgrind, register the interim stack asap.
   1549    // This is needed to allow the outer valgrind to do stacktraces during init.
   1550    // Note that this stack is not unregistered when the main thread
   1551    // is switching to the (real) stack. Unregistering this would imply
   1552    // to save the stack id in a global variable, and have a "if"
   1553    // in run_a_thread_NORETURN to do the unregistration only for the
   1554    // main thread. This unregistration is not worth this complexity.
   1555    INNER_REQUEST
   1556       ((void) VALGRIND_STACK_REGISTER
   1557        (&VG_(interim_stack).bytes[0],
   1558         &VG_(interim_stack).bytes[0] + sizeof(VG_(interim_stack))));
   1559 
   1560 
   1561    //============================================================
   1562    //
   1563    // Nb: startup is complex.  Prerequisites are shown at every step.
   1564    // *** Be very careful when messing with the order ***
   1565    //
   1566    // The first order of business is to get debug logging, the address
   1567    // space manager and the dynamic memory manager up and running.
   1568    // Once that's done, we can relax a bit.
   1569    //
   1570    //============================================================
   1571 
   1572    /* This is needed to make VG_(getenv) usable early. */
   1573    VG_(client_envp) = (Char**)envp;
   1574 
   1575    //--------------------------------------------------------------
   1576    // Start up Mach kernel interface, if any
   1577    //   p: none
   1578    //--------------------------------------------------------------
   1579 #  if defined(VGO_darwin)
   1580    VG_(mach_init)();
   1581 #  endif
   1582 
   1583    //--------------------------------------------------------------
   1584    // Start up the logging mechanism
   1585    //   p: none
   1586    //--------------------------------------------------------------
   1587    /* Start the debugging-log system ASAP.  First find out how many
   1588       "-d"s were specified.  This is a pre-scan of the command line.  Also
   1589       get --profile-heap=yes, --core-redzone-size, --redzone-size which are
   1590       needed by the time we start up dynamic memory management.  */
   1591    loglevel = 0;
   1592    for (i = 1; i < argc; i++) {
   1593       if (argv[i][0] != '-') break;
   1594       if VG_STREQ(argv[i], "--") break;
   1595       if VG_STREQ(argv[i], "-d") loglevel++;
   1596       if VG_BOOL_CLO(argv[i], "--profile-heap", VG_(clo_profile_heap)) {}
   1597       if VG_BINT_CLO(argv[i], "--core-redzone-size", VG_(clo_core_redzone_size),
   1598                      0, MAX_CLO_REDZONE_SZB) {}
   1599       if VG_BINT_CLO(argv[i], "--redzone-size", VG_(clo_redzone_size),
   1600                      0, MAX_CLO_REDZONE_SZB) {}
   1601    }
   1602 
   1603    /* ... and start the debug logger.  Now we can safely emit logging
   1604       messages all through startup. */
   1605    VG_(debugLog_startup)(loglevel, "Stage 2 (main)");
   1606    VG_(debugLog)(1, "main", "Welcome to Valgrind version "
   1607                             VERSION " debug logging\n");
   1608 
   1609    //--------------------------------------------------------------
   1610    // Ensure we're on a plausible stack.
   1611    //   p: logging
   1612    //--------------------------------------------------------------
   1613    VG_(debugLog)(1, "main", "Checking current stack is plausible\n");
   1614    { HChar* limLo  = (HChar*)(&VG_(interim_stack).bytes[0]);
   1615      HChar* limHi  = limLo + sizeof(VG_(interim_stack));
   1616      HChar* volatile
   1617             aLocal = (HChar*)&limLo; /* any auto local will do */
   1618      /* Re "volatile": Apple clang version 4.0
   1619         (tags/Apple/clang-421.0.57) (based on LLVM 3.1svn)" appeared
   1620         to miscompile the following check, causing run to abort at
   1621         this point (in 64-bit mode) even though aLocal is within limLo
   1622         .. limHi.  But in fact clang is within its rights to do
   1623         strange things here.  "The reason is that the comparisons
   1624         aLocal < limLo and aLocal >= limHi cause undefined behaviour
   1625         (according to c99 6.5.8) because they compare pointers that do
   1626         not point into the same aggregate."  Adding "volatile" appears
   1627         to fix it because "The compiler would have to prove that there
   1628         is undefined behavior in order to exploit it.  But as a
   1629         volatile variable can change its value in ways invisible to
   1630         the compiler, the compiler must make the conservative
   1631         assumption that it points into the same aggregate as the other
   1632         pointer its compared against.  I.e. the behaviour is possibly
   1633         defined." (Analysis by Florian Krohm). */
   1634      if (aLocal < limLo || aLocal >= limHi) {
   1635         /* something's wrong.  Stop. */
   1636         VG_(debugLog)(0, "main", "Root stack %p to %p, a local %p\n",
   1637                           limLo, limHi, aLocal );
   1638         VG_(debugLog)(0, "main", "Valgrind: FATAL: "
   1639                                  "Initial stack switched failed.\n");
   1640         VG_(debugLog)(0, "main", "   Cannot continue.  Sorry.\n");
   1641         VG_(exit)(1);
   1642      }
   1643    }
   1644 
   1645    //--------------------------------------------------------------
   1646    // Ensure we have a plausible pointer to the stack on which
   1647    // we gained control (not the current stack!)
   1648    //   p: logging
   1649    //--------------------------------------------------------------
   1650    VG_(debugLog)(1, "main", "Checking initial stack was noted\n");
   1651    if (the_iicii.sp_at_startup == 0) {
   1652       VG_(debugLog)(0, "main", "Valgrind: FATAL: "
   1653                                "Initial stack was not noted.\n");
   1654       VG_(debugLog)(0, "main", "   Cannot continue.  Sorry.\n");
   1655       VG_(exit)(1);
   1656    }
   1657 
   1658    //--------------------------------------------------------------
   1659    // Start up the address space manager, and determine the
   1660    // approximate location of the client's stack
   1661    //   p: logging, plausible-stack
   1662    //--------------------------------------------------------------
   1663    VG_(debugLog)(1, "main", "Starting the address space manager\n");
   1664    vg_assert(VKI_PAGE_SIZE     == 4096 || VKI_PAGE_SIZE     == 65536
   1665              || VKI_PAGE_SIZE     == 16384);
   1666    vg_assert(VKI_MAX_PAGE_SIZE == 4096 || VKI_MAX_PAGE_SIZE == 65536
   1667              || VKI_MAX_PAGE_SIZE == 16384);
   1668    vg_assert(VKI_PAGE_SIZE <= VKI_MAX_PAGE_SIZE);
   1669    vg_assert(VKI_PAGE_SIZE     == (1 << VKI_PAGE_SHIFT));
   1670    vg_assert(VKI_MAX_PAGE_SIZE == (1 << VKI_MAX_PAGE_SHIFT));
   1671    the_iicii.clstack_top = VG_(am_startup)( the_iicii.sp_at_startup );
   1672    VG_(debugLog)(1, "main", "Address space manager is running\n");
   1673 
   1674    //--------------------------------------------------------------
   1675    // Start up the dynamic memory manager
   1676    //   p: address space management
   1677    //   p: getting --profile-heap,--core-redzone-size,--redzone-size
   1678    //   In fact m_mallocfree is self-initialising, so there's no
   1679    //   initialisation call to do.  Instead, try a simple malloc/
   1680    //   free pair right now to check that nothing is broken.
   1681    //--------------------------------------------------------------
   1682    VG_(debugLog)(1, "main", "Starting the dynamic memory manager\n");
   1683    { void* p = VG_(malloc)( "main.vm.1", 12345 );
   1684      if (p) VG_(free)( p );
   1685    }
   1686    VG_(debugLog)(1, "main", "Dynamic memory manager is running\n");
   1687 
   1688    //============================================================
   1689    //
   1690    // Dynamic memory management is now available.
   1691    //
   1692    //============================================================
   1693 
   1694    //--------------------------------------------------------------
   1695    // Initialise m_debuginfo
   1696    //  p: dynamic memory allocation
   1697    VG_(debugLog)(1, "main", "Initialise m_debuginfo\n");
   1698    VG_(di_initialise)();
   1699 
   1700    //--------------------------------------------------------------
   1701    // Look for alternative libdir
   1702    { HChar *cp = VG_(getenv)(VALGRIND_LIB);
   1703      if (cp != NULL)
   1704         VG_(libdir) = cp;
   1705      VG_(debugLog)(1, "main", "VG_(libdir) = %s\n", VG_(libdir));
   1706    }
   1707 
   1708    //--------------------------------------------------------------
   1709    // Extract the launcher name from the environment.
   1710    VG_(debugLog)(1, "main", "Getting launcher's name ...\n");
   1711    VG_(name_of_launcher) = VG_(getenv)(VALGRIND_LAUNCHER);
   1712    if (VG_(name_of_launcher) == NULL) {
   1713       VG_(printf)("valgrind: You cannot run '%s' directly.\n", argv[0]);
   1714       VG_(printf)("valgrind: You should use $prefix/bin/valgrind.\n");
   1715       VG_(exit)(1);
   1716    }
   1717    VG_(debugLog)(1, "main", "... %s\n", VG_(name_of_launcher));
   1718 
   1719    //--------------------------------------------------------------
   1720    // Get the current process datasize rlimit, and set it to zero.
   1721    // This prevents any internal uses of brk() from having any effect.
   1722    // We remember the old value so we can restore it on exec, so that
   1723    // child processes will have a reasonable brk value.
   1724    VG_(getrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data));
   1725    zero.rlim_max = VG_(client_rlimit_data).rlim_max;
   1726    VG_(setrlimit)(VKI_RLIMIT_DATA, &zero);
   1727 
   1728    // Get the current process stack rlimit.
   1729    VG_(getrlimit)(VKI_RLIMIT_STACK, &VG_(client_rlimit_stack));
   1730 
   1731    //--------------------------------------------------------------
   1732    // Figure out what sort of CPU we're on, and whether it is
   1733    // able to run V.
   1734    VG_(debugLog)(1, "main", "Get hardware capabilities ...\n");
   1735    { VexArch     vex_arch;
   1736      VexArchInfo vex_archinfo;
   1737      Bool ok = VG_(machine_get_hwcaps)();
   1738      if (!ok) {
   1739         VG_(printf)("\n");
   1740         VG_(printf)("valgrind: fatal error: unsupported CPU.\n");
   1741         VG_(printf)("   Supported CPUs are:\n");
   1742         VG_(printf)("   * x86 (practically any; Pentium-I or above), "
   1743                     "AMD Athlon or above)\n");
   1744         VG_(printf)("   * AMD Athlon64/Opteron\n");
   1745         VG_(printf)("   * PowerPC (most; ppc405 and above)\n");
   1746         VG_(printf)("   * System z (64bit only - s390x; z900 and above)\n");
   1747         VG_(printf)("\n");
   1748         VG_(exit)(1);
   1749      }
   1750      VG_(machine_get_VexArchInfo)( &vex_arch, &vex_archinfo );
   1751      VG_(debugLog)(
   1752         1, "main", "... arch = %s, hwcaps = %s\n",
   1753            LibVEX_ppVexArch   ( vex_arch ),
   1754            LibVEX_ppVexHwCaps ( vex_arch, vex_archinfo.hwcaps )
   1755      );
   1756    }
   1757 
   1758    //--------------------------------------------------------------
   1759    // Record the working directory at startup
   1760    //   p: none
   1761    VG_(debugLog)(1, "main", "Getting the working directory at startup\n");
   1762    { Bool ok = VG_(record_startup_wd)();
   1763      if (!ok)
   1764         VG_(err_config_error)( "Can't establish current working "
   1765                                "directory at startup\n");
   1766    }
   1767    { Char buf[VKI_PATH_MAX+1];
   1768      Bool ok = VG_(get_startup_wd)( buf, sizeof(buf) );
   1769      vg_assert(ok);
   1770      buf[VKI_PATH_MAX] = 0;
   1771      VG_(debugLog)(1, "main", "... %s\n", buf );
   1772    }
   1773 
   1774    //============================================================
   1775    // Command line argument handling order:
   1776    // * If --help/--help-debug are present, show usage message
   1777    //   (including the tool-specific usage)
   1778    // * (If no --tool option given, default to Memcheck)
   1779    // * Then, if client is missing, abort with error msg
   1780    // * Then, if any cmdline args are bad, abort with error msg
   1781    //============================================================
   1782 
   1783    //--------------------------------------------------------------
   1784    // Split up argv into: C args, V args, V extra args, and exename.
   1785    //   p: dynamic memory allocation
   1786    //--------------------------------------------------------------
   1787    VG_(debugLog)(1, "main", "Split up command line\n");
   1788    VG_(split_up_argv)( argc, argv );
   1789    vg_assert( VG_(args_for_valgrind) );
   1790    vg_assert( VG_(args_for_client) );
   1791    if (0) {
   1792       for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++)
   1793          VG_(printf)(
   1794             "varg %s\n",
   1795             * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i )
   1796          );
   1797       VG_(printf)(" exe %s\n", VG_(args_the_exename));
   1798       for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++)
   1799          VG_(printf)(
   1800             "carg %s\n",
   1801             * (HChar**) VG_(indexXA)( VG_(args_for_client), i )
   1802          );
   1803    }
   1804 
   1805    //--------------------------------------------------------------
   1806    // Extract tool name and whether help has been requested.
   1807    // Note we can't print the help message yet, even if requested,
   1808    // because the tool has not been initialised.
   1809    //   p: split_up_argv [for VG_(args_for_valgrind)]
   1810    //--------------------------------------------------------------
   1811    VG_(debugLog)(1, "main",
   1812                     "(early_) Process Valgrind's command line options\n");
   1813    early_process_cmd_line_options(&need_help, &toolname);
   1814 
   1815    // Set default vex control params
   1816    LibVEX_default_VexControl(& VG_(clo_vex_control));
   1817 
   1818    //--------------------------------------------------------------
   1819    // Load client executable, finding in $PATH if necessary
   1820    //   p: early_process_cmd_line_options()  [for 'exec', 'need_help',
   1821    //                                         clo_max_stackframe,
   1822    //                                         clo_main_stacksize]
   1823    //   p: layout_remaining_space            [so there's space]
   1824    //
   1825    // Set up client's environment
   1826    //   p: set-libdir                     [for VG_(libdir)]
   1827    //   p: early_process_cmd_line_options [for toolname]
   1828    //
   1829    // Setup client stack, eip, and VG_(client_arg[cv])
   1830    //   p: load_client()     [for 'info']
   1831    //   p: fix_environment() [for 'env']
   1832    //
   1833    // Setup client data (brk) segment.  Initially a 1-page segment
   1834    // which abuts a shrinkable reservation.
   1835    //     p: load_client()     [for 'info' and hence VG_(brk_base)]
   1836    //
   1837    // p: _start_in_C (for zeroing out the_iicii and putting some
   1838    //    initial values into it)
   1839    //--------------------------------------------------------------
   1840    if (!need_help) {
   1841       VG_(debugLog)(1, "main", "Create initial image\n");
   1842 
   1843 #     if defined(VGO_linux) || defined(VGO_darwin)
   1844       the_iicii.argv              = argv;
   1845       the_iicii.envp              = envp;
   1846       the_iicii.toolname          = toolname;
   1847 #     else
   1848 #       error "Unknown platform"
   1849 #     endif
   1850 
   1851       /* NOTE: this call reads VG_(clo_main_stacksize). */
   1852       the_iifii = VG_(ii_create_image)( the_iicii );
   1853    }
   1854 
   1855    //==============================================================
   1856    //
   1857    // Finished loading/setting up the client address space.
   1858    //
   1859    //==============================================================
   1860 
   1861    //--------------------------------------------------------------
   1862    // setup file descriptors
   1863    //   p: n/a
   1864    //--------------------------------------------------------------
   1865    VG_(debugLog)(1, "main", "Setup file descriptors\n");
   1866    setup_file_descriptors();
   1867 
   1868    //--------------------------------------------------------------
   1869    // create the fake /proc/<pid>/cmdline file and then unlink it,
   1870    // but hold onto the fd, so we can hand it out to the client
   1871    // when it tries to open /proc/<pid>/cmdline for itself.
   1872    //   p: setup file descriptors
   1873    //--------------------------------------------------------------
   1874 #if !defined(VGO_linux)
   1875    // client shouldn't be using /proc!
   1876    VG_(cl_cmdline_fd) = -1;
   1877 #else
   1878    if (!need_help) {
   1879       HChar  buf[50], buf2[50+64];
   1880       HChar  nul[1];
   1881       Int    fd, r;
   1882       const HChar* exename;
   1883 
   1884       VG_(debugLog)(1, "main", "Create fake /proc/<pid>/cmdline\n");
   1885 
   1886       VG_(sprintf)(buf, "proc_%d_cmdline", VG_(getpid)());
   1887       fd = VG_(mkstemp)( buf, buf2 );
   1888       if (fd == -1)
   1889          VG_(err_config_error)("Can't create client cmdline file in %s\n", buf2);
   1890 
   1891       nul[0] = 0;
   1892       exename = VG_(args_the_exename) ? VG_(args_the_exename)
   1893                                       : "unknown_exename";
   1894       VG_(write)(fd, exename, VG_(strlen)( exename ));
   1895       VG_(write)(fd, nul, 1);
   1896 
   1897       for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
   1898          HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i );
   1899          VG_(write)(fd, arg, VG_(strlen)( arg ));
   1900          VG_(write)(fd, nul, 1);
   1901       }
   1902 
   1903       /* Don't bother to seek the file back to the start; instead do
   1904 	 it every time a copy of it is given out (by PRE(sys_open)).
   1905 	 That is probably more robust across fork() etc. */
   1906 
   1907       /* Now delete it, but hang on to the fd. */
   1908       r = VG_(unlink)( buf2 );
   1909       if (r)
   1910          VG_(err_config_error)("Can't delete client cmdline file in %s\n", buf2);
   1911 
   1912       VG_(cl_cmdline_fd) = fd;
   1913    }
   1914 #endif
   1915 
   1916    //--------------------------------------------------------------
   1917    // Init tool part 1: pre_clo_init
   1918    //   p: setup_client_stack()      [for 'VG_(client_arg[cv]']
   1919    //   p: setup_file_descriptors()  [for 'VG_(fd_xxx_limit)']
   1920    //--------------------------------------------------------------
   1921    VG_(debugLog)(1, "main", "Initialise the tool part 1 (pre_clo_init)\n");
   1922    VG_(tl_pre_clo_init)();
   1923 
   1924    //--------------------------------------------------------------
   1925    // If --tool and --help/--help-debug was given, now give the core+tool
   1926    // help message
   1927    //   p: early_process_cmd_line_options() [for 'need_help']
   1928    //   p: tl_pre_clo_init                  [for 'VG_(tdict).usage']
   1929    //--------------------------------------------------------------
   1930    VG_(debugLog)(1, "main", "Print help and quit, if requested\n");
   1931    if (need_help) {
   1932       usage_NORETURN(/*--help-debug?*/need_help >= 2);
   1933    }
   1934 
   1935    //--------------------------------------------------------------
   1936    // Process command line options to Valgrind + tool
   1937    //   p: setup_client_stack()      [for 'VG_(client_arg[cv]']
   1938    //   p: setup_file_descriptors()  [for 'VG_(fd_xxx_limit)']
   1939    //--------------------------------------------------------------
   1940    VG_(debugLog)(1, "main",
   1941                     "(main_) Process Valgrind's command line options, "
   1942                     "setup logging\n");
   1943    main_process_cmd_line_options ( &logging_to_fd, &xml_fname_unexpanded,
   1944                                    toolname );
   1945 
   1946    //--------------------------------------------------------------
   1947    // Zeroise the millisecond counter by doing a first read of it.
   1948    //   p: none
   1949    //--------------------------------------------------------------
   1950    (void) VG_(read_millisecond_timer)();
   1951 
   1952    //--------------------------------------------------------------
   1953    // Print the preamble
   1954    //   p: tl_pre_clo_init            [for 'VG_(details).name' and friends]
   1955    //   p: main_process_cmd_line_options()
   1956    //         [for VG_(clo_verbosity), VG_(clo_xml),
   1957    //          logging_to_fd, xml_fname_unexpanded]
   1958    //--------------------------------------------------------------
   1959    VG_(debugLog)(1, "main", "Print the preamble...\n");
   1960    print_preamble(logging_to_fd, xml_fname_unexpanded, toolname);
   1961    VG_(debugLog)(1, "main", "...finished the preamble\n");
   1962 
   1963    //--------------------------------------------------------------
   1964    // Init tool part 2: post_clo_init
   1965    //   p: setup_client_stack()      [for 'VG_(client_arg[cv]']
   1966    //   p: setup_file_descriptors()  [for 'VG_(fd_xxx_limit)']
   1967    //   p: print_preamble()          [so any warnings printed in post_clo_init
   1968    //                                 are shown after the preamble]
   1969    //--------------------------------------------------------------
   1970    VG_(debugLog)(1, "main", "Initialise the tool part 2 (post_clo_init)\n");
   1971    VG_TDICT_CALL(tool_post_clo_init);
   1972    {
   1973       /* The tool's "needs" will by now be finalised, since it has no
   1974          further opportunity to specify them.  So now sanity check
   1975          them. */
   1976       Char* s;
   1977       Bool  ok;
   1978       ok = VG_(sanity_check_needs)( &s );
   1979       if (!ok) {
   1980          VG_(tool_panic)(s);
   1981       }
   1982    }
   1983 
   1984    //--------------------------------------------------------------
   1985    // Initialise translation table and translation cache
   1986    //   p: aspacem         [??]
   1987    //   p: tl_pre_clo_init [for 'VG_(details).avg_translation_sizeB']
   1988    //--------------------------------------------------------------
   1989    VG_(debugLog)(1, "main", "Initialise TT/TC\n");
   1990    VG_(init_tt_tc)();
   1991 
   1992    //--------------------------------------------------------------
   1993    // Initialise the redirect table.
   1994    //   p: init_tt_tc [so it can call VG_(search_transtab) safely]
   1995    //   p: aspacem [so can change ownership of sysinfo pages]
   1996    //--------------------------------------------------------------
   1997    VG_(debugLog)(1, "main", "Initialise redirects\n");
   1998    VG_(redir_initialise)();
   1999 
   2000    //--------------------------------------------------------------
   2001    // Allow GDB attach
   2002    //   p: main_process_cmd_line_options()  [for VG_(clo_wait_for_gdb)]
   2003    //--------------------------------------------------------------
   2004    /* Hook to delay things long enough so we can get the pid and
   2005       attach GDB in another shell. */
   2006    if (VG_(clo_wait_for_gdb)) {
   2007       ULong iters, q;
   2008       VG_(debugLog)(1, "main", "Wait for GDB\n");
   2009       VG_(printf)("pid=%d, entering delay loop\n", VG_(getpid)());
   2010 
   2011 #     if defined(VGP_x86_linux)
   2012       iters = 10;
   2013 #     elif defined(VGP_amd64_linux) || defined(VGP_ppc64_linux)
   2014       iters = 10;
   2015 #     elif defined(VGP_ppc32_linux)
   2016       iters = 5;
   2017 #     elif defined(VGP_arm_linux)
   2018       iters = 5;
   2019 #     elif defined(VGP_s390x_linux)
   2020       iters = 10;
   2021 #     elif defined(VGP_mips32_linux)
   2022       iters = 10;
   2023 #     elif defined(VGO_darwin)
   2024       iters = 3;
   2025 #     else
   2026 #       error "Unknown plat"
   2027 #     endif
   2028 
   2029       iters *= 1000ULL * 1000 * 1000;
   2030       for (q = 0; q < iters; q++)
   2031          __asm__ __volatile__("" ::: "memory","cc");
   2032    }
   2033 
   2034    //--------------------------------------------------------------
   2035    // Search for file descriptors that are inherited from our parent
   2036    //   p: main_process_cmd_line_options  [for VG_(clo_track_fds)]
   2037    //--------------------------------------------------------------
   2038    if (VG_(clo_track_fds)) {
   2039       VG_(debugLog)(1, "main", "Init preopened fds\n");
   2040       VG_(init_preopened_fds)();
   2041    }
   2042 
   2043    //--------------------------------------------------------------
   2044    // Load debug info for the existing segments.
   2045    //   p: setup_code_redirect_table [so that redirs can be recorded]
   2046    //   p: mallocfree
   2047    //   p: probably: setup fds and process CLOs, so that logging works
   2048    //   p: initialise m_debuginfo
   2049    //
   2050    // While doing this, make a note of the debuginfo-handles that
   2051    // come back from VG_(di_notify_mmap).
   2052    // Later, in "Tell the tool about the initial client memory permissions"
   2053    // (just below) we can then hand these handles off to the tool in
   2054    // calls to VG_TRACK(new_mem_startup, ...).  This gives the tool the
   2055    // opportunity to make further queries to m_debuginfo before the
   2056    // client is started, if it wants.  We put this information into an
   2057    // XArray, each handle along with the associated segment start address,
   2058    // and search the XArray for the handles later, when calling
   2059    // VG_TRACK(new_mem_startup, ...).
   2060    //--------------------------------------------------------------
   2061    VG_(debugLog)(1, "main", "Load initial debug info\n");
   2062 
   2063    tl_assert(!addr2dihandle);
   2064    addr2dihandle = VG_(newXA)( VG_(malloc), "main.vm.2",
   2065                                VG_(free), sizeof(Addr_n_ULong) );
   2066    tl_assert(addr2dihandle);
   2067 
   2068 #  if defined(VGO_linux)
   2069    { Addr* seg_starts;
   2070      Int   n_seg_starts;
   2071      Addr_n_ULong anu;
   2072 
   2073      seg_starts = VG_(get_segment_starts)( &n_seg_starts );
   2074      vg_assert(seg_starts && n_seg_starts >= 0);
   2075 
   2076      /* show them all to the debug info reader.  allow_SkFileV has to
   2077         be True here so that we read info from the valgrind executable
   2078         itself. */
   2079      for (i = 0; i < n_seg_starts; i++) {
   2080         anu.ull = VG_(di_notify_mmap)( seg_starts[i], True/*allow_SkFileV*/,
   2081                                        -1/*Don't use_fd*/);
   2082         /* anu.ull holds the debuginfo handle returned by di_notify_mmap,
   2083            if any. */
   2084         if (anu.ull > 0) {
   2085            anu.a = seg_starts[i];
   2086            VG_(addToXA)( addr2dihandle, &anu );
   2087         }
   2088      }
   2089 
   2090      VG_(free)( seg_starts );
   2091    }
   2092 #  elif defined(VGO_darwin)
   2093    { Addr* seg_starts;
   2094      Int   n_seg_starts;
   2095      seg_starts = VG_(get_segment_starts)( &n_seg_starts );
   2096      vg_assert(seg_starts && n_seg_starts >= 0);
   2097 
   2098      /* show them all to the debug info reader.
   2099         Don't read from V segments (unlike Linux) */
   2100      // GrP fixme really?
   2101      for (i = 0; i < n_seg_starts; i++) {
   2102         VG_(di_notify_mmap)( seg_starts[i], False/*don't allow_SkFileV*/,
   2103                              -1/*don't use_fd*/);
   2104      }
   2105 
   2106      VG_(free)( seg_starts );
   2107    }
   2108 #  else
   2109 #    error Unknown OS
   2110 #  endif
   2111 
   2112    //--------------------------------------------------------------
   2113    // Tell aspacem of ownership change of the asm helpers, so that
   2114    // m_translate allows them to be translated.  However, only do this
   2115    // after the initial debug info read, since making a hole in the
   2116    // address range for the stage2 binary confuses the debug info reader.
   2117    //   p: aspacem
   2118    //--------------------------------------------------------------
   2119    { Bool change_ownership_v_c_OK;
   2120      Addr co_start   = VG_PGROUNDDN( (Addr)&VG_(trampoline_stuff_start) );
   2121      Addr co_endPlus = VG_PGROUNDUP( (Addr)&VG_(trampoline_stuff_end) );
   2122      VG_(debugLog)(1,"redir",
   2123                      "transfer ownership V -> C of 0x%llx .. 0x%llx\n",
   2124                      (ULong)co_start, (ULong)co_endPlus-1 );
   2125 
   2126      change_ownership_v_c_OK
   2127         = VG_(am_change_ownership_v_to_c)( co_start, co_endPlus - co_start );
   2128      vg_assert(change_ownership_v_c_OK);
   2129    }
   2130 
   2131    if (VG_(clo_xml)) {
   2132       HChar buf[50];
   2133       VG_(elapsed_wallclock_time)(buf);
   2134       VG_(printf_xml)( "<status>\n"
   2135                        "  <state>RUNNING</state>\n"
   2136                        "  <time>%pS</time>\n"
   2137                        "</status>\n",
   2138                        buf );
   2139       VG_(printf_xml)( "\n" );
   2140    }
   2141 
   2142    VG_(init_Threads)();
   2143 
   2144    //--------------------------------------------------------------
   2145    // Initialise the scheduler (phase 1) [generates tid_main]
   2146    //   p: none, afaics
   2147    //--------------------------------------------------------------
   2148    VG_(debugLog)(1, "main", "Initialise scheduler (phase 1)\n");
   2149    tid_main = VG_(scheduler_init_phase1)();
   2150    vg_assert(tid_main >= 0 && tid_main < VG_N_THREADS
   2151              && tid_main != VG_INVALID_THREADID);
   2152    /* Tell the tool about tid_main */
   2153    VG_TRACK( pre_thread_ll_create, VG_INVALID_THREADID, tid_main );
   2154 
   2155    //--------------------------------------------------------------
   2156    // Tell the tool about the initial client memory permissions
   2157    //   p: aspacem
   2158    //   p: mallocfree
   2159    //   p: setup_client_stack
   2160    //   p: setup_client_dataseg
   2161    //
   2162    // For each segment we tell the client about, look up in
   2163    // addr2dihandle as created above, to see if there's a debuginfo
   2164    // handle associated with the segment, that we can hand along
   2165    // to the tool, to be helpful.
   2166    //--------------------------------------------------------------
   2167    VG_(debugLog)(1, "main", "Tell tool about initial permissions\n");
   2168    { Addr*     seg_starts;
   2169      Int       n_seg_starts;
   2170 
   2171      tl_assert(addr2dihandle);
   2172 
   2173      /* Mark the main thread as running while we tell the tool about
   2174         the client memory so that the tool can associate that memory
   2175         with the main thread. */
   2176      tl_assert(VG_(running_tid) == VG_INVALID_THREADID);
   2177      VG_(running_tid) = tid_main;
   2178 
   2179      seg_starts = VG_(get_segment_starts)( &n_seg_starts );
   2180      vg_assert(seg_starts && n_seg_starts >= 0);
   2181 
   2182      /* show interesting ones to the tool */
   2183      for (i = 0; i < n_seg_starts; i++) {
   2184         Word j, n;
   2185         NSegment const* seg
   2186            = VG_(am_find_nsegment)( seg_starts[i] );
   2187         vg_assert(seg);
   2188         if (seg->kind == SkFileC || seg->kind == SkAnonC) {
   2189           /* This next assertion is tricky.  If it is placed
   2190              immediately before this 'if', it very occasionally fails.
   2191              Why?  Because previous iterations of the loop may have
   2192              caused tools (via the new_mem_startup calls) to do
   2193              dynamic memory allocation, and that may affect the mapped
   2194              segments; in particular it may cause segment merging to
   2195              happen.  Hence we cannot assume that seg_starts[i], which
   2196              reflects the state of the world before we started this
   2197              loop, is the same as seg->start, as the latter reflects
   2198              the state of the world (viz, mappings) at this particular
   2199              iteration of the loop.
   2200 
   2201              Why does moving it inside the 'if' make it safe?  Because
   2202              any dynamic memory allocation done by the tools will
   2203              affect only the state of Valgrind-owned segments, not of
   2204              Client-owned segments.  And the 'if' guards against that
   2205              -- we only get in here for Client-owned segments.
   2206 
   2207              In other words: the loop may change the state of
   2208              Valgrind-owned segments as it proceeds.  But it should
   2209              not cause the Client-owned segments to change. */
   2210            vg_assert(seg->start == seg_starts[i]);
   2211            VG_(debugLog)(2, "main",
   2212                             "tell tool about %010lx-%010lx %c%c%c\n",
   2213                              seg->start, seg->end,
   2214                              seg->hasR ? 'r' : '-',
   2215                              seg->hasW ? 'w' : '-',
   2216                              seg->hasX ? 'x' : '-' );
   2217            /* search addr2dihandle to see if we have an entry
   2218               matching seg->start. */
   2219            n = VG_(sizeXA)( addr2dihandle );
   2220            for (j = 0; j < n; j++) {
   2221               Addr_n_ULong* anl = VG_(indexXA)( addr2dihandle, j );
   2222               if (anl->a == seg->start) {
   2223                   tl_assert(anl->ull > 0); /* check it's a valid handle */
   2224                   break;
   2225               }
   2226            }
   2227            vg_assert(j >= 0 && j <= n);
   2228            VG_TRACK( new_mem_startup, seg->start, seg->end+1-seg->start,
   2229                      seg->hasR, seg->hasW, seg->hasX,
   2230                      /* and the retrieved debuginfo handle, if any */
   2231                      j < n
   2232                      ? ((Addr_n_ULong*)VG_(indexXA)( addr2dihandle, j ))->ull
   2233                         : 0 );
   2234         }
   2235      }
   2236 
   2237      VG_(free)( seg_starts );
   2238      VG_(deleteXA)( addr2dihandle );
   2239 
   2240      /* Also do the initial stack permissions. */
   2241      {
   2242        SSizeT inaccessible_len;
   2243        NSegment const* seg
   2244           = VG_(am_find_nsegment)( the_iifii.initial_client_SP );
   2245        vg_assert(seg);
   2246        vg_assert(seg->kind == SkAnonC);
   2247        vg_assert(the_iifii.initial_client_SP >= seg->start);
   2248        vg_assert(the_iifii.initial_client_SP <= seg->end);
   2249 
   2250        /* Stuff below the initial SP is unaddressable.  Take into
   2251 	  account any ABI-mandated space below the stack pointer that
   2252 	  is required (VG_STACK_REDZONE_SZB).  setup_client_stack()
   2253 	  will have allocated an extra page if a red zone is required,
   2254 	  to be on the safe side. */
   2255        inaccessible_len = the_iifii.initial_client_SP - VG_STACK_REDZONE_SZB
   2256                           - seg->start;
   2257        vg_assert(inaccessible_len >= 0);
   2258        if (inaccessible_len > 0)
   2259           VG_TRACK( die_mem_stack,
   2260                     seg->start,
   2261                     inaccessible_len );
   2262        VG_(debugLog)(2, "main", "mark stack inaccessible %010lx-%010lx\n",
   2263                         seg->start,
   2264                         the_iifii.initial_client_SP-1 - VG_STACK_REDZONE_SZB);
   2265      }
   2266 
   2267      /* Also the assembly helpers. */
   2268      VG_TRACK( new_mem_startup,
   2269                (Addr)&VG_(trampoline_stuff_start),
   2270                (Addr)&VG_(trampoline_stuff_end)
   2271                   - (Addr)&VG_(trampoline_stuff_start),
   2272                False, /* readable? */
   2273                False, /* writable? */
   2274                True   /* executable? */,
   2275                0 /* di_handle: no associated debug info */ );
   2276 
   2277      /* Clear the running thread indicator */
   2278      VG_(running_tid) = VG_INVALID_THREADID;
   2279      tl_assert(VG_(running_tid) == VG_INVALID_THREADID);
   2280    }
   2281 
   2282    //--------------------------------------------------------------
   2283    // Initialise the scheduler (phase 2)
   2284    //   p: Initialise the scheduler (phase 1) [for tid_main]
   2285    //   p: setup_file_descriptors() [else VG_(safe_fd)() breaks]
   2286    //   p: setup_client_stack
   2287    //--------------------------------------------------------------
   2288    VG_(debugLog)(1, "main", "Initialise scheduler (phase 2)\n");
   2289    { NSegment const* seg
   2290         = VG_(am_find_nsegment)( the_iifii.initial_client_SP );
   2291      vg_assert(seg);
   2292      vg_assert(seg->kind == SkAnonC);
   2293      vg_assert(the_iifii.initial_client_SP >= seg->start);
   2294      vg_assert(the_iifii.initial_client_SP <= seg->end);
   2295      VG_(scheduler_init_phase2)( tid_main,
   2296                                  seg->end, the_iifii.clstack_max_size );
   2297    }
   2298 
   2299    //--------------------------------------------------------------
   2300    // Set up state for the root thread
   2301    //   p: ?
   2302    //      setup_scheduler()      [for sched-specific thread 1 stuff]
   2303    //      VG_(ii_create_image)   [for 'the_iicii' initial info]
   2304    //--------------------------------------------------------------
   2305    VG_(debugLog)(1, "main", "Finalise initial image\n");
   2306    VG_(ii_finalise_image)( the_iifii );
   2307 
   2308    //--------------------------------------------------------------
   2309    // Initialise the signal handling subsystem
   2310    //   p: n/a
   2311    //--------------------------------------------------------------
   2312    // Nb: temporarily parks the saved blocking-mask in saved_sigmask.
   2313    VG_(debugLog)(1, "main", "Initialise signal management\n");
   2314    /* Check that the kernel-interface signal definitions look sane */
   2315    VG_(vki_do_initial_consistency_checks)();
   2316    /* .. and go on to use them. */
   2317    VG_(sigstartup_actions)();
   2318 
   2319    //--------------------------------------------------------------
   2320    // Read suppression file
   2321    //   p: main_process_cmd_line_options()  [for VG_(clo_suppressions)]
   2322    //--------------------------------------------------------------
   2323    if (VG_(needs).core_errors || VG_(needs).tool_errors) {
   2324       VG_(debugLog)(1, "main", "Load suppressions\n");
   2325       VG_(load_suppressions)();
   2326    }
   2327 
   2328    //--------------------------------------------------------------
   2329    // register client stack
   2330    //--------------------------------------------------------------
   2331    VG_(clstk_id) = VG_(register_stack)(VG_(clstk_base), VG_(clstk_end));
   2332 
   2333    //--------------------------------------------------------------
   2334    // Show the address space state so far
   2335    //--------------------------------------------------------------
   2336    VG_(debugLog)(1, "main", "\n");
   2337    VG_(debugLog)(1, "main", "\n");
   2338    VG_(am_show_nsegments)(1,"Memory layout at client startup");
   2339    VG_(debugLog)(1, "main", "\n");
   2340    VG_(debugLog)(1, "main", "\n");
   2341 
   2342    //--------------------------------------------------------------
   2343    // Run!
   2344    //--------------------------------------------------------------
   2345    VG_(debugLog)(1, "main", "Running thread 1\n");
   2346 
   2347    /* As a result of the following call, the last thread standing
   2348       eventually winds up running shutdown_actions_NORETURN
   2349       just below.  Unfortunately, simply exporting said function
   2350       causes m_main to be part of a module cycle, which is pretty
   2351       nonsensical.  So instead of doing that, the address of said
   2352       function is stored in a global variable 'owned' by m_syswrap,
   2353       and it uses that function pointer to get back here when it needs
   2354       to. */
   2355 
   2356    /* Set continuation address. */
   2357    VG_(address_of_m_main_shutdown_actions_NORETURN)
   2358       = & shutdown_actions_NORETURN;
   2359 
   2360    /* Run the first thread, eventually ending up at the continuation
   2361       address. */
   2362    VG_(main_thread_wrapper_NORETURN)(1);
   2363 
   2364    /*NOTREACHED*/
   2365    vg_assert(0);
   2366 }
   2367 
   2368 /* Do everything which needs doing when the last thread exits or when
   2369    a thread exits requesting a complete process exit.
   2370 
   2371    We enter here holding The Lock.  For the case VgSrc_ExitProcess we
   2372    must never release it, because to do so would allow other threads
   2373    to continue after the system is ostensibly shut down.  So we must
   2374    go to our grave, so to speak, holding the lock.
   2375 
   2376    In fact, there is never any point in releasing the lock at this
   2377    point - we have it, we're shutting down the entire system, and
   2378    for the case VgSrc_ExitProcess doing so positively causes trouble.
   2379    So don't.
   2380 
   2381    The final_tidyup call makes a bit of a nonsense of the ExitProcess
   2382    case, since it will run the libc_freeres function, thus allowing
   2383    other lurking threads to run again.  Hmm. */
   2384 
   2385 static
   2386 void shutdown_actions_NORETURN( ThreadId tid,
   2387                                 VgSchedReturnCode tids_schedretcode )
   2388 {
   2389    VG_(debugLog)(1, "main", "entering VG_(shutdown_actions_NORETURN)\n");
   2390    VG_(am_show_nsegments)(1,"Memory layout at client shutdown");
   2391 
   2392    vg_assert(VG_(is_running_thread)(tid));
   2393 
   2394    vg_assert(tids_schedretcode == VgSrc_ExitThread
   2395 	     || tids_schedretcode == VgSrc_ExitProcess
   2396              || tids_schedretcode == VgSrc_FatalSig );
   2397 
   2398    if (tids_schedretcode == VgSrc_ExitThread) {
   2399 
   2400       // We are the last surviving thread.  Right?
   2401       vg_assert( VG_(count_living_threads)() == 1 );
   2402 
   2403       // Wait for all other threads to exit.
   2404       // jrs: Huh?  but they surely are already gone
   2405       VG_(reap_threads)(tid);
   2406 
   2407       // Clean the client up before the final report
   2408       // this causes the libc_freeres function to run
   2409       final_tidyup(tid);
   2410 
   2411       /* be paranoid */
   2412       vg_assert(VG_(is_running_thread)(tid));
   2413       vg_assert(VG_(count_living_threads)() == 1);
   2414 
   2415    } else {
   2416 
   2417       // We may not be the last surviving thread.  However, we
   2418       // want to shut down the entire process.  We hold the lock
   2419       // and we need to keep hold of it all the way out, in order
   2420       // that none of the other threads ever run again.
   2421       vg_assert( VG_(count_living_threads)() >= 1 );
   2422 
   2423       // Clean the client up before the final report
   2424       // this causes the libc_freeres function to run
   2425       // perhaps this is unsafe, as per comment above
   2426       final_tidyup(tid);
   2427 
   2428       /* be paranoid */
   2429       vg_assert(VG_(is_running_thread)(tid));
   2430       vg_assert(VG_(count_living_threads)() >= 1);
   2431    }
   2432 
   2433    VG_(threads)[tid].status = VgTs_Empty;
   2434    //--------------------------------------------------------------
   2435    // Finalisation: cleanup, messages, etc.  Order not so important, only
   2436    // affects what order the messages come.
   2437    //--------------------------------------------------------------
   2438    // First thing in the post-amble is a blank line.
   2439    if (VG_(clo_xml))
   2440       VG_(printf_xml)("\n");
   2441    else if (VG_(clo_verbosity) > 0)
   2442       VG_(message)(Vg_UserMsg, "\n");
   2443 
   2444    if (VG_(clo_xml)) {
   2445       HChar buf[50];
   2446       VG_(elapsed_wallclock_time)(buf);
   2447       VG_(printf_xml)( "<status>\n"
   2448                               "  <state>FINISHED</state>\n"
   2449                               "  <time>%pS</time>\n"
   2450                               "</status>\n"
   2451                               "\n",
   2452                               buf);
   2453    }
   2454 
   2455    /* Print out file descriptor summary and stats. */
   2456    if (VG_(clo_track_fds))
   2457       VG_(show_open_fds)();
   2458 
   2459    /* Call the tool's finalisation function.  This makes Memcheck's
   2460       leak checker run, and possibly chuck a bunch of leak errors into
   2461       the error management machinery. */
   2462    VG_TDICT_CALL(tool_fini, 0/*exitcode*/);
   2463 
   2464    /* Show the error counts. */
   2465    if (VG_(clo_xml)
   2466        && (VG_(needs).core_errors || VG_(needs).tool_errors)) {
   2467       VG_(show_error_counts_as_XML)();
   2468    }
   2469 
   2470    /* In XML mode, this merely prints the used suppressions. */
   2471    if (VG_(needs).core_errors || VG_(needs).tool_errors)
   2472       VG_(show_all_errors)(VG_(clo_verbosity), VG_(clo_xml));
   2473 
   2474    if (VG_(clo_xml)) {
   2475       VG_(printf_xml)("\n");
   2476       VG_(printf_xml)("</valgrindoutput>\n");
   2477       VG_(printf_xml)("\n");
   2478    }
   2479 
   2480    VG_(sanity_check_general)( True /*include expensive checks*/ );
   2481 
   2482    if (VG_(clo_stats))
   2483       print_all_stats();
   2484 
   2485    /* Show a profile of the heap(s) at shutdown.  Optionally, first
   2486       throw away all the debug info, as that makes it easy to spot
   2487       leaks in the debuginfo reader. */
   2488    if (VG_(clo_profile_heap)) {
   2489       if (0) VG_(di_discard_ALL_debuginfo)();
   2490       VG_(print_arena_cc_analysis)();
   2491    }
   2492 
   2493    if (VG_(clo_profile_flags) > 0) {
   2494       #define N_MAX 200
   2495       BBProfEntry tops[N_MAX];
   2496       ULong score_total = VG_(get_BB_profile) (tops, N_MAX);
   2497       show_BB_profile(tops, N_MAX, score_total);
   2498    }
   2499 
   2500    /* Print Vex storage stats */
   2501    if (0)
   2502        LibVEX_ShowAllocStats();
   2503 
   2504    /* Flush any output cached by previous calls to VG_(message). */
   2505    VG_(message_flush)();
   2506 
   2507    /* terminate gdbserver if ever it was started. We terminate it here so that it get
   2508       the output above if output was redirected to gdb */
   2509    VG_(gdbserver) (0);
   2510 
   2511    /* Ok, finally exit in the os-specific way, according to the scheduler's
   2512       return code.  In short, if the (last) thread exited by calling
   2513       sys_exit, do likewise; if the (last) thread stopped due to a fatal
   2514       signal, terminate the entire system with that same fatal signal. */
   2515    VG_(debugLog)(1, "core_os",
   2516                     "VG_(terminate_NORETURN)(tid=%lld)\n", (ULong)tid);
   2517 
   2518    switch (tids_schedretcode) {
   2519    case VgSrc_ExitThread:  /* the normal way out (Linux) */
   2520    case VgSrc_ExitProcess: /* the normal way out (AIX) -- still needed? */
   2521       /* Change the application return code to user's return code,
   2522          if an error was found */
   2523       if (VG_(clo_error_exitcode) > 0
   2524           && VG_(get_n_errs_found)() > 0) {
   2525          VG_(exit)( VG_(clo_error_exitcode) );
   2526       } else {
   2527          /* otherwise, return the client's exit code, in the normal
   2528             way. */
   2529          VG_(exit)( VG_(threads)[tid].os_state.exitcode );
   2530       }
   2531       /* NOT ALIVE HERE! */
   2532       VG_(core_panic)("entered the afterlife in main() -- ExitT/P");
   2533       break; /* what the hell :) */
   2534 
   2535    case VgSrc_FatalSig:
   2536       /* We were killed by a fatal signal, so replicate the effect */
   2537       vg_assert(VG_(threads)[tid].os_state.fatalsig != 0);
   2538       VG_(kill_self)(VG_(threads)[tid].os_state.fatalsig);
   2539       /* we shouldn't be alive at this point.  But VG_(kill_self)
   2540          sometimes fails with EPERM on Darwin, for unclear reasons. */
   2541 #     if defined(VGO_darwin)
   2542       VG_(debugLog)(0, "main", "VG_(kill_self) failed.  Exiting normally.\n");
   2543       VG_(exit)(0); /* bogus, but we really need to exit now */
   2544       /* fall through .. */
   2545 #     endif
   2546       VG_(core_panic)("main(): signal was supposed to be fatal");
   2547       break;
   2548 
   2549    default:
   2550       VG_(core_panic)("main(): unexpected scheduler return code");
   2551    }
   2552 }
   2553 
   2554 /* -------------------- */
   2555 
   2556 /* Final clean-up before terminating the process.
   2557    Clean up the client by calling __libc_freeres() (if requested)
   2558    This is Linux-specific?
   2559    GrP fixme glibc-specific, anyway
   2560 */
   2561 static void final_tidyup(ThreadId tid)
   2562 {
   2563 #if !defined(VGO_darwin)
   2564 #  if defined(VGP_ppc64_linux)
   2565    Addr r2;
   2566 #  endif
   2567    Addr __libc_freeres_wrapper = VG_(client___libc_freeres_wrapper);
   2568 
   2569    vg_assert(VG_(is_running_thread)(tid));
   2570 
   2571    if ( !VG_(needs).libc_freeres ||
   2572         !VG_(clo_run_libc_freeres) ||
   2573         0 == __libc_freeres_wrapper )
   2574       return;			/* can't/won't do it */
   2575 
   2576 #  if defined(VGP_ppc64_linux)
   2577    r2 = VG_(get_tocptr)( __libc_freeres_wrapper );
   2578    if (r2 == 0) {
   2579       VG_(message)(Vg_UserMsg,
   2580                    "Caught __NR_exit, but can't run __libc_freeres()\n");
   2581       VG_(message)(Vg_UserMsg,
   2582                    "   since cannot establish TOC pointer for it.\n");
   2583       return;
   2584    }
   2585 #  endif
   2586 
   2587    if (VG_(clo_verbosity) > 2  ||
   2588        VG_(clo_trace_syscalls) ||
   2589        VG_(clo_trace_sched))
   2590       VG_(message)(Vg_DebugMsg,
   2591 		   "Caught __NR_exit; running __libc_freeres()\n");
   2592 
   2593    /* set thread context to point to libc_freeres_wrapper */
   2594    /* ppc64-linux note: __libc_freeres_wrapper gives us the real
   2595       function entry point, not a fn descriptor, so can use it
   2596       directly.  However, we need to set R2 (the toc pointer)
   2597       appropriately. */
   2598    VG_(set_IP)(tid, __libc_freeres_wrapper);
   2599 #  if defined(VGP_ppc64_linux)
   2600    VG_(threads)[tid].arch.vex.guest_GPR2 = r2;
   2601 #  endif
   2602    /* mips-linux note: we need to set t9 */
   2603 #  if defined(VGP_mips32_linux)
   2604    VG_(threads)[tid].arch.vex.guest_r25 = __libc_freeres_wrapper;
   2605 #  endif
   2606 
   2607    /* Block all blockable signals by copying the real block state into
   2608       the thread's block state*/
   2609    VG_(sigprocmask)(VKI_SIG_BLOCK, NULL, &VG_(threads)[tid].sig_mask);
   2610    VG_(threads)[tid].tmp_sig_mask = VG_(threads)[tid].sig_mask;
   2611 
   2612    /* and restore handlers to default */
   2613    VG_(set_default_handler)(VKI_SIGSEGV);
   2614    VG_(set_default_handler)(VKI_SIGBUS);
   2615    VG_(set_default_handler)(VKI_SIGILL);
   2616    VG_(set_default_handler)(VKI_SIGFPE);
   2617 
   2618    // We were exiting, so assert that...
   2619    vg_assert(VG_(is_exiting)(tid));
   2620    // ...but now we're not again
   2621    VG_(threads)[tid].exitreason = VgSrc_None;
   2622 
   2623    // run until client thread exits - ideally with LIBC_FREERES_DONE,
   2624    // but exit/exitgroup/signal will do
   2625    VG_(scheduler)(tid);
   2626 
   2627    vg_assert(VG_(is_exiting)(tid));
   2628 #endif
   2629 }
   2630 
   2631 
   2632 /*====================================================================*/
   2633 /*=== Getting to main() alive: LINUX                               ===*/
   2634 /*====================================================================*/
   2635 
   2636 #if defined(VGO_linux)
   2637 
   2638 /* If linking of the final executables is done with glibc present,
   2639    then Valgrind starts at main() above as usual, and all of the
   2640    following code is irrelevant.
   2641 
   2642    However, this is not the intended mode of use.  The plan is to
   2643    avoid linking against glibc, by giving gcc the flags
   2644    -nodefaultlibs -lgcc -nostartfiles at startup.
   2645 
   2646    From this derive two requirements:
   2647 
   2648    1. gcc may emit calls to memcpy and memset to deal with structure
   2649       assignments etc.  Since we have chosen to ignore all the
   2650       "normal" supporting libraries, we have to provide our own
   2651       implementations of them.  No problem.
   2652 
   2653    2. We have to provide a symbol "_start", to which the kernel
   2654       hands control at startup.  Hence the code below.
   2655 */
   2656 
   2657 /* ---------------- Requirement 1 ---------------- */
   2658 
   2659 void* memcpy(void *dest, const void *src, SizeT n);
   2660 void* memcpy(void *dest, const void *src, SizeT n) {
   2661    return VG_(memcpy)(dest,src,n);
   2662 }
   2663 void* memset(void *s, int c, SizeT n);
   2664 void* memset(void *s, int c, SizeT n) {
   2665   return VG_(memset)(s,c,n);
   2666 }
   2667 
   2668 /* BVA: abort() for those platforms that need it (PPC and ARM). */
   2669 void abort(void);
   2670 void abort(void){
   2671    VG_(printf)("Something called raise().\n");
   2672    vg_assert(0);
   2673 }
   2674 
   2675 /* EAZG: ARM's EABI will call floating point exception handlers in
   2676    libgcc which boil down to an abort or raise, that's usually defined
   2677    in libc. Instead, define them here. */
   2678 #if defined(VGP_arm_linux)
   2679 void raise(void);
   2680 void raise(void){
   2681    VG_(printf)("Something called raise().\n");
   2682    vg_assert(0);
   2683 }
   2684 
   2685 void __aeabi_unwind_cpp_pr0(void);
   2686 void __aeabi_unwind_cpp_pr0(void){
   2687    VG_(printf)("Something called __aeabi_unwind_cpp_pr0()\n");
   2688    vg_assert(0);
   2689 }
   2690 
   2691 void __aeabi_unwind_cpp_pr1(void);
   2692 void __aeabi_unwind_cpp_pr1(void){
   2693    VG_(printf)("Something called __aeabi_unwind_cpp_pr1()\n");
   2694    vg_assert(0);
   2695 }
   2696 #endif
   2697 
   2698 /* ---------------- Requirement 2 ---------------- */
   2699 
   2700 /* Glibc's sysdeps/i386/elf/start.S has the following gem of a
   2701    comment, which explains how the stack looks right at process start
   2702    (when _start is jumped to).  Hence _start passes %esp to
   2703    _start_in_C_linux, which extracts argc/argv/envp and starts up
   2704    correctly. */
   2705 
   2706 /* This is the canonical entry point, usually the first thing in the text
   2707    segment.  The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
   2708    point runs, most registers' values are unspecified, except for:
   2709 
   2710    %edx         Contains a function pointer to be registered with `atexit'.
   2711                 This is how the dynamic linker arranges to have DT_FINI
   2712                 functions called for shared libraries that have been loaded
   2713                 before this code runs.
   2714 
   2715    %esp         The stack contains the arguments and environment:
   2716                 0(%esp)                 argc
   2717                 4(%esp)                 argv[0]
   2718                 ...
   2719                 (4*argc)(%esp)          NULL
   2720                 (4*(argc+1))(%esp)      envp[0]
   2721                 ...
   2722                                         NULL
   2723 */
   2724 
   2725 /* The kernel hands control to _start, which extracts the initial
   2726    stack pointer and calls onwards to _start_in_C_linux.  This also switches
   2727    the new stack.  */
   2728 #if defined(VGP_x86_linux)
   2729 asm("\n"
   2730     ".text\n"
   2731     "\t.globl _start\n"
   2732     "\t.type _start,@function\n"
   2733     "_start:\n"
   2734     /* set up the new stack in %eax */
   2735     "\tmovl  $vgPlain_interim_stack, %eax\n"
   2736     "\taddl  $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n"
   2737     "\taddl  $"VG_STRINGIFY(VG_STACK_ACTIVE_SZB)", %eax\n"
   2738     "\tsubl  $16, %eax\n"
   2739     "\tandl  $~15, %eax\n"
   2740     /* install it, and collect the original one */
   2741     "\txchgl %eax, %esp\n"
   2742     /* call _start_in_C_linux, passing it the startup %esp */
   2743     "\tpushl %eax\n"
   2744     "\tcall  _start_in_C_linux\n"
   2745     "\thlt\n"
   2746     ".previous\n"
   2747 );
   2748 #elif defined(VGP_amd64_linux)
   2749 asm("\n"
   2750     ".text\n"
   2751     "\t.globl _start\n"
   2752     "\t.type _start,@function\n"
   2753     "_start:\n"
   2754     /* set up the new stack in %rdi */
   2755     "\tmovq  $vgPlain_interim_stack, %rdi\n"
   2756     "\taddq  $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rdi\n"
   2757     "\taddq  $"VG_STRINGIFY(VG_STACK_ACTIVE_SZB)", %rdi\n"
   2758     "\tandq  $~15, %rdi\n"
   2759     /* install it, and collect the original one */
   2760     "\txchgq %rdi, %rsp\n"
   2761     /* call _start_in_C_linux, passing it the startup %rsp */
   2762     "\tcall  _start_in_C_linux\n"
   2763     "\thlt\n"
   2764     ".previous\n"
   2765 );
   2766 #elif defined(VGP_ppc32_linux)
   2767 asm("\n"
   2768     ".text\n"
   2769     "\t.globl _start\n"
   2770     "\t.type _start,@function\n"
   2771     "_start:\n"
   2772     /* set up the new stack in r16 */
   2773     "\tlis 16,vgPlain_interim_stack@ha\n"
   2774     "\tla  16,vgPlain_interim_stack@l(16)\n"
   2775     "\tlis    17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" >> 16)\n"
   2776     "\tori 17,17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" & 0xFFFF)\n"
   2777     "\tlis    18,("VG_STRINGIFY(VG_STACK_ACTIVE_SZB)" >> 16)\n"
   2778     "\tori 18,18,("VG_STRINGIFY(VG_STACK_ACTIVE_SZB)" & 0xFFFF)\n"
   2779     "\tadd 16,17,16\n"
   2780     "\tadd 16,18,16\n"
   2781     "\trlwinm 16,16,0,0,27\n"
   2782     /* now r16 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
   2783        VG_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
   2784        boundary.  And r1 is the original SP.  Set the SP to r16 and
   2785        call _start_in_C_linux, passing it the initial SP. */
   2786     "\tmr 3,1\n"
   2787     "\tmr 1,16\n"
   2788     "\tbl _start_in_C_linux\n"
   2789     "\ttrap\n"
   2790     ".previous\n"
   2791 );
   2792 #elif defined(VGP_ppc64_linux)
   2793 asm("\n"
   2794     /* PPC64 ELF ABI says '_start' points to a function descriptor.
   2795        So we must have one, and that is what goes into the .opd section. */
   2796     "\t.align 2\n"
   2797     "\t.global _start\n"
   2798     "\t.section \".opd\",\"aw\"\n"
   2799     "\t.align 3\n"
   2800     "_start:\n"
   2801     "\t.quad ._start,.TOC.@tocbase,0\n"
   2802     "\t.previous\n"
   2803     "\t.type ._start,@function\n"
   2804     "\t.global  ._start\n"
   2805     "._start:\n"
   2806     /* set up the new stack in r16 */
   2807     "\tlis  16,   vgPlain_interim_stack@highest\n"
   2808     "\tori  16,16,vgPlain_interim_stack@higher\n"
   2809     "\tsldi 16,16,32\n"
   2810     "\toris 16,16,vgPlain_interim_stack@h\n"
   2811     "\tori  16,16,vgPlain_interim_stack@l\n"
   2812     "\txor  17,17,17\n"
   2813     "\tlis    17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" >> 16)\n"
   2814     "\tori 17,17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" & 0xFFFF)\n"
   2815     "\txor 18,18,18\n"
   2816     "\tlis    18,("VG_STRINGIFY(VG_STACK_ACTIVE_SZB)" >> 16)\n"
   2817     "\tori 18,18,("VG_STRINGIFY(VG_STACK_ACTIVE_SZB)" & 0xFFFF)\n"
   2818     "\tadd 16,17,16\n"
   2819     "\tadd 16,18,16\n"
   2820     "\trldicr 16,16,0,59\n"
   2821     /* now r16 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
   2822        VG_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
   2823        boundary.  And r1 is the original SP.  Set the SP to r16 and
   2824        call _start_in_C_linux, passing it the initial SP. */
   2825     "\tmr 3,1\n"
   2826     "\tmr 1,16\n"
   2827     "\tlis  14,   _start_in_C_linux@highest\n"
   2828     "\tori  14,14,_start_in_C_linux@higher\n"
   2829     "\tsldi 14,14,32\n"
   2830     "\toris 14,14,_start_in_C_linux@h\n"
   2831     "\tori  14,14,_start_in_C_linux@l\n"
   2832     "\tld 14,0(14)\n"
   2833     "\tmtctr 14\n"
   2834     "\tbctrl\n"
   2835     "\tnop\n"
   2836     "\ttrap\n"
   2837 );
   2838 #elif defined(VGP_s390x_linux)
   2839 /*
   2840     This is the canonical entry point, usually the first thing in the text
   2841     segment. Most registers' values are unspecified, except for:
   2842 
   2843     %r14         Contains a function pointer to be registered with `atexit'.
   2844                  This is how the dynamic linker arranges to have DT_FINI
   2845                  functions called for shared libraries that have been loaded
   2846                  before this code runs.
   2847 
   2848     %r15         The stack contains the arguments and environment:
   2849                  0(%r15)              argc
   2850                  8(%r15)              argv[0]
   2851                  ...
   2852                  (8*argc)(%r15)       NULL
   2853                  (8*(argc+1))(%r15)   envp[0]
   2854                  ...
   2855                                       NULL
   2856 */
   2857 asm("\n\t"
   2858     ".text\n\t"
   2859     ".globl _start\n\t"
   2860     ".type  _start,@function\n\t"
   2861     "_start:\n\t"
   2862     /* set up the new stack in %r1 */
   2863     "larl   %r1,  vgPlain_interim_stack\n\t"
   2864     "larl   %r5,  1f\n\t"
   2865     "ag     %r1,  0(%r5)\n\t"
   2866     "ag     %r1,  2f-1f(%r5)\n\t"
   2867     "nill   %r1,  0xFFF0\n\t"
   2868     /* install it, and collect the original one */
   2869     "lgr    %r2,  %r15\n\t"
   2870     "lgr    %r15, %r1\n\t"
   2871     /* call _start_in_C_linux, passing it the startup %r15 */
   2872     "brasl  %r14, _start_in_C_linux\n\t"
   2873     /* trigger execution of an invalid opcode -> halt machine */
   2874     "j      .+2\n\t"
   2875     "1:   .quad "VG_STRINGIFY(VG_STACK_GUARD_SZB)"\n\t"
   2876     "2:   .quad "VG_STRINGIFY(VG_STACK_ACTIVE_SZB)"\n\t"
   2877     ".previous\n"
   2878 );
   2879 #elif defined(VGP_arm_linux)
   2880 asm("\n"
   2881     "\t.text\n"
   2882     "\t.align 4\n"
   2883     "\t.type _start,#function\n"
   2884     "\t.global _start\n"
   2885     "_start:\n"
   2886     "\tldr  r0, [pc, #36]\n"
   2887     "\tldr  r1, [pc, #36]\n"
   2888     "\tadd  r0, r1, r0\n"
   2889     "\tldr  r1, [pc, #32]\n"
   2890     "\tadd  r0, r1, r0\n"
   2891     "\tmvn  r1, #15\n"
   2892     "\tand  r0, r0, r1\n"
   2893     "\tmov  r1, sp\n"
   2894     "\tmov  sp, r0\n"
   2895     "\tmov  r0, r1\n"
   2896     "\tb _start_in_C_linux\n"
   2897     "\t.word vgPlain_interim_stack\n"
   2898     "\t.word "VG_STRINGIFY(VG_STACK_GUARD_SZB)"\n"
   2899     "\t.word "VG_STRINGIFY(VG_STACK_ACTIVE_SZB)"\n"
   2900 );
   2901 #elif defined(VGP_mips32_linux)
   2902 asm("\n"
   2903     "\t.type _gp_disp,@object\n"
   2904     ".text\n"
   2905     "\t.globl __start\n"
   2906     "\t.type __start,@function\n"
   2907     "__start:\n"
   2908 
   2909     "\tbal 1f\n"
   2910     "\tnop\n"
   2911 
   2912     "1:\n"
   2913 
   2914     "\tlui      $28, %hi(_gp_disp)\n"
   2915     "\taddiu    $28, $28, %lo(_gp_disp)\n"
   2916     "\taddu     $28, $28, $31\n"
   2917     /* t1/$9 <- Addr(interim_stack) */
   2918     "\tlui      $9, %hi(vgPlain_interim_stack)\n"
   2919     /* t1/$9 <- Addr(interim_stack) */
   2920     "\taddiu    $9, %lo(vgPlain_interim_stack)\n"
   2921 
   2922 
   2923     "\tli    $10, "VG_STRINGIFY(VG_STACK_GUARD_SZB)"\n"
   2924     "\tli    $11, "VG_STRINGIFY(VG_STACK_ACTIVE_SZB)"\n"
   2925 
   2926     "\taddu     $9, $9, $10\n"
   2927     "\taddu     $9, $9, $11\n"
   2928     "\tli       $12, 0xFFFFFFF0\n"
   2929     "\tand      $9, $9, $12\n"
   2930     /* now t1/$9 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
   2931        VG_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
   2932        boundary.  And $29 is the original SP.  Set the SP to t1 and
   2933        call _start_in_C, passing it the initial SP. */
   2934 
   2935     "\tmove    $4, $29\n"     // a0 <- $sp (_start_in_C first arg)
   2936     "\tmove    $29, $9\n"     // $sp <- t1 (new sp)
   2937 
   2938     "\tlui     $25, %hi(_start_in_C_linux)\n"
   2939     "\taddiu   $25, %lo(_start_in_C_linux)\n"
   2940 
   2941     "\tbal  _start_in_C_linux\n"
   2942     "\tbreak  0x7\n"
   2943     ".previous\n"
   2944 );
   2945 #else
   2946 #  error "Unknown linux platform"
   2947 #endif
   2948 
   2949 /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */
   2950 #define _GNU_SOURCE
   2951 #define _FILE_OFFSET_BITS 64
   2952 /* This is in order to get AT_NULL and AT_PAGESIZE. */
   2953 #include <elf.h>
   2954 /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */
   2955 
   2956 /* Avoid compiler warnings: this fn _is_ used, but labelling it
   2957    'static' causes gcc to complain it isn't.
   2958    attribute 'used' also ensures the code is not eliminated at link
   2959    time */
   2960 __attribute__ ((used))
   2961 void _start_in_C_linux ( UWord* pArgc );
   2962 __attribute__ ((used))
   2963 void _start_in_C_linux ( UWord* pArgc )
   2964 {
   2965    Int     r;
   2966    Word    argc = pArgc[0];
   2967    HChar** argv = (HChar**)&pArgc[1];
   2968    HChar** envp = (HChar**)&pArgc[1+argc+1];
   2969 
   2970    VG_(memset)( &the_iicii, 0, sizeof(the_iicii) );
   2971    VG_(memset)( &the_iifii, 0, sizeof(the_iifii) );
   2972 
   2973    the_iicii.sp_at_startup = (Addr)pArgc;
   2974 
   2975 #  if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
   2976    {
   2977       /* ppc/ppc64 can be configured with different page sizes.
   2978          Determine this early.  This is an ugly hack and really should
   2979          be moved into valgrind_main. */
   2980       UWord *sp = &pArgc[1+argc+1];
   2981       while (*sp++ != 0)
   2982          ;
   2983       for (; *sp != AT_NULL && *sp != AT_PAGESZ; sp += 2);
   2984       if (*sp == AT_PAGESZ) {
   2985          VKI_PAGE_SIZE = sp[1];
   2986          for (VKI_PAGE_SHIFT = 12;
   2987               VKI_PAGE_SHIFT <= VKI_MAX_PAGE_SHIFT; VKI_PAGE_SHIFT++)
   2988             if (VKI_PAGE_SIZE == (1UL << VKI_PAGE_SHIFT))
   2989          break;
   2990       }
   2991    }
   2992 #  endif
   2993 
   2994    r = valgrind_main( (Int)argc, argv, envp );
   2995    /* NOTREACHED */
   2996    VG_(exit)(r);
   2997 }
   2998 
   2999 
   3000 /*====================================================================*/
   3001 /*=== Getting to main() alive: darwin                              ===*/
   3002 /*====================================================================*/
   3003 
   3004 #elif defined(VGO_darwin)
   3005 
   3006 /*
   3007    Memory layout established by kernel:
   3008 
   3009    0(%esp)   argc
   3010    4(%esp)   argv[0]
   3011              ...
   3012              argv[argc-1]
   3013              NULL
   3014              envp[0]
   3015              ...
   3016              envp[n]
   3017              NULL
   3018              executable name (presumably, a pointer to it)
   3019              NULL
   3020 
   3021    Ditto in the 64-bit case, except all offsets from SP are obviously
   3022    twice as large.
   3023 */
   3024 
   3025 /* The kernel hands control to _start, which extracts the initial
   3026    stack pointer and calls onwards to _start_in_C_darwin.  This also
   3027    switches to the new stack.  */
   3028 #if defined(VGP_x86_darwin)
   3029 asm("\n"
   3030     ".text\n"
   3031     ".align 2,0x90\n"
   3032     "\t.globl __start\n"
   3033     "__start:\n"
   3034     /* set up the new stack in %eax */
   3035     "\tmovl  $_vgPlain_interim_stack, %eax\n"
   3036     "\taddl  $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n"
   3037     "\taddl  $"VG_STRINGIFY(VG_STACK_ACTIVE_SZB)", %eax\n"
   3038     "\tsubl  $16, %eax\n"
   3039     "\tandl  $~15, %eax\n"
   3040     /* install it, and collect the original one */
   3041     "\txchgl %eax, %esp\n"
   3042     "\tsubl  $12, %esp\n"  // keep stack 16 aligned; see #295428
   3043     /* call _start_in_C_darwin, passing it the startup %esp */
   3044     "\tpushl %eax\n"
   3045     "\tcall  __start_in_C_darwin\n"
   3046     "\tint $3\n"
   3047     "\tint $3\n"
   3048 );
   3049 #elif defined(VGP_amd64_darwin)
   3050 asm("\n"
   3051     ".text\n"
   3052     "\t.globl __start\n"
   3053     ".align 3,0x90\n"
   3054     "__start:\n"
   3055     /* set up the new stack in %rdi */
   3056     "\tmovabsq $_vgPlain_interim_stack, %rdi\n"
   3057     "\taddq    $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rdi\n"
   3058     "\taddq    $"VG_STRINGIFY(VG_STACK_ACTIVE_SZB)", %rdi\n"
   3059     "\tandq    $~15, %rdi\n"
   3060     /* install it, and collect the original one */
   3061     "\txchgq %rdi, %rsp\n"
   3062     /* call _start_in_C_darwin, passing it the startup %rsp */
   3063     "\tcall  __start_in_C_darwin\n"
   3064     "\tint $3\n"
   3065     "\tint $3\n"
   3066 );
   3067 #endif
   3068 
   3069 void* __memcpy_chk(void *dest, const void *src, SizeT n, SizeT n2);
   3070 void* __memcpy_chk(void *dest, const void *src, SizeT n, SizeT n2) {
   3071     // skip check
   3072    return VG_(memcpy)(dest,src,n);
   3073 }
   3074 void* __memset_chk(void *s, int c, SizeT n, SizeT n2);
   3075 void* __memset_chk(void *s, int c, SizeT n, SizeT n2) {
   3076     // skip check
   3077   return VG_(memset)(s,c,n);
   3078 }
   3079 void bzero(void *s, SizeT n);
   3080 void bzero(void *s, SizeT n) {
   3081     VG_(memset)(s,0,n);
   3082 }
   3083 
   3084 void* memcpy(void *dest, const void *src, SizeT n);
   3085 void* memcpy(void *dest, const void *src, SizeT n) {
   3086    return VG_(memcpy)(dest,src,n);
   3087 }
   3088 void* memset(void *s, int c, SizeT n);
   3089 void* memset(void *s, int c, SizeT n) {
   3090   return VG_(memset)(s,c,n);
   3091 }
   3092 
   3093 /* Avoid compiler warnings: this fn _is_ used, but labelling it
   3094    'static' causes gcc to complain it isn't. */
   3095 void _start_in_C_darwin ( UWord* pArgc );
   3096 void _start_in_C_darwin ( UWord* pArgc )
   3097 {
   3098    Int     r;
   3099    Int     argc = *(Int *)pArgc;  // not pArgc[0] on LP64
   3100    HChar** argv = (HChar**)&pArgc[1];
   3101    HChar** envp = (HChar**)&pArgc[1+argc+1];
   3102 
   3103    VG_(memset)( &the_iicii, 0, sizeof(the_iicii) );
   3104    VG_(memset)( &the_iifii, 0, sizeof(the_iifii) );
   3105 
   3106    the_iicii.sp_at_startup = (Addr)pArgc;
   3107 
   3108    r = valgrind_main( (Int)argc, argv, envp );
   3109    /* NOTREACHED */
   3110    VG_(exit)(r);
   3111 }
   3112 
   3113 
   3114 #else
   3115 
   3116 #  error "Unknown OS"
   3117 #endif
   3118 
   3119 
   3120 /*====================================================================*/
   3121 /*=== {u,}{div,mod}di3 replacements                                ===*/
   3122 /*====================================================================*/
   3123 
   3124 /* For static linking on x86-darwin, we need to supply our own 64-bit
   3125    integer division code, else the link dies thusly:
   3126 
   3127    ld_classic: Undefined symbols:
   3128      ___udivdi3
   3129      ___umoddi3
   3130 */
   3131 #if defined(VGP_x86_darwin)
   3132 
   3133 /* Routines for doing signed/unsigned 64 x 64 ==> 64 div and mod
   3134    (udivdi3, umoddi3, divdi3, moddi3) using only 32 x 32 ==> 32
   3135    division.  Cobbled together from
   3136 
   3137    http://www.hackersdelight.org/HDcode/divlu.c
   3138    http://www.hackersdelight.org/HDcode/divls.c
   3139    http://www.hackersdelight.org/HDcode/newCode/divDouble.c
   3140 
   3141    The code from those three files is covered by the following license,
   3142    as it appears at:
   3143 
   3144    http://www.hackersdelight.org/permissions.htm
   3145 
   3146       You are free to use, copy, and distribute any of the code on
   3147       this web site, whether modified by you or not. You need not give
   3148       attribution. This includes the algorithms (some of which appear
   3149       in Hacker's Delight), the Hacker's Assistant, and any code
   3150       submitted by readers. Submitters implicitly agree to this.
   3151 */
   3152 
   3153 /* Long division, unsigned (64/32 ==> 32).
   3154    This procedure performs unsigned "long division" i.e., division of a
   3155 64-bit unsigned dividend by a 32-bit unsigned divisor, producing a
   3156 32-bit quotient.  In the overflow cases (divide by 0, or quotient
   3157 exceeds 32 bits), it returns a remainder of 0xFFFFFFFF (an impossible
   3158 value).
   3159    The dividend is u1 and u0, with u1 being the most significant word.
   3160 The divisor is parameter v. The value returned is the quotient.
   3161    Max line length is 57, to fit in hacker.book. */
   3162 
   3163 static Int nlz32(UInt x)
   3164 {
   3165    Int n;
   3166    if (x == 0) return(32);
   3167    n = 0;
   3168    if (x <= 0x0000FFFF) {n = n +16; x = x <<16;}
   3169    if (x <= 0x00FFFFFF) {n = n + 8; x = x << 8;}
   3170    if (x <= 0x0FFFFFFF) {n = n + 4; x = x << 4;}
   3171    if (x <= 0x3FFFFFFF) {n = n + 2; x = x << 2;}
   3172    if (x <= 0x7FFFFFFF) {n = n + 1;}
   3173    return n;
   3174 }
   3175 
   3176 /* 64 x 32 ==> 32 unsigned division, using only 32 x 32 ==> 32
   3177    division as a primitive. */
   3178 static UInt divlu2(UInt u1, UInt u0, UInt v, UInt *r)
   3179 {
   3180    const UInt b = 65536;     // Number base (16 bits).
   3181    UInt un1, un0,            // Norm. dividend LSD's.
   3182         vn1, vn0,            // Norm. divisor digits.
   3183         q1, q0,              // Quotient digits.
   3184         un32, un21, un10,    // Dividend digit pairs.
   3185         rhat;                // A remainder.
   3186    Int s;                    // Shift amount for norm.
   3187 
   3188    if (u1 >= v) {            // If overflow, set rem.
   3189       if (r != NULL)         // to an impossible value,
   3190          *r = 0xFFFFFFFF;    // and return the largest
   3191       return 0xFFFFFFFF;}    // possible quotient.
   3192 
   3193    s = nlz32(v);             // 0 <= s <= 31.
   3194    v = v << s;               // Normalize divisor.
   3195    vn1 = v >> 16;            // Break divisor up into
   3196    vn0 = v & 0xFFFF;         // two 16-bit digits.
   3197 
   3198    un32 = (u1 << s) | ((u0 >> (32 - s)) & (-s >> 31));
   3199    un10 = u0 << s;           // Shift dividend left.
   3200 
   3201    un1 = un10 >> 16;         // Break right half of
   3202    un0 = un10 & 0xFFFF;      // dividend into two digits.
   3203 
   3204    q1 = un32/vn1;            // Compute the first
   3205    rhat = un32 - q1*vn1;     // quotient digit, q1.
   3206  again1:
   3207    if (q1 >= b || q1*vn0 > b*rhat + un1) {
   3208      q1 = q1 - 1;
   3209      rhat = rhat + vn1;
   3210      if (rhat < b) goto again1;}
   3211 
   3212    un21 = un32*b + un1 - q1*v;  // Multiply and subtract.
   3213 
   3214    q0 = un21/vn1;            // Compute the second
   3215    rhat = un21 - q0*vn1;     // quotient digit, q0.
   3216  again2:
   3217    if (q0 >= b || q0*vn0 > b*rhat + un0) {
   3218      q0 = q0 - 1;
   3219      rhat = rhat + vn1;
   3220      if (rhat < b) goto again2;}
   3221 
   3222    if (r != NULL)            // If remainder is wanted,
   3223       *r = (un21*b + un0 - q0*v) >> s;     // return it.
   3224    return q1*b + q0;
   3225 }
   3226 
   3227 
   3228 /* 64 x 32 ==> 32 signed division, using only 32 x 32 ==> 32 division
   3229    as a primitive. */
   3230 static Int divls(Int u1, UInt u0, Int v, Int *r)
   3231 {
   3232    Int q, uneg, vneg, diff, borrow;
   3233 
   3234    uneg = u1 >> 31;          // -1 if u < 0.
   3235    if (uneg) {               // Compute the absolute
   3236       u0 = -u0;              // value of the dividend u.
   3237       borrow = (u0 != 0);
   3238       u1 = -u1 - borrow;}
   3239 
   3240    vneg = v >> 31;           // -1 if v < 0.
   3241    v = (v ^ vneg) - vneg;    // Absolute value of v.
   3242 
   3243    if ((UInt)u1 >= (UInt)v) goto overflow;
   3244 
   3245    q = divlu2(u1, u0, v, (UInt *)r);
   3246 
   3247    diff = uneg ^ vneg;       // Negate q if signs of
   3248    q = (q ^ diff) - diff;    // u and v differed.
   3249    if (uneg && r != NULL)
   3250       *r = -*r;
   3251 
   3252    if ((diff ^ q) < 0 && q != 0) {  // If overflow,
   3253  overflow:                    // set remainder
   3254       if (r != NULL)         // to an impossible value,
   3255          *r = 0x80000000;    // and return the largest
   3256       q = 0x80000000;}       // possible neg. quotient.
   3257    return q;
   3258 }
   3259 
   3260 
   3261 
   3262 /* This file contains a program for doing 64/64 ==> 64 division, on a
   3263 machine that does not have that instruction but that does have
   3264 instructions for "long division" (64/32 ==> 32). Code for unsigned
   3265 division is given first, followed by a simple program for doing the
   3266 signed version by using the unsigned version.
   3267    These programs are useful in implementing "long long" (64-bit)
   3268 arithmetic on a machine that has the long division instruction. It will
   3269 work on 64- and 32-bit machines, provided the compiler implements long
   3270 long's (64-bit integers). It is desirable that the machine have the
   3271 Count Leading Zeros instruction.
   3272    In the GNU world, these programs are known as __divdi3 and __udivdi3,
   3273 and similar names are used here.
   3274    This material is not in HD, but may be in a future edition.
   3275 Max line length is 57, to fit in hacker.book. */
   3276 
   3277 
   3278 static Int nlz64(ULong x)
   3279 {
   3280    Int n;
   3281    if (x == 0) return(64);
   3282    n = 0;
   3283    if (x <= 0x00000000FFFFFFFFULL) {n = n + 32; x = x << 32;}
   3284    if (x <= 0x0000FFFFFFFFFFFFULL) {n = n + 16; x = x << 16;}
   3285    if (x <= 0x00FFFFFFFFFFFFFFULL) {n = n +  8; x = x <<  8;}
   3286    if (x <= 0x0FFFFFFFFFFFFFFFULL) {n = n +  4; x = x <<  4;}
   3287    if (x <= 0x3FFFFFFFFFFFFFFFULL) {n = n +  2; x = x <<  2;}
   3288    if (x <= 0x7FFFFFFFFFFFFFFFULL) {n = n +  1;}
   3289    return n;
   3290 }
   3291 
   3292 // ---------------------------- udivdi3 --------------------------------
   3293 
   3294    /* The variables u0, u1, etc. take on only 32-bit values, but they
   3295    are declared long long to avoid some compiler warning messages and to
   3296    avoid some unnecessary EXTRs that the compiler would put in, to
   3297    convert long longs to ints.
   3298 
   3299    First the procedure takes care of the case in which the divisor is a
   3300    32-bit quantity. There are two subcases: (1) If the left half of the
   3301    dividend is less than the divisor, one execution of DIVU is all that
   3302    is required (overflow is not possible). (2) Otherwise it does two
   3303    divisions, using the grade school method, with variables used as
   3304    suggested below.
   3305 
   3306        q1 q0
   3307     ________
   3308    v)  u1 u0
   3309      q1*v
   3310      ____
   3311         k u0   */
   3312 
   3313 /* These macros must be used with arguments of the appropriate type
   3314 (unsigned long long for DIVU and long long for DIVS. They are
   3315 simulations of the presumed machines ops. I.e., they look at only the
   3316 low-order 32 bits of the divisor, they return garbage if the division
   3317 overflows, and they return garbage in the high-order half of the
   3318 quotient doubleword.
   3319    In practice, these would be replaced with uses of the machine's DIVU
   3320 and DIVS instructions (e.g., by using the GNU "asm" facility). */
   3321 
   3322 static UInt DIVU ( ULong u, UInt v )
   3323 {
   3324   UInt uHi = (UInt)(u >> 32);
   3325   UInt uLo = (UInt)u;
   3326   return divlu2(uHi, uLo, v, NULL);
   3327 }
   3328 
   3329 static Int DIVS ( Long u, Int v )
   3330 {
   3331   Int  uHi = (Int)(u >> 32);
   3332   UInt uLo = (UInt)u;
   3333   return divls(uHi, uLo, v, NULL);
   3334 }
   3335 
   3336 /* 64 x 64 ==> 64 unsigned division, using only 32 x 32 ==> 32
   3337    division as a primitive. */
   3338 static ULong udivdi3(ULong u, ULong v)
   3339 {
   3340    ULong u0, u1, v1, q0, q1, k, n;
   3341 
   3342    if (v >> 32 == 0) {          // If v < 2**32:
   3343       if (u >> 32 < v)          // If u/v cannot overflow,
   3344          return DIVU(u, v)      // just do one division.
   3345             & 0xFFFFFFFF;
   3346       else {                    // If u/v would overflow:
   3347          u1 = u >> 32;          // Break u up into two
   3348          u0 = u & 0xFFFFFFFF;   // halves.
   3349          q1 = DIVU(u1, v)       // First quotient digit.
   3350             & 0xFFFFFFFF;
   3351          k = u1 - q1*v;         // First remainder, < v.
   3352          q0 = DIVU((k << 32) + u0, v) // 2nd quot. digit.
   3353             & 0xFFFFFFFF;
   3354          return (q1 << 32) + q0;
   3355       }
   3356    }
   3357                                 // Here v >= 2**32.
   3358    n = nlz64(v);                // 0 <= n <= 31.
   3359    v1 = (v << n) >> 32;         // Normalize the divisor
   3360                                 // so its MSB is 1.
   3361    u1 = u >> 1;                 // To ensure no overflow.
   3362    q1 = DIVU(u1, v1)            // Get quotient from
   3363        & 0xFFFFFFFF;            // divide unsigned insn.
   3364    q0 = (q1 << n) >> 31;        // Undo normalization and
   3365                                 // division of u by 2.
   3366    if (q0 != 0)                 // Make q0 correct or
   3367       q0 = q0 - 1;              // too small by 1.
   3368    if ((u - q0*v) >= v)
   3369       q0 = q0 + 1;              // Now q0 is correct.
   3370    return q0;
   3371 }
   3372 
   3373 
   3374 // ----------------------------- divdi3 --------------------------------
   3375 
   3376 /* This routine presumes that smallish cases (those which can be done in
   3377 one execution of DIVS) are common. If this is not the case, the test for
   3378 this case should be deleted.
   3379    Note that the test for when DIVS can be used is not entirely
   3380 accurate. For example, DIVS is not used if v = 0xFFFFFFFF8000000,
   3381 whereas if could be (if u is sufficiently small in magnitude). */
   3382 
   3383 // ------------------------------ cut ----------------------------------
   3384 
   3385 static ULong my_llabs ( Long x )
   3386 {
   3387    ULong t = x >> 63;
   3388    return (x ^ t) - t;
   3389 }
   3390 
   3391 /* 64 x 64 ==> 64 signed division, using only 32 x 32 ==> 32 division
   3392    as a primitive. */
   3393 static Long divdi3(Long u, Long v)
   3394 {
   3395    ULong au, av;
   3396    Long q, t;
   3397    au = my_llabs(u);
   3398    av = my_llabs(v);
   3399    if (av >> 31 == 0) {         // If |v| < 2**31 and
   3400    // if (v << 32 >> 32 == v) { // If v is in range and
   3401       if (au < av << 31) {      // |u|/|v| cannot
   3402          q = DIVS(u, v);        // overflow, use DIVS.
   3403          return (q << 32) >> 32;
   3404       }
   3405    }
   3406    q = udivdi3(au,av);          // Invoke udivdi3.
   3407    t = (u ^ v) >> 63;           // If u, v have different
   3408    return (q ^ t) - t;          // signs, negate q.
   3409 }
   3410 
   3411 // ---------------------------- end cut --------------------------------
   3412 
   3413 ULong __udivdi3 (ULong u, ULong v);
   3414 ULong __udivdi3 (ULong u, ULong v)
   3415 {
   3416   return udivdi3(u,v);
   3417 }
   3418 
   3419 Long __divdi3 (Long u, Long v);
   3420 Long __divdi3 (Long u, Long v)
   3421 {
   3422   return divdi3(u,v);
   3423 }
   3424 
   3425 ULong __umoddi3 (ULong u, ULong v);
   3426 ULong __umoddi3 (ULong u, ULong v)
   3427 {
   3428   ULong q = __udivdi3(u, v);
   3429   ULong r = u - q * v;
   3430   return r;
   3431 }
   3432 
   3433 Long __moddi3 (Long u, Long v);
   3434 Long __moddi3 (Long u, Long v)
   3435 {
   3436   Long q = __divdi3(u, v);
   3437   Long r = u - q * v;
   3438   return r;
   3439 }
   3440 
   3441 /* ------------------------------------------------
   3442    ld_classic: Undefined symbols:
   3443       ___fixunsdfdi
   3444    ------------------------------------------------
   3445 */
   3446 
   3447 /* ===-- fixunsdfdi.c - Implement __fixunsdfdi -----------------------------===
   3448  *
   3449  *                     The LLVM Compiler Infrastructure
   3450  *
   3451  * This file is dual licensed under the MIT and the University of Illinois Open
   3452  * Source Licenses. See LICENSE.TXT for details.
   3453  *
   3454  * ===----------------------------------------------------------------------===
   3455  *
   3456  * This file implements __fixunsdfdi for the compiler_rt library.
   3457  *
   3458  * ===----------------------------------------------------------------------===
   3459  */
   3460 
   3461 /* As per http://www.gnu.org/licenses/license-list.html#GPLCompatibleLicenses,
   3462 
   3463    the "NCSA/University of Illinois Open Source License" is compatible
   3464    with the GPL (both version 2 and 3).  What is claimed to be
   3465    compatible is this
   3466 
   3467    http://www.opensource.org/licenses/UoI-NCSA.php
   3468 
   3469    and the LLVM documentation at
   3470 
   3471    http://www.llvm.org/docs/DeveloperPolicy.html#license
   3472 
   3473    says all the code in LLVM is available under the University of
   3474    Illinois/NCSA Open Source License, at this URL
   3475 
   3476    http://www.opensource.org/licenses/UoI-NCSA.php
   3477 
   3478    viz, the same one that the FSF pages claim is compatible.  So I
   3479    think it's OK to include it.
   3480 */
   3481 
   3482 /* Returns: convert a to a unsigned long long, rounding toward zero.
   3483  *          Negative values all become zero.
   3484  */
   3485 
   3486 /* Assumption: double is a IEEE 64 bit floating point type
   3487  *             du_int is a 64 bit integral type
   3488  *             value in double is representable in du_int or is negative
   3489  *                 (no range checking performed)
   3490  */
   3491 
   3492 /* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
   3493 
   3494 typedef unsigned long long du_int;
   3495 typedef unsigned su_int;
   3496 
   3497 typedef union
   3498 {
   3499     du_int all;
   3500     struct
   3501     {
   3502 #if VG_LITTLEENDIAN
   3503         su_int low;
   3504         su_int high;
   3505 #else
   3506         su_int high;
   3507         su_int low;
   3508 #endif /* VG_LITTLEENDIAN */
   3509     }s;
   3510 } udwords;
   3511 
   3512 typedef union
   3513 {
   3514     udwords u;
   3515     double  f;
   3516 } double_bits;
   3517 
   3518 du_int __fixunsdfdi(double a);
   3519 
   3520 du_int
   3521 __fixunsdfdi(double a)
   3522 {
   3523     double_bits fb;
   3524     fb.f = a;
   3525     int e = ((fb.u.s.high & 0x7FF00000) >> 20) - 1023;
   3526     if (e < 0 || (fb.u.s.high & 0x80000000))
   3527         return 0;
   3528     udwords r;
   3529     r.s.high = (fb.u.s.high & 0x000FFFFF) | 0x00100000;
   3530     r.s.low = fb.u.s.low;
   3531     if (e > 52)
   3532         r.all <<= (e - 52);
   3533     else
   3534         r.all >>= (52 - e);
   3535     return r.all;
   3536 }
   3537 
   3538 
   3539 #endif
   3540 
   3541 
   3542 /*--------------------------------------------------------------------*/
   3543 /*--- end                                                          ---*/
   3544 /*--------------------------------------------------------------------*/
   3545