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