sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 3 | /*--- Startup: the real stuff m_main.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 9 | |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2007 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 11 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 28 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 31 | #include "pub_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 32 | #include "pub_core_vki.h" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 33 | #include "pub_core_vkiscnums.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 34 | #include "pub_core_threadstate.h" |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 35 | #include "pub_core_xarray.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 36 | #include "pub_core_clientstate.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 37 | #include "pub_core_aspacemgr.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 38 | #include "pub_core_commandline.h" |
njn | 2521d32 | 2005-05-08 14:45:13 +0000 | [diff] [blame] | 39 | #include "pub_core_debuglog.h" |
| 40 | #include "pub_core_errormgr.h" |
| 41 | #include "pub_core_execontext.h" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 42 | #include "pub_core_initimg.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 43 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 44 | #include "pub_core_libcassert.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 45 | #include "pub_core_libcfile.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 46 | #include "pub_core_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 47 | #include "pub_core_libcproc.h" |
njn | de62cbf | 2005-06-10 22:08:14 +0000 | [diff] [blame] | 48 | #include "pub_core_libcsignal.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 49 | #include "pub_core_syscall.h" // VG_(strerror) |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 50 | #include "pub_core_machine.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 51 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 52 | #include "pub_core_options.h" |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 53 | #include "pub_core_debuginfo.h" |
njn | d1af003 | 2005-05-29 17:01:48 +0000 | [diff] [blame] | 54 | #include "pub_core_redir.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 55 | #include "pub_core_scheduler.h" |
njn | 0c24647 | 2005-05-31 01:00:08 +0000 | [diff] [blame] | 56 | #include "pub_core_signals.h" |
njn | 2025cf9 | 2005-06-26 20:44:48 +0000 | [diff] [blame] | 57 | #include "pub_core_stacks.h" // For VG_(register_stack) |
njn | c1b0181 | 2005-06-17 22:19:06 +0000 | [diff] [blame] | 58 | #include "pub_core_syswrap.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 59 | #include "pub_core_tooliface.h" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 60 | #include "pub_core_translate.h" // For VG_(translate) |
njn | a7598f6 | 2005-06-18 03:27:58 +0000 | [diff] [blame] | 61 | #include "pub_core_trampoline.h" |
njn | 8bddf58 | 2005-05-13 23:40:55 +0000 | [diff] [blame] | 62 | #include "pub_core_transtab.h" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 63 | |
| 64 | /* Stuff for reading AIX5 /proc/<pid>/sysent files */ |
| 65 | #if defined(VGO_aix5) |
| 66 | /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */ |
| 67 | # include <sys/procfs.h> /* prsysent_t */ |
| 68 | /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */ |
| 69 | # define VG_AIX5_SYSENT_SIZE 100000 |
| 70 | static UChar aix5_sysent_buf[VG_AIX5_SYSENT_SIZE]; |
| 71 | #endif |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 72 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 73 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 74 | /*====================================================================*/ |
| 75 | /*=== Counters, for profiling purposes only ===*/ |
| 76 | /*====================================================================*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 77 | |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 78 | static void print_all_stats ( void ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 79 | { |
njn | 42c8355 | 2005-12-05 20:45:59 +0000 | [diff] [blame] | 80 | VG_(print_translation_stats)(); |
nethercote | 92e7b7f | 2004-08-07 17:52:25 +0000 | [diff] [blame] | 81 | VG_(print_tt_tc_stats)(); |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 82 | VG_(print_scheduler_stats)(); |
njn | 9271cbc | 2005-03-13 05:38:25 +0000 | [diff] [blame] | 83 | VG_(print_ExeContext_stats)(); |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 84 | VG_(print_errormgr_stats)(); |
njn | 9271cbc | 2005-03-13 05:38:25 +0000 | [diff] [blame] | 85 | |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 86 | // Memory stats |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 87 | if (VG_(clo_verbosity) > 2) { |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 88 | VG_(message)(Vg_DebugMsg, ""); |
| 89 | VG_(message)(Vg_DebugMsg, |
| 90 | "------ Valgrind's internal memory use stats follow ------" ); |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 91 | VG_(sanity_check_malloc_all)(); |
njn | 11adeb2 | 2005-07-03 20:22:39 +0000 | [diff] [blame] | 92 | VG_(message)(Vg_DebugMsg, "------" ); |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 93 | VG_(print_all_arena_stats)(); |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 94 | VG_(message)(Vg_DebugMsg, ""); |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 95 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | /*====================================================================*/ |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 100 | /*=== Command-line: variables, processing, etc ===*/ |
| 101 | /*====================================================================*/ |
| 102 | |
| 103 | // See pub_{core,tool}_options.h for explanations of all these. |
| 104 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 105 | static void usage_NORETURN ( Bool debug_help ) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 106 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 107 | Char* usage1 = |
njn | 00cfcfc | 2005-11-12 18:53:50 +0000 | [diff] [blame] | 108 | "usage: valgrind [options] prog-and-args\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 109 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 110 | " common user options for all Valgrind tools, with defaults in [ ]:\n" |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 111 | " --tool=<name> use the Valgrind tool named <name> [memcheck]\n" |
nethercote | a76368b | 2004-06-16 11:56:29 +0000 | [diff] [blame] | 112 | " -h --help show this message\n" |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 113 | " --help-debug show this message, plus debugging options\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 114 | " --version show version\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 115 | " -q --quiet run silently; only print error msgs\n" |
| 116 | " -v --verbose be more verbose, incl counts of errors\n" |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 117 | " --trace-children=no|yes Valgrind-ise child processes? [no]\n" |
nethercote | 0d58850 | 2004-06-21 13:27:11 +0000 | [diff] [blame] | 118 | " --track-fds=no|yes track open file descriptors? [no]\n" |
thughes | 6233a38 | 2004-08-21 11:10:44 +0000 | [diff] [blame] | 119 | " --time-stamp=no|yes add timestamps to log messages? [no]\n" |
njn | ce54555 | 2005-07-25 22:36:52 +0000 | [diff] [blame] | 120 | " --log-fd=<number> log messages to file descriptor [2=stderr]\n" |
tom | bbe14b1 | 2005-11-04 16:49:39 +0000 | [diff] [blame] | 121 | " --log-file=<file> log messages to <file>.<pid>\n" |
njn | ce54555 | 2005-07-25 22:36:52 +0000 | [diff] [blame] | 122 | " --log-file-exactly=<file> log messages to <file>\n" |
| 123 | " --log-file-qualifier=<VAR> incorporate $VAR in logfile name [none]\n" |
| 124 | " --log-socket=ipaddr:port log messages to socket ipaddr:port\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 125 | "\n" |
| 126 | " uncommon user options for all Valgrind tools:\n" |
nethercote | 0d58850 | 2004-06-21 13:27:11 +0000 | [diff] [blame] | 127 | " --run-libc-freeres=no|yes free up glibc memory at exit? [yes]\n" |
njn | 628add6 | 2005-11-12 18:21:40 +0000 | [diff] [blame] | 128 | " --sim-hints=hint1,hint2,... known hints:\n" |
sewardj | 19617ae | 2005-10-20 01:09:57 +0000 | [diff] [blame] | 129 | " lax-ioctls, enable-outer [none]\n" |
sewardj | b1131a8 | 2005-03-19 15:12:21 +0000 | [diff] [blame] | 130 | " --show-emwarns=no|yes show warnings about emulation limits? [no]\n" |
sewardj | 6c3a219 | 2005-07-24 07:00:45 +0000 | [diff] [blame] | 131 | " --smc-check=none|stack|all checks for self-modifying code: none,\n" |
| 132 | " only for code found in stacks, or all [stack]\n" |
sewardj | 3c3f0c1 | 2005-11-08 15:52:36 +0000 | [diff] [blame] | 133 | " --kernel-variant=variant1,variant2,... known variants: bproc [none]\n" |
| 134 | " handle non-standard kernel variants\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 135 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 136 | " user options for Valgrind tools that report errors:\n" |
njn | ca54af3 | 2006-04-16 10:25:43 +0000 | [diff] [blame] | 137 | " --xml=yes all output is in XML (some tools only)\n" |
njn | c8ec9f8 | 2005-07-20 03:03:28 +0000 | [diff] [blame] | 138 | " --xml-user-comment=STR copy STR verbatim to XML output\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 139 | " --demangle=no|yes automatically demangle C++ names? [yes]\n" |
njn | 20b4a15 | 2005-10-19 22:39:40 +0000 | [diff] [blame] | 140 | " --num-callers=<number> show <number> callers in stack traces [12]\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 141 | " --error-limit=no|yes stop showing new errors if too many? [yes]\n" |
sewardj | b977908 | 2006-05-12 23:50:15 +0000 | [diff] [blame] | 142 | " --error-exitcode=<number> exit code to return if errors found [0=disable]\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 143 | " --show-below-main=no|yes continue stack traces below main() [no]\n" |
| 144 | " --suppressions=<filename> suppress errors described in <filename>\n" |
sewardj | d153fae | 2005-01-10 17:24:47 +0000 | [diff] [blame] | 145 | " --gen-suppressions=no|yes|all print suppressions for errors? [no]\n" |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 146 | " --db-attach=no|yes start debugger when errors detected? [no]\n" |
| 147 | " --db-command=<command> command to start debugger [gdb -nw %%f %%p]\n" |
| 148 | " --input-fd=<number> file descriptor for input [0=stdin]\n" |
sewardj | 97724e5 | 2005-04-02 23:40:59 +0000 | [diff] [blame] | 149 | " --max-stackframe=<number> assume stack switch for SP changes larger\n" |
| 150 | " than <number> bytes [2000000]\n" |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 151 | "\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 152 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 153 | Char* usage2 = |
| 154 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 155 | " debugging options for all Valgrind tools:\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 156 | " --sanity-level=<number> level of sanity checking to do [1]\n" |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 157 | " --trace-flags=<XXXXXXXX> show generated code? (X = 0|1) [00000000]\n" |
| 158 | " --profile-flags=<XXXXXXXX> ditto, but for profiling (X = 0|1) [00000000]\n" |
sewardj | 33afdb5 | 2006-01-17 02:36:40 +0000 | [diff] [blame] | 159 | " --trace-notbelow=<number> only show BBs above <number> [999999999]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 160 | " --trace-syscalls=no|yes show all system calls? [no]\n" |
| 161 | " --trace-signals=no|yes show signal handling details? [no]\n" |
| 162 | " --trace-symtab=no|yes show symbol table details? [no]\n" |
sewardj | f767d96 | 2007-02-12 17:47:14 +0000 | [diff] [blame] | 163 | " --trace-symtab-patt=<patt> limit debuginfo tracing to obj name <patt>\n" |
sewardj | ce058b0 | 2005-05-01 08:55:38 +0000 | [diff] [blame] | 164 | " --trace-cfi=no|yes show call-frame-info details? [no]\n" |
sewardj | f767d96 | 2007-02-12 17:47:14 +0000 | [diff] [blame] | 165 | " --debug-dump=syms mimic /usr/bin/readelf --syms\n" |
| 166 | " --debug-dump=line mimic /usr/bin/readelf --debug-dump=line\n" |
| 167 | " --debug-dump=frames mimic /usr/bin/readelf --debug-dump=frames\n" |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 168 | " --trace-redir=no|yes show redirection details? [no]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 169 | " --trace-sched=no|yes show thread scheduler details? [no]\n" |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 170 | " --wait-for-gdb=yes|no pause on startup to wait for gdb attach\n" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 171 | " --sym-offsets=yes|no show syms in form 'name+offset' ? [no]\n" |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 172 | #if 0 |
| 173 | " --model-pthreads=yes|no model the pthreads library [no]\n" |
| 174 | #endif |
| 175 | " --command-line-only=no|yes only use command line options [no]\n" |
njn | 613812e | 2005-03-11 04:57:30 +0000 | [diff] [blame] | 176 | "\n" |
| 177 | " --vex-iropt-verbosity 0 .. 9 [0]\n" |
| 178 | " --vex-iropt-level 0 .. 2 [2]\n" |
| 179 | " --vex-iropt-precise-memory-exns [no]\n" |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 180 | " --vex-iropt-unroll-thresh 0 .. 400 [120]\n" |
| 181 | " --vex-guest-max-insns 1 .. 100 [50]\n" |
| 182 | " --vex-guest-chase-thresh 0 .. 99 [10]\n" |
| 183 | "\n" |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 184 | " --trace-flags and --profile-flags values (omit the middle space):\n" |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 185 | " 1000 0000 show conversion into IR\n" |
| 186 | " 0100 0000 show after initial opt\n" |
| 187 | " 0010 0000 show after instrumentation\n" |
| 188 | " 0001 0000 show after second opt\n" |
| 189 | " 0000 1000 show after tree building\n" |
| 190 | " 0000 0100 show selecting insns\n" |
| 191 | " 0000 0010 show after reg-alloc\n" |
| 192 | " 0000 0001 show final assembly\n" |
njn | 33dbfce | 2006-06-02 22:58:34 +0000 | [diff] [blame] | 193 | " (Nb: you need --trace-notbelow with --trace-flags for full details)\n" |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 194 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 195 | " debugging options for Valgrind tools that report errors\n" |
| 196 | " --dump-error=<number> show translation for basic block associated\n" |
| 197 | " with <number>'th error context [0=show none]\n" |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 198 | "\n"; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 199 | |
| 200 | Char* usage3 = |
| 201 | "\n" |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 202 | " Extra options read from ~/.valgrindrc, $VALGRIND_OPTS, ./.valgrindrc\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 203 | "\n" |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 204 | " Valgrind is Copyright (C) 2000-2007 Julian Seward et al.\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 205 | " and licensed under the GNU General Public License, version 2.\n" |
| 206 | " Bug reports, feedback, admiration, abuse, etc, to: %s.\n" |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 207 | "\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 208 | " Tools are copyright and licensed by their authors. See each\n" |
| 209 | " tool's start-up message for more information.\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 210 | "\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 211 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 212 | // Ensure the message goes to stdout |
| 213 | VG_(clo_log_fd) = 1; |
| 214 | vg_assert( !VG_(logging_to_socket) ); |
| 215 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 216 | VG_(printf)(usage1); |
| 217 | if (VG_(details).name) { |
| 218 | VG_(printf)(" user options for %s:\n", VG_(details).name); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 219 | if (VG_(needs).command_line_options) |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 220 | VG_TDICT_CALL(tool_print_usage); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 221 | else |
| 222 | VG_(printf)(" (none)\n"); |
| 223 | } |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 224 | if (debug_help) { |
| 225 | VG_(printf)(usage2); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 226 | |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 227 | if (VG_(details).name) { |
| 228 | VG_(printf)(" debugging options for %s:\n", VG_(details).name); |
| 229 | |
| 230 | if (VG_(needs).command_line_options) |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 231 | VG_TDICT_CALL(tool_print_debug_usage); |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 232 | else |
| 233 | VG_(printf)(" (none)\n"); |
| 234 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 235 | } |
nethercote | 421281e | 2003-11-20 16:20:55 +0000 | [diff] [blame] | 236 | VG_(printf)(usage3, VG_BUGS_TO); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 237 | VG_(exit)(0); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 238 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 239 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 240 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 241 | /* Peer at previously set up VG_(args_for_valgrind) and extract any |
| 242 | request for help and also the tool name. */ |
| 243 | |
| 244 | static void get_helprequest_and_toolname ( Int* need_help, HChar** tool ) |
| 245 | { |
| 246 | UInt i; |
| 247 | HChar* str; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 248 | |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 249 | vg_assert( VG_(args_for_valgrind) ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 250 | |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 251 | /* parse the options we have (only the options we care about now) */ |
| 252 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) { |
| 253 | |
| 254 | str = * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 255 | vg_assert(str); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 256 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 257 | if (VG_STREQ(str, "--version")) { |
njn | 2e9f3f3 | 2005-10-26 16:17:46 +0000 | [diff] [blame] | 258 | // Ensure the version string goes to stdout |
| 259 | VG_(clo_log_fd) = 1; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 260 | VG_(printf)("valgrind-" VERSION "\n"); |
| 261 | VG_(exit)(0); |
| 262 | |
| 263 | } else if (VG_CLO_STREQ(str, "--help") || |
| 264 | VG_CLO_STREQ(str, "-h")) { |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 265 | *need_help = 1; |
| 266 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 267 | } else if (VG_CLO_STREQ(str, "--help-debug")) { |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 268 | *need_help = 2; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 269 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 270 | // The tool has already been determined, but we need to know the name |
| 271 | // here. |
| 272 | } else if (VG_CLO_STREQN(7, str, "--tool=")) { |
| 273 | *tool = &str[7]; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 276 | } |
| 277 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 278 | static Bool process_cmd_line_options( UInt* client_auxv, const char* toolname ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 279 | { |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 280 | // VG_(clo_log_fd) is used by all the messaging. It starts as 2 (stderr) |
| 281 | // and we cannot change it until we know what we are changing it to is |
| 282 | // ok. So we have tmp_log_fd to hold the tmp fd prior to that point. |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 283 | SysRes sres; |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 284 | Int i, tmp_log_fd; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 285 | Int toolname_len = VG_(strlen)(toolname); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 286 | enum { |
| 287 | VgLogTo_Fd, |
| 288 | VgLogTo_File, |
| 289 | VgLogTo_FileExactly, |
| 290 | VgLogTo_Socket |
| 291 | } log_to = VgLogTo_Fd; // Where is logging output to be sent? |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 292 | |
nethercote | e173069 | 2003-11-20 10:38:07 +0000 | [diff] [blame] | 293 | /* log to stderr by default, but usage message goes to stdout */ |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 294 | tmp_log_fd = 2; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 295 | |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 296 | /* Check for sane path in ./configure --prefix=... */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 297 | if (VG_LIBDIR[0] != '/') |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 298 | VG_(err_config_error)("Please use absolute paths in " |
| 299 | "./configure --prefix=... or --libdir=..."); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 300 | |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 301 | vg_assert( VG_(args_for_valgrind) ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 302 | |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 303 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) { |
| 304 | |
| 305 | HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 306 | HChar* colon = arg; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 307 | |
thughes | 3bfd5a0 | 2004-07-18 08:05:44 +0000 | [diff] [blame] | 308 | /* Look for a colon in the switch name */ |
| 309 | while (*colon && *colon != ':' && *colon != '=') |
| 310 | colon++; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 311 | |
| 312 | /* Look for matching "--toolname:foo" */ |
thughes | 3bfd5a0 | 2004-07-18 08:05:44 +0000 | [diff] [blame] | 313 | if (*colon == ':') { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 314 | if (VG_CLO_STREQN(2, arg, "--") && |
| 315 | VG_CLO_STREQN(toolname_len, arg+2, toolname) && |
| 316 | VG_CLO_STREQN(1, arg+2+toolname_len, ":")) |
| 317 | { |
| 318 | // prefix matches, convert "--toolname:foo" to "--foo" |
| 319 | if (0) |
| 320 | VG_(printf)("tool-specific arg: %s\n", arg); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 321 | arg = VG_(strdup)(arg + toolname_len + 1); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 322 | arg[0] = '-'; |
| 323 | arg[1] = '-'; |
| 324 | |
| 325 | } else { |
| 326 | // prefix doesn't match, skip to next arg |
| 327 | continue; |
| 328 | } |
| 329 | } |
| 330 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 331 | /* Ignore these options - they've already been handled */ |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 332 | if (VG_CLO_STREQN( 7, arg, "--tool=")) goto skip_arg; |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 333 | if (VG_CLO_STREQN(20, arg, "--command-line-only=")) goto skip_arg; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 334 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 335 | if ( VG_CLO_STREQ(arg, "--")) goto skip_arg; |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 336 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 337 | else if (VG_CLO_STREQ(arg, "-v") || |
| 338 | VG_CLO_STREQ(arg, "--verbose")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 339 | VG_(clo_verbosity)++; |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 340 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 341 | else if (VG_CLO_STREQ(arg, "-q") || |
| 342 | VG_CLO_STREQ(arg, "--quiet")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 343 | VG_(clo_verbosity)--; |
| 344 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 345 | else if (VG_CLO_STREQ(arg, "-d")) { |
| 346 | /* do nothing */ |
| 347 | } |
| 348 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 349 | else VG_BOOL_CLO(arg, "--xml", VG_(clo_xml)) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 350 | else VG_BOOL_CLO(arg, "--db-attach", VG_(clo_db_attach)) |
| 351 | else VG_BOOL_CLO(arg, "--demangle", VG_(clo_demangle)) |
| 352 | else VG_BOOL_CLO(arg, "--error-limit", VG_(clo_error_limit)) |
sewardj | b977908 | 2006-05-12 23:50:15 +0000 | [diff] [blame] | 353 | else VG_NUM_CLO (arg, "--error-exitcode", VG_(clo_error_exitcode)) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 354 | else VG_BOOL_CLO(arg, "--show-emwarns", VG_(clo_show_emwarns)) |
sewardj | 97724e5 | 2005-04-02 23:40:59 +0000 | [diff] [blame] | 355 | else VG_NUM_CLO (arg, "--max-stackframe", VG_(clo_max_stackframe)) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 356 | else VG_BOOL_CLO(arg, "--run-libc-freeres", VG_(clo_run_libc_freeres)) |
| 357 | else VG_BOOL_CLO(arg, "--show-below-main", VG_(clo_show_below_main)) |
| 358 | else VG_BOOL_CLO(arg, "--time-stamp", VG_(clo_time_stamp)) |
| 359 | else VG_BOOL_CLO(arg, "--track-fds", VG_(clo_track_fds)) |
| 360 | else VG_BOOL_CLO(arg, "--trace-children", VG_(clo_trace_children)) |
| 361 | else VG_BOOL_CLO(arg, "--trace-sched", VG_(clo_trace_sched)) |
| 362 | else VG_BOOL_CLO(arg, "--trace-signals", VG_(clo_trace_signals)) |
| 363 | else VG_BOOL_CLO(arg, "--trace-symtab", VG_(clo_trace_symtab)) |
sewardj | f767d96 | 2007-02-12 17:47:14 +0000 | [diff] [blame] | 364 | else VG_STR_CLO (arg, "--trace-symtab-patt", VG_(clo_trace_symtab_patt)) |
sewardj | ce058b0 | 2005-05-01 08:55:38 +0000 | [diff] [blame] | 365 | else VG_BOOL_CLO(arg, "--trace-cfi", VG_(clo_trace_cfi)) |
sewardj | f767d96 | 2007-02-12 17:47:14 +0000 | [diff] [blame] | 366 | else VG_XACT_CLO(arg, "--debug-dump=syms", VG_(clo_debug_dump_syms)) |
| 367 | else VG_XACT_CLO(arg, "--debug-dump=line", VG_(clo_debug_dump_line)) |
| 368 | else VG_XACT_CLO(arg, "--debug-dump=frames", VG_(clo_debug_dump_frames)) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 369 | else VG_BOOL_CLO(arg, "--trace-redir", VG_(clo_trace_redir)) |
sewardj | f767d96 | 2007-02-12 17:47:14 +0000 | [diff] [blame] | 370 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 371 | else VG_BOOL_CLO(arg, "--trace-syscalls", VG_(clo_trace_syscalls)) |
| 372 | else VG_BOOL_CLO(arg, "--trace-pthreads", VG_(clo_trace_pthreads)) |
| 373 | else VG_BOOL_CLO(arg, "--wait-for-gdb", VG_(clo_wait_for_gdb)) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 374 | else VG_STR_CLO (arg, "--db-command", VG_(clo_db_command)) |
njn | 628add6 | 2005-11-12 18:21:40 +0000 | [diff] [blame] | 375 | else VG_STR_CLO (arg, "--sim-hints", VG_(clo_sim_hints)) |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 376 | else VG_BOOL_CLO(arg, "--sym-offsets", VG_(clo_sym_offsets)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 377 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 378 | else VG_NUM_CLO (arg, "--dump-error", VG_(clo_dump_error)) |
| 379 | else VG_NUM_CLO (arg, "--input-fd", VG_(clo_input_fd)) |
| 380 | else VG_NUM_CLO (arg, "--sanity-level", VG_(clo_sanity_level)) |
| 381 | else VG_BNUM_CLO(arg, "--num-callers", VG_(clo_backtrace_size), 1, |
| 382 | VG_DEEPEST_BACKTRACE) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 383 | |
sewardj | 6c3a219 | 2005-07-24 07:00:45 +0000 | [diff] [blame] | 384 | else if (VG_CLO_STREQ(arg, "--smc-check=none")) |
| 385 | VG_(clo_smc_check) = Vg_SmcNone; |
| 386 | else if (VG_CLO_STREQ(arg, "--smc-check=stack")) |
| 387 | VG_(clo_smc_check) = Vg_SmcStack; |
| 388 | else if (VG_CLO_STREQ(arg, "--smc-check=all")) |
| 389 | VG_(clo_smc_check) = Vg_SmcAll; |
sewardj | 26412bd | 2005-07-07 10:05:05 +0000 | [diff] [blame] | 390 | |
sewardj | ce5a566 | 2005-10-06 03:19:49 +0000 | [diff] [blame] | 391 | else VG_STR_CLO (arg, "--kernel-variant", VG_(clo_kernel_variant)) |
| 392 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 393 | else VG_BNUM_CLO(arg, "--vex-iropt-verbosity", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 394 | VG_(clo_vex_control).iropt_verbosity, 0, 10) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 395 | else VG_BNUM_CLO(arg, "--vex-iropt-level", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 396 | VG_(clo_vex_control).iropt_level, 0, 2) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 397 | else VG_BOOL_CLO(arg, "--vex-iropt-precise-memory-exns", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 398 | VG_(clo_vex_control).iropt_precise_memory_exns) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 399 | else VG_BNUM_CLO(arg, "--vex-iropt-unroll-thresh", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 400 | VG_(clo_vex_control).iropt_unroll_thresh, 0, 400) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 401 | else VG_BNUM_CLO(arg, "--vex-guest-max-insns", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 402 | VG_(clo_vex_control).guest_max_insns, 1, 100) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 403 | else VG_BNUM_CLO(arg, "--vex-guest-chase-thresh", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 404 | VG_(clo_vex_control).guest_chase_thresh, 0, 99) |
| 405 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 406 | else if (VG_CLO_STREQN(9, arg, "--log-fd=")) { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 407 | log_to = VgLogTo_Fd; |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 408 | VG_(clo_log_name) = NULL; |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 409 | tmp_log_fd = (Int)VG_(atoll)(&arg[9]); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 410 | } |
| 411 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 412 | else if (VG_CLO_STREQN(11, arg, "--log-file=")) { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 413 | log_to = VgLogTo_File; |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 414 | VG_(clo_log_name) = &arg[11]; |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 415 | } |
njn | d6bc3c3 | 2005-03-27 00:44:31 +0000 | [diff] [blame] | 416 | |
sewardj | ad31116 | 2005-07-19 11:25:02 +0000 | [diff] [blame] | 417 | else if (VG_CLO_STREQN(21, arg, "--log-file-qualifier=")) { |
| 418 | VG_(clo_log_file_qualifier) = &arg[21]; |
| 419 | } |
| 420 | |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 421 | else if (VG_CLO_STREQN(19, arg, "--log-file-exactly=")) { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 422 | log_to = VgLogTo_FileExactly; |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 423 | VG_(clo_log_name) = &arg[19]; |
| 424 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 425 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 426 | else if (VG_CLO_STREQN(13, arg, "--log-socket=")) { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 427 | log_to = VgLogTo_Socket; |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 428 | VG_(clo_log_name) = &arg[13]; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 429 | } |
| 430 | |
sewardj | 768db0e | 2005-07-19 14:18:56 +0000 | [diff] [blame] | 431 | else if (VG_CLO_STREQN(19, arg, "--xml-user-comment=")) { |
| 432 | VG_(clo_xml_user_comment) = &arg[19]; |
| 433 | } |
| 434 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 435 | else if (VG_CLO_STREQN(15, arg, "--suppressions=")) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 436 | if (VG_(clo_n_suppressions) >= VG_CLO_MAX_SFILES) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 437 | VG_(message)(Vg_UserMsg, "Too many suppression files specified."); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 438 | VG_(message)(Vg_UserMsg, |
| 439 | "Increase VG_CLO_MAX_SFILES and recompile."); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 440 | VG_(err_bad_option)(arg); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 441 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 442 | VG_(clo_suppressions)[VG_(clo_n_suppressions)] = &arg[15]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 443 | VG_(clo_n_suppressions)++; |
| 444 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 445 | |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 446 | /* "stuvwxyz" --> stuvwxyz (binary) */ |
| 447 | else if (VG_CLO_STREQN(14, arg, "--trace-flags=")) { |
| 448 | Int j; |
| 449 | char* opt = & arg[14]; |
| 450 | |
| 451 | if (8 != VG_(strlen)(opt)) { |
| 452 | VG_(message)(Vg_UserMsg, |
| 453 | "--trace-flags argument must have 8 digits"); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 454 | VG_(err_bad_option)(arg); |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 455 | } |
| 456 | for (j = 0; j < 8; j++) { |
| 457 | if ('0' == opt[j]) { /* do nothing */ } |
| 458 | else if ('1' == opt[j]) VG_(clo_trace_flags) |= (1 << (7-j)); |
| 459 | else { |
| 460 | VG_(message)(Vg_UserMsg, "--trace-flags argument can only " |
| 461 | "contain 0s and 1s"); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 462 | VG_(err_bad_option)(arg); |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /* "stuvwxyz" --> stuvwxyz (binary) */ |
| 468 | else if (VG_CLO_STREQN(16, arg, "--profile-flags=")) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 469 | Int j; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 470 | char* opt = & arg[16]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 471 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 472 | if (8 != VG_(strlen)(opt)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 473 | VG_(message)(Vg_UserMsg, |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 474 | "--profile-flags argument must have 8 digits"); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 475 | VG_(err_bad_option)(arg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 476 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 477 | for (j = 0; j < 8; j++) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 478 | if ('0' == opt[j]) { /* do nothing */ } |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 479 | else if ('1' == opt[j]) VG_(clo_profile_flags) |= (1 << (7-j)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 480 | else { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 481 | VG_(message)(Vg_UserMsg, "--profile-flags argument can only " |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 482 | "contain 0s and 1s"); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 483 | VG_(err_bad_option)(arg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 487 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 488 | else VG_NUM_CLO (arg, "--trace-notbelow", VG_(clo_trace_notbelow)) |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 489 | |
sewardj | d153fae | 2005-01-10 17:24:47 +0000 | [diff] [blame] | 490 | else if (VG_CLO_STREQ(arg, "--gen-suppressions=no")) |
| 491 | VG_(clo_gen_suppressions) = 0; |
| 492 | else if (VG_CLO_STREQ(arg, "--gen-suppressions=yes")) |
| 493 | VG_(clo_gen_suppressions) = 1; |
| 494 | else if (VG_CLO_STREQ(arg, "--gen-suppressions=all")) |
| 495 | VG_(clo_gen_suppressions) = 2; |
| 496 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 497 | else if ( ! VG_(needs).command_line_options |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 498 | || ! VG_TDICT_CALL(tool_process_cmd_line_option, arg) ) { |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 499 | VG_(err_bad_option)(arg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 500 | } |
njn | 8c0b3bb | 2005-03-12 21:20:39 +0000 | [diff] [blame] | 501 | skip_arg: |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 502 | if (arg != * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i )) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 503 | VG_(free)(arg); |
| 504 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 505 | } |
| 506 | |
sewardj | 998d40d | 2004-12-06 14:24:52 +0000 | [diff] [blame] | 507 | /* Make VEX control parameters sane */ |
| 508 | |
| 509 | if (VG_(clo_vex_control).guest_chase_thresh |
| 510 | >= VG_(clo_vex_control).guest_max_insns) |
| 511 | VG_(clo_vex_control).guest_chase_thresh |
| 512 | = VG_(clo_vex_control).guest_max_insns - 1; |
| 513 | |
| 514 | if (VG_(clo_vex_control).guest_chase_thresh < 0) |
| 515 | VG_(clo_vex_control).guest_chase_thresh = 0; |
| 516 | |
| 517 | /* Check various option values */ |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 518 | |
njn | f9ebf67 | 2003-05-12 21:41:30 +0000 | [diff] [blame] | 519 | if (VG_(clo_verbosity) < 0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 520 | VG_(clo_verbosity) = 0; |
| 521 | |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 522 | if (VG_(clo_db_attach) && VG_(clo_trace_children)) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 523 | VG_(message)(Vg_UserMsg, ""); |
| 524 | VG_(message)(Vg_UserMsg, |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 525 | "--db-attach=yes conflicts with --trace-children=yes"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 526 | VG_(message)(Vg_UserMsg, |
| 527 | "Please choose one or the other, but not both."); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 528 | VG_(err_bad_option)("--db-attach=yes and --trace-children=yes"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 529 | } |
| 530 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 531 | if (VG_(clo_gen_suppressions) > 0 && |
| 532 | !VG_(needs).core_errors && !VG_(needs).tool_errors) { |
| 533 | VG_(message)(Vg_UserMsg, |
| 534 | "Can't use --gen-suppressions= with this tool,"); |
| 535 | VG_(message)(Vg_UserMsg, |
| 536 | "as it doesn't generate errors."); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 537 | VG_(err_bad_option)("--gen-suppressions="); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 538 | } |
| 539 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 540 | /* If we've been asked to emit XML, mash around various other |
| 541 | options so as to constrain the output somewhat, and to remove |
| 542 | any need for user input during the run. */ |
| 543 | if (VG_(clo_xml)) { |
| 544 | /* Disable suppression generation (requires user input) */ |
| 545 | VG_(clo_gen_suppressions) = 0; |
| 546 | /* Disable attaching to GDB (requires user input) */ |
| 547 | VG_(clo_db_attach) = False; |
| 548 | /* Set a known verbosity level */ |
| 549 | VG_(clo_verbosity) = 1; |
| 550 | /* Disable error limits (this might be a bad idea!) */ |
| 551 | VG_(clo_error_limit) = False; |
| 552 | /* Disable emulation warnings */ |
| 553 | VG_(clo_show_emwarns) = False; |
| 554 | /* Disable waiting for GDB to debug Valgrind */ |
| 555 | VG_(clo_wait_for_gdb) = False; |
| 556 | /* No file-descriptor leak checking yet */ |
| 557 | VG_(clo_track_fds) = False; |
sewardj | 21f7f0c | 2005-07-06 19:46:48 +0000 | [diff] [blame] | 558 | /* Disable timestamped output */ |
| 559 | VG_(clo_time_stamp) = False; |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 560 | /* Also, we want to set options for the leak checker, but that |
| 561 | will have to be done in Memcheck's flag-handling code, not |
| 562 | here. */ |
| 563 | } |
| 564 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 565 | /* All non-logging-related options have been checked. If the logging |
| 566 | option specified is ok, we can switch to it, as we know we won't |
| 567 | have to generate any other command-line-related error messages. |
| 568 | (So far we should be still attached to stderr, so we can show on |
| 569 | the terminal any problems to do with processing command line |
| 570 | opts.) |
| 571 | |
| 572 | So set up logging now. After this is done, VG_(clo_log_fd) |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 573 | should be connected to whatever sink has been selected, and we |
| 574 | indiscriminately chuck stuff into it without worrying what the |
| 575 | nature of it is. Oh the wonder of Unix streams. */ |
| 576 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 577 | vg_assert(VG_(clo_log_fd) == 2 /* stderr */); |
| 578 | vg_assert(VG_(logging_to_socket) == False); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 579 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 580 | switch (log_to) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 581 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 582 | case VgLogTo_Fd: |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 583 | vg_assert(VG_(clo_log_name) == NULL); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 584 | break; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 585 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 586 | case VgLogTo_File: { |
sewardj | ad31116 | 2005-07-19 11:25:02 +0000 | [diff] [blame] | 587 | HChar logfilename[1000]; |
| 588 | Int seq = 0; |
| 589 | Int pid = VG_(getpid)(); |
| 590 | HChar* qual = NULL; |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 591 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 592 | vg_assert(VG_(clo_log_name) != NULL); |
| 593 | vg_assert(VG_(strlen)(VG_(clo_log_name)) <= 900); /* paranoia */ |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 594 | |
sewardj | ad31116 | 2005-07-19 11:25:02 +0000 | [diff] [blame] | 595 | if (VG_(clo_log_file_qualifier)) { |
| 596 | qual = VG_(getenv)(VG_(clo_log_file_qualifier)); |
| 597 | } |
| 598 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 599 | for (;;) { |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 600 | HChar pidtxt[20], seqtxt[20]; |
| 601 | |
| 602 | VG_(sprintf)(pidtxt, "%d", pid); |
| 603 | |
| 604 | if (seq == 0) |
| 605 | seqtxt[0] = 0; |
| 606 | else |
| 607 | VG_(sprintf)(seqtxt, ".%d", seq); |
| 608 | |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 609 | seq++; |
| 610 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 611 | /* Result: |
| 612 | if (qual) base_name ++ "." ++ qual ++ seqtxt |
| 613 | if (not qual) base_name ++ "." ++ pid ++ seqtxt |
| 614 | */ |
| 615 | VG_(sprintf)( logfilename, |
| 616 | "%s.%s%s", |
| 617 | VG_(clo_log_name), |
| 618 | qual ? qual : pidtxt, |
| 619 | seqtxt ); |
| 620 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 621 | // EXCL: it will fail with EEXIST if the file already exists. |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 622 | sres = VG_(open)(logfilename, |
| 623 | VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC, |
| 624 | VKI_S_IRUSR|VKI_S_IWUSR); |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 625 | if (!sres.isError) { |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 626 | tmp_log_fd = sres.res; |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 627 | break; /* for (;;) */ |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 628 | } else { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 629 | // If the file already existed, we try the next name. If it |
| 630 | // was some other file error, we give up. |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 631 | if (sres.err != VKI_EEXIST) { |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 632 | VG_(message)(Vg_UserMsg, |
sewardj | c40fce5 | 2006-05-03 13:52:17 +0000 | [diff] [blame] | 633 | "Can't create log file '%s' (%s); giving up!", |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 634 | logfilename, VG_(strerror)(sres.err)); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 635 | VG_(err_bad_option)( |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 636 | "--log-file=<file> (didn't work out for some reason.)"); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 637 | /*NOTREACHED*/ |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 641 | break; /* switch (VG_(clo_log_to)) */ |
| 642 | } |
| 643 | |
| 644 | case VgLogTo_FileExactly: { |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 645 | vg_assert(VG_(clo_log_name) != NULL); |
| 646 | vg_assert(VG_(strlen)(VG_(clo_log_name)) <= 900); /* paranoia */ |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 647 | |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 648 | sres = VG_(open)(VG_(clo_log_name), |
| 649 | VKI_O_CREAT|VKI_O_WRONLY|VKI_O_TRUNC, |
| 650 | VKI_S_IRUSR|VKI_S_IWUSR); |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 651 | if (!sres.isError) { |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 652 | tmp_log_fd = sres.res; |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 653 | } else { |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 654 | VG_(message)(Vg_UserMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 655 | "Can't create/open log file '%s'; giving up!", |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 656 | VG_(clo_log_name)); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 657 | VG_(err_bad_option)( |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 658 | "--log-file-exactly=<file> (didn't work out for some reason.)"); |
| 659 | /*NOTREACHED*/ |
| 660 | } |
| 661 | break; /* switch (VG_(clo_log_to)) */ |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | case VgLogTo_Socket: { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 665 | vg_assert(VG_(clo_log_name) != NULL); |
| 666 | vg_assert(VG_(strlen)(VG_(clo_log_name)) <= 900); /* paranoia */ |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 667 | tmp_log_fd = VG_(connect_via_socket)( VG_(clo_log_name) ); |
| 668 | if (tmp_log_fd == -1) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 669 | VG_(message)(Vg_UserMsg, |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 670 | "Invalid --log-socket=ipaddr or --log-socket=ipaddr:port spec"); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 671 | VG_(message)(Vg_UserMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 672 | "of '%s'; giving up!", VG_(clo_log_name) ); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 673 | VG_(err_bad_option)( |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 674 | "--log-socket="); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 675 | /*NOTREACHED*/ |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 676 | } |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 677 | if (tmp_log_fd == -2) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 678 | VG_(message)(Vg_UserMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 679 | "valgrind: failed to connect to logging server '%s'.", |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 680 | VG_(clo_log_name) ); |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 681 | VG_(message)(Vg_UserMsg, |
| 682 | "Log messages will sent to stderr instead." ); |
| 683 | VG_(message)(Vg_UserMsg, |
| 684 | "" ); |
| 685 | /* We don't change anything here. */ |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 686 | vg_assert(VG_(clo_log_fd) == 2); |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 687 | tmp_log_fd = 2; |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 688 | } else { |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 689 | vg_assert(tmp_log_fd > 0); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 690 | VG_(logging_to_socket) = True; |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 691 | } |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 692 | break; |
| 693 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 694 | } |
| 695 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 696 | |
| 697 | /* Check that the requested tool actually supports XML output. */ |
njn | ca54af3 | 2006-04-16 10:25:43 +0000 | [diff] [blame] | 698 | if (VG_(clo_xml) && !VG_(needs).xml_output) { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 699 | VG_(clo_xml) = False; |
| 700 | VG_(message)(Vg_UserMsg, |
njn | ca54af3 | 2006-04-16 10:25:43 +0000 | [diff] [blame] | 701 | "%s does not support XML output.", VG_(details).name); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 702 | VG_(err_bad_option)("--xml=yes"); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 703 | /*NOTREACHED*/ |
| 704 | } |
| 705 | |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 706 | if (tmp_log_fd >= 0) { |
| 707 | // Move log_fd into the safe range, so it doesn't conflict with any app fds. |
| 708 | tmp_log_fd = VG_(fcntl)(tmp_log_fd, VKI_F_DUPFD, VG_(fd_hard_limit)); |
| 709 | if (tmp_log_fd < 0) { |
| 710 | VG_(message)(Vg_UserMsg, "valgrind: failed to move logfile fd into safe range, using stderr"); |
| 711 | VG_(clo_log_fd) = 2; // stderr |
| 712 | } else { |
| 713 | VG_(clo_log_fd) = tmp_log_fd; |
| 714 | VG_(fcntl)(VG_(clo_log_fd), VKI_F_SETFD, VKI_FD_CLOEXEC); |
| 715 | } |
| 716 | } else { |
| 717 | // If they said --log-fd=-1, don't print anything. Plausible for use in |
| 718 | // regression testing suites that use client requests to count errors. |
| 719 | VG_(clo_log_fd) = tmp_log_fd; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 720 | } |
| 721 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 722 | if (VG_(clo_n_suppressions) < VG_CLO_MAX_SFILES-1 && |
| 723 | (VG_(needs).core_errors || VG_(needs).tool_errors)) { |
| 724 | /* If we haven't reached the max number of suppressions, load |
| 725 | the default one. */ |
| 726 | static const Char default_supp[] = "default.supp"; |
| 727 | Int len = VG_(strlen)(VG_(libdir)) + 1 + sizeof(default_supp); |
| 728 | Char *buf = VG_(arena_malloc)(VG_AR_CORE, len); |
| 729 | VG_(sprintf)(buf, "%s/%s", VG_(libdir), default_supp); |
| 730 | VG_(clo_suppressions)[VG_(clo_n_suppressions)] = buf; |
| 731 | VG_(clo_n_suppressions)++; |
| 732 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 733 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 734 | return (log_to == VgLogTo_Fd); |
| 735 | } |
| 736 | |
| 737 | |
| 738 | /*====================================================================*/ |
| 739 | /*=== Printing the preamble ===*/ |
| 740 | /*====================================================================*/ |
| 741 | |
| 742 | /* Ok, the logging sink is running now. Print a suitable preamble. |
| 743 | If logging to file or a socket, write details of parent PID and |
| 744 | command line args, to help people trying to interpret the |
| 745 | results of a run which encompasses multiple processes. */ |
| 746 | static void print_preamble(Bool logging_to_fd, const char* toolname) |
| 747 | { |
tom | 60a4b0b | 2005-10-12 10:45:27 +0000 | [diff] [blame] | 748 | HChar* xpre = VG_(clo_xml) ? " <line>" : ""; |
| 749 | HChar* xpost = VG_(clo_xml) ? "</line>" : ""; |
| 750 | Int i; |
| 751 | |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 752 | vg_assert( VG_(args_for_client) ); |
| 753 | vg_assert( VG_(args_for_valgrind) ); |
| 754 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 755 | if (VG_(clo_xml)) { |
sewardj | 8665d8e | 2005-06-01 17:35:23 +0000 | [diff] [blame] | 756 | VG_(message)(Vg_UserMsg, "<?xml version=\"1.0\"?>"); |
| 757 | VG_(message)(Vg_UserMsg, ""); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 758 | VG_(message)(Vg_UserMsg, "<valgrindoutput>"); |
| 759 | VG_(message)(Vg_UserMsg, ""); |
cerion | 20241ba | 2005-11-15 19:07:53 +0000 | [diff] [blame] | 760 | VG_(message)(Vg_UserMsg, "<protocolversion>2</protocolversion>"); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 761 | VG_(message)(Vg_UserMsg, ""); |
| 762 | } |
| 763 | |
sewardj | 83adf41 | 2002-05-01 01:25:45 +0000 | [diff] [blame] | 764 | if (VG_(clo_verbosity > 0)) { |
sewardj | d7bddad | 2005-06-13 16:48:32 +0000 | [diff] [blame] | 765 | |
| 766 | if (VG_(clo_xml)) |
| 767 | VG_(message)(Vg_UserMsg, "<preamble>"); |
| 768 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 769 | /* Tool details */ |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 770 | VG_(message)(Vg_UserMsg, "%s%s%s%s, %s.%s", |
| 771 | xpre, |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 772 | VG_(details).name, |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 773 | NULL == VG_(details).version ? "" : "-", |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 774 | NULL == VG_(details).version |
| 775 | ? (Char*)"" : VG_(details).version, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 776 | VG_(details).description, |
| 777 | xpost); |
| 778 | VG_(message)(Vg_UserMsg, "%s%s%s", |
| 779 | xpre, VG_(details).copyright_author, xpost); |
sewardj | 3b2736a | 2002-03-24 12:18:35 +0000 | [diff] [blame] | 780 | |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 781 | /* Core details */ |
| 782 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 783 | "%sUsing LibVEX rev %s, a library for dynamic binary translation.%s", |
| 784 | xpre, LibVEX_Version(), xpost ); |
sewardj | c702533 | 2004-12-13 18:30:39 +0000 | [diff] [blame] | 785 | VG_(message)(Vg_UserMsg, |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 786 | "%sCopyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.%s", |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 787 | xpre, xpost ); |
sewardj | c702533 | 2004-12-13 18:30:39 +0000 | [diff] [blame] | 788 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 789 | "%sUsing valgrind-%s, a dynamic binary instrumentation framework.%s", |
| 790 | xpre, VERSION, xpost); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 791 | VG_(message)(Vg_UserMsg, |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 792 | "%sCopyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.%s", |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 793 | xpre, xpost ); |
sewardj | d7bddad | 2005-06-13 16:48:32 +0000 | [diff] [blame] | 794 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 795 | if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) |
| 796 | VG_(message)(Vg_UserMsg, "For more details, rerun with: -v"); |
| 797 | |
sewardj | d7bddad | 2005-06-13 16:48:32 +0000 | [diff] [blame] | 798 | if (VG_(clo_xml)) |
| 799 | VG_(message)(Vg_UserMsg, "</preamble>"); |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 800 | } |
| 801 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 802 | if (!VG_(clo_xml) && VG_(clo_verbosity) > 0 && !logging_to_fd) { |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 803 | VG_(message)(Vg_UserMsg, ""); |
| 804 | VG_(message)(Vg_UserMsg, |
| 805 | "My PID = %d, parent PID = %d. Prog and args are:", |
| 806 | VG_(getpid)(), VG_(getppid)() ); |
sewardj | 5e94078 | 2005-09-28 19:59:19 +0000 | [diff] [blame] | 807 | if (VG_(args_the_exename)) |
| 808 | VG_(message)(Vg_UserMsg, " %s", VG_(args_the_exename)); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 809 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) |
| 810 | VG_(message)(Vg_UserMsg, |
| 811 | " %s", |
| 812 | * (HChar**) VG_(indexXA)( VG_(args_for_client), i )); |
sewardj | 5b742c3 | 2005-07-26 23:43:26 +0000 | [diff] [blame] | 813 | if (VG_(clo_log_file_qualifier)) { |
| 814 | HChar* val = VG_(getenv)(VG_(clo_log_file_qualifier)); |
| 815 | VG_(message)(Vg_UserMsg, ""); |
| 816 | VG_(message)(Vg_UserMsg, "Log file qualifier: var %s, value %s.", |
| 817 | VG_(clo_log_file_qualifier), |
| 818 | val ? val : ""); |
| 819 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 820 | } |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 821 | else |
| 822 | if (VG_(clo_xml)) { |
| 823 | VG_(message)(Vg_UserMsg, ""); |
| 824 | VG_(message)(Vg_UserMsg, "<pid>%d</pid>", VG_(getpid)()); |
| 825 | VG_(message)(Vg_UserMsg, "<ppid>%d</ppid>", VG_(getppid)()); |
sewardj | 97f7e0c | 2005-07-19 15:00:25 +0000 | [diff] [blame] | 826 | VG_(message)(Vg_UserMsg, "<tool>%t</tool>", toolname); |
sewardj | ad31116 | 2005-07-19 11:25:02 +0000 | [diff] [blame] | 827 | if (VG_(clo_log_file_qualifier)) { |
| 828 | HChar* val = VG_(getenv)(VG_(clo_log_file_qualifier)); |
sewardj | 97f7e0c | 2005-07-19 15:00:25 +0000 | [diff] [blame] | 829 | VG_(message)(Vg_UserMsg, "<logfilequalifier> <var>%t</var> " |
| 830 | "<value>%t</value> </logfilequalifier>", |
sewardj | ad31116 | 2005-07-19 11:25:02 +0000 | [diff] [blame] | 831 | VG_(clo_log_file_qualifier), |
| 832 | val ? val : ""); |
| 833 | } |
sewardj | 768db0e | 2005-07-19 14:18:56 +0000 | [diff] [blame] | 834 | if (VG_(clo_xml_user_comment)) { |
| 835 | /* Note: the user comment itself is XML and is therefore to |
| 836 | be passed through verbatim (%s) rather than escaped |
| 837 | (%t). */ |
| 838 | VG_(message)(Vg_UserMsg, "<usercomment>%s</usercomment>", |
| 839 | VG_(clo_xml_user_comment)); |
| 840 | } |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 841 | VG_(message)(Vg_UserMsg, ""); |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 842 | VG_(message)(Vg_UserMsg, "<args>"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 843 | |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 844 | VG_(message)(Vg_UserMsg, " <vargv>"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 845 | if (VG_(name_of_launcher)) |
| 846 | VG_(message)(Vg_UserMsg, " <exe>%t</exe>", |
| 847 | VG_(name_of_launcher)); |
sewardj | 125fd4f | 2007-03-08 19:56:14 +0000 | [diff] [blame^] | 848 | else |
| 849 | VG_(message)(Vg_UserMsg, " <exe>%t</exe>", |
| 850 | "(launcher name unknown)"); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 851 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 852 | VG_(message)(Vg_UserMsg, |
| 853 | " <arg>%t</arg>", |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 854 | * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i )); |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 855 | } |
| 856 | VG_(message)(Vg_UserMsg, " </vargv>"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 857 | |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 858 | VG_(message)(Vg_UserMsg, " <argv>"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 859 | if (VG_(args_the_exename)) |
| 860 | VG_(message)(Vg_UserMsg, " <exe>%t</exe>", |
| 861 | VG_(args_the_exename)); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 862 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) { |
| 863 | VG_(message)(Vg_UserMsg, |
| 864 | " <arg>%t</arg>", |
| 865 | * (HChar**) VG_(indexXA)( VG_(args_for_client), i )); |
sewardj | 8665d8e | 2005-06-01 17:35:23 +0000 | [diff] [blame] | 866 | } |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 867 | VG_(message)(Vg_UserMsg, " </argv>"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 868 | |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 869 | VG_(message)(Vg_UserMsg, "</args>"); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 870 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 871 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 872 | // Empty line after the preamble |
| 873 | if (VG_(clo_verbosity) > 0) |
| 874 | VG_(message)(Vg_UserMsg, ""); |
| 875 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 876 | if (VG_(clo_verbosity) > 1) { |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 877 | SysRes fd; |
sewardj | 1f0bbc7 | 2005-11-16 03:51:02 +0000 | [diff] [blame] | 878 | VexArch vex_arch; |
| 879 | VexArchInfo vex_archinfo; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 880 | if (!logging_to_fd) |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 881 | VG_(message)(Vg_DebugMsg, ""); |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 882 | VG_(message)(Vg_DebugMsg, "Command line"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 883 | if (VG_(args_the_exename)) |
| 884 | VG_(message)(Vg_DebugMsg, " %s", VG_(args_the_exename)); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 885 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) |
| 886 | VG_(message)(Vg_DebugMsg, |
| 887 | " %s", |
| 888 | * (HChar**) VG_(indexXA)( VG_(args_for_client), i )); |
njn | 86dc2bc | 2003-09-09 07:26:21 +0000 | [diff] [blame] | 889 | |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 890 | VG_(message)(Vg_DebugMsg, "Startup, with flags:"); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 891 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) { |
| 892 | VG_(message)(Vg_DebugMsg, |
| 893 | " %s", |
| 894 | * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i )); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 895 | } |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 896 | |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 897 | VG_(message)(Vg_DebugMsg, "Contents of /proc/version:"); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 898 | fd = VG_(open) ( "/proc/version", VKI_O_RDONLY, 0 ); |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 899 | if (fd.isError) { |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 900 | VG_(message)(Vg_DebugMsg, " can't open /proc/version"); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 901 | } else { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 902 | # define BUF_LEN 256 |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 903 | Char version_buf[BUF_LEN]; |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 904 | Int n = VG_(read) ( fd.res, version_buf, BUF_LEN ); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 905 | vg_assert(n <= BUF_LEN); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 906 | if (n > 0) { |
| 907 | version_buf[n-1] = '\0'; |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 908 | VG_(message)(Vg_DebugMsg, " %s", version_buf); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 909 | } else { |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 910 | VG_(message)(Vg_DebugMsg, " (empty?)"); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 911 | } |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 912 | VG_(close)(fd.res); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 913 | # undef BUF_LEN |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 914 | } |
sewardj | 1f0bbc7 | 2005-11-16 03:51:02 +0000 | [diff] [blame] | 915 | |
| 916 | VG_(machine_get_VexArchInfo)( &vex_arch, &vex_archinfo ); |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 917 | VG_(message)( |
| 918 | Vg_DebugMsg, |
| 919 | "Arch and hwcaps: %s, %s", |
| 920 | LibVEX_ppVexArch ( vex_arch ), |
| 921 | LibVEX_ppVexHwCaps ( vex_arch, vex_archinfo.hwcaps ) |
| 922 | ); |
sewardj | e66f2e0 | 2006-12-30 17:45:08 +0000 | [diff] [blame] | 923 | VG_(message)( |
| 924 | Vg_DebugMsg, |
sewardj | 0785468 | 2006-12-30 17:56:32 +0000 | [diff] [blame] | 925 | "Page sizes: currently %d, max supported %d", |
sewardj | e66f2e0 | 2006-12-30 17:45:08 +0000 | [diff] [blame] | 926 | (Int)VKI_PAGE_SIZE, (Int)VKI_MAX_PAGE_SIZE |
| 927 | ); |
sewardj | 1f0bbc7 | 2005-11-16 03:51:02 +0000 | [diff] [blame] | 928 | VG_(message)(Vg_DebugMsg, "Valgrind library directory: %s", VG_(libdir)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 929 | } |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 930 | } |
| 931 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 932 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 933 | /*====================================================================*/ |
| 934 | /*=== File descriptor setup ===*/ |
| 935 | /*====================================================================*/ |
| 936 | |
sewardj | 5f229e2 | 2005-09-28 01:36:01 +0000 | [diff] [blame] | 937 | /* Number of file descriptors that Valgrind tries to reserve for |
| 938 | it's own use - just a small constant. */ |
| 939 | #define N_RESERVED_FDS (10) |
| 940 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 941 | static void setup_file_descriptors(void) |
| 942 | { |
| 943 | struct vki_rlimit rl; |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 944 | Bool show = False; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 945 | |
| 946 | /* Get the current file descriptor limits. */ |
| 947 | if (VG_(getrlimit)(VKI_RLIMIT_NOFILE, &rl) < 0) { |
| 948 | rl.rlim_cur = 1024; |
| 949 | rl.rlim_max = 1024; |
| 950 | } |
| 951 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 952 | if (show) |
| 953 | VG_(printf)("fd limits: host, before: cur %u max %u\n", |
| 954 | rl.rlim_cur, rl.rlim_max); |
| 955 | |
| 956 | # if defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 957 | /* I don't know why this kludge is needed; however if rl.rlim_cur |
| 958 | is RLIM_INFINITY, then VG_(safe_fd)'s attempts using VG_(fcntl) |
| 959 | to lift V's file descriptors above the threshold RLIM_INFINITY - |
| 960 | N_RESERVED_FDS fail. So just use a relatively conservative |
| 961 | value in this case. */ |
| 962 | if (rl.rlim_cur > 1024) |
| 963 | rl.rlim_cur = 1024; |
| 964 | # endif |
| 965 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 966 | /* Work out where to move the soft limit to. */ |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 967 | if (rl.rlim_cur + N_RESERVED_FDS <= rl.rlim_max) { |
| 968 | rl.rlim_cur = rl.rlim_cur + N_RESERVED_FDS; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 969 | } else { |
| 970 | rl.rlim_cur = rl.rlim_max; |
| 971 | } |
| 972 | |
| 973 | /* Reserve some file descriptors for our use. */ |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 974 | VG_(fd_soft_limit) = rl.rlim_cur - N_RESERVED_FDS; |
| 975 | VG_(fd_hard_limit) = rl.rlim_cur - N_RESERVED_FDS; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 976 | |
| 977 | /* Update the soft limit. */ |
| 978 | VG_(setrlimit)(VKI_RLIMIT_NOFILE, &rl); |
| 979 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 980 | if (show) { |
| 981 | VG_(printf)("fd limits: host, after: cur %u max %u\n", |
| 982 | rl.rlim_cur, rl.rlim_max); |
| 983 | VG_(printf)("fd limits: guest : cur %u max %u\n", |
| 984 | VG_(fd_soft_limit), VG_(fd_hard_limit)); |
| 985 | } |
| 986 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 987 | if (VG_(cl_exec_fd) != -1) |
| 988 | VG_(cl_exec_fd) = VG_(safe_fd)( VG_(cl_exec_fd) ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 989 | } |
| 990 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 991 | |
njn | 2da7335 | 2005-06-18 01:35:16 +0000 | [diff] [blame] | 992 | /*====================================================================*/ |
njn | 2025cf9 | 2005-06-26 20:44:48 +0000 | [diff] [blame] | 993 | /*=== BB profiling ===*/ |
| 994 | /*====================================================================*/ |
| 995 | |
| 996 | static |
| 997 | void show_BB_profile ( BBProfEntry tops[], UInt n_tops, ULong score_total ) |
| 998 | { |
| 999 | ULong score_cumul, score_here; |
| 1000 | Char buf_cumul[10], buf_here[10]; |
| 1001 | Char name[64]; |
| 1002 | Int r; |
| 1003 | |
| 1004 | VG_(printf)("\n"); |
| 1005 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1006 | VG_(printf)("--- BEGIN BB Profile (summary of scores) ---\n"); |
| 1007 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1008 | VG_(printf)("\n"); |
| 1009 | |
| 1010 | VG_(printf)("Total score = %lld\n\n", score_total); |
| 1011 | |
| 1012 | score_cumul = 0; |
| 1013 | for (r = 0; r < n_tops; r++) { |
| 1014 | if (tops[r].addr == 0) |
| 1015 | continue; |
| 1016 | name[0] = 0; |
| 1017 | VG_(get_fnname_w_offset)(tops[r].addr, name, 64); |
| 1018 | name[63] = 0; |
| 1019 | score_here = tops[r].score; |
| 1020 | score_cumul += score_here; |
| 1021 | VG_(percentify)(score_cumul, score_total, 2, 6, buf_cumul); |
| 1022 | VG_(percentify)(score_here, score_total, 2, 6, buf_here); |
| 1023 | VG_(printf)("%3d: (%9lld %s) %9lld %s 0x%llx %s\n", |
| 1024 | r, |
| 1025 | score_cumul, buf_cumul, |
| 1026 | score_here, buf_here, tops[r].addr, name ); |
| 1027 | } |
| 1028 | |
| 1029 | VG_(printf)("\n"); |
| 1030 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1031 | VG_(printf)("--- BB Profile (BB details) ---\n"); |
| 1032 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1033 | VG_(printf)("\n"); |
| 1034 | |
| 1035 | score_cumul = 0; |
| 1036 | for (r = 0; r < n_tops; r++) { |
| 1037 | if (tops[r].addr == 0) |
| 1038 | continue; |
| 1039 | name[0] = 0; |
| 1040 | VG_(get_fnname_w_offset)(tops[r].addr, name, 64); |
| 1041 | name[63] = 0; |
| 1042 | score_here = tops[r].score; |
| 1043 | score_cumul += score_here; |
| 1044 | VG_(percentify)(score_cumul, score_total, 2, 6, buf_cumul); |
| 1045 | VG_(percentify)(score_here, score_total, 2, 6, buf_here); |
| 1046 | VG_(printf)("\n"); |
| 1047 | VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= begin BB rank %d " |
| 1048 | "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r); |
| 1049 | VG_(printf)("%3d: (%9lld %s) %9lld %s 0x%llx %s\n", |
| 1050 | r, |
| 1051 | score_cumul, buf_cumul, |
| 1052 | score_here, buf_here, tops[r].addr, name ); |
| 1053 | VG_(printf)("\n"); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1054 | VG_(translate)(0, tops[r].addr, True, VG_(clo_profile_flags), 0, True); |
njn | 2025cf9 | 2005-06-26 20:44:48 +0000 | [diff] [blame] | 1055 | VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= end BB rank %d " |
| 1056 | "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r); |
| 1057 | } |
| 1058 | |
| 1059 | VG_(printf)("\n"); |
| 1060 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1061 | VG_(printf)("--- END BB Profile ---\n"); |
| 1062 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1063 | VG_(printf)("\n"); |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | /*====================================================================*/ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1068 | /*=== main() ===*/ |
| 1069 | /*====================================================================*/ |
| 1070 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1071 | /* When main() is entered, we should be on the following stack, not |
| 1072 | the one the kernel gave us. We will run on this stack until |
| 1073 | simulation of the root thread is started, at which point a transfer |
| 1074 | is made to a dynamically allocated stack. This is for the sake of |
| 1075 | uniform overflow detection for all Valgrind threads. This is |
| 1076 | marked global even though it isn't, because assembly code below |
| 1077 | needs to reference the name. */ |
| 1078 | |
| 1079 | /*static*/ VgStack VG_(interim_stack); |
| 1080 | |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1081 | /* These are the structures used to hold info for creating the initial |
| 1082 | client image. |
| 1083 | |
| 1084 | 'iicii' mostly holds important register state present at system |
| 1085 | startup (_start_valgrind). valgrind_main() then fills in the rest |
| 1086 | of it and passes it to VG_(ii_create_image)(). That produces |
| 1087 | 'iifii', which is later handed to VG_(ii_finalise_image). */ |
| 1088 | |
| 1089 | /* In all OS-instantiations, the_iicii has a field .sp_at_startup. |
| 1090 | This should get some address inside the stack on which we gained |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1091 | control (eg, it could be the SP at startup). It doesn't matter |
| 1092 | exactly where in the stack it is. This value is passed to the |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1093 | address space manager at startup. On Linux, aspacem then uses it |
| 1094 | to identify the initial stack segment and hence the upper end of |
| 1095 | the usable address space. */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1096 | |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1097 | static IICreateImageInfo the_iicii; |
| 1098 | static IIFinaliseImageInfo the_iifii; |
| 1099 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1100 | |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 1101 | /* --- Forwards decls to do with shutdown --- */ |
| 1102 | |
| 1103 | static void final_tidyup(ThreadId tid); |
| 1104 | |
| 1105 | /* Do everything which needs doing when the last thread exits */ |
| 1106 | static |
| 1107 | void shutdown_actions_NORETURN( ThreadId tid, |
| 1108 | VgSchedReturnCode tids_schedretcode ); |
| 1109 | |
| 1110 | /* --- end of Forwards decls to do with shutdown --- */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1111 | |
| 1112 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1113 | /* TODO: GIVE THIS A PROPER HOME |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1114 | TODO: MERGE THIS WITH DUPLICATE IN mc_leakcheck.c and coredump-elf.c. |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1115 | Extract from aspacem a vector of the current segment start |
| 1116 | addresses. The vector is dynamically allocated and should be freed |
| 1117 | by the caller when done. REQUIRES m_mallocfree to be running. |
| 1118 | Writes the number of addresses required into *n_acquired. */ |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 1119 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1120 | static Addr* get_seg_starts ( /*OUT*/Int* n_acquired ) |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1121 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1122 | Addr* starts; |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 1123 | Int n_starts, r = 0; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1124 | |
| 1125 | n_starts = 1; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1126 | while (True) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1127 | starts = VG_(malloc)( n_starts * sizeof(Addr) ); |
| 1128 | if (starts == NULL) |
| 1129 | break; |
| 1130 | r = VG_(am_get_segment_starts)( starts, n_starts ); |
| 1131 | if (r >= 0) |
| 1132 | break; |
| 1133 | VG_(free)(starts); |
| 1134 | n_starts *= 2; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1135 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1136 | |
| 1137 | if (starts == NULL) { |
| 1138 | *n_acquired = 0; |
| 1139 | return NULL; |
| 1140 | } |
| 1141 | |
| 1142 | *n_acquired = r; |
| 1143 | return starts; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1147 | /* By the time we get to valgrind_main, the_iicii should already have |
| 1148 | been filled in with any important details as required by whatever |
| 1149 | OS we have been built for. |
| 1150 | */ |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1151 | static |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1152 | Int valgrind_main ( Int argc, HChar **argv, HChar **envp ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1153 | { |
sewardj | 13247ca | 2005-12-30 22:52:20 +0000 | [diff] [blame] | 1154 | HChar* toolname = "memcheck"; // default to Memcheck |
sewardj | 13247ca | 2005-12-30 22:52:20 +0000 | [diff] [blame] | 1155 | Int need_help = 0; // 0 = no, 1 = --help, 2 = --help-debug |
sewardj | 4c3faae | 2006-03-15 11:50:32 +0000 | [diff] [blame] | 1156 | UInt* client_auxv = NULL; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1157 | Int loglevel, i; |
| 1158 | Bool logging_to_fd; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1159 | struct vki_rlimit zero = { 0, 0 }; |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1160 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1161 | //============================================================ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1162 | // |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1163 | // Nb: startup is complex. Prerequisites are shown at every step. |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1164 | // *** Be very careful when messing with the order *** |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1165 | // |
| 1166 | // The first order of business is to get debug logging, the address |
| 1167 | // space manager and the dynamic memory manager up and running. |
| 1168 | // Once that's done, we can relax a bit. |
| 1169 | // |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1170 | //============================================================ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1171 | |
| 1172 | /* This is needed to make VG_(getenv) usable early. */ |
| 1173 | VG_(client_envp) = (Char**)envp; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1174 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1175 | //-------------------------------------------------------------- |
| 1176 | // Start up the logging mechanism |
| 1177 | // p: none |
| 1178 | //-------------------------------------------------------------- |
| 1179 | /* Start the debugging-log system ASAP. First find out how many |
| 1180 | "-d"s were specified. This is a pre-scan of the command line. */ |
| 1181 | loglevel = 0; |
| 1182 | for (i = 1; i < argc; i++) { |
sewardj | 1075931 | 2005-05-30 23:52:47 +0000 | [diff] [blame] | 1183 | if (argv[i][0] != '-') |
| 1184 | break; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1185 | if (VG_STREQ(argv[i], "--")) |
sewardj | 1075931 | 2005-05-30 23:52:47 +0000 | [diff] [blame] | 1186 | break; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1187 | if (VG_STREQ(argv[i], "-d")) |
sewardj | 1075931 | 2005-05-30 23:52:47 +0000 | [diff] [blame] | 1188 | loglevel++; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | /* ... and start the debug logger. Now we can safely emit logging |
| 1192 | messages all through startup. */ |
sewardj | 1075931 | 2005-05-30 23:52:47 +0000 | [diff] [blame] | 1193 | VG_(debugLog_startup)(loglevel, "Stage 2 (main)"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1194 | VG_(debugLog)(1, "main", "Welcome to Valgrind version " |
| 1195 | VERSION " debug logging\n"); |
| 1196 | |
| 1197 | //-------------------------------------------------------------- |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1198 | // AIX5 only: register the system call numbers |
| 1199 | // p: logging |
| 1200 | // p: that the initial few syscall numbers stated in the |
| 1201 | // bootblock have been installed (else we can't |
| 1202 | // open/read/close). |
| 1203 | //-------------------------------------------------------------- |
| 1204 | # if defined(VGO_aix5) |
| 1205 | VG_(debugLog)(1, "main", "aix5: registering syscalls ..\n"); |
| 1206 | { UChar sysent_name[50]; |
| 1207 | SysRes fd; |
| 1208 | Bool ok; |
| 1209 | Int n_unregd, sysent_used = 0; |
| 1210 | prsysent_t* sysent_hdr; |
| 1211 | |
| 1212 | VG_(sprintf)(sysent_name, "/proc/%d/sysent", VG_(getpid)()); |
| 1213 | fd = VG_(open)(sysent_name, VKI_O_RDONLY, 0); |
| 1214 | if (fd.isError) |
| 1215 | VG_(err_config_error)("aix5: can't open /proc/<pid>/sysent"); |
| 1216 | |
| 1217 | sysent_used = VG_(read)(fd.res, aix5_sysent_buf, VG_AIX5_SYSENT_SIZE); |
| 1218 | if (sysent_used < 0) |
| 1219 | VG_(err_config_error)("aix5: error reading /proc/<pid>/sysent"); |
| 1220 | if (sysent_used >= VG_AIX5_SYSENT_SIZE) |
| 1221 | VG_(err_config_error)("aix5: VG_AIX5_SYSENT_SIZE is too low; " |
| 1222 | "increase and recompile"); |
| 1223 | VG_(close)(fd.res); |
| 1224 | |
| 1225 | vg_assert(sysent_used > 0 && sysent_used < VG_AIX5_SYSENT_SIZE); |
| 1226 | |
| 1227 | sysent_hdr = (prsysent_t*)&aix5_sysent_buf[0]; |
| 1228 | |
| 1229 | n_unregd = 0; |
| 1230 | for (i = 0; i < sysent_hdr->pr_nsyscalls; i++) { |
| 1231 | UChar* name = &aix5_sysent_buf[ sysent_hdr |
| 1232 | ->pr_syscall[i].pr_nameoff ]; |
| 1233 | UInt nmbr = sysent_hdr->pr_syscall[i].pr_number; |
| 1234 | VG_(debugLog)(3, "main", "aix5: bind syscall %d to \"%s\"\n", |
| 1235 | nmbr, name); |
| 1236 | ok = VG_(aix5_register_syscall)(nmbr, name); |
| 1237 | if (!ok) |
| 1238 | n_unregd++; |
| 1239 | if (!ok) |
| 1240 | VG_(debugLog)(3, "main", |
| 1241 | "aix5: bind FAILED: %d to \"%s\"\n", |
| 1242 | nmbr, name); |
| 1243 | } |
| 1244 | VG_(debugLog)(1, "main", "aix5: .. %d syscalls known, %d unknown\n", |
| 1245 | sysent_hdr->pr_nsyscalls - n_unregd, n_unregd ); |
| 1246 | VG_(debugLog)(1, "main", "aix5: __NR_AIX5_FAKE_SIGRETURN = %d\n", |
| 1247 | __NR_AIX5_FAKE_SIGRETURN ); |
| 1248 | } |
| 1249 | # endif |
| 1250 | |
| 1251 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1252 | // Ensure we're on a plausible stack. |
| 1253 | // p: logging |
| 1254 | //-------------------------------------------------------------- |
| 1255 | VG_(debugLog)(1, "main", "Checking current stack is plausible\n"); |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1256 | { HChar* limLo = (HChar*)(&VG_(interim_stack).bytes[0]); |
| 1257 | HChar* limHi = limLo + sizeof(VG_(interim_stack)); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1258 | HChar* aLocal = (HChar*)&zero; /* any auto local will do */ |
| 1259 | if (aLocal < limLo || aLocal >= limHi) { |
| 1260 | /* something's wrong. Stop. */ |
| 1261 | VG_(debugLog)(0, "main", "Root stack %p to %p, a local %p\n", |
| 1262 | limLo, limHi, aLocal ); |
| 1263 | VG_(debugLog)(0, "main", "Valgrind: FATAL: " |
| 1264 | "Initial stack switched failed.\n"); |
| 1265 | VG_(debugLog)(0, "main", " Cannot continue. Sorry.\n"); |
| 1266 | VG_(exit)(1); |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | //-------------------------------------------------------------- |
| 1271 | // Ensure we have a plausible pointer to the stack on which |
| 1272 | // we gained control (not the current stack!) |
| 1273 | // p: logging |
| 1274 | //-------------------------------------------------------------- |
| 1275 | VG_(debugLog)(1, "main", "Checking initial stack was noted\n"); |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1276 | if (the_iicii.sp_at_startup == 0) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1277 | VG_(debugLog)(0, "main", "Valgrind: FATAL: " |
| 1278 | "Initial stack was not noted.\n"); |
| 1279 | VG_(debugLog)(0, "main", " Cannot continue. Sorry.\n"); |
| 1280 | VG_(exit)(1); |
| 1281 | } |
| 1282 | |
| 1283 | //-------------------------------------------------------------- |
| 1284 | // Start up the address space manager, and determine the |
| 1285 | // approximate location of the client's stack |
| 1286 | // p: logging, plausible-stack |
| 1287 | //-------------------------------------------------------------- |
| 1288 | VG_(debugLog)(1, "main", "Starting the address space manager\n"); |
sewardj | e66f2e0 | 2006-12-30 17:45:08 +0000 | [diff] [blame] | 1289 | vg_assert(VKI_PAGE_SIZE == 4096 || VKI_PAGE_SIZE == 65536); |
| 1290 | vg_assert(VKI_MAX_PAGE_SIZE == 4096 || VKI_MAX_PAGE_SIZE == 65536); |
| 1291 | vg_assert(VKI_PAGE_SIZE <= VKI_MAX_PAGE_SIZE); |
| 1292 | vg_assert(VKI_PAGE_SIZE == (1 << VKI_PAGE_SHIFT)); |
| 1293 | vg_assert(VKI_MAX_PAGE_SIZE == (1 << VKI_MAX_PAGE_SHIFT)); |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1294 | the_iicii.clstack_top = VG_(am_startup)( the_iicii.sp_at_startup ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1295 | VG_(debugLog)(1, "main", "Address space manager is running\n"); |
| 1296 | |
| 1297 | //-------------------------------------------------------------- |
| 1298 | // Start up the dynamic memory manager |
| 1299 | // p: address space management |
| 1300 | // In fact m_mallocfree is self-initialising, so there's no |
| 1301 | // initialisation call to do. Instead, try a simple malloc/ |
| 1302 | // free pair right now to check that nothing is broken. |
| 1303 | //-------------------------------------------------------------- |
| 1304 | VG_(debugLog)(1, "main", "Starting the dynamic memory manager\n"); |
| 1305 | { void* p = VG_(malloc)( 12345 ); |
| 1306 | if (p) VG_(free)( p ); |
| 1307 | } |
| 1308 | VG_(debugLog)(1, "main", "Dynamic memory manager is running\n"); |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1309 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1310 | //============================================================ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1311 | // |
| 1312 | // Dynamic memory management is now available. |
| 1313 | // |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1314 | //============================================================ |
| 1315 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1316 | //-------------------------------------------------------------- |
| 1317 | // Look for alternative libdir |
| 1318 | { HChar *cp = VG_(getenv)(VALGRIND_LIB); |
| 1319 | if (cp != NULL) |
| 1320 | VG_(libdir) = cp; |
| 1321 | } |
| 1322 | |
| 1323 | //-------------------------------------------------------------- |
| 1324 | // Extract the launcher name from the environment. |
| 1325 | VG_(debugLog)(1, "main", "Getting stage1's name\n"); |
| 1326 | VG_(name_of_launcher) = VG_(getenv)(VALGRIND_LAUNCHER); |
| 1327 | if (VG_(name_of_launcher) == NULL) { |
| 1328 | VG_(printf)("valgrind: You cannot run '%s' directly.\n", argv[0]); |
| 1329 | VG_(printf)("valgrind: You should use $prefix/bin/valgrind.\n"); |
| 1330 | VG_(exit)(1); |
| 1331 | } |
| 1332 | |
| 1333 | //-------------------------------------------------------------- |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 1334 | // Get the current process datasize rlimit, and set it to zero. |
| 1335 | // This prevents any internal uses of brk() from having any effect. |
| 1336 | // We remember the old value so we can restore it on exec, so that |
| 1337 | // child processes will have a reasonable brk value. |
| 1338 | VG_(getrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data)); |
| 1339 | zero.rlim_max = VG_(client_rlimit_data).rlim_max; |
| 1340 | VG_(setrlimit)(VKI_RLIMIT_DATA, &zero); |
thughes | c37184f | 2004-09-11 14:16:57 +0000 | [diff] [blame] | 1341 | |
| 1342 | // Get the current process stack rlimit. |
| 1343 | VG_(getrlimit)(VKI_RLIMIT_STACK, &VG_(client_rlimit_stack)); |
| 1344 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1345 | //-------------------------------------------------------------- |
| 1346 | // Figure out what sort of CPU we're on, and whether it is |
| 1347 | // able to run V. |
| 1348 | VG_(debugLog)(1, "main", "Get hardware capabilities ...\n"); |
| 1349 | { VexArch vex_arch; |
| 1350 | VexArchInfo vex_archinfo; |
| 1351 | Bool ok = VG_(machine_get_hwcaps)(); |
| 1352 | if (!ok) { |
| 1353 | VG_(printf)("\n"); |
| 1354 | VG_(printf)("valgrind: fatal error: unsupported CPU.\n"); |
| 1355 | VG_(printf)(" Supported CPUs are:\n"); |
| 1356 | VG_(printf)(" * x86 (practically any; Pentium-I or above), " |
| 1357 | "AMD Athlon or above)\n"); |
| 1358 | VG_(printf)(" * AMD Athlon64/Opteron\n"); |
| 1359 | VG_(printf)(" * PowerPC (most; ppc405 and above)\n"); |
| 1360 | VG_(printf)("\n"); |
| 1361 | VG_(exit)(1); |
| 1362 | } |
| 1363 | VG_(machine_get_VexArchInfo)( &vex_arch, &vex_archinfo ); |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 1364 | VG_(debugLog)( |
| 1365 | 1, "main", "... arch = %s, hwcaps = %s\n", |
| 1366 | LibVEX_ppVexArch ( vex_arch ), |
| 1367 | LibVEX_ppVexHwCaps ( vex_arch, vex_archinfo.hwcaps ) |
| 1368 | ); |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1371 | //============================================================ |
| 1372 | // Command line argument handling order: |
| 1373 | // * If --help/--help-debug are present, show usage message |
| 1374 | // (including the tool-specific usage) |
| 1375 | // * (If no --tool option given, default to Memcheck) |
| 1376 | // * Then, if client is missing, abort with error msg |
| 1377 | // * Then, if any cmdline args are bad, abort with error msg |
| 1378 | //============================================================ |
| 1379 | |
| 1380 | //-------------------------------------------------------------- |
| 1381 | // Split up argv into: C args, V args, V extra args, and exename. |
| 1382 | // p: dynamic memory allocation |
| 1383 | //-------------------------------------------------------------- |
| 1384 | VG_(debugLog)(1, "main", "Split up command line\n"); |
| 1385 | VG_(split_up_argv)( argc, argv ); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1386 | vg_assert( VG_(args_for_valgrind) ); |
| 1387 | vg_assert( VG_(args_for_client) ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1388 | if (0) { |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1389 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) |
| 1390 | VG_(printf)( |
| 1391 | "varg %s\n", |
| 1392 | * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i ) |
| 1393 | ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1394 | VG_(printf)(" exe %s\n", VG_(args_the_exename)); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1395 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) |
| 1396 | VG_(printf)( |
| 1397 | "carg %s\n", |
| 1398 | * (HChar**) VG_(indexXA)( VG_(args_for_client), i ) |
| 1399 | ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1403 | // Extract tool name and whether help has been requested. |
| 1404 | // Note we can't print the help message yet, even if requested, |
| 1405 | // because the tool has not been initialised. |
| 1406 | // p: split_up_argv [for VG_(args_for_valgrind)] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1407 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 1408 | VG_(debugLog)(1, "main", "Preprocess command line opts\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1409 | get_helprequest_and_toolname(&need_help, &toolname); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1410 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1411 | // Set default vex control params |
| 1412 | LibVEX_default_VexControl(& VG_(clo_vex_control)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1413 | |
| 1414 | //-------------------------------------------------------------- |
| 1415 | // Load client executable, finding in $PATH if necessary |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1416 | // p: get_helprequest_and_toolname() [for 'exec', 'need_help'] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1417 | // p: layout_remaining_space [so there's space] |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1418 | // |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1419 | // Set up client's environment |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1420 | // p: set-libdir [for VG_(libdir)] |
| 1421 | // p: get_helprequest_and_toolname [for toolname] |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1422 | // |
nethercote | 5ee67ca | 2004-06-22 14:00:09 +0000 | [diff] [blame] | 1423 | // Setup client stack, eip, and VG_(client_arg[cv]) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1424 | // p: load_client() [for 'info'] |
| 1425 | // p: fix_environment() [for 'env'] |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1426 | // |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1427 | // Setup client data (brk) segment. Initially a 1-page segment |
| 1428 | // which abuts a shrinkable reservation. |
| 1429 | // p: load_client() [for 'info' and hence VG_(brk_base)] |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1430 | // |
| 1431 | // p: _start_in_C (for zeroing out the_iicii and putting some |
| 1432 | // initial values into it) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1433 | //-------------------------------------------------------------- |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1434 | if (!need_help) { |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1435 | VG_(debugLog)(1, "main", "Create initial image\n"); |
| 1436 | |
| 1437 | # if defined(VGO_linux) |
| 1438 | the_iicii.argv = argv; |
| 1439 | the_iicii.envp = envp; |
| 1440 | the_iicii.toolname = toolname; |
| 1441 | # elif defined(VGO_aix5) |
| 1442 | /* the_iicii.intregs37 already set up */ |
| 1443 | /* the_iicii.bootblock already set up */ |
| 1444 | /* the_iicii.adler32_exp already set up */ |
| 1445 | /* the_iicii.sp_at_startup is irrelevant */ |
| 1446 | /* the_iicii.clstack_top is irrelevant */ |
| 1447 | the_iicii.toolname = toolname; |
| 1448 | # else |
| 1449 | # error "Uknown platform" |
| 1450 | # endif |
| 1451 | |
| 1452 | the_iifii = VG_(ii_create_image)( the_iicii ); |
| 1453 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1454 | # if defined(VGO_aix5) |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1455 | /* Tell aspacem where the initial client stack is, so that it |
| 1456 | can later produce a faked-up NSegment in response to |
| 1457 | VG_(am_find_nsegment) for that address range, if asked. */ |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1458 | VG_(am_aix5_set_initial_client_sp)( the_iifii.initial_client_SP ); |
| 1459 | /* Now have a look at said fake segment, so we can find out |
| 1460 | the size of it. */ |
| 1461 | { SizeT sz; |
| 1462 | NSegment const* seg |
| 1463 | = VG_(am_find_nsegment)( the_iifii.initial_client_SP ); |
| 1464 | vg_assert(seg); |
| 1465 | sz = seg->end - seg->start + 1; |
| 1466 | vg_assert(sz >= 0 && sz <= 64*1024*1024); /* stay sane */ |
| 1467 | the_iifii.clstack_max_size = sz; |
| 1468 | } |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1469 | # endif |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1470 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1471 | |
| 1472 | //============================================================== |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1473 | // |
| 1474 | // Finished loading/setting up the client address space. |
| 1475 | // |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1476 | //============================================================== |
| 1477 | |
| 1478 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1479 | // setup file descriptors |
| 1480 | // p: n/a |
| 1481 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 1482 | VG_(debugLog)(1, "main", "Setup file descriptors\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1483 | setup_file_descriptors(); |
| 1484 | |
| 1485 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1486 | // create the fake /proc/<pid>/cmdline file and then unlink it, |
| 1487 | // but hold onto the fd, so we can hand it out to the client |
| 1488 | // when it tries to open /proc/<pid>/cmdline for itself. |
| 1489 | // p: setup file descriptors |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 1490 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1491 | if (!need_help) { |
| 1492 | HChar buf[50], buf2[50+64]; |
| 1493 | HChar nul[1]; |
| 1494 | Int fd, r; |
| 1495 | HChar* exename; |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 1496 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1497 | VG_(debugLog)(1, "main", "Create fake /proc/<pid>/cmdline\n"); |
| 1498 | |
| 1499 | VG_(sprintf)(buf, "proc_%d_cmdline", VG_(getpid)()); |
| 1500 | fd = VG_(mkstemp)( buf, buf2 ); |
| 1501 | if (fd == -1) |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1502 | VG_(err_config_error)("Can't create client cmdline file in /tmp."); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1503 | |
| 1504 | nul[0] = 0; |
| 1505 | exename = VG_(args_the_exename) ? VG_(args_the_exename) |
| 1506 | : "unknown_exename"; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1507 | VG_(write)(fd, VG_(args_the_exename), |
| 1508 | VG_(strlen)( VG_(args_the_exename) )); |
| 1509 | VG_(write)(fd, nul, 1); |
| 1510 | |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1511 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) { |
| 1512 | HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i ); |
| 1513 | VG_(write)(fd, arg, VG_(strlen)( arg )); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1514 | VG_(write)(fd, nul, 1); |
| 1515 | } |
| 1516 | |
| 1517 | /* Don't bother to seek the file back to the start; instead do |
| 1518 | it every time a copy of it is given out (by PRE(sys_open)). |
| 1519 | That is probably more robust across fork() etc. */ |
| 1520 | |
| 1521 | /* Now delete it, but hang on to the fd. */ |
| 1522 | r = VG_(unlink)( buf2 ); |
| 1523 | if (r) |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1524 | VG_(err_config_error)("Can't delete client cmdline file in /tmp."); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1525 | |
| 1526 | VG_(cl_cmdline_fd) = fd; |
| 1527 | } |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 1528 | |
| 1529 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1530 | // Init tool part 1: pre_clo_init |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 1531 | // p: setup_client_stack() [for 'VG_(client_arg[cv]'] |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 1532 | // p: setup_file_descriptors() [for 'VG_(fd_xxx_limit)'] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1533 | //-------------------------------------------------------------- |
njn | 64c8324 | 2005-06-21 01:54:38 +0000 | [diff] [blame] | 1534 | { |
| 1535 | Char* s; |
| 1536 | Bool ok; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1537 | VG_(debugLog)(1, "main", "Initialise the tool part 1 (pre_clo_init)\n"); |
| 1538 | (VG_(tool_info).tl_pre_clo_init)(); |
| 1539 | ok = VG_(sanity_check_needs)( &s ); |
njn | 64c8324 | 2005-06-21 01:54:38 +0000 | [diff] [blame] | 1540 | if (!ok) { |
| 1541 | VG_(tool_panic)(s); |
| 1542 | } |
| 1543 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1544 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1545 | //-------------------------------------------------------------- |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1546 | // If --tool and --help/--help-debug was given, now give the core+tool |
| 1547 | // help message |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1548 | // p: get_helprequest_and_toolname() [for 'need_help'] |
| 1549 | // p: tl_pre_clo_init [for 'VG_(tdict).usage'] |
| 1550 | //-------------------------------------------------------------- |
| 1551 | VG_(debugLog)(1, "main", "Print help and quit, if requested\n"); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1552 | if (need_help) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1553 | usage_NORETURN(/*--help-debug?*/2 == need_help); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1554 | } |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 1555 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1556 | //-------------------------------------------------------------- |
| 1557 | // Process command line options to Valgrind + tool |
| 1558 | // p: setup_client_stack() [for 'VG_(client_arg[cv]'] |
| 1559 | // p: setup_file_descriptors() [for 'VG_(fd_xxx_limit)'] |
| 1560 | //-------------------------------------------------------------- |
| 1561 | VG_(debugLog)(1, "main", "Process Valgrind's command line options, " |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 1562 | "setup logging\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1563 | logging_to_fd = process_cmd_line_options(client_auxv, toolname); |
| 1564 | |
| 1565 | //-------------------------------------------------------------- |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 1566 | // Zeroise the millisecond counter by doing a first read of it. |
| 1567 | // p: none |
| 1568 | //-------------------------------------------------------------- |
| 1569 | (void) VG_(read_millisecond_timer)(); |
| 1570 | |
| 1571 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1572 | // Print the preamble |
| 1573 | // p: tl_pre_clo_init [for 'VG_(details).name' and friends] |
| 1574 | // p: process_cmd_line_options() [for VG_(clo_verbosity), VG_(clo_xml), |
| 1575 | // VG_(clo_log_file_qualifier), |
| 1576 | // logging_to_fd] |
| 1577 | //-------------------------------------------------------------- |
| 1578 | VG_(debugLog)(1, "main", "Print the preamble...\n"); |
| 1579 | print_preamble(logging_to_fd, toolname); |
| 1580 | VG_(debugLog)(1, "main", "...finished the preamble\n"); |
| 1581 | |
| 1582 | //-------------------------------------------------------------- |
| 1583 | // Init tool part 2: post_clo_init |
| 1584 | // p: setup_client_stack() [for 'VG_(client_arg[cv]'] |
| 1585 | // p: setup_file_descriptors() [for 'VG_(fd_xxx_limit)'] |
| 1586 | // p: print_preamble() [so any warnings printed in post_clo_init |
| 1587 | // are shown after the preamble] |
| 1588 | //-------------------------------------------------------------- |
| 1589 | VG_(debugLog)(1, "main", "Initialise the tool part 2 (post_clo_init)\n"); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1590 | VG_TDICT_CALL(tool_post_clo_init); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1591 | |
| 1592 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1593 | // Initialise translation table and translation cache |
| 1594 | // p: aspacem [??] |
| 1595 | // p: tl_pre_clo_init [for 'VG_(details).avg_translation_sizeB'] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1596 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1597 | VG_(debugLog)(1, "main", "Initialise TT/TC\n"); |
| 1598 | VG_(init_tt_tc)(); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1599 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1600 | //-------------------------------------------------------------- |
| 1601 | // Initialise the redirect table. |
| 1602 | // p: init_tt_tc [so it can call VG_(search_transtab) safely] |
| 1603 | // p: aspacem [so can change ownership of sysinfo pages] |
| 1604 | //-------------------------------------------------------------- |
| 1605 | VG_(debugLog)(1, "main", "Initialise redirects\n"); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 1606 | VG_(redir_initialise)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1607 | |
| 1608 | //-------------------------------------------------------------- |
| 1609 | // Allow GDB attach |
| 1610 | // p: process_cmd_line_options() [for VG_(clo_wait_for_gdb)] |
| 1611 | //-------------------------------------------------------------- |
| 1612 | /* Hook to delay things long enough so we can get the pid and |
| 1613 | attach GDB in another shell. */ |
| 1614 | if (VG_(clo_wait_for_gdb)) { |
sewardj | 95611ff | 2007-02-16 13:57:07 +0000 | [diff] [blame] | 1615 | Long iters; |
| 1616 | volatile Long q; |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 1617 | VG_(debugLog)(1, "main", "Wait for GDB\n"); |
sewardj | 93ab857 | 2005-02-06 14:10:40 +0000 | [diff] [blame] | 1618 | VG_(printf)("pid=%d, entering delay loop\n", VG_(getpid)()); |
sewardj | 8211a57 | 2005-06-23 21:37:47 +0000 | [diff] [blame] | 1619 | |
| 1620 | # if defined(VGP_x86_linux) |
| 1621 | iters = 5; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 1622 | # elif defined(VGP_amd64_linux) || defined(VGP_ppc64_linux) |
sewardj | 8211a57 | 2005-06-23 21:37:47 +0000 | [diff] [blame] | 1623 | iters = 10; |
| 1624 | # elif defined(VGP_ppc32_linux) |
sewardj | d714d2e | 2005-07-08 18:24:04 +0000 | [diff] [blame] | 1625 | iters = 5; |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1626 | # elif defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 1627 | iters = 4; |
sewardj | 8211a57 | 2005-06-23 21:37:47 +0000 | [diff] [blame] | 1628 | # else |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1629 | # error "Unknown plat" |
sewardj | 8211a57 | 2005-06-23 21:37:47 +0000 | [diff] [blame] | 1630 | # endif |
| 1631 | |
| 1632 | iters *= 1000*1000*1000; |
| 1633 | for (q = 0; q < iters; q++) |
| 1634 | ; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
sewardj | b5d320c | 2005-03-13 18:57:15 +0000 | [diff] [blame] | 1637 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1638 | // Search for file descriptors that are inherited from our parent |
| 1639 | // p: process_cmd_line_options [for VG_(clo_track_fds)] |
| 1640 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 1641 | if (VG_(clo_track_fds)) { |
| 1642 | VG_(debugLog)(1, "main", "Init preopened fds\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1643 | VG_(init_preopened_fds)(); |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 1644 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1645 | |
| 1646 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1647 | // Load debug info for the existing segments. |
| 1648 | // p: setup_code_redirect_table [so that redirs can be recorded] |
| 1649 | // p: mallocfree |
| 1650 | // p: probably: setup fds and process CLOs, so that logging works |
| 1651 | //-------------------------------------------------------------- |
| 1652 | VG_(debugLog)(1, "main", "Load initial debug info\n"); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1653 | # if defined(VGO_linux) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1654 | { Addr* seg_starts; |
| 1655 | Int n_seg_starts; |
| 1656 | |
| 1657 | seg_starts = get_seg_starts( &n_seg_starts ); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1658 | vg_assert(seg_starts && n_seg_starts >= 0); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1659 | |
sewardj | f72cced | 2005-11-08 00:45:47 +0000 | [diff] [blame] | 1660 | /* show them all to the debug info reader. allow_SkFileV has to |
| 1661 | be True here so that we read info from the valgrind executable |
| 1662 | itself. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1663 | for (i = 0; i < n_seg_starts; i++) |
sewardj | f72cced | 2005-11-08 00:45:47 +0000 | [diff] [blame] | 1664 | VG_(di_notify_mmap)( seg_starts[i], True/*allow_SkFileV*/ ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1665 | |
| 1666 | VG_(free)( seg_starts ); |
| 1667 | } |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1668 | # elif defined(VGO_aix5) |
| 1669 | { AixCodeSegChange* changes; |
| 1670 | Int changes_size, changes_used; |
| 1671 | |
| 1672 | /* Find out how many AixCodeSegChange records we will need, |
| 1673 | and acquire them. */ |
| 1674 | changes_size = VG_(am_aix5_reread_procmap_howmany_directives)(); |
| 1675 | changes = VG_(malloc)(changes_size * sizeof(AixCodeSegChange)); |
| 1676 | vg_assert(changes); |
| 1677 | |
| 1678 | /* Now re-read /proc/<pid>/map and acquire a change set */ |
| 1679 | VG_(am_aix5_reread_procmap)( changes, &changes_used ); |
| 1680 | vg_assert(changes_used >= 0 && changes_used <= changes_size); |
| 1681 | |
| 1682 | /* And notify m_debuginfo of the changes. */ |
| 1683 | for (i = 0; i < changes_used; i++) |
| 1684 | VG_(di_aix5_notify_segchange)( |
| 1685 | changes[i].code_start, |
| 1686 | changes[i].code_len, |
| 1687 | changes[i].data_start, |
| 1688 | changes[i].data_len, |
| 1689 | changes[i].file_name, |
| 1690 | changes[i].mem_name, |
| 1691 | changes[i].is_mainexe, |
| 1692 | changes[i].acquire |
| 1693 | ); |
| 1694 | |
| 1695 | VG_(free)(changes); |
| 1696 | } |
| 1697 | # else |
| 1698 | # error Unknown OS |
| 1699 | # endif |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1700 | |
| 1701 | //-------------------------------------------------------------- |
| 1702 | // Tell aspacem of ownership change of the asm helpers, so that |
| 1703 | // m_translate allows them to be translated. However, only do this |
| 1704 | // after the initial debug info read, since making a hole in the |
| 1705 | // address range for the stage2 binary confuses the debug info reader. |
| 1706 | // p: aspacem |
| 1707 | //-------------------------------------------------------------- |
| 1708 | { Bool change_ownership_v_c_OK; |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 1709 | Addr co_start = VG_PGROUNDDN( (Addr)&VG_(trampoline_stuff_start) ); |
| 1710 | Addr co_endPlus = VG_PGROUNDUP( (Addr)&VG_(trampoline_stuff_end) ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1711 | VG_(debugLog)(1,"redir", |
| 1712 | "transfer ownership V -> C of 0x%llx .. 0x%llx\n", |
| 1713 | (ULong)co_start, (ULong)co_endPlus-1 ); |
| 1714 | |
| 1715 | change_ownership_v_c_OK |
| 1716 | = VG_(am_change_ownership_v_to_c)( co_start, co_endPlus - co_start ); |
| 1717 | vg_assert(change_ownership_v_c_OK); |
| 1718 | } |
| 1719 | |
| 1720 | //-------------------------------------------------------------- |
| 1721 | // Tell the tool about the initial client memory permissions |
| 1722 | // p: aspacem |
| 1723 | // p: mallocfree |
| 1724 | // p: setup_client_stack |
| 1725 | // p: setup_client_dataseg |
| 1726 | //-------------------------------------------------------------- |
| 1727 | VG_(debugLog)(1, "main", "Tell tool about initial permissions\n"); |
| 1728 | { Addr* seg_starts; |
| 1729 | Int n_seg_starts; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1730 | |
| 1731 | seg_starts = get_seg_starts( &n_seg_starts ); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1732 | vg_assert(seg_starts && n_seg_starts >= 0); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1733 | |
| 1734 | /* show interesting ones to the tool */ |
| 1735 | for (i = 0; i < n_seg_starts; i++) { |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 1736 | NSegment const* seg |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1737 | = VG_(am_find_nsegment)( seg_starts[i] ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1738 | vg_assert(seg); |
| 1739 | if (seg->kind == SkFileC || seg->kind == SkAnonC) { |
| 1740 | VG_(debugLog)(2, "main", |
| 1741 | "tell tool about %010lx-%010lx %c%c%c\n", |
| 1742 | seg->start, seg->end, |
| 1743 | seg->hasR ? 'r' : '-', |
| 1744 | seg->hasW ? 'w' : '-', |
| 1745 | seg->hasX ? 'x' : '-' ); |
| 1746 | VG_TRACK( new_mem_startup, seg->start, seg->end+1-seg->start, |
| 1747 | seg->hasR, seg->hasW, seg->hasX ); |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | VG_(free)( seg_starts ); |
| 1752 | |
| 1753 | /* Also do the initial stack permissions. */ |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 1754 | { NSegment const* seg |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1755 | = VG_(am_find_nsegment)( the_iifii.initial_client_SP ); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1756 | vg_assert(seg); |
| 1757 | vg_assert(seg->kind == SkAnonC); |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1758 | vg_assert(the_iifii.initial_client_SP >= seg->start); |
| 1759 | vg_assert(the_iifii.initial_client_SP <= seg->end); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1760 | # if defined(VGO_aix5) |
| 1761 | VG_(clstk_base) = seg->start; |
| 1762 | VG_(clstk_end) = seg->end; |
| 1763 | # endif |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1764 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1765 | /* Stuff below the initial SP is unaddressable. Take into |
| 1766 | account any ABI-mandated space below the stack pointer that |
| 1767 | is required (VG_STACK_REDZONE_SZB). setup_client_stack() |
| 1768 | will have allocated an extra page if a red zone is required, |
| 1769 | to be on the safe side. */ |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1770 | vg_assert(the_iifii.initial_client_SP - VG_STACK_REDZONE_SZB |
| 1771 | >= seg->start); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1772 | VG_TRACK( die_mem_stack, |
| 1773 | seg->start, |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1774 | the_iifii.initial_client_SP - VG_STACK_REDZONE_SZB |
| 1775 | - seg->start ); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1776 | VG_(debugLog)(2, "main", "mark stack inaccessible %010lx-%010lx\n", |
| 1777 | seg->start, |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1778 | the_iifii.initial_client_SP-1 - VG_STACK_REDZONE_SZB); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1779 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1780 | |
| 1781 | /* Also the assembly helpers. */ |
| 1782 | VG_TRACK( new_mem_startup, |
| 1783 | (Addr)&VG_(trampoline_stuff_start), |
sewardj | c6527d6 | 2006-02-13 17:54:31 +0000 | [diff] [blame] | 1784 | (Addr)&VG_(trampoline_stuff_end) |
| 1785 | - (Addr)&VG_(trampoline_stuff_start), |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1786 | False, /* readable? */ |
| 1787 | False, /* writable? */ |
| 1788 | True /* executable? */ ); |
| 1789 | } |
| 1790 | |
| 1791 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1792 | // Initialise the scheduler |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1793 | // p: setup_file_descriptors() [else VG_(safe_fd)() breaks] |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1794 | // p: setup_client_stack |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1795 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 1796 | VG_(debugLog)(1, "main", "Initialise scheduler\n"); |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 1797 | { NSegment const* seg |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1798 | = VG_(am_find_nsegment)( the_iifii.initial_client_SP ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1799 | vg_assert(seg); |
| 1800 | vg_assert(seg->kind == SkAnonC); |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1801 | vg_assert(the_iifii.initial_client_SP >= seg->start); |
| 1802 | vg_assert(the_iifii.initial_client_SP <= seg->end); |
| 1803 | VG_(scheduler_init)( seg->end, the_iifii.clstack_max_size ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1804 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1805 | |
| 1806 | //-------------------------------------------------------------- |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1807 | // Set up state for the root thread |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1808 | // p: ? |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1809 | // setup_scheduler() [for sched-specific thread 1 stuff] |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1810 | // VG_(ii_create_image) [for 'the_iicii' initial info] |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 1811 | //-------------------------------------------------------------- |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1812 | VG_(debugLog)(1, "main", "Finalise initial image\n"); |
| 1813 | VG_(ii_finalise_image)( the_iifii ); |
njn | ea4b28c | 2004-11-30 16:04:58 +0000 | [diff] [blame] | 1814 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 1815 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1816 | // Initialise the signal handling subsystem |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1817 | // p: n/a |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1818 | //-------------------------------------------------------------- |
| 1819 | // Nb: temporarily parks the saved blocking-mask in saved_sigmask. |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 1820 | VG_(debugLog)(1, "main", "Initialise signal management\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1821 | VG_(sigstartup_actions)(); |
| 1822 | |
| 1823 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1824 | // Read suppression file |
| 1825 | // p: process_cmd_line_options() [for VG_(clo_suppressions)] |
| 1826 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 1827 | if (VG_(needs).core_errors || VG_(needs).tool_errors) { |
| 1828 | VG_(debugLog)(1, "main", "Load suppressions\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1829 | VG_(load_suppressions)(); |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 1830 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1831 | |
| 1832 | //-------------------------------------------------------------- |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1833 | // register client stack |
| 1834 | //-------------------------------------------------------------- |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 1835 | VG_(clstk_id) = VG_(register_stack)(VG_(clstk_base), VG_(clstk_end)); |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 1836 | |
| 1837 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1838 | // Show the address space state so far |
| 1839 | //-------------------------------------------------------------- |
| 1840 | VG_(debugLog)(1, "main", "\n"); |
| 1841 | VG_(debugLog)(1, "main", "\n"); |
| 1842 | VG_(am_show_nsegments)(1,"Memory layout at client startup"); |
| 1843 | VG_(debugLog)(1, "main", "\n"); |
| 1844 | VG_(debugLog)(1, "main", "\n"); |
| 1845 | |
| 1846 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1847 | // Run! |
| 1848 | //-------------------------------------------------------------- |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1849 | if (VG_(clo_xml)) { |
sewardj | 68cde6f | 2005-07-19 12:17:51 +0000 | [diff] [blame] | 1850 | HChar buf[50]; |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 1851 | VG_(elapsed_wallclock_time)(buf); |
sewardj | 753673f | 2005-08-09 22:03:08 +0000 | [diff] [blame] | 1852 | VG_(message)(Vg_UserMsg, "<status>\n" |
| 1853 | " <state>RUNNING</state>\n" |
| 1854 | " <time>%t</time>\n" |
| 1855 | "</status>", |
| 1856 | buf); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1857 | VG_(message)(Vg_UserMsg, ""); |
| 1858 | } |
| 1859 | |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 1860 | VG_(debugLog)(1, "main", "Running thread 1\n"); |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 1861 | |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 1862 | /* As a result of the following call, the last thread standing |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 1863 | eventually winds up running shutdown_actions_NORETURN |
| 1864 | just below. Unfortunately, simply exporting said function |
| 1865 | causes m_main to be part of a module cycle, which is pretty |
| 1866 | nonsensical. So instead of doing that, the address of said |
| 1867 | function is stored in a global variable 'owned' by m_syswrap, |
| 1868 | and it uses that function pointer to get back here when it needs |
| 1869 | to. */ |
| 1870 | |
| 1871 | /* Set continuation address. */ |
| 1872 | VG_(address_of_m_main_shutdown_actions_NORETURN) |
| 1873 | = & shutdown_actions_NORETURN; |
| 1874 | |
| 1875 | /* Run the first thread, eventually ending up at the continuation |
| 1876 | address. */ |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1877 | VG_(main_thread_wrapper_NORETURN)(1); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1878 | |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 1879 | /*NOTREACHED*/ |
| 1880 | vg_assert(0); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1883 | /* Do everything which needs doing when the last thread exits or when |
| 1884 | a thread exits requesting a complete process exit (exit on AIX). |
| 1885 | |
| 1886 | We enter here holding The Lock. For the case VgSrc_ExitProcess we |
| 1887 | must never release it, because to do so would allow other threads |
| 1888 | to continue after the system is ostensibly shut down. So we must |
| 1889 | go to our grave, so to speak, holding the lock. |
| 1890 | |
| 1891 | In fact, there is never any point in releasing the lock at this |
| 1892 | point - we have it, we're shutting down the entire system, and |
| 1893 | for the case VgSrc_ExitProcess doing so positively causes trouble. |
| 1894 | So don't. |
| 1895 | |
| 1896 | The final_tidyup call makes a bit of a nonsense of the ExitProcess |
| 1897 | case, since it will run the libc_freeres function, thus allowing |
| 1898 | other lurking threads to run again. Hmm. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1899 | |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 1900 | static |
| 1901 | void shutdown_actions_NORETURN( ThreadId tid, |
| 1902 | VgSchedReturnCode tids_schedretcode ) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1903 | { |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 1904 | VG_(debugLog)(1, "main", "entering VG_(shutdown_actions_NORETURN)\n"); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1905 | VG_(am_show_nsegments)(1,"Memory layout at client shutdown"); |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 1906 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1907 | vg_assert(VG_(is_running_thread)(tid)); |
| 1908 | |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 1909 | vg_assert(tids_schedretcode == VgSrc_ExitThread |
| 1910 | || tids_schedretcode == VgSrc_ExitProcess |
| 1911 | || tids_schedretcode == VgSrc_FatalSig ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1912 | |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 1913 | if (tids_schedretcode == VgSrc_ExitThread) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1914 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1915 | // We are the last surviving thread. Right? |
| 1916 | vg_assert( VG_(count_living_threads)() == 1 ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1917 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1918 | // Wait for all other threads to exit. |
| 1919 | // jrs: Huh? but they surely are already gone |
| 1920 | VG_(reap_threads)(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1921 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1922 | // Clean the client up before the final report |
| 1923 | // this causes the libc_freeres function to run |
| 1924 | final_tidyup(tid); |
| 1925 | |
| 1926 | /* be paranoid */ |
| 1927 | vg_assert(VG_(is_running_thread)(tid)); |
| 1928 | vg_assert(VG_(count_living_threads)() == 1); |
| 1929 | |
| 1930 | } else { |
| 1931 | |
| 1932 | // We may not be the last surviving thread. However, we |
| 1933 | // want to shut down the entire process. We hold the lock |
| 1934 | // and we need to keep hold of it all the way out, in order |
| 1935 | // that none of the other threads ever run again. |
| 1936 | vg_assert( VG_(count_living_threads)() >= 1 ); |
| 1937 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1938 | // Clean the client up before the final report |
| 1939 | // this causes the libc_freeres function to run |
| 1940 | // perhaps this is unsafe, as per comment above |
| 1941 | final_tidyup(tid); |
| 1942 | |
| 1943 | /* be paranoid */ |
| 1944 | vg_assert(VG_(is_running_thread)(tid)); |
| 1945 | vg_assert(VG_(count_living_threads)() >= 1); |
| 1946 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1947 | |
| 1948 | VG_(threads)[tid].status = VgTs_Empty; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1949 | //-------------------------------------------------------------- |
| 1950 | // Finalisation: cleanup, messages, etc. Order no so important, only |
| 1951 | // affects what order the messages come. |
| 1952 | //-------------------------------------------------------------- |
| 1953 | if (VG_(clo_verbosity) > 0) |
| 1954 | VG_(message)(Vg_UserMsg, ""); |
| 1955 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1956 | if (VG_(clo_xml)) { |
sewardj | 68cde6f | 2005-07-19 12:17:51 +0000 | [diff] [blame] | 1957 | HChar buf[50]; |
sewardj | 9f297ca | 2005-05-20 02:29:52 +0000 | [diff] [blame] | 1958 | if (VG_(needs).core_errors || VG_(needs).tool_errors) { |
| 1959 | VG_(show_error_counts_as_XML)(); |
| 1960 | VG_(message)(Vg_UserMsg, ""); |
| 1961 | } |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 1962 | VG_(elapsed_wallclock_time)(buf); |
sewardj | 753673f | 2005-08-09 22:03:08 +0000 | [diff] [blame] | 1963 | VG_(message)(Vg_UserMsg, "<status>\n" |
| 1964 | " <state>FINISHED</state>\n" |
| 1965 | " <time>%t</time>\n" |
| 1966 | "</status>", |
| 1967 | buf); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1968 | VG_(message)(Vg_UserMsg, ""); |
| 1969 | } |
| 1970 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1971 | /* Print out file descriptor summary and stats. */ |
| 1972 | if (VG_(clo_track_fds)) |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 1973 | VG_(show_open_fds)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1974 | |
njn | 95ec870 | 2004-11-22 16:46:13 +0000 | [diff] [blame] | 1975 | if (VG_(needs).core_errors || VG_(needs).tool_errors) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1976 | VG_(show_all_errors)(); |
| 1977 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1978 | VG_TDICT_CALL(tool_fini, 0/*exitcode*/); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1979 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1980 | if (VG_(clo_xml)) { |
| 1981 | VG_(message)(Vg_UserMsg, ""); |
| 1982 | VG_(message)(Vg_UserMsg, "</valgrindoutput>"); |
| 1983 | VG_(message)(Vg_UserMsg, ""); |
| 1984 | } |
| 1985 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1986 | VG_(sanity_check_general)( True /*include expensive checks*/ ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1987 | |
| 1988 | if (VG_(clo_verbosity) > 1) |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 1989 | print_all_stats(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1990 | |
njn | 2025cf9 | 2005-06-26 20:44:48 +0000 | [diff] [blame] | 1991 | if (VG_(clo_profile_flags) > 0) { |
sewardj | 5471ec6 | 2006-10-17 13:58:17 +0000 | [diff] [blame] | 1992 | #define N_MAX 200 |
njn | 2025cf9 | 2005-06-26 20:44:48 +0000 | [diff] [blame] | 1993 | BBProfEntry tops[N_MAX]; |
| 1994 | ULong score_total = VG_(get_BB_profile) (tops, N_MAX); |
| 1995 | show_BB_profile(tops, N_MAX, score_total); |
| 1996 | } |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1997 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1998 | /* Print Vex storage stats */ |
sewardj | bf42651 | 2005-01-17 18:35:30 +0000 | [diff] [blame] | 1999 | if (0) |
| 2000 | LibVEX_ShowAllocStats(); |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2001 | |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2002 | /* Ok, finally exit in the os-specific way, according to the scheduler's |
| 2003 | return code. In short, if the (last) thread exited by calling |
| 2004 | sys_exit, do likewise; if the (last) thread stopped due to a fatal |
| 2005 | signal, terminate the entire system with that same fatal signal. */ |
| 2006 | VG_(debugLog)(1, "core_os", |
njn | 7b85dd5 | 2005-06-12 17:26:29 +0000 | [diff] [blame] | 2007 | "VG_(terminate_NORETURN)(tid=%lld)\n", (ULong)tid); |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2008 | |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2009 | switch (tids_schedretcode) { |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 2010 | case VgSrc_ExitThread: /* the normal way out (Linux) */ |
| 2011 | case VgSrc_ExitProcess: /* the normal way out (AIX) */ |
sewardj | b977908 | 2006-05-12 23:50:15 +0000 | [diff] [blame] | 2012 | /* Change the application return code to user's return code, |
| 2013 | if an error was found */ |
| 2014 | if (VG_(clo_error_exitcode) > 0 |
| 2015 | && VG_(get_n_errs_found)() > 0) { |
| 2016 | VG_(exit)( VG_(clo_error_exitcode) ); |
| 2017 | } else { |
| 2018 | /* otherwise, return the client's exit code, in the normal |
| 2019 | way. */ |
| 2020 | VG_(exit)( VG_(threads)[tid].os_state.exitcode ); |
| 2021 | } |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2022 | /* NOT ALIVE HERE! */ |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2023 | VG_(core_panic)("entered the afterlife in main() -- ExitT/P"); |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2024 | break; /* what the hell :) */ |
| 2025 | |
| 2026 | case VgSrc_FatalSig: |
| 2027 | /* We were killed by a fatal signal, so replicate the effect */ |
| 2028 | vg_assert(VG_(threads)[tid].os_state.fatalsig != 0); |
| 2029 | VG_(kill_self)(VG_(threads)[tid].os_state.fatalsig); |
| 2030 | VG_(core_panic)("main(): signal was supposed to be fatal"); |
| 2031 | break; |
| 2032 | |
| 2033 | default: |
| 2034 | VG_(core_panic)("main(): unexpected scheduler return code"); |
| 2035 | } |
njn | e96be67 | 2005-05-08 19:08:54 +0000 | [diff] [blame] | 2036 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 2037 | |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2038 | /* -------------------- */ |
| 2039 | |
| 2040 | /* Final clean-up before terminating the process. |
| 2041 | Clean up the client by calling __libc_freeres() (if requested) |
| 2042 | This is Linux-specific? |
| 2043 | */ |
| 2044 | static void final_tidyup(ThreadId tid) |
| 2045 | { |
sewardj | cf95181 | 2006-01-17 02:22:21 +0000 | [diff] [blame] | 2046 | # if defined(VGP_ppc64_linux) |
| 2047 | Addr r2; |
| 2048 | # endif |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 2049 | Addr __libc_freeres_wrapper = VG_(client___libc_freeres_wrapper); |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2050 | |
| 2051 | vg_assert(VG_(is_running_thread)(tid)); |
| 2052 | |
| 2053 | if ( !VG_(needs).libc_freeres || |
| 2054 | !VG_(clo_run_libc_freeres) || |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 2055 | 0 == __libc_freeres_wrapper ) |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2056 | return; /* can't/won't do it */ |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2057 | # if defined(VGO_aix5) |
| 2058 | return; /* inapplicable on non-Linux platforms */ |
| 2059 | # endif |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2060 | |
sewardj | cf95181 | 2006-01-17 02:22:21 +0000 | [diff] [blame] | 2061 | # if defined(VGP_ppc64_linux) |
| 2062 | r2 = VG_(get_tocptr)( __libc_freeres_wrapper ); |
| 2063 | if (r2 == 0) { |
| 2064 | VG_(message)(Vg_UserMsg, |
| 2065 | "Caught __NR_exit, but can't run __libc_freeres()"); |
| 2066 | VG_(message)(Vg_UserMsg, |
| 2067 | " since cannot establish TOC pointer for it."); |
| 2068 | return; |
| 2069 | } |
| 2070 | # endif |
| 2071 | |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2072 | if (VG_(clo_verbosity) > 2 || |
| 2073 | VG_(clo_trace_syscalls) || |
| 2074 | VG_(clo_trace_sched)) |
| 2075 | VG_(message)(Vg_DebugMsg, |
| 2076 | "Caught __NR_exit; running __libc_freeres()"); |
| 2077 | |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 2078 | /* set thread context to point to libc_freeres_wrapper */ |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 2079 | /* ppc64-linux note: __libc_freeres_wrapper gives us the real |
| 2080 | function entry point, not a fn descriptor, so can use it |
| 2081 | directly. However, we need to set R2 (the toc pointer) |
| 2082 | appropriately. */ |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2083 | VG_(set_IP)(tid, __libc_freeres_wrapper); |
sewardj | cf95181 | 2006-01-17 02:22:21 +0000 | [diff] [blame] | 2084 | # if defined(VGP_ppc64_linux) |
| 2085 | VG_(threads)[tid].arch.vex.guest_GPR2 = r2; |
| 2086 | # endif |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2087 | |
| 2088 | /* Block all blockable signals by copying the real block state into |
| 2089 | the thread's block state*/ |
| 2090 | VG_(sigprocmask)(VKI_SIG_BLOCK, NULL, &VG_(threads)[tid].sig_mask); |
| 2091 | VG_(threads)[tid].tmp_sig_mask = VG_(threads)[tid].sig_mask; |
| 2092 | |
| 2093 | /* and restore handlers to default */ |
| 2094 | VG_(set_default_handler)(VKI_SIGSEGV); |
| 2095 | VG_(set_default_handler)(VKI_SIGBUS); |
| 2096 | VG_(set_default_handler)(VKI_SIGILL); |
| 2097 | VG_(set_default_handler)(VKI_SIGFPE); |
| 2098 | |
| 2099 | // We were exiting, so assert that... |
| 2100 | vg_assert(VG_(is_exiting)(tid)); |
| 2101 | // ...but now we're not again |
| 2102 | VG_(threads)[tid].exitreason = VgSrc_None; |
| 2103 | |
| 2104 | // run until client thread exits - ideally with LIBC_FREERES_DONE, |
| 2105 | // but exit/exitgroup/signal will do |
| 2106 | VG_(scheduler)(tid); |
| 2107 | |
| 2108 | vg_assert(VG_(is_exiting)(tid)); |
| 2109 | } |
| 2110 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2111 | |
| 2112 | /*====================================================================*/ |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2113 | /*=== Getting to main() alive: LINUX (for AIX5 see below) ===*/ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2114 | /*====================================================================*/ |
| 2115 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2116 | #if defined(VGO_linux) |
| 2117 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2118 | /* If linking of the final executables is done with glibc present, |
| 2119 | then Valgrind starts at main() above as usual, and all of the |
| 2120 | following code is irrelevant. |
| 2121 | |
| 2122 | However, this is not the intended mode of use. The plan is to |
| 2123 | avoid linking against glibc, by giving gcc the flags |
| 2124 | -nodefaultlibs -lgcc -nostartfiles at startup. |
| 2125 | |
| 2126 | From this derive two requirements: |
| 2127 | |
| 2128 | 1. gcc may emit calls to memcpy and memset to deal with structure |
| 2129 | assignments etc. Since we have chosen to ignore all the |
| 2130 | "normal" supporting libraries, we have to provide our own |
| 2131 | implementations of them. No problem. |
| 2132 | |
| 2133 | 2. We have to provide a symbol "_start", to which the kernel |
| 2134 | hands control at startup. Hence the code below. |
| 2135 | */ |
| 2136 | |
| 2137 | /* ---------------- Requirement 1 ---------------- */ |
| 2138 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2139 | void* memcpy(void *dest, const void *src, SizeT n); |
| 2140 | void* memcpy(void *dest, const void *src, SizeT n) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2141 | return VG_(memcpy)(dest,src,n); |
| 2142 | } |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2143 | void* memset(void *s, int c, SizeT n); |
| 2144 | void* memset(void *s, int c, SizeT n) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2145 | return VG_(memset)(s,c,n); |
| 2146 | } |
| 2147 | |
| 2148 | /* ---------------- Requirement 2 ---------------- */ |
| 2149 | |
| 2150 | /* Glibc's sysdeps/i386/elf/start.S has the following gem of a |
| 2151 | comment, which explains how the stack looks right at process start |
| 2152 | (when _start is jumped to). Hence _start passes %esp to |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2153 | _start_in_C_linux, which extracts argc/argv/envp and starts up |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2154 | correctly. */ |
| 2155 | |
| 2156 | /* This is the canonical entry point, usually the first thing in the text |
| 2157 | segment. The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry |
| 2158 | point runs, most registers' values are unspecified, except for: |
| 2159 | |
| 2160 | %edx Contains a function pointer to be registered with `atexit'. |
| 2161 | This is how the dynamic linker arranges to have DT_FINI |
| 2162 | functions called for shared libraries that have been loaded |
| 2163 | before this code runs. |
| 2164 | |
| 2165 | %esp The stack contains the arguments and environment: |
| 2166 | 0(%esp) argc |
| 2167 | 4(%esp) argv[0] |
| 2168 | ... |
| 2169 | (4*argc)(%esp) NULL |
| 2170 | (4*(argc+1))(%esp) envp[0] |
| 2171 | ... |
| 2172 | NULL |
| 2173 | */ |
| 2174 | |
| 2175 | /* The kernel hands control to _start, which extracts the initial |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2176 | stack pointer and calls onwards to _start_in_C_linux. This also switches |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 2177 | the new stack. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2178 | #if defined(VGP_x86_linux) |
| 2179 | asm("\n" |
sewardj | d9fc382 | 2005-11-18 23:50:43 +0000 | [diff] [blame] | 2180 | ".text\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2181 | "\t.globl _start\n" |
| 2182 | "\t.type _start,@function\n" |
| 2183 | "_start:\n" |
| 2184 | /* set up the new stack in %eax */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 2185 | "\tmovl $vgPlain_interim_stack, %eax\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2186 | "\taddl $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n" |
| 2187 | "\taddl $"VG_STRINGIFY(VG_STACK_ACTIVE_SZB)", %eax\n" |
| 2188 | "\tsubl $16, %eax\n" |
| 2189 | "\tandl $~15, %eax\n" |
| 2190 | /* install it, and collect the original one */ |
| 2191 | "\txchgl %eax, %esp\n" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2192 | /* call _start_in_C_linux, passing it the startup %esp */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2193 | "\tpushl %eax\n" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2194 | "\tcall _start_in_C_linux\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2195 | "\thlt\n" |
sewardj | 2fedc64 | 2005-11-19 02:02:57 +0000 | [diff] [blame] | 2196 | ".previous\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2197 | ); |
| 2198 | #elif defined(VGP_amd64_linux) |
| 2199 | asm("\n" |
sewardj | d9fc382 | 2005-11-18 23:50:43 +0000 | [diff] [blame] | 2200 | ".text\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2201 | "\t.globl _start\n" |
| 2202 | "\t.type _start,@function\n" |
| 2203 | "_start:\n" |
| 2204 | /* set up the new stack in %rdi */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 2205 | "\tmovq $vgPlain_interim_stack, %rdi\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2206 | "\taddq $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rdi\n" |
| 2207 | "\taddq $"VG_STRINGIFY(VG_STACK_ACTIVE_SZB)", %rdi\n" |
| 2208 | "\tandq $~15, %rdi\n" |
| 2209 | /* install it, and collect the original one */ |
| 2210 | "\txchgq %rdi, %rsp\n" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2211 | /* call _start_in_C_linux, passing it the startup %rsp */ |
| 2212 | "\tcall _start_in_C_linux\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2213 | "\thlt\n" |
sewardj | 2fedc64 | 2005-11-19 02:02:57 +0000 | [diff] [blame] | 2214 | ".previous\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2215 | ); |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 2216 | #elif defined(VGP_ppc32_linux) |
| 2217 | asm("\n" |
sewardj | d9fc382 | 2005-11-18 23:50:43 +0000 | [diff] [blame] | 2218 | ".text\n" |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 2219 | "\t.globl _start\n" |
| 2220 | "\t.type _start,@function\n" |
| 2221 | "_start:\n" |
| 2222 | /* set up the new stack in r16 */ |
| 2223 | "\tlis 16,vgPlain_interim_stack@ha\n" |
| 2224 | "\tla 16,vgPlain_interim_stack@l(16)\n" |
| 2225 | "\tlis 17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" >> 16)\n" |
| 2226 | "\tori 17,17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" & 0xFFFF)\n" |
| 2227 | "\tlis 18,("VG_STRINGIFY(VG_STACK_ACTIVE_SZB)" >> 16)\n" |
| 2228 | "\tori 18,18,("VG_STRINGIFY(VG_STACK_ACTIVE_SZB)" & 0xFFFF)\n" |
| 2229 | "\tadd 16,17,16\n" |
| 2230 | "\tadd 16,18,16\n" |
| 2231 | "\trlwinm 16,16,0,0,27\n" |
| 2232 | /* now r16 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB + |
| 2233 | VG_STACK_ACTIVE_SZB rounded down to the nearest 16-byte |
| 2234 | boundary. And r1 is the original SP. Set the SP to r16 and |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2235 | call _start_in_C_linux, passing it the initial SP. */ |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 2236 | "\tmr 3,1\n" |
| 2237 | "\tmr 1,16\n" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2238 | "\tbl _start_in_C_linux\n" |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 2239 | "\ttrap\n" |
sewardj | 2fedc64 | 2005-11-19 02:02:57 +0000 | [diff] [blame] | 2240 | ".previous\n" |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 2241 | ); |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 2242 | #elif defined(VGP_ppc64_linux) |
| 2243 | asm("\n" |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 2244 | /* PPC64 ELF ABI says '_start' points to a function descriptor. |
| 2245 | So we must have one, and that is what goes into the .opd section. */ |
cerion | 297c88f | 2005-12-22 15:53:12 +0000 | [diff] [blame] | 2246 | "\t.align 2\n" |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 2247 | "\t.global _start\n" |
| 2248 | "\t.section \".opd\",\"aw\"\n" |
| 2249 | "\t.align 3\n" |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 2250 | "_start:\n" |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 2251 | "\t.quad ._start,.TOC.@tocbase,0\n" |
| 2252 | "\t.previous\n" |
| 2253 | "\t.type ._start,@function\n" |
| 2254 | "\t.global ._start\n" |
| 2255 | "._start:\n" |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 2256 | /* set up the new stack in r16 */ |
| 2257 | "\tlis 16, vgPlain_interim_stack@highest\n" |
| 2258 | "\tori 16,16,vgPlain_interim_stack@higher\n" |
| 2259 | "\tsldi 16,16,32\n" |
| 2260 | "\toris 16,16,vgPlain_interim_stack@h\n" |
| 2261 | "\tori 16,16,vgPlain_interim_stack@l\n" |
| 2262 | "\txor 17,17,17\n" |
| 2263 | "\tlis 17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" >> 16)\n" |
| 2264 | "\tori 17,17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" & 0xFFFF)\n" |
| 2265 | "\txor 18,18,18\n" |
| 2266 | "\tlis 18,("VG_STRINGIFY(VG_STACK_ACTIVE_SZB)" >> 16)\n" |
| 2267 | "\tori 18,18,("VG_STRINGIFY(VG_STACK_ACTIVE_SZB)" & 0xFFFF)\n" |
| 2268 | "\tadd 16,17,16\n" |
| 2269 | "\tadd 16,18,16\n" |
| 2270 | "\trldicr 16,16,0,59\n" |
| 2271 | /* now r16 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB + |
| 2272 | VG_STACK_ACTIVE_SZB rounded down to the nearest 16-byte |
| 2273 | boundary. And r1 is the original SP. Set the SP to r16 and |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2274 | call _start_in_C_linux, passing it the initial SP. */ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 2275 | "\tmr 3,1\n" |
| 2276 | "\tmr 1,16\n" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2277 | "\tbl ._start_in_C_linux\n" |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 2278 | "\tnop\n" |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 2279 | "\ttrap\n" |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 2280 | ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2281 | #else |
| 2282 | #error "_start: needs implementation on this platform" |
| 2283 | #endif |
| 2284 | |
sewardj | e66f2e0 | 2006-12-30 17:45:08 +0000 | [diff] [blame] | 2285 | /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */ |
| 2286 | #define _GNU_SOURCE |
| 2287 | #define _FILE_OFFSET_BITS 64 |
| 2288 | /* This is in order to get AT_NULL and AT_PAGESIZE. */ |
| 2289 | #include <elf.h> |
| 2290 | /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */ |
| 2291 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2292 | /* Avoid compiler warnings: this fn _is_ used, but labelling it |
| 2293 | 'static' causes gcc to complain it isn't. */ |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2294 | void _start_in_C_linux ( UWord* pArgc ); |
| 2295 | void _start_in_C_linux ( UWord* pArgc ) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2296 | { |
| 2297 | Int r; |
| 2298 | Word argc = pArgc[0]; |
| 2299 | HChar** argv = (HChar**)&pArgc[1]; |
| 2300 | HChar** envp = (HChar**)&pArgc[1+argc+1]; |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2301 | |
| 2302 | VG_(memset)( &the_iicii, 0, sizeof(the_iicii) ); |
| 2303 | VG_(memset)( &the_iifii, 0, sizeof(the_iifii) ); |
| 2304 | |
| 2305 | the_iicii.sp_at_startup = (Addr)pArgc; |
| 2306 | |
sewardj | e66f2e0 | 2006-12-30 17:45:08 +0000 | [diff] [blame] | 2307 | # if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
| 2308 | { |
| 2309 | /* ppc/ppc64 can be configured with different page sizes. |
| 2310 | Determine this early. This is an ugly hack and really should |
| 2311 | be moved into valgrind_main. */ |
| 2312 | UWord *sp = &pArgc[1+argc+1]; |
| 2313 | while (*sp++ != 0) |
| 2314 | ; |
| 2315 | for (; *sp != AT_NULL && *sp != AT_PAGESZ; sp += 2); |
| 2316 | if (*sp == AT_PAGESZ) { |
| 2317 | VKI_PAGE_SIZE = sp[1]; |
| 2318 | for (VKI_PAGE_SHIFT = 12; |
| 2319 | VKI_PAGE_SHIFT <= VKI_MAX_PAGE_SHIFT; VKI_PAGE_SHIFT++) |
| 2320 | if (VKI_PAGE_SIZE == (1UL << VKI_PAGE_SHIFT)) |
| 2321 | break; |
| 2322 | } |
| 2323 | } |
| 2324 | # endif |
| 2325 | |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2326 | r = valgrind_main( (Int)argc, argv, envp ); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2327 | /* NOTREACHED */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2328 | VG_(exit)(r); |
| 2329 | } |
| 2330 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2331 | #endif /* defined(VGO_linux) */ |
| 2332 | |
| 2333 | |
| 2334 | /*====================================================================*/ |
| 2335 | /*=== Getting to main() alive: AIX5 ===*/ |
| 2336 | /*====================================================================*/ |
| 2337 | |
| 2338 | #if defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 2339 | |
| 2340 | /* This is somewhat simpler than the Linux case. _start_valgrind |
| 2341 | receives control from the magic piece of code created in this |
| 2342 | process' address space by the launcher, via use of ptrace(). At |
| 2343 | the point of entry: |
| 2344 | |
| 2345 | - the initial client process image is in memory and ready to roll, |
| 2346 | except that we've partially trashed its integer register state |
| 2347 | in order to get this far. So .. |
| 2348 | |
| 2349 | - intregs37 holds the client's initial integer register state, so |
| 2350 | we can restore it before starting the client on the VCPU. |
| 2351 | |
| 2352 | - we're on the client's stack. This is not good; therefore the |
| 2353 | first order of business is to switch to our temporary stack. |
| 2354 | |
| 2355 | - the client's initial argc/v/envp is in r3/r4/r5 (32 bit mode) or |
| 2356 | r14/r15/r16 (64 bit mode). They are pulled out of the stashed |
| 2357 | integer register state and passed to our main(). |
| 2358 | |
| 2359 | The launcher will have played some games with argv. If the launcher |
| 2360 | ($prefix/bin/valgrind) was started like this |
| 2361 | |
| 2362 | valgrind [args-for-V] app [args-for-app] |
| 2363 | |
| 2364 | then the launcher will have started the client as |
| 2365 | |
| 2366 | app [args-for-V] app [args-for-app] |
| 2367 | |
| 2368 | m_initimg will have to mess with the client's initial r4/r5 |
| 2369 | (32-bit) or r15/r16 (64-bit) so that it believes it was execd as |
| 2370 | "app [args-for-app]". Well, that's no big deal. |
| 2371 | */ |
| 2372 | |
| 2373 | #include "launcher-aix5-bootblock.h" |
| 2374 | |
| 2375 | void _start_in_C_aix5 ( AIX5Bootblock* bootblock ); |
| 2376 | void _start_in_C_aix5 ( AIX5Bootblock* bootblock ) |
| 2377 | { |
| 2378 | Int r; |
| 2379 | ULong* intregs37; |
| 2380 | UWord argc, argv, envp; |
| 2381 | __NR_getpid = bootblock->__NR_getpid; |
| 2382 | __NR_write = bootblock->__NR_write; |
| 2383 | __NR_exit = bootblock->__NR_exit; |
| 2384 | __NR_open = bootblock->__NR_open; |
| 2385 | __NR_read = bootblock->__NR_read; |
| 2386 | __NR_close = bootblock->__NR_close; |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2387 | |
| 2388 | VG_(memset)( &the_iicii, 0, sizeof(the_iicii) ); |
| 2389 | VG_(memset)( &the_iifii, 0, sizeof(the_iifii) ); |
| 2390 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2391 | intregs37 = &bootblock->iregs_pc_cr_lr_ctr_xer[0]; |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2392 | the_iicii.intregs37 = intregs37; |
| 2393 | the_iicii.bootblock = (void*)bootblock; |
| 2394 | the_iicii.adler32_exp = bootblock->adler32; |
| 2395 | |
| 2396 | /* Not important on AIX. */ |
| 2397 | the_iicii.sp_at_startup = (Addr)0x31415927ULL; |
| 2398 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2399 | # if defined(VGP_ppc32_aix5) |
| 2400 | argc = (UWord)intregs37[3]; /* client's r3 == argc */ |
| 2401 | argv = (UWord)intregs37[4]; |
| 2402 | envp = (UWord)intregs37[5]; |
| 2403 | # else /* defined(VGP_ppc64_aix5) */ |
| 2404 | argc = (UWord)intregs37[14]; /* client's r14 == argc */ |
| 2405 | argv = (UWord)intregs37[15]; |
| 2406 | envp = (UWord)intregs37[16]; |
| 2407 | # endif |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2408 | |
| 2409 | r = valgrind_main( (Int)argc, (HChar**)argv, (HChar**)envp ); |
| 2410 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2411 | /* NOTREACHED */ |
| 2412 | VG_(exit)(r); |
| 2413 | } |
| 2414 | |
| 2415 | /* THE ENTRY POINT */ |
| 2416 | void _start_valgrind ( AIX5Bootblock* bootblock ); |
| 2417 | void _start_valgrind ( AIX5Bootblock* bootblock ) |
| 2418 | { |
| 2419 | /* Switch immediately to our temporary stack, and continue. This |
| 2420 | is pretty dodgy in that it assumes that gcc does not place on |
| 2421 | the stack, anything needed to form the _start_in_C_aix5 call, |
| 2422 | since it will be on the old stack. */ |
| 2423 | register UWord new_r1; |
| 2424 | new_r1 = (UWord)&VG_(interim_stack); |
| 2425 | new_r1 += VG_STACK_GUARD_SZB; /* step over lower guard page */ |
| 2426 | new_r1 += VG_STACK_ACTIVE_SZB; /* step to top of active area */ |
| 2427 | new_r1 -= 512; /* paranoia */ |
| 2428 | __asm__ __volatile__("mr 1,%0" :/*wr*/ |
| 2429 | :/*rd*/ "b"(new_r1) |
| 2430 | :/*trash*/"r1","memory"); |
| 2431 | _start_in_C_aix5(bootblock); |
| 2432 | /*NOTREACHED*/ |
| 2433 | VG_(exit)(0); |
| 2434 | } |
| 2435 | |
| 2436 | #endif /* defined(VGP_ppc{32,64}_aix5) */ |
| 2437 | |
| 2438 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2439 | /*--------------------------------------------------------------------*/ |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 2440 | /*--- end ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2441 | /*--------------------------------------------------------------------*/ |