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