blob: 01c1a6ddf81afb0fa2e9fe004270ba0191896053 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
njn04e16982005-05-31 00:23:43 +00003/*--- Startup: the real stuff m_main.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00004/*--------------------------------------------------------------------*/
5
6/*
njnb9c427c2004-12-01 14:14:42 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardjde4a1d02002-03-22 01:27:54 +00009
sewardjb3a1e4b2015-08-21 11:32:26 +000010 Copyright (C) 2000-2015 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000011 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000012
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
njn25e49d8e72002-09-23 09:36:25 +000028 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000029*/
30
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
sewardj4cfea4f2006-10-14 19:26:10 +000032#include "pub_core_vki.h"
sewardj17c11042006-10-15 01:26:40 +000033#include "pub_core_vkiscnums.h"
njnc7561b92005-06-19 01:24:32 +000034#include "pub_core_threadstate.h"
sewardj14c7cc52007-02-25 15:08:24 +000035#include "pub_core_xarray.h"
sewardj45f4e7c2005-09-27 19:20:21 +000036#include "pub_core_clientstate.h"
sewardj55f9d1a2005-04-25 11:11:44 +000037#include "pub_core_aspacemgr.h"
njnac1e0332009-05-08 00:39:31 +000038#include "pub_core_aspacehl.h"
Elliott Hughesa0664b92017-04-18 17:46:52 -070039#include "pub_core_clreq.h"
sewardj45f4e7c2005-09-27 19:20:21 +000040#include "pub_core_commandline.h"
njn2521d322005-05-08 14:45:13 +000041#include "pub_core_debuglog.h"
42#include "pub_core_errormgr.h"
43#include "pub_core_execontext.h"
sewardj3b290482011-05-06 21:02:55 +000044#include "pub_core_gdbserver.h"
sewardj17c11042006-10-15 01:26:40 +000045#include "pub_core_initimg.h"
njn97405b22005-06-02 03:39:33 +000046#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000047#include "pub_core_libcassert.h"
njneb8896b2005-06-04 20:03:55 +000048#include "pub_core_libcfile.h"
njn36a20fa2005-06-03 03:08:39 +000049#include "pub_core_libcprint.h"
njnf39e9a32005-06-12 02:43:17 +000050#include "pub_core_libcproc.h"
njnde62cbf2005-06-10 22:08:14 +000051#include "pub_core_libcsignal.h"
sewardj17c5e2e2012-12-28 09:12:14 +000052#include "pub_core_sbprofile.h"
sewardj45f4e7c2005-09-27 19:20:21 +000053#include "pub_core_syscall.h" // VG_(strerror)
njnf76d27a2009-05-28 01:53:07 +000054#include "pub_core_mach.h"
njnf536bbb2005-06-13 04:21:38 +000055#include "pub_core_machine.h"
njnaf1d7df2005-06-11 01:31:52 +000056#include "pub_core_mallocfree.h"
njn20242342005-05-16 23:31:24 +000057#include "pub_core_options.h"
sewardjfdf91b42005-09-28 00:53:09 +000058#include "pub_core_debuginfo.h"
njnd1af0032005-05-29 17:01:48 +000059#include "pub_core_redir.h"
njnc7561b92005-06-19 01:24:32 +000060#include "pub_core_scheduler.h"
sewardjf9ebc392010-05-09 22:30:43 +000061#include "pub_core_seqmatch.h" // For VG_(string_match)
njn0c246472005-05-31 01:00:08 +000062#include "pub_core_signals.h"
njn2025cf92005-06-26 20:44:48 +000063#include "pub_core_stacks.h" // For VG_(register_stack)
njnc1b01812005-06-17 22:19:06 +000064#include "pub_core_syswrap.h"
njn43b9a8a2005-05-10 04:37:01 +000065#include "pub_core_tooliface.h"
sewardj17c11042006-10-15 01:26:40 +000066#include "pub_core_translate.h" // For VG_(translate)
njna7598f62005-06-18 03:27:58 +000067#include "pub_core_trampoline.h"
njn8bddf582005-05-13 23:40:55 +000068#include "pub_core_transtab.h"
florianc91f5842013-09-15 10:42:26 +000069#include "pub_core_inner.h"
philippe14711e82012-06-14 22:18:50 +000070#if defined(ENABLE_INNER_CLIENT_REQUEST)
florian1a046d52013-09-16 20:56:35 +000071#include "pub_core_clreq.h"
philippe14711e82012-06-14 22:18:50 +000072#endif
sewardj17c11042006-10-15 01:26:40 +000073
sewardjb5f6f512005-03-10 23:59:00 +000074
nethercote71980f02004-01-24 18:18:54 +000075/*====================================================================*/
sewardj71bc3cb2005-05-19 00:25:45 +000076/*=== Command-line: variables, processing, etc ===*/
77/*====================================================================*/
78
79// See pub_{core,tool}_options.h for explanations of all these.
80
sewardj45f4e7c2005-09-27 19:20:21 +000081static void usage_NORETURN ( Bool debug_help )
njn7cf0bd32002-06-08 13:36:03 +000082{
florian95a128b2011-09-29 14:26:38 +000083 /* 'usage1' contains a %s
84 - for the name of the GDB executable
85 - for the name of vgdb's path prefix
86 which must be supplied when they are VG_(printf)'d. */
floriane543f302012-10-21 19:43:43 +000087 const HChar usage1[] =
njn00cfcfc2005-11-12 18:53:50 +000088"usage: valgrind [options] prog-and-args\n"
njn25e49d8e72002-09-23 09:36:25 +000089"\n"
njn97db7612009-08-04 02:32:55 +000090" tool-selection option, with default in [ ]:\n"
sewardjb5f6f512005-03-10 23:59:00 +000091" --tool=<name> use the Valgrind tool named <name> [memcheck]\n"
njn97db7612009-08-04 02:32:55 +000092"\n"
93" basic user options for all Valgrind tools, with defaults in [ ]:\n"
nethercotea76368b2004-06-16 11:56:29 +000094" -h --help show this message\n"
nethercote6c999f22004-01-31 22:55:15 +000095" --help-debug show this message, plus debugging options\n"
njn25e49d8e72002-09-23 09:36:25 +000096" --version show version\n"
njn25e49d8e72002-09-23 09:36:25 +000097" -q --quiet run silently; only print error msgs\n"
sewardj2d9e8742009-08-07 15:46:56 +000098" -v --verbose be more verbose -- show misc extra info\n"
sewardj6e31f802007-11-17 22:29:25 +000099" --trace-children=no|yes Valgrind-ise child processes (follow execve)? [no]\n"
sewardj06421272009-11-05 08:55:13 +0000100" --trace-children-skip=patt1,patt2,... specifies a list of executables\n"
101" that --trace-children=yes should not trace into\n"
sewardj9ab64a42010-12-06 11:40:04 +0000102" --trace-children-skip-by-arg=patt1,patt2,... same as --trace-children-skip=\n"
103" but check the argv[] entries for children, rather\n"
104" than the exe name, to make a follow/no-follow decision\n"
njn97db7612009-08-04 02:32:55 +0000105" --child-silent-after-fork=no|yes omit child output between fork & exec? [no]\n"
sewardj3b290482011-05-06 21:02:55 +0000106" --vgdb=no|yes|full activate gdbserver? [yes]\n"
107" full is slower but provides precise watchpoint/step\n"
sewardj1568e172011-06-18 08:28:04 +0000108" --vgdb-error=<number> invoke gdbserver after <number> errors [%d]\n"
109" to get started quickly, use --vgdb-error=0\n"
110" and follow the on-screen directions\n"
philippe180a7502014-04-20 13:41:10 +0000111" --vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none]\n"
sewardj122f6af2014-09-03 21:58:54 +0000112" where event is one of:\n"
113" startup exit valgrindabexit all none\n"
nethercote0d588502004-06-21 13:27:11 +0000114" --track-fds=no|yes track open file descriptors? [no]\n"
thughes6233a382004-08-21 11:10:44 +0000115" --time-stamp=no|yes add timestamps to log messages? [no]\n"
njnce545552005-07-25 22:36:52 +0000116" --log-fd=<number> log messages to file descriptor [2=stderr]\n"
njn374a36d2007-11-23 01:41:32 +0000117" --log-file=<file> log messages to <file>\n"
njnce545552005-07-25 22:36:52 +0000118" --log-socket=ipaddr:port log messages to socket ipaddr:port\n"
nethercote2b0793f2003-12-02 10:41:18 +0000119"\n"
nethercote2b0793f2003-12-02 10:41:18 +0000120" user options for Valgrind tools that report errors:\n"
sewardj738856f2009-07-15 14:48:32 +0000121" --xml=yes emit error output in XML (some tools only)\n"
122" --xml-fd=<number> XML output to file descriptor\n"
123" --xml-file=<file> XML output to <file>\n"
124" --xml-socket=ipaddr:port XML output to socket ipaddr:port\n"
125" --xml-user-comment=STR copy STR verbatim into XML output\n"
nethercote2b0793f2003-12-02 10:41:18 +0000126" --demangle=no|yes automatically demangle C++ names? [yes]\n"
njn20b4a152005-10-19 22:39:40 +0000127" --num-callers=<number> show <number> callers in stack traces [12]\n"
nethercote2b0793f2003-12-02 10:41:18 +0000128" --error-limit=no|yes stop showing new errors if too many? [yes]\n"
sewardjb9779082006-05-12 23:50:15 +0000129" --error-exitcode=<number> exit code to return if errors found [0=disable]\n"
philippe7b3d3562014-11-12 19:43:29 +0000130" --error-markers=<begin>,<end> add lines with begin/end markers before/after\n"
131" each error output in plain text mode [none]\n"
nethercote2b0793f2003-12-02 10:41:18 +0000132" --show-below-main=no|yes continue stack traces below main() [no]\n"
barta6efdfa2014-06-24 05:08:21 +0000133" --default-suppressions=yes|no\n"
134" load default suppressions [yes]\n"
nethercote2b0793f2003-12-02 10:41:18 +0000135" --suppressions=<filename> suppress errors described in <filename>\n"
sewardjd153fae2005-01-10 17:24:47 +0000136" --gen-suppressions=no|yes|all print suppressions for errors? [no]\n"
nethercote04d0fbc2004-01-26 16:48:06 +0000137" --input-fd=<number> file descriptor for input [0=stdin]\n"
sewardj29dd9e62015-08-31 14:37:25 +0000138" --dsymutil=no|yes run dsymutil on Mac OS X when helpful? [yes]\n"
sewardj97724e52005-04-02 23:40:59 +0000139" --max-stackframe=<number> assume stack switch for SP changes larger\n"
140" than <number> bytes [2000000]\n"
sewardj95d86c02007-12-18 01:49:23 +0000141" --main-stacksize=<number> set size of main thread's stack (in bytes)\n"
philippe3bcd51d2013-06-12 21:45:39 +0000142" [min(max(current 'ulimit' value,1MB),16MB)]\n"
njn97db7612009-08-04 02:32:55 +0000143"\n"
144" user options for Valgrind tools that replace malloc:\n"
philipped99c26a2012-07-31 22:17:28 +0000145" --alignment=<number> set minimum alignment of heap allocations [%s]\n"
146" --redzone-size=<number> set minimum size of redzones added before/after\n"
147" heap blocks (in bytes). [%s]\n"
njn97db7612009-08-04 02:32:55 +0000148"\n"
149" uncommon user options for all Valgrind tools:\n"
sewardj14cdbf82010-10-12 00:44:05 +0000150" --fullpath-after= (with nothing after the '=')\n"
151" show full source paths in call stacks\n"
152" --fullpath-after=string like --fullpath-after=, but only show the\n"
153" part of the path after 'string'. Allows removal\n"
154" of path prefixes. Use this flag multiple times\n"
155" to specify a set of prefixes to remove.\n"
sewardj8b6573d2012-12-05 22:15:14 +0000156" --extra-debuginfo-path=path absolute path to search for additional\n"
157" debug symbols, in addition to existing default\n"
158" well known search paths.\n"
sewardj5d616df2013-07-02 08:07:15 +0000159" --debuginfo-server=ipaddr:port also query this server\n"
160" (valgrind-di-server) for debug symbols\n"
161" --allow-mismatched-debuginfo=no|yes [no]\n"
162" for the above two flags only, accept debuginfo\n"
163" objects that don't \"match\" the main object\n"
sewardj6d9a3382015-08-31 13:05:35 +0000164" --smc-check=none|stack|all|all-non-file [all-non-file]\n"
sewardj6dbcc632011-06-07 21:39:28 +0000165" checks for self-modifying code: none, only for\n"
166" code found in stacks, for all code, or for all\n"
167" code except that from file-backed mappings\n"
philippea0a73932014-06-15 15:42:20 +0000168" --read-inline-info=yes|no read debug info about inlined function calls\n"
sewardj47c6d142014-09-12 09:22:36 +0000169" and use it to do better stack traces. [yes]\n"
iraisr4e1d9462015-09-01 11:51:16 +0000170" on Linux/Android/Solaris for Memcheck/Helgrind/DRD\n"
sewardj47c6d142014-09-12 09:22:36 +0000171" only. [no] for all other tools and platforms.\n"
njn97db7612009-08-04 02:32:55 +0000172" --read-var-info=yes|no read debug info on stack and global variables\n"
173" and use it to print better error messages in\n"
174" tools that make use of it (Memcheck, Helgrind,\n"
bartf6122a02010-03-27 07:38:39 +0000175" DRD) [no]\n"
sewardj3b290482011-05-06 21:02:55 +0000176" --vgdb-poll=<number> gdbserver poll max every <number> basic blocks [%d] \n"
177" --vgdb-shadow-registers=no|yes let gdb see the shadow registers [no]\n"
178" --vgdb-prefix=<prefix> prefix for vgdb FIFOs [%s]\n"
njn97db7612009-08-04 02:32:55 +0000179" --run-libc-freeres=no|yes free up glibc memory at exit on Linux? [yes]\n"
Elliott Hughesa0664b92017-04-18 17:46:52 -0700180" --run-cxx-freeres=no|yes free up libstdc++ memory at exit on Linux\n"
181" and Solaris? [yes]\n"
philippeec905f72014-08-17 20:03:51 +0000182" --sim-hints=hint1,hint2,... activate unusual sim behaviours [none] \n"
sewardj122f6af2014-09-03 21:58:54 +0000183" where hint is one of:\n"
sewardj8eb8bab2015-07-21 14:44:28 +0000184" lax-ioctls lax-doors fuse-compatible enable-outer\n"
philippe98486902014-08-19 22:46:44 +0000185" no-inner-prefix no-nptl-pthread-stackcache none\n"
bart78bfc712011-12-08 16:14:59 +0000186" --fair-sched=no|yes|try schedule threads fairly on multicore systems [no]\n"
sewardj4450a0e2014-09-03 15:19:25 +0000187" --kernel-variant=variant1,variant2,...\n"
188" handle non-standard kernel variants [none]\n"
189" where variant is one of:\n"
sewardj124e56d2014-09-06 14:45:12 +0000190" bproc android-no-hw-tls\n"
sewardj4450a0e2014-09-03 15:19:25 +0000191" android-gpu-sgx5xx android-gpu-adreno3xx none\n"
philippe46207652013-01-20 17:11:58 +0000192" --merge-recursive-frames=<number> merge frames between identical\n"
193" program counters in max <number> frames) [0]\n"
philippe8e1bee42013-10-18 00:08:20 +0000194" --num-transtab-sectors=<number> size of translated code cache [%d]\n"
sewardja11ec172013-10-18 11:18:45 +0000195" more sectors may increase performance, but use more memory.\n"
philippe924c8522015-03-15 12:24:19 +0000196" --avg-transtab-entry-size=<number> avg size in bytes of a translated\n"
197" basic block [0, meaning use tool provided default]\n"
philippee4d78122014-04-20 14:20:37 +0000198" --aspace-minaddr=0xPP avoid mapping memory below 0xPP [guessed]\n"
philipped0720e42015-03-12 20:43:46 +0000199" --valgrind-stacksize=<number> size of valgrind (host) thread's stack\n"
200" (in bytes) ["
201 VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)
202 "]\n"
njn97db7612009-08-04 02:32:55 +0000203" --show-emwarns=no|yes show warnings about emulation limits? [no]\n"
sewardjf9ebc392010-05-09 22:30:43 +0000204" --require-text-symbol=:sonamepattern:symbolpattern abort run if the\n"
205" stated shared object doesn't have the stated\n"
206" text symbol. Patterns can contain ? and *.\n"
philippe1e470b52012-05-11 19:33:46 +0000207" --soname-synonyms=syn1=pattern1,syn2=pattern2,... synonym soname\n"
sewardj260c6482012-08-07 14:46:34 +0000208" specify patterns for function wrapping or replacement.\n"
209" To use a non-libc malloc library that is\n"
210" in the main exe: --soname-synonyms=somalloc=NONE\n"
211" in libxyzzy.so: --soname-synonyms=somalloc=libxyzzy.so\n"
sewardjc30cd9b2012-12-06 18:08:54 +0000212" --sigill-diagnostics=yes|no warn about illegal instructions? [yes]\n"
sewardj49984ea2013-10-18 13:21:26 +0000213" --unw-stack-scan-thresh=<number> Enable stack-scan unwind if fewer\n"
214" than <number> good frames found [0, meaning \"disabled\"]\n"
215" NOTE: stack scanning is only available on arm-linux.\n"
216" --unw-stack-scan-frames=<number> Max number of frames that can be\n"
217" recovered by stack scanning [5]\n"
sewardj2f870902014-10-23 21:49:58 +0000218" --resync-filter=no|yes|verbose [yes on MacOS, no on other OSes]\n"
219" attempt to avoid expensive address-space-resync operations\n"
florian1e802b62015-02-13 19:08:26 +0000220" --max-threads=<number> maximum number of threads that valgrind can\n"
221" handle [%d]\n"
fitzhardinge98abfc72003-12-16 02:05:15 +0000222"\n";
njn7cf0bd32002-06-08 13:36:03 +0000223
floriane543f302012-10-21 19:43:43 +0000224 const HChar usage2[] =
njn25e49d8e72002-09-23 09:36:25 +0000225"\n"
nethercote2b0793f2003-12-02 10:41:18 +0000226" debugging options for all Valgrind tools:\n"
njn97db7612009-08-04 02:32:55 +0000227" -d show verbose debugging output\n"
njnb1cc5d62010-07-06 04:05:23 +0000228" --stats=no|yes show tool and core statistics [no]\n"
njn25e49d8e72002-09-23 09:36:25 +0000229" --sanity-level=<number> level of sanity checking to do [1]\n"
sewardjfa8ec112005-01-19 11:55:34 +0000230" --trace-flags=<XXXXXXXX> show generated code? (X = 0|1) [00000000]\n"
231" --profile-flags=<XXXXXXXX> ditto, but for profiling (X = 0|1) [00000000]\n"
sewardj17c5e2e2012-12-28 09:12:14 +0000232" --profile-interval=<number> show profile every <number> event checks\n"
233" [0, meaning only at the end of the run]\n"
sewardj33afdb52006-01-17 02:36:40 +0000234" --trace-notbelow=<number> only show BBs above <number> [999999999]\n"
florian29e022d2012-07-02 21:13:34 +0000235" --trace-notabove=<number> only show BBs below <number> [0]\n"
njn25e49d8e72002-09-23 09:36:25 +0000236" --trace-syscalls=no|yes show all system calls? [no]\n"
237" --trace-signals=no|yes show signal handling details? [no]\n"
238" --trace-symtab=no|yes show symbol table details? [no]\n"
sewardjf767d962007-02-12 17:47:14 +0000239" --trace-symtab-patt=<patt> limit debuginfo tracing to obj name <patt>\n"
sewardjce058b02005-05-01 08:55:38 +0000240" --trace-cfi=no|yes show call-frame-info details? [no]\n"
sewardjf767d962007-02-12 17:47:14 +0000241" --debug-dump=syms mimic /usr/bin/readelf --syms\n"
242" --debug-dump=line mimic /usr/bin/readelf --debug-dump=line\n"
243" --debug-dump=frames mimic /usr/bin/readelf --debug-dump=frames\n"
sewardj0ec07f32006-01-12 12:32:32 +0000244" --trace-redir=no|yes show redirection details? [no]\n"
njn25e49d8e72002-09-23 09:36:25 +0000245" --trace-sched=no|yes show thread scheduler details? [no]\n"
sewardj9c606bd2008-09-18 18:12:50 +0000246" --profile-heap=no|yes profile Valgrind's own space use\n"
philippe06444372012-10-12 21:46:55 +0000247" --core-redzone-size=<number> set minimum size of redzones added before/after\n"
philipped99c26a2012-07-31 22:17:28 +0000248" heap blocks allocated for Valgrind internal use (in bytes) [4]\n"
jsgf855d93d2003-10-13 22:26:55 +0000249" --wait-for-gdb=yes|no pause on startup to wait for gdb attach\n"
sewardj17c11042006-10-15 01:26:40 +0000250" --sym-offsets=yes|no show syms in form 'name+offset' ? [no]\n"
sewardjb5f6f512005-03-10 23:59:00 +0000251" --command-line-only=no|yes only use command line options [no]\n"
njn613812e2005-03-11 04:57:30 +0000252"\n"
njn97db7612009-08-04 02:32:55 +0000253" Vex options for all Valgrind tools:\n"
254" --vex-iropt-verbosity=<0..9> [0]\n"
255" --vex-iropt-level=<0..2> [2]\n"
njn97db7612009-08-04 02:32:55 +0000256" --vex-iropt-unroll-thresh=<0..400> [120]\n"
257" --vex-guest-max-insns=<1..100> [50]\n"
258" --vex-guest-chase-thresh=<0..99> [10]\n"
sewardj540cc4a2010-01-15 10:57:57 +0000259" --vex-guest-chase-cond=no|yes [no]\n"
sewardj8d47a612015-02-05 12:59:46 +0000260" Precise exception control. Possible values for 'mode' are as follows\n"
261" and specify the minimum set of registers guaranteed to be correct\n"
262" immediately prior to memory access instructions:\n"
263" sp-at-mem-access stack pointer only\n"
264" unwindregs-at-mem-access registers needed for stack unwinding\n"
265" allregs-at-mem-access all registers\n"
266" allregs-at-each-insn all registers are always correct\n"
267" Default value for all 3 following flags is [unwindregs-at-mem-access].\n"
268" --vex-iropt-register-updates=mode setting to use by default\n"
269" --px-default=mode synonym for --vex-iropt-register-updates\n"
270" --px-file-backed=mode optional setting for file-backed (non-JIT) code\n"
271" Tracing and profile control:\n"
272" --trace-flags and --profile-flags values (omit the middle space):\n"
273" 1000 0000 show conversion into IR\n"
274" 0100 0000 show after initial opt\n"
275" 0010 0000 show after instrumentation\n"
276" 0001 0000 show after second opt\n"
277" 0000 1000 show after tree building\n"
278" 0000 0100 show selecting insns\n"
279" 0000 0010 show after reg-alloc\n"
280" 0000 0001 show final assembly\n"
281" 0000 0000 show summary profile only\n"
282" (Nb: you need --trace-notbelow and/or --trace-notabove\n"
283" with --trace-flags for full details)\n"
sewardj2a99cf62004-11-24 10:44:19 +0000284"\n"
nethercote2b0793f2003-12-02 10:41:18 +0000285" debugging options for Valgrind tools that report errors\n"
286" --dump-error=<number> show translation for basic block associated\n"
287" with <number>'th error context [0=show none]\n"
njn97db7612009-08-04 02:32:55 +0000288"\n"
289" debugging options for Valgrind tools that replace malloc:\n"
290" --trace-malloc=no|yes show client malloc details? [no]\n"
fitzhardinge98abfc72003-12-16 02:05:15 +0000291"\n";
njn3e884182003-04-15 13:03:23 +0000292
floriane543f302012-10-21 19:43:43 +0000293 const HChar usage3[] =
njn3e884182003-04-15 13:03:23 +0000294"\n"
nethercote71980f02004-01-24 18:18:54 +0000295" Extra options read from ~/.valgrindrc, $VALGRIND_OPTS, ./.valgrindrc\n"
njn25e49d8e72002-09-23 09:36:25 +0000296"\n"
njn10b9aea2009-07-14 06:55:05 +0000297" %s is %s\n"
sewardjb3a1e4b2015-08-21 11:32:26 +0000298" Valgrind is Copyright (C) 2000-2015, and GNU GPL'd, by Julian Seward et al.\n"
299" LibVEX is Copyright (C) 2004-2015, and GNU GPL'd, by OpenWorks LLP et al.\n"
njnd04b7c62002-10-03 14:05:52 +0000300"\n"
njn10b9aea2009-07-14 06:55:05 +0000301" Bug reports, feedback, admiration, abuse, etc, to: %s.\n"
njn25e49d8e72002-09-23 09:36:25 +0000302"\n";
njn7cf0bd32002-06-08 13:36:03 +0000303
florianf44ff622014-12-20 16:52:08 +0000304 HChar default_alignment[30]; // large enough
305 HChar default_redzone_size[30]; // large enough
sewardj12373b12007-11-20 21:38:14 +0000306
njnbe9b47b2005-05-15 16:22:58 +0000307 // Ensure the message goes to stdout
sewardj738856f2009-07-15 14:48:32 +0000308 VG_(log_output_sink).fd = 1;
309 VG_(log_output_sink).is_socket = False;
njnbe9b47b2005-05-15 16:22:58 +0000310
philipped99c26a2012-07-31 22:17:28 +0000311 if (VG_(needs).malloc_replacement) {
312 VG_(sprintf)(default_alignment, "%d", VG_MIN_MALLOC_SZB);
313 VG_(sprintf)(default_redzone_size, "%lu", VG_(tdict).tool_client_redzone_szB);
314 } else {
315 VG_(strcpy)(default_alignment, "not used by this tool");
316 VG_(strcpy)(default_redzone_size, "not used by this tool");
317 }
318 /* 'usage1' a type as described after each arg. */
sewardj3b290482011-05-06 21:02:55 +0000319 VG_(printf)(usage1,
philipped99c26a2012-07-31 22:17:28 +0000320 VG_(clo_vgdb_error) /* int */,
philipped99c26a2012-07-31 22:17:28 +0000321 default_alignment /* char* */,
322 default_redzone_size /* char* */,
323 VG_(clo_vgdb_poll) /* int */,
philippe8e1bee42013-10-18 00:08:20 +0000324 VG_(vgdb_prefix_default)() /* char* */,
florian1e802b62015-02-13 19:08:26 +0000325 N_SECTORS_DEFAULT /* int */,
326 MAX_THREADS_DEFAULT /* int */
philipped99c26a2012-07-31 22:17:28 +0000327 );
fitzhardinge98abfc72003-12-16 02:05:15 +0000328 if (VG_(details).name) {
329 VG_(printf)(" user options for %s:\n", VG_(details).name);
fitzhardinge98abfc72003-12-16 02:05:15 +0000330 if (VG_(needs).command_line_options)
njn51d827b2005-05-09 01:02:08 +0000331 VG_TDICT_CALL(tool_print_usage);
fitzhardinge98abfc72003-12-16 02:05:15 +0000332 else
333 VG_(printf)(" (none)\n");
334 }
nethercote6c999f22004-01-31 22:55:15 +0000335 if (debug_help) {
sewardjbbaef872008-11-01 23:55:32 +0000336 VG_(printf)("%s", usage2);
fitzhardinge98abfc72003-12-16 02:05:15 +0000337
nethercote6c999f22004-01-31 22:55:15 +0000338 if (VG_(details).name) {
339 VG_(printf)(" debugging options for %s:\n", VG_(details).name);
340
341 if (VG_(needs).command_line_options)
njn51d827b2005-05-09 01:02:08 +0000342 VG_TDICT_CALL(tool_print_debug_usage);
nethercote6c999f22004-01-31 22:55:15 +0000343 else
344 VG_(printf)(" (none)\n");
345 }
fitzhardinge98abfc72003-12-16 02:05:15 +0000346 }
njn10b9aea2009-07-14 06:55:05 +0000347 VG_(printf)(usage3, VG_(details).name, VG_(details).copyright_author,
348 VG_BUGS_TO);
nethercotef4928da2004-06-15 10:54:40 +0000349 VG_(exit)(0);
njn7cf0bd32002-06-08 13:36:03 +0000350}
sewardjde4a1d02002-03-22 01:27:54 +0000351
sewardjde4a1d02002-03-22 01:27:54 +0000352
sewardj95d86c02007-12-18 01:49:23 +0000353/* Peer at previously set up VG_(args_for_valgrind) and do some
354 minimal command line processing that must happen early on:
sewardj45f4e7c2005-09-27 19:20:21 +0000355
sewardj95d86c02007-12-18 01:49:23 +0000356 - show the version string, if requested (-v)
357 - extract any request for help (--help, -h, --help-debug)
358 - get the toolname (--tool=)
359 - set VG_(clo_max_stackframe) (--max-stackframe=)
360 - set VG_(clo_main_stacksize) (--main-stacksize=)
philippe72faf102012-03-11 22:24:03 +0000361 - set VG_(clo_sim_hints) (--sim-hints=)
floriane13efb82015-03-11 13:46:14 +0000362 - set VG_(clo_max_threads) (--max-threads)
sewardj95d86c02007-12-18 01:49:23 +0000363
364 That's all it does. The main command line processing is done below
365 by main_process_cmd_line_options. Note that
366 main_process_cmd_line_options has to handle but ignore the ones we
367 have handled here.
368*/
369static void early_process_cmd_line_options ( /*OUT*/Int* need_help,
florian19f91bb2012-11-10 22:29:54 +0000370 /*OUT*/const HChar** tool )
sewardj45f4e7c2005-09-27 19:20:21 +0000371{
372 UInt i;
373 HChar* str;
sewardj8b635a42004-11-22 19:01:47 +0000374
sewardj14c7cc52007-02-25 15:08:24 +0000375 vg_assert( VG_(args_for_valgrind) );
nethercote71980f02004-01-24 18:18:54 +0000376
sewardj14c7cc52007-02-25 15:08:24 +0000377 /* parse the options we have (only the options we care about now) */
378 for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) {
379
380 str = * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i );
sewardj45f4e7c2005-09-27 19:20:21 +0000381 vg_assert(str);
nethercote71980f02004-01-24 18:18:54 +0000382
njn83df0b62009-02-25 01:01:05 +0000383 // Nb: the version string goes to stdout.
sewardj738856f2009-07-15 14:48:32 +0000384 if VG_XACT_CLO(str, "--version", VG_(log_output_sink).fd, 1) {
385 VG_(log_output_sink).is_socket = False;
sewardj45f4e7c2005-09-27 19:20:21 +0000386 VG_(printf)("valgrind-" VERSION "\n");
387 VG_(exit)(0);
njn83df0b62009-02-25 01:01:05 +0000388 }
njncce38e62010-07-06 04:25:12 +0000389 else if VG_XACT_CLO(str, "--help", *need_help, *need_help+1) {}
390 else if VG_XACT_CLO(str, "-h", *need_help, *need_help+1) {}
sewardj45f4e7c2005-09-27 19:20:21 +0000391
njncce38e62010-07-06 04:25:12 +0000392 else if VG_XACT_CLO(str, "--help-debug", *need_help, *need_help+2) {}
nethercote71980f02004-01-24 18:18:54 +0000393
sewardj45f4e7c2005-09-27 19:20:21 +0000394 // The tool has already been determined, but we need to know the name
395 // here.
njn83df0b62009-02-25 01:01:05 +0000396 else if VG_STR_CLO(str, "--tool", *tool) {}
sewardj5bdfbd22007-12-15 22:13:05 +0000397
sewardj95d86c02007-12-18 01:49:23 +0000398 // Set up VG_(clo_max_stackframe) and VG_(clo_main_stacksize).
399 // These are needed by VG_(ii_create_image), which happens
400 // before main_process_cmd_line_options().
njn83df0b62009-02-25 01:01:05 +0000401 else if VG_INT_CLO(str, "--max-stackframe", VG_(clo_max_stackframe)) {}
402 else if VG_INT_CLO(str, "--main-stacksize", VG_(clo_main_stacksize)) {}
philippe72faf102012-03-11 22:24:03 +0000403
florian1e802b62015-02-13 19:08:26 +0000404 // Set up VG_(clo_max_threads); needed for VG_(tl_pre_clo_init)
405 else if VG_INT_CLO(str, "--max-threads", VG_(clo_max_threads)) {}
406
philippe72faf102012-03-11 22:24:03 +0000407 // Set up VG_(clo_sim_hints). This is needed a.o. for an inner
408 // running in an outer, to have "no-inner-prefix" enabled
409 // as early as possible.
philippeec905f72014-08-17 20:03:51 +0000410 else if VG_USETX_CLO (str, "--sim-hints",
sewardj8eb8bab2015-07-21 14:44:28 +0000411 "lax-ioctls,lax-doors,fuse-compatible,"
philippeb071b712014-08-24 11:24:10 +0000412 "enable-outer,no-inner-prefix,"
philippe98486902014-08-19 22:46:44 +0000413 "no-nptl-pthread-stackcache",
philippeec905f72014-08-17 20:03:51 +0000414 VG_(clo_sim_hints)) {}
nethercote71980f02004-01-24 18:18:54 +0000415 }
florian1e802b62015-02-13 19:08:26 +0000416
417 /* For convenience */
418 VG_N_THREADS = VG_(clo_max_threads);
nethercote71980f02004-01-24 18:18:54 +0000419}
420
sewardj95d86c02007-12-18 01:49:23 +0000421/* The main processing for command line options. See comments above
sewardj738856f2009-07-15 14:48:32 +0000422 on early_process_cmd_line_options.
423
424 Comments on how the logging options are handled:
425
426 User can specify:
427 --log-fd= for a fd to write to (default setting, fd = 2)
428 --log-file= for a file name to write to
429 --log-socket= for a socket to write to
430
431 As a result of examining these and doing relevant socket/file
432 opening, a final fd is established. This is stored in
433 VG_(log_output_sink) in m_libcprint. Also, if --log-file=STR was
434 specified, then STR, after expansion of %p and %q templates within
435 it, is stored in VG_(clo_log_fname_expanded), in m_options, just in
436 case anybody wants to know what it is.
437
438 When printing, VG_(log_output_sink) is consulted to find the
439 fd to send output to.
440
441 Exactly analogous actions are undertaken for the XML output
442 channel, with the one difference that the default fd is -1, meaning
443 the channel is disabled by default.
sewardj95d86c02007-12-18 01:49:23 +0000444*/
sewardj738856f2009-07-15 14:48:32 +0000445static
446void main_process_cmd_line_options ( /*OUT*/Bool* logging_to_fd,
florian801c1992014-10-17 21:07:37 +0000447 /*OUT*/const HChar** xml_fname_unexpanded,
sewardj738856f2009-07-15 14:48:32 +0000448 const HChar* toolname )
nethercote71980f02004-01-24 18:18:54 +0000449{
njnda033f52005-12-19 21:27:58 +0000450 // VG_(clo_log_fd) is used by all the messaging. It starts as 2 (stderr)
451 // and we cannot change it until we know what we are changing it to is
452 // ok. So we have tmp_log_fd to hold the tmp fd prior to that point.
sewardj92645592005-07-23 09:18:34 +0000453 SysRes sres;
sewardj738856f2009-07-15 14:48:32 +0000454 Int i, tmp_log_fd, tmp_xml_fd;
sewardj92645592005-07-23 09:18:34 +0000455 Int toolname_len = VG_(strlen)(toolname);
florian19f91bb2012-11-10 22:29:54 +0000456 const HChar* tmp_str; // Used in a couple of places.
njnbe9b47b2005-05-15 16:22:58 +0000457 enum {
458 VgLogTo_Fd,
459 VgLogTo_File,
njnbe9b47b2005-05-15 16:22:58 +0000460 VgLogTo_Socket
sewardj738856f2009-07-15 14:48:32 +0000461 } log_to = VgLogTo_Fd, // Where is logging output to be sent?
462 xml_to = VgLogTo_Fd; // Where is XML output to be sent?
sewardjde4a1d02002-03-22 01:27:54 +0000463
sewardj738856f2009-07-15 14:48:32 +0000464 /* Temporarily holds the string STR specified with
465 --{log,xml}-{name,socket}=STR. 'fs' stands for
466 file-or-socket. */
florian19f91bb2012-11-10 22:29:54 +0000467 const HChar* log_fsname_unexpanded = NULL;
468 const HChar* xml_fsname_unexpanded = NULL;
sewardj738856f2009-07-15 14:48:32 +0000469
sewardjc30cd9b2012-12-06 18:08:54 +0000470 /* Whether the user has explicitly provided --sigill-diagnostics.
471 If not explicitly given depends on general verbosity setting. */
472 Bool sigill_diag_set = False;
473
sewardj738856f2009-07-15 14:48:32 +0000474 /* Log to stderr by default, but usage message goes to stdout. XML
475 output is initially disabled. */
njnda033f52005-12-19 21:27:58 +0000476 tmp_log_fd = 2;
sewardj738856f2009-07-15 14:48:32 +0000477 tmp_xml_fd = -1;
478
sewardj19d81412002-06-03 01:10:40 +0000479 /* Check for sane path in ./configure --prefix=... */
fitzhardinge98abfc72003-12-16 02:05:15 +0000480 if (VG_LIBDIR[0] != '/')
sewardj17c11042006-10-15 01:26:40 +0000481 VG_(err_config_error)("Please use absolute paths in "
florian1763e812011-07-12 19:07:05 +0000482 "./configure --prefix=... or --libdir=...\n");
sewardj38170912002-05-10 21:07:22 +0000483
sewardj14c7cc52007-02-25 15:08:24 +0000484 vg_assert( VG_(args_for_valgrind) );
nethercote71980f02004-01-24 18:18:54 +0000485
florian79316272014-10-07 18:36:28 +0000486 VG_(clo_suppressions) = VG_(newXA)(VG_(malloc), "main.mpclo.4",
487 VG_(free), sizeof(HChar *));
488 VG_(clo_fullpath_after) = VG_(newXA)(VG_(malloc), "main.mpclo.5",
489 VG_(free), sizeof(HChar *));
490 VG_(clo_req_tsyms) = VG_(newXA)(VG_(malloc), "main.mpclo.6",
491 VG_(free), sizeof(HChar *));
492
sewardj8d47a612015-02-05 12:59:46 +0000493 /* Constants for parsing PX control flags. */
494 const HChar* pxStrings[5]
495 = { "sp-at-mem-access", "unwindregs-at-mem-access",
496 "allregs-at-mem-access", "allregs-at-each-insn", NULL };
497 const VexRegisterUpdates pxVals[5]
498 = { VexRegUpdSpAtMemAccess, VexRegUpdUnwindregsAtMemAccess,
499 VexRegUpdAllregsAtMemAccess, VexRegUpdAllregsAtEachInsn, 0/*inval*/ };
500
sewardj738856f2009-07-15 14:48:32 +0000501 /* BEGIN command-line processing loop */
502
sewardj14c7cc52007-02-25 15:08:24 +0000503 for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) {
504
505 HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i );
sewardj45f4e7c2005-09-27 19:20:21 +0000506 HChar* colon = arg;
sewardj8d47a612015-02-05 12:59:46 +0000507 UInt ix = 0;
nethercote71980f02004-01-24 18:18:54 +0000508
njn1274d242007-03-26 23:38:42 +0000509 // Look for a colon in the option name.
thughes3bfd5a02004-07-18 08:05:44 +0000510 while (*colon && *colon != ':' && *colon != '=')
511 colon++;
nethercote71980f02004-01-24 18:18:54 +0000512
njn1274d242007-03-26 23:38:42 +0000513 // Does it have the form "--toolname:foo"? We have to do it at the start
514 // in case someone has combined a prefix with a core-specific option,
515 // eg. "--memcheck:verbose".
thughes3bfd5a02004-07-18 08:05:44 +0000516 if (*colon == ':') {
njn83df0b62009-02-25 01:01:05 +0000517 if (VG_STREQN(2, arg, "--") &&
518 VG_STREQN(toolname_len, arg+2, toolname) &&
519 VG_STREQN(1, arg+2+toolname_len, ":"))
nethercote71980f02004-01-24 18:18:54 +0000520 {
njn1274d242007-03-26 23:38:42 +0000521 // Prefix matches, convert "--toolname:foo" to "--foo".
522 // Two things to note:
523 // - We cannot modify the option in-place. If we did, and then
524 // a child was spawned with --trace-children=yes, the
525 // now-non-prefixed option would be passed and could screw up
526 // the child.
527 // - We create copies, and never free them. Why? Non-prefixed
528 // options hang around forever, so tools need not make copies
529 // of strings within them. We need to have the same behaviour
530 // for prefixed options. The pointer to the copy will be lost
531 // once we leave this function (although a tool may keep a
532 // pointer into it), but the space wasted is insignificant.
533 // (In bug #142197, the copies were being freed, which caused
534 // problems for tools that reasonably assumed that arguments
535 // wouldn't disappear on them.)
nethercote71980f02004-01-24 18:18:54 +0000536 if (0)
537 VG_(printf)("tool-specific arg: %s\n", arg);
sewardj9c606bd2008-09-18 18:12:50 +0000538 arg = VG_(strdup)("main.mpclo.1", arg + toolname_len + 1);
nethercote71980f02004-01-24 18:18:54 +0000539 arg[0] = '-';
540 arg[1] = '-';
541
542 } else {
543 // prefix doesn't match, skip to next arg
544 continue;
545 }
546 }
547
fitzhardinge98abfc72003-12-16 02:05:15 +0000548 /* Ignore these options - they've already been handled */
njn83df0b62009-02-25 01:01:05 +0000549 if VG_STREQN( 7, arg, "--tool=") {}
550 else if VG_STREQN(20, arg, "--command-line-only=") {}
551 else if VG_STREQ( arg, "--") {}
552 else if VG_STREQ( arg, "-d") {}
philippe06444372012-10-12 21:46:55 +0000553 else if VG_STREQN(17, arg, "--max-stackframe=") {}
554 else if VG_STREQN(17, arg, "--main-stacksize=") {}
florian1e802b62015-02-13 19:08:26 +0000555 else if VG_STREQN(14, arg, "--max-threads=") {}
philippee4d78122014-04-20 14:20:37 +0000556 else if VG_STREQN(12, arg, "--sim-hints=") {}
philippe06444372012-10-12 21:46:55 +0000557 else if VG_STREQN(15, arg, "--profile-heap=") {}
558 else if VG_STREQN(20, arg, "--core-redzone-size=") {}
559 else if VG_STREQN(15, arg, "--redzone-size=") {}
philippee4d78122014-04-20 14:20:37 +0000560 else if VG_STREQN(17, arg, "--aspace-minaddr=") {}
nethercote27fec902004-06-16 21:26:32 +0000561
philipped0720e42015-03-12 20:43:46 +0000562 else if VG_BINT_CLO(arg, "--valgrind-stacksize",
563 VG_(clo_valgrind_stacksize),
564 2*VKI_PAGE_SIZE, 10*VG_DEFAULT_STACK_ACTIVE_SZB)
565 {VG_(clo_valgrind_stacksize)
566 = VG_PGROUNDUP(VG_(clo_valgrind_stacksize));}
567
philippe0c0291a2012-08-01 22:03:12 +0000568 /* Obsolete options. Report an error and exit */
569 else if VG_STREQN(34, arg, "--vex-iropt-precise-memory-exns=no") {
570 VG_(fmsg_bad_option)
571 (arg,
572 "--vex-iropt-precise-memory-exns is obsolete\n"
573 "Use --vex-iropt-register-updates=unwindregs-at-mem-access instead\n");
574 }
575 else if VG_STREQN(35, arg, "--vex-iropt-precise-memory-exns=yes") {
576 VG_(fmsg_bad_option)
577 (arg,
578 "--vex-iropt-precise-memory-exns is obsolete\n"
579 "Use --vex-iropt-register-updates=allregs-at-mem-access instead\n"
580 " (or --vex-iropt-register-updates=allregs-at-each-insn)\n");
581 }
582
njn83df0b62009-02-25 01:01:05 +0000583 // These options are new.
584 else if (VG_STREQ(arg, "-v") ||
585 VG_STREQ(arg, "--verbose"))
sewardjde4a1d02002-03-22 01:27:54 +0000586 VG_(clo_verbosity)++;
nethercote27fec902004-06-16 21:26:32 +0000587
njn83df0b62009-02-25 01:01:05 +0000588 else if (VG_STREQ(arg, "-q") ||
589 VG_STREQ(arg, "--quiet"))
sewardjde4a1d02002-03-22 01:27:54 +0000590 VG_(clo_verbosity)--;
591
sewardjc30cd9b2012-12-06 18:08:54 +0000592 else if VG_BOOL_CLO(arg, "--sigill-diagnostics", VG_(clo_sigill_diag))
593 sigill_diag_set = True;
594
sewardj2d9e8742009-08-07 15:46:56 +0000595 else if VG_BOOL_CLO(arg, "--stats", VG_(clo_stats)) {}
bartdb4384e2011-10-11 18:49:35 +0000596 else if VG_BOOL_CLO(arg, "--xml", VG_(clo_xml))
597 VG_(debugLog_setXml)(VG_(clo_xml));
598
sewardj3b290482011-05-06 21:02:55 +0000599 else if VG_XACT_CLO(arg, "--vgdb=no", VG_(clo_vgdb), Vg_VgdbNo) {}
600 else if VG_XACT_CLO(arg, "--vgdb=yes", VG_(clo_vgdb), Vg_VgdbYes) {}
philippe0c0291a2012-08-01 22:03:12 +0000601 else if VG_XACT_CLO(arg, "--vgdb=full", VG_(clo_vgdb), Vg_VgdbFull) {
602 /* automatically updates register values at each insn
603 with --vgdb=full */
sewardj8d47a612015-02-05 12:59:46 +0000604 VG_(clo_vex_control).iropt_register_updates_default
605 = VG_(clo_px_file_backed)
philippe0c0291a2012-08-01 22:03:12 +0000606 = VexRegUpdAllregsAtEachInsn;
607 }
sewardj3b290482011-05-06 21:02:55 +0000608 else if VG_INT_CLO (arg, "--vgdb-poll", VG_(clo_vgdb_poll)) {}
609 else if VG_INT_CLO (arg, "--vgdb-error", VG_(clo_vgdb_error)) {}
philippeec905f72014-08-17 20:03:51 +0000610 else if VG_USET_CLO (arg, "--vgdb-stop-at",
611 "startup,exit,valgrindabexit",
612 VG_(clo_vgdb_stop_at)) {}
philippecffe2a52014-01-11 13:56:48 +0000613 else if VG_STR_CLO (arg, "--vgdb-prefix", VG_(clo_vgdb_prefix)) {
614 VG_(arg_vgdb_prefix) = arg;
615 }
sewardj3b290482011-05-06 21:02:55 +0000616 else if VG_BOOL_CLO(arg, "--vgdb-shadow-registers",
617 VG_(clo_vgdb_shadow_registers)) {}
njn83df0b62009-02-25 01:01:05 +0000618 else if VG_BOOL_CLO(arg, "--demangle", VG_(clo_demangle)) {}
philippe1e470b52012-05-11 19:33:46 +0000619 else if VG_STR_CLO (arg, "--soname-synonyms",VG_(clo_soname_synonyms)) {}
njn83df0b62009-02-25 01:01:05 +0000620 else if VG_BOOL_CLO(arg, "--error-limit", VG_(clo_error_limit)) {}
621 else if VG_INT_CLO (arg, "--error-exitcode", VG_(clo_error_exitcode)) {}
philippe7b3d3562014-11-12 19:43:29 +0000622 else if VG_STR_CLO (arg, "--error-markers", tmp_str) {
623 Int m;
624 const HChar *startpos = tmp_str;
625 const HChar *nextpos;
626 for (m = 0;
627 m < sizeof(VG_(clo_error_markers))
628 /sizeof(VG_(clo_error_markers)[0]);
629 m++) {
630 /* Release previous value if clo given multiple times. */
631 VG_(free)(VG_(clo_error_markers)[m]);
632 VG_(clo_error_markers)[m] = NULL;
633
634 nextpos = VG_(strchr)(startpos, ',');
635 if (!nextpos)
636 nextpos = startpos + VG_(strlen)(startpos);
637 if (startpos != nextpos) {
638 VG_(clo_error_markers)[m]
philippe89488172014-11-12 21:10:37 +0000639 = VG_(malloc)("main.mpclo.2", nextpos - startpos + 1);
philippe7b3d3562014-11-12 19:43:29 +0000640 VG_(memcpy)(VG_(clo_error_markers)[m], startpos,
641 nextpos - startpos);
642 VG_(clo_error_markers)[m][nextpos - startpos] = '\0';
643 }
644 startpos = *nextpos ? nextpos + 1 : nextpos;
645 }
646 }
njn83df0b62009-02-25 01:01:05 +0000647 else if VG_BOOL_CLO(arg, "--show-emwarns", VG_(clo_show_emwarns)) {}
sewardj95d86c02007-12-18 01:49:23 +0000648
njn83df0b62009-02-25 01:01:05 +0000649 else if VG_BOOL_CLO(arg, "--run-libc-freeres", VG_(clo_run_libc_freeres)) {}
Elliott Hughesa0664b92017-04-18 17:46:52 -0700650 else if VG_BOOL_CLO(arg, "--run-cxx-freeres", VG_(clo_run_cxx_freeres)) {}
njn83df0b62009-02-25 01:01:05 +0000651 else if VG_BOOL_CLO(arg, "--show-below-main", VG_(clo_show_below_main)) {}
652 else if VG_BOOL_CLO(arg, "--time-stamp", VG_(clo_time_stamp)) {}
653 else if VG_BOOL_CLO(arg, "--track-fds", VG_(clo_track_fds)) {}
654 else if VG_BOOL_CLO(arg, "--trace-children", VG_(clo_trace_children)) {}
655 else if VG_BOOL_CLO(arg, "--child-silent-after-fork",
656 VG_(clo_child_silent_after_fork)) {}
bart78bfc712011-12-08 16:14:59 +0000657 else if VG_STR_CLO(arg, "--fair-sched", tmp_str) {
658 if (VG_(strcmp)(tmp_str, "yes") == 0)
659 VG_(clo_fair_sched) = enable_fair_sched;
660 else if (VG_(strcmp)(tmp_str, "try") == 0)
661 VG_(clo_fair_sched) = try_fair_sched;
662 else if (VG_(strcmp)(tmp_str, "no") == 0)
663 VG_(clo_fair_sched) = disable_fair_sched;
664 else
florian5b99e662014-11-29 14:41:32 +0000665 VG_(fmsg_bad_option)(arg,
666 "Bad argument, should be 'yes', 'try' or 'no'\n");
bart78bfc712011-12-08 16:14:59 +0000667 }
njn83df0b62009-02-25 01:01:05 +0000668 else if VG_BOOL_CLO(arg, "--trace-sched", VG_(clo_trace_sched)) {}
669 else if VG_BOOL_CLO(arg, "--trace-signals", VG_(clo_trace_signals)) {}
670 else if VG_BOOL_CLO(arg, "--trace-symtab", VG_(clo_trace_symtab)) {}
671 else if VG_STR_CLO (arg, "--trace-symtab-patt", VG_(clo_trace_symtab_patt)) {}
672 else if VG_BOOL_CLO(arg, "--trace-cfi", VG_(clo_trace_cfi)) {}
673 else if VG_XACT_CLO(arg, "--debug-dump=syms", VG_(clo_debug_dump_syms),
674 True) {}
675 else if VG_XACT_CLO(arg, "--debug-dump=line", VG_(clo_debug_dump_line),
676 True) {}
677 else if VG_XACT_CLO(arg, "--debug-dump=frames",
678 VG_(clo_debug_dump_frames), True) {}
679 else if VG_BOOL_CLO(arg, "--trace-redir", VG_(clo_trace_redir)) {}
sewardj95d86c02007-12-18 01:49:23 +0000680
njn83df0b62009-02-25 01:01:05 +0000681 else if VG_BOOL_CLO(arg, "--trace-syscalls", VG_(clo_trace_syscalls)) {}
682 else if VG_BOOL_CLO(arg, "--wait-for-gdb", VG_(clo_wait_for_gdb)) {}
njn83df0b62009-02-25 01:01:05 +0000683 else if VG_BOOL_CLO(arg, "--sym-offsets", VG_(clo_sym_offsets)) {}
philippea0a73932014-06-15 15:42:20 +0000684 else if VG_BOOL_CLO(arg, "--read-inline-info", VG_(clo_read_inline_info)) {}
njn83df0b62009-02-25 01:01:05 +0000685 else if VG_BOOL_CLO(arg, "--read-var-info", VG_(clo_read_var_info)) {}
sewardjf767d962007-02-12 17:47:14 +0000686
njn83df0b62009-02-25 01:01:05 +0000687 else if VG_INT_CLO (arg, "--dump-error", VG_(clo_dump_error)) {}
688 else if VG_INT_CLO (arg, "--input-fd", VG_(clo_input_fd)) {}
689 else if VG_INT_CLO (arg, "--sanity-level", VG_(clo_sanity_level)) {}
690 else if VG_BINT_CLO(arg, "--num-callers", VG_(clo_backtrace_size), 1,
691 VG_DEEPEST_BACKTRACE) {}
philippe8e1bee42013-10-18 00:08:20 +0000692 else if VG_BINT_CLO(arg, "--num-transtab-sectors",
693 VG_(clo_num_transtab_sectors),
694 MIN_N_SECTORS, MAX_N_SECTORS) {}
philippe924c8522015-03-15 12:24:19 +0000695 else if VG_BINT_CLO(arg, "--avg-transtab-entry-size",
696 VG_(clo_avg_transtab_entry_size),
697 50, 5000) {}
philippe46207652013-01-20 17:11:58 +0000698 else if VG_BINT_CLO(arg, "--merge-recursive-frames",
699 VG_(clo_merge_recursive_frames), 0,
700 VG_DEEPEST_BACKTRACE) {}
sewardjde4a1d02002-03-22 01:27:54 +0000701
sewardj13e3f1b2015-02-05 10:05:43 +0000702 else if VG_XACT_CLO(arg, "--smc-check=none",
703 VG_(clo_smc_check), Vg_SmcNone) {}
704 else if VG_XACT_CLO(arg, "--smc-check=stack",
705 VG_(clo_smc_check), Vg_SmcStack) {}
706 else if VG_XACT_CLO(arg, "--smc-check=all",
707 VG_(clo_smc_check), Vg_SmcAll) {}
sewardj6dbcc632011-06-07 21:39:28 +0000708 else if VG_XACT_CLO(arg, "--smc-check=all-non-file",
sewardj13e3f1b2015-02-05 10:05:43 +0000709 VG_(clo_smc_check), Vg_SmcAllNonFile) {}
sewardjde4a1d02002-03-22 01:27:54 +0000710
sewardj4450a0e2014-09-03 15:19:25 +0000711 else if VG_USETX_CLO (arg, "--kernel-variant",
712 "bproc,"
sewardj124e56d2014-09-06 14:45:12 +0000713 "android-no-hw-tls,"
sewardj4450a0e2014-09-03 15:19:25 +0000714 "android-gpu-sgx5xx,"
715 "android-gpu-adreno3xx",
philippeec905f72014-08-17 20:03:51 +0000716 VG_(clo_kernel_variant)) {}
sewardj26412bd2005-07-07 10:05:05 +0000717
njn97db7612009-08-04 02:32:55 +0000718 else if VG_BOOL_CLO(arg, "--dsymutil", VG_(clo_dsymutil)) {}
njnf76d27a2009-05-28 01:53:07 +0000719
sewardj9ab64a42010-12-06 11:40:04 +0000720 else if VG_STR_CLO (arg, "--trace-children-skip",
721 VG_(clo_trace_children_skip)) {}
722 else if VG_STR_CLO (arg, "--trace-children-skip-by-arg",
723 VG_(clo_trace_children_skip_by_arg)) {}
sewardj06421272009-11-05 08:55:13 +0000724
njn83df0b62009-02-25 01:01:05 +0000725 else if VG_BINT_CLO(arg, "--vex-iropt-verbosity",
726 VG_(clo_vex_control).iropt_verbosity, 0, 10) {}
727 else if VG_BINT_CLO(arg, "--vex-iropt-level",
728 VG_(clo_vex_control).iropt_level, 0, 2) {}
sewardj13e3f1b2015-02-05 10:05:43 +0000729
sewardj8d47a612015-02-05 12:59:46 +0000730 else if VG_STRINDEX_CLO(arg, "--vex-iropt-register-updates",
731 pxStrings, ix) {
732 vg_assert(ix < 4);
733 vg_assert(pxVals[ix] >= VexRegUpdSpAtMemAccess);
734 vg_assert(pxVals[ix] <= VexRegUpdAllregsAtEachInsn);
735 VG_(clo_vex_control).iropt_register_updates_default = pxVals[ix];
736 }
737 else if VG_STRINDEX_CLO(arg, "--px-default", pxStrings, ix) {
738 // NB: --px-default is an alias for the hard-to-remember
739 // --vex-iropt-register-updates, hence the same logic.
740 vg_assert(ix < 4);
741 vg_assert(pxVals[ix] >= VexRegUpdSpAtMemAccess);
742 vg_assert(pxVals[ix] <= VexRegUpdAllregsAtEachInsn);
743 VG_(clo_vex_control).iropt_register_updates_default = pxVals[ix];
744 }
745 else if VG_STRINDEX_CLO(arg, "--px-file-backed", pxStrings, ix) {
746 // Whereas --px-file-backed isn't
747 // the same flag as --vex-iropt-register-updates.
748 vg_assert(ix < 4);
749 vg_assert(pxVals[ix] >= VexRegUpdSpAtMemAccess);
750 vg_assert(pxVals[ix] <= VexRegUpdAllregsAtEachInsn);
751 VG_(clo_px_file_backed) = pxVals[ix];
752 }
sewardj13e3f1b2015-02-05 10:05:43 +0000753
njn83df0b62009-02-25 01:01:05 +0000754 else if VG_BINT_CLO(arg, "--vex-iropt-unroll-thresh",
755 VG_(clo_vex_control).iropt_unroll_thresh, 0, 400) {}
756 else if VG_BINT_CLO(arg, "--vex-guest-max-insns",
757 VG_(clo_vex_control).guest_max_insns, 1, 100) {}
758 else if VG_BINT_CLO(arg, "--vex-guest-chase-thresh",
759 VG_(clo_vex_control).guest_chase_thresh, 0, 99) {}
sewardj540cc4a2010-01-15 10:57:57 +0000760 else if VG_BOOL_CLO(arg, "--vex-guest-chase-cond",
761 VG_(clo_vex_control).guest_chase_cond) {}
sewardj94c8eb42008-09-19 20:13:39 +0000762
njn83df0b62009-02-25 01:01:05 +0000763 else if VG_INT_CLO(arg, "--log-fd", tmp_log_fd) {
764 log_to = VgLogTo_Fd;
sewardj738856f2009-07-15 14:48:32 +0000765 log_fsname_unexpanded = NULL;
766 }
767 else if VG_INT_CLO(arg, "--xml-fd", tmp_xml_fd) {
768 xml_to = VgLogTo_Fd;
769 xml_fsname_unexpanded = NULL;
sewardj4cf05692002-10-27 20:28:29 +0000770 }
771
sewardj738856f2009-07-15 14:48:32 +0000772 else if VG_STR_CLO(arg, "--log-file", log_fsname_unexpanded) {
njn83df0b62009-02-25 01:01:05 +0000773 log_to = VgLogTo_File;
sewardj4cf05692002-10-27 20:28:29 +0000774 }
sewardj738856f2009-07-15 14:48:32 +0000775 else if VG_STR_CLO(arg, "--xml-file", xml_fsname_unexpanded) {
776 xml_to = VgLogTo_File;
777 }
778
779 else if VG_STR_CLO(arg, "--log-socket", log_fsname_unexpanded) {
njn83df0b62009-02-25 01:01:05 +0000780 log_to = VgLogTo_Socket;
sewardj73cf3bc2002-11-03 03:20:15 +0000781 }
sewardj738856f2009-07-15 14:48:32 +0000782 else if VG_STR_CLO(arg, "--xml-socket", xml_fsname_unexpanded) {
783 xml_to = VgLogTo_Socket;
784 }
sewardj73cf3bc2002-11-03 03:20:15 +0000785
sewardj5d616df2013-07-02 08:07:15 +0000786 else if VG_STR_CLO(arg, "--debuginfo-server",
787 VG_(clo_debuginfo_server)) {}
788
789 else if VG_BOOL_CLO(arg, "--allow-mismatched-debuginfo",
790 VG_(clo_allow_mismatched_debuginfo)) {}
791
njn83df0b62009-02-25 01:01:05 +0000792 else if VG_STR_CLO(arg, "--xml-user-comment",
793 VG_(clo_xml_user_comment)) {}
sewardj768db0e2005-07-19 14:18:56 +0000794
barta6efdfa2014-06-24 05:08:21 +0000795 else if VG_BOOL_CLO(arg, "--default-suppressions",
sewardj13e3f1b2015-02-05 10:05:43 +0000796 VG_(clo_default_supp)) {}
bart2c68e3e2014-06-22 10:11:59 +0000797
njn83df0b62009-02-25 01:01:05 +0000798 else if VG_STR_CLO(arg, "--suppressions", tmp_str) {
florian79316272014-10-07 18:36:28 +0000799 VG_(addToXA)(VG_(clo_suppressions), &tmp_str);
sewardjde4a1d02002-03-22 01:27:54 +0000800 }
sewardjde4a1d02002-03-22 01:27:54 +0000801
sewardj14cdbf82010-10-12 00:44:05 +0000802 else if VG_STR_CLO (arg, "--fullpath-after", tmp_str) {
florian79316272014-10-07 18:36:28 +0000803 VG_(addToXA)(VG_(clo_fullpath_after), &tmp_str);
sewardj14cdbf82010-10-12 00:44:05 +0000804 }
805
sewardj8b6573d2012-12-05 22:15:14 +0000806 else if VG_STR_CLO (arg, "--extra-debuginfo-path",
807 VG_(clo_extra_debuginfo_path)) {}
808
sewardjf9ebc392010-05-09 22:30:43 +0000809 else if VG_STR_CLO(arg, "--require-text-symbol", tmp_str) {
sewardjf9ebc392010-05-09 22:30:43 +0000810 /* String needs to be of the form C?*C?*, where C is any
811 character, but is the same both times. Having it in this
812 form facilitates finding the boundary between the sopatt
813 and the fnpatt just by looking for the second occurrence
814 of C, without hardwiring any assumption about what C
815 is. */
florian19f91bb2012-11-10 22:29:54 +0000816 HChar patt[7];
sewardjf9ebc392010-05-09 22:30:43 +0000817 Bool ok = True;
818 ok = tmp_str && VG_(strlen)(tmp_str) > 0;
819 if (ok) {
820 patt[0] = patt[3] = tmp_str[0];
821 patt[1] = patt[4] = '?';
822 patt[2] = patt[5] = '*';
823 patt[6] = 0;
824 ok = VG_(string_match)(patt, tmp_str);
825 }
826 if (!ok) {
njnb1cc5d62010-07-06 04:05:23 +0000827 VG_(fmsg_bad_option)(arg,
828 "Invalid --require-text-symbol= specification.\n");
sewardjf9ebc392010-05-09 22:30:43 +0000829 }
florian79316272014-10-07 18:36:28 +0000830 VG_(addToXA)(VG_(clo_req_tsyms), &tmp_str);
sewardjf9ebc392010-05-09 22:30:43 +0000831 }
832
sewardjfa8ec112005-01-19 11:55:34 +0000833 /* "stuvwxyz" --> stuvwxyz (binary) */
njn83df0b62009-02-25 01:01:05 +0000834 else if VG_STR_CLO(arg, "--trace-flags", tmp_str) {
sewardjfa8ec112005-01-19 11:55:34 +0000835 Int j;
njn83df0b62009-02-25 01:01:05 +0000836 if (8 != VG_(strlen)(tmp_str)) {
njnb1cc5d62010-07-06 04:05:23 +0000837 VG_(fmsg_bad_option)(arg,
838 "--trace-flags argument must have 8 digits\n");
sewardjfa8ec112005-01-19 11:55:34 +0000839 }
840 for (j = 0; j < 8; j++) {
njn83df0b62009-02-25 01:01:05 +0000841 if ('0' == tmp_str[j]) { /* do nothing */ }
842 else if ('1' == tmp_str[j]) VG_(clo_trace_flags) |= (1 << (7-j));
sewardjfa8ec112005-01-19 11:55:34 +0000843 else {
njnb1cc5d62010-07-06 04:05:23 +0000844 VG_(fmsg_bad_option)(arg,
845 "--trace-flags argument can only contain 0s and 1s\n");
sewardjfa8ec112005-01-19 11:55:34 +0000846 }
847 }
848 }
849
sewardj17c5e2e2012-12-28 09:12:14 +0000850 else if VG_INT_CLO (arg, "--trace-notbelow", VG_(clo_trace_notbelow)) {}
851
852 else if VG_INT_CLO (arg, "--trace-notabove", VG_(clo_trace_notabove)) {}
853
sewardjfa8ec112005-01-19 11:55:34 +0000854 /* "stuvwxyz" --> stuvwxyz (binary) */
njn83df0b62009-02-25 01:01:05 +0000855 else if VG_STR_CLO(arg, "--profile-flags", tmp_str) {
njn25e49d8e72002-09-23 09:36:25 +0000856 Int j;
njn83df0b62009-02-25 01:01:05 +0000857 if (8 != VG_(strlen)(tmp_str)) {
njnb1cc5d62010-07-06 04:05:23 +0000858 VG_(fmsg_bad_option)(arg,
859 "--profile-flags argument must have 8 digits\n");
njn25e49d8e72002-09-23 09:36:25 +0000860 }
sewardj8b635a42004-11-22 19:01:47 +0000861 for (j = 0; j < 8; j++) {
njn83df0b62009-02-25 01:01:05 +0000862 if ('0' == tmp_str[j]) { /* do nothing */ }
sewardj17c5e2e2012-12-28 09:12:14 +0000863 else if ('1' == tmp_str[j]) VG_(clo_profyle_flags) |= (1 << (7-j));
njn25e49d8e72002-09-23 09:36:25 +0000864 else {
njnb1cc5d62010-07-06 04:05:23 +0000865 VG_(fmsg_bad_option)(arg,
866 "--profile-flags argument can only contain 0s and 1s\n");
njn25e49d8e72002-09-23 09:36:25 +0000867 }
868 }
sewardj17c5e2e2012-12-28 09:12:14 +0000869 VG_(clo_profyle_sbs) = True;
njn25e49d8e72002-09-23 09:36:25 +0000870 }
sewardjde4a1d02002-03-22 01:27:54 +0000871
sewardj17c5e2e2012-12-28 09:12:14 +0000872 else if VG_INT_CLO (arg, "--profile-interval",
873 VG_(clo_profyle_interval)) {}
florian29e022d2012-07-02 21:13:34 +0000874
njn83df0b62009-02-25 01:01:05 +0000875 else if VG_XACT_CLO(arg, "--gen-suppressions=no",
876 VG_(clo_gen_suppressions), 0) {}
877 else if VG_XACT_CLO(arg, "--gen-suppressions=yes",
878 VG_(clo_gen_suppressions), 1) {}
879 else if VG_XACT_CLO(arg, "--gen-suppressions=all",
880 VG_(clo_gen_suppressions), 2) {}
sewardjd153fae2005-01-10 17:24:47 +0000881
sewardj49984ea2013-10-18 13:21:26 +0000882 else if VG_BINT_CLO(arg, "--unw-stack-scan-thresh",
883 VG_(clo_unw_stack_scan_thresh), 0, 100) {}
884 else if VG_BINT_CLO(arg, "--unw-stack-scan-frames",
885 VG_(clo_unw_stack_scan_frames), 0, 32) {}
886
sewardj67f7c032014-10-23 19:48:01 +0000887 else if VG_XACT_CLO(arg, "--resync-filter=no",
888 VG_(clo_resync_filter), 0) {}
889 else if VG_XACT_CLO(arg, "--resync-filter=yes",
890 VG_(clo_resync_filter), 1) {}
891 else if VG_XACT_CLO(arg, "--resync-filter=verbose",
892 VG_(clo_resync_filter), 2) {}
893
nethercote71980f02004-01-24 18:18:54 +0000894 else if ( ! VG_(needs).command_line_options
njn51d827b2005-05-09 01:02:08 +0000895 || ! VG_TDICT_CALL(tool_process_cmd_line_option, arg) ) {
florian5b99e662014-11-29 14:41:32 +0000896 VG_(fmsg_unknown_option)(arg);
njn25e49d8e72002-09-23 09:36:25 +0000897 }
sewardjde4a1d02002-03-22 01:27:54 +0000898 }
899
sewardj738856f2009-07-15 14:48:32 +0000900 /* END command-line processing loop */
901
florianb9749a52015-07-24 11:50:12 +0000902 /* Notify about deprecated features here. */
florian882038d2014-09-01 06:37:07 +0000903
florianb985e2d2011-09-29 03:03:45 +0000904 /* Determine the path prefix for vgdb */
905 if (VG_(clo_vgdb_prefix) == NULL)
906 VG_(clo_vgdb_prefix) = VG_(vgdb_prefix_default)();
907
sewardj998d40d2004-12-06 14:24:52 +0000908 /* Make VEX control parameters sane */
909
910 if (VG_(clo_vex_control).guest_chase_thresh
911 >= VG_(clo_vex_control).guest_max_insns)
912 VG_(clo_vex_control).guest_chase_thresh
913 = VG_(clo_vex_control).guest_max_insns - 1;
914
915 if (VG_(clo_vex_control).guest_chase_thresh < 0)
916 VG_(clo_vex_control).guest_chase_thresh = 0;
917
918 /* Check various option values */
nethercote27fec902004-06-16 21:26:32 +0000919
njnf9ebf672003-05-12 21:41:30 +0000920 if (VG_(clo_verbosity) < 0)
sewardjde4a1d02002-03-22 01:27:54 +0000921 VG_(clo_verbosity) = 0;
922
sewardjc30cd9b2012-12-06 18:08:54 +0000923 if (!sigill_diag_set)
924 VG_(clo_sigill_diag) = (VG_(clo_verbosity) > 0);
925
florian29e022d2012-07-02 21:13:34 +0000926 if (VG_(clo_trace_notbelow) == -1) {
927 if (VG_(clo_trace_notabove) == -1) {
928 /* [] */
929 VG_(clo_trace_notbelow) = 2147483647;
930 VG_(clo_trace_notabove) = 0;
931 } else {
932 /* [0 .. notabove] */
933 VG_(clo_trace_notbelow) = 0;
934 }
935 } else {
936 if (VG_(clo_trace_notabove) == -1) {
937 /* [notbelow .. ] */
938 VG_(clo_trace_notabove) = 2147483647;
939 } else {
940 /* [notbelow .. notabove] */
941 }
942 }
943
sewardj3b290482011-05-06 21:02:55 +0000944 VG_(dyn_vgdb_error) = VG_(clo_vgdb_error);
945
njnbe9b47b2005-05-15 16:22:58 +0000946 if (VG_(clo_gen_suppressions) > 0 &&
947 !VG_(needs).core_errors && !VG_(needs).tool_errors) {
njnb1cc5d62010-07-06 04:05:23 +0000948 VG_(fmsg_bad_option)("--gen-suppressions=yes",
949 "Can't use --gen-suppressions= with %s\n"
950 "because it doesn't generate errors.\n", VG_(details).name);
njnbe9b47b2005-05-15 16:22:58 +0000951 }
952
sewardj67f7c032014-10-23 19:48:01 +0000953# if !defined(VGO_darwin)
954 if (VG_(clo_resync_filter) != 0) {
955 VG_(fmsg_bad_option)("--resync-filter=yes or =verbose",
956 "--resync-filter= is only available on MacOS X.\n");
957 /*NOTREACHED*/
958 }
959# endif
960
sewardj738856f2009-07-15 14:48:32 +0000961 /* If XML output is requested, check that the tool actually
962 supports it. */
963 if (VG_(clo_xml) && !VG_(needs).xml_output) {
964 VG_(clo_xml) = False;
njnb1cc5d62010-07-06 04:05:23 +0000965 VG_(fmsg_bad_option)("--xml=yes",
sewardj738856f2009-07-15 14:48:32 +0000966 "%s does not support XML output.\n", VG_(details).name);
sewardj738856f2009-07-15 14:48:32 +0000967 /*NOTREACHED*/
968 }
969
970 vg_assert( VG_(clo_gen_suppressions) >= 0 );
971 vg_assert( VG_(clo_gen_suppressions) <= 2 );
972
sewardj71bc3cb2005-05-19 00:25:45 +0000973 /* If we've been asked to emit XML, mash around various other
974 options so as to constrain the output somewhat, and to remove
sewardj738856f2009-07-15 14:48:32 +0000975 any need for user input during the run.
976 */
sewardj71bc3cb2005-05-19 00:25:45 +0000977 if (VG_(clo_xml)) {
sewardj738856f2009-07-15 14:48:32 +0000978
979 /* We can't allow --gen-suppressions=yes, since that requires us
980 to print the error and then ask the user if she wants a
981 suppression for it, but in XML mode we won't print it until
982 we know whether we also need to print a suppression. Hence a
983 circular dependency. So disallow this.
984 (--gen-suppressions=all is still OK since we don't need any
985 user interaction in this case.) */
986 if (VG_(clo_gen_suppressions) == 1) {
njnb1cc5d62010-07-06 04:05:23 +0000987 VG_(fmsg_bad_option)(
988 "--xml=yes together with --gen-suppressions=yes",
989 "When --xml=yes is specified, --gen-suppressions=no\n"
990 "or --gen-suppressions=all is allowed, but not "
sewardj738856f2009-07-15 14:48:32 +0000991 "--gen-suppressions=yes.\n");
sewardj738856f2009-07-15 14:48:32 +0000992 }
993
sewardj738856f2009-07-15 14:48:32 +0000994 /* Disallow dump_error in XML mode; sounds like a recipe for
995 chaos. No big deal; dump_error is a flag for debugging V
996 itself. */
997 if (VG_(clo_dump_error) > 0) {
florian5b99e662014-11-29 14:41:32 +0000998 VG_(fmsg_bad_option)("--xml=yes",
999 "Cannot be used together with --dump-error");
sewardj738856f2009-07-15 14:48:32 +00001000 }
1001
sewardj71bc3cb2005-05-19 00:25:45 +00001002 /* Disable error limits (this might be a bad idea!) */
1003 VG_(clo_error_limit) = False;
1004 /* Disable emulation warnings */
sewardj738856f2009-07-15 14:48:32 +00001005
sewardj71bc3cb2005-05-19 00:25:45 +00001006 /* Also, we want to set options for the leak checker, but that
1007 will have to be done in Memcheck's flag-handling code, not
1008 here. */
1009 }
1010
njnbe9b47b2005-05-15 16:22:58 +00001011 /* All non-logging-related options have been checked. If the logging
1012 option specified is ok, we can switch to it, as we know we won't
1013 have to generate any other command-line-related error messages.
1014 (So far we should be still attached to stderr, so we can show on
1015 the terminal any problems to do with processing command line
1016 opts.)
1017
sewardj738856f2009-07-15 14:48:32 +00001018 So set up logging now. After this is done, VG_(log_output_sink)
1019 and (if relevant) VG_(xml_output_sink) should be connected to
1020 whatever sink has been selected, and we indiscriminately chuck
1021 stuff into it without worrying what the nature of it is. Oh the
1022 wonder of Unix streams. */
sewardj4cf05692002-10-27 20:28:29 +00001023
sewardj738856f2009-07-15 14:48:32 +00001024 vg_assert(VG_(log_output_sink).fd == 2 /* stderr */);
1025 vg_assert(VG_(log_output_sink).is_socket == False);
1026 vg_assert(VG_(clo_log_fname_expanded) == NULL);
1027
1028 vg_assert(VG_(xml_output_sink).fd == -1 /* disabled */);
1029 vg_assert(VG_(xml_output_sink).is_socket == False);
1030 vg_assert(VG_(clo_xml_fname_expanded) == NULL);
1031
1032 /* --- set up the normal text output channel --- */
sewardj4cf05692002-10-27 20:28:29 +00001033
njnbe9b47b2005-05-15 16:22:58 +00001034 switch (log_to) {
sewardj73cf3bc2002-11-03 03:20:15 +00001035
sewardj4cf05692002-10-27 20:28:29 +00001036 case VgLogTo_Fd:
sewardj738856f2009-07-15 14:48:32 +00001037 vg_assert(log_fsname_unexpanded == NULL);
sewardj4cf05692002-10-27 20:28:29 +00001038 break;
sewardj73cf3bc2002-11-03 03:20:15 +00001039
sewardj4cf05692002-10-27 20:28:29 +00001040 case VgLogTo_File: {
florian19f91bb2012-11-10 22:29:54 +00001041 HChar* logfilename;
jsgff3c3f1a2003-10-14 22:13:28 +00001042
sewardj738856f2009-07-15 14:48:32 +00001043 vg_assert(log_fsname_unexpanded != NULL);
1044 vg_assert(VG_(strlen)(log_fsname_unexpanded) <= 900); /* paranoia */
jsgff3c3f1a2003-10-14 22:13:28 +00001045
njn374a36d2007-11-23 01:41:32 +00001046 // Nb: we overwrite an existing file of this name without asking
1047 // any questions.
sewardj738856f2009-07-15 14:48:32 +00001048 logfilename = VG_(expand_file_name)("--log-file",
1049 log_fsname_unexpanded);
njn374a36d2007-11-23 01:41:32 +00001050 sres = VG_(open)(logfilename,
njnda033f52005-12-19 21:27:58 +00001051 VKI_O_CREAT|VKI_O_WRONLY|VKI_O_TRUNC,
florian204197a2015-01-07 14:47:15 +00001052 VKI_S_IRUSR|VKI_S_IWUSR|VKI_S_IRGRP|VKI_S_IROTH);
njncda2f0f2009-05-18 02:12:08 +00001053 if (!sr_isError(sres)) {
1054 tmp_log_fd = sr_Res(sres);
sewardj738856f2009-07-15 14:48:32 +00001055 VG_(clo_log_fname_expanded) = logfilename;
njnbe9b47b2005-05-15 16:22:58 +00001056 } else {
njnb1cc5d62010-07-06 04:05:23 +00001057 VG_(fmsg)("can't create log file '%s': %s\n",
1058 logfilename, VG_(strerror)(sr_Err(sres)));
1059 VG_(exit)(1);
sewardj603d4102005-01-11 14:01:02 +00001060 /*NOTREACHED*/
njn374a36d2007-11-23 01:41:32 +00001061 }
sewardj738856f2009-07-15 14:48:32 +00001062 break;
sewardj73cf3bc2002-11-03 03:20:15 +00001063 }
1064
1065 case VgLogTo_Socket: {
sewardj738856f2009-07-15 14:48:32 +00001066 vg_assert(log_fsname_unexpanded != NULL);
1067 vg_assert(VG_(strlen)(log_fsname_unexpanded) <= 900); /* paranoia */
1068 tmp_log_fd = VG_(connect_via_socket)( log_fsname_unexpanded );
njnda033f52005-12-19 21:27:58 +00001069 if (tmp_log_fd == -1) {
njnb1cc5d62010-07-06 04:05:23 +00001070 VG_(fmsg)("Invalid --log-socket spec of '%s'\n",
1071 log_fsname_unexpanded);
1072 VG_(exit)(1);
njnbe9b47b2005-05-15 16:22:58 +00001073 /*NOTREACHED*/
sewardj4cf05692002-10-27 20:28:29 +00001074 }
njnda033f52005-12-19 21:27:58 +00001075 if (tmp_log_fd == -2) {
njnb1cc5d62010-07-06 04:05:23 +00001076 VG_(umsg)("failed to connect to logging server '%s'.\n"
1077 "Log messages will sent to stderr instead.\n",
1078 log_fsname_unexpanded );
1079
sewardj570f8902002-11-03 11:44:36 +00001080 /* We don't change anything here. */
sewardj738856f2009-07-15 14:48:32 +00001081 vg_assert(VG_(log_output_sink).fd == 2);
njnda033f52005-12-19 21:27:58 +00001082 tmp_log_fd = 2;
sewardj570f8902002-11-03 11:44:36 +00001083 } else {
njnda033f52005-12-19 21:27:58 +00001084 vg_assert(tmp_log_fd > 0);
sewardj738856f2009-07-15 14:48:32 +00001085 VG_(log_output_sink).is_socket = True;
sewardj570f8902002-11-03 11:44:36 +00001086 }
sewardj73cf3bc2002-11-03 03:20:15 +00001087 break;
1088 }
sewardj4cf05692002-10-27 20:28:29 +00001089 }
1090
sewardj738856f2009-07-15 14:48:32 +00001091 /* --- set up the XML output channel --- */
sewardj71bc3cb2005-05-19 00:25:45 +00001092
sewardj738856f2009-07-15 14:48:32 +00001093 switch (xml_to) {
1094
1095 case VgLogTo_Fd:
1096 vg_assert(xml_fsname_unexpanded == NULL);
1097 break;
1098
1099 case VgLogTo_File: {
florian19f91bb2012-11-10 22:29:54 +00001100 HChar* xmlfilename;
sewardj738856f2009-07-15 14:48:32 +00001101
1102 vg_assert(xml_fsname_unexpanded != NULL);
1103 vg_assert(VG_(strlen)(xml_fsname_unexpanded) <= 900); /* paranoia */
1104
1105 // Nb: we overwrite an existing file of this name without asking
1106 // any questions.
1107 xmlfilename = VG_(expand_file_name)("--xml-file",
1108 xml_fsname_unexpanded);
1109 sres = VG_(open)(xmlfilename,
1110 VKI_O_CREAT|VKI_O_WRONLY|VKI_O_TRUNC,
florian204197a2015-01-07 14:47:15 +00001111 VKI_S_IRUSR|VKI_S_IWUSR|VKI_S_IRGRP|VKI_S_IROTH);
sewardj738856f2009-07-15 14:48:32 +00001112 if (!sr_isError(sres)) {
1113 tmp_xml_fd = sr_Res(sres);
1114 VG_(clo_xml_fname_expanded) = xmlfilename;
florian801c1992014-10-17 21:07:37 +00001115 *xml_fname_unexpanded = xml_fsname_unexpanded;
sewardj738856f2009-07-15 14:48:32 +00001116 } else {
njnb1cc5d62010-07-06 04:05:23 +00001117 VG_(fmsg)("can't create XML file '%s': %s\n",
1118 xmlfilename, VG_(strerror)(sr_Err(sres)));
1119 VG_(exit)(1);
sewardj738856f2009-07-15 14:48:32 +00001120 /*NOTREACHED*/
1121 }
1122 break;
1123 }
1124
1125 case VgLogTo_Socket: {
1126 vg_assert(xml_fsname_unexpanded != NULL);
1127 vg_assert(VG_(strlen)(xml_fsname_unexpanded) <= 900); /* paranoia */
1128 tmp_xml_fd = VG_(connect_via_socket)( xml_fsname_unexpanded );
1129 if (tmp_xml_fd == -1) {
njnb1cc5d62010-07-06 04:05:23 +00001130 VG_(fmsg)("Invalid --xml-socket spec of '%s'\n",
1131 xml_fsname_unexpanded );
1132 VG_(exit)(1);
sewardj738856f2009-07-15 14:48:32 +00001133 /*NOTREACHED*/
1134 }
1135 if (tmp_xml_fd == -2) {
njnb1cc5d62010-07-06 04:05:23 +00001136 VG_(umsg)("failed to connect to XML logging server '%s'.\n"
1137 "XML output will sent to stderr instead.\n",
1138 xml_fsname_unexpanded);
sewardj738856f2009-07-15 14:48:32 +00001139 /* We don't change anything here. */
1140 vg_assert(VG_(xml_output_sink).fd == 2);
1141 tmp_xml_fd = 2;
1142 } else {
1143 vg_assert(tmp_xml_fd > 0);
1144 VG_(xml_output_sink).is_socket = True;
1145 }
1146 break;
1147 }
sewardj71bc3cb2005-05-19 00:25:45 +00001148 }
1149
sewardj738856f2009-07-15 14:48:32 +00001150 /* If we've got this far, and XML mode was requested, but no XML
1151 output channel appears to have been specified, just stop. We
1152 could continue, and XML output will simply vanish into nowhere,
1153 but that is likely to confuse the hell out of users, which is
1154 distinctly Ungood. */
1155 if (VG_(clo_xml) && tmp_xml_fd == -1) {
njnb1cc5d62010-07-06 04:05:23 +00001156 VG_(fmsg_bad_option)(
1157 "--xml=yes, but no XML destination specified",
sewardj738856f2009-07-15 14:48:32 +00001158 "--xml=yes has been specified, but there is no XML output\n"
1159 "destination. You must specify an XML output destination\n"
njnb1cc5d62010-07-06 04:05:23 +00001160 "using --xml-fd, --xml-file or --xml-socket.\n"
1161 );
sewardj738856f2009-07-15 14:48:32 +00001162 }
1163
1164 // Finalise the output fds: the log fd ..
1165
njnda033f52005-12-19 21:27:58 +00001166 if (tmp_log_fd >= 0) {
sewardj738856f2009-07-15 14:48:32 +00001167 // Move log_fd into the safe range, so it doesn't conflict with
1168 // any app fds.
njnda033f52005-12-19 21:27:58 +00001169 tmp_log_fd = VG_(fcntl)(tmp_log_fd, VKI_F_DUPFD, VG_(fd_hard_limit));
1170 if (tmp_log_fd < 0) {
sewardj738856f2009-07-15 14:48:32 +00001171 VG_(message)(Vg_UserMsg, "valgrind: failed to move logfile fd "
1172 "into safe range, using stderr\n");
1173 VG_(log_output_sink).fd = 2; // stderr
1174 VG_(log_output_sink).is_socket = False;
njnda033f52005-12-19 21:27:58 +00001175 } else {
sewardj738856f2009-07-15 14:48:32 +00001176 VG_(log_output_sink).fd = tmp_log_fd;
1177 VG_(fcntl)(VG_(log_output_sink).fd, VKI_F_SETFD, VKI_FD_CLOEXEC);
njnda033f52005-12-19 21:27:58 +00001178 }
1179 } else {
1180 // If they said --log-fd=-1, don't print anything. Plausible for use in
1181 // regression testing suites that use client requests to count errors.
sewardj738856f2009-07-15 14:48:32 +00001182 VG_(log_output_sink).fd = -1;
1183 VG_(log_output_sink).is_socket = False;
jsgf855d93d2003-10-13 22:26:55 +00001184 }
1185
sewardj738856f2009-07-15 14:48:32 +00001186 // Finalise the output fds: and the XML fd ..
1187
1188 if (tmp_xml_fd >= 0) {
1189 // Move xml_fd into the safe range, so it doesn't conflict with
1190 // any app fds.
1191 tmp_xml_fd = VG_(fcntl)(tmp_xml_fd, VKI_F_DUPFD, VG_(fd_hard_limit));
1192 if (tmp_xml_fd < 0) {
1193 VG_(message)(Vg_UserMsg, "valgrind: failed to move XML file fd "
1194 "into safe range, using stderr\n");
1195 VG_(xml_output_sink).fd = 2; // stderr
1196 VG_(xml_output_sink).is_socket = False;
1197 } else {
1198 VG_(xml_output_sink).fd = tmp_xml_fd;
1199 VG_(fcntl)(VG_(xml_output_sink).fd, VKI_F_SETFD, VKI_FD_CLOEXEC);
1200 }
1201 } else {
1202 // If they said --xml-fd=-1, don't print anything. Plausible for use in
1203 // regression testing suites that use client requests to count errors.
1204 VG_(xml_output_sink).fd = -1;
1205 VG_(xml_output_sink).is_socket = False;
1206 }
1207
1208 // Suppressions related stuff
1209
bart2c68e3e2014-06-22 10:11:59 +00001210 if (VG_(clo_default_supp) &&
sewardj45f4e7c2005-09-27 19:20:21 +00001211 (VG_(needs).core_errors || VG_(needs).tool_errors)) {
1212 /* If we haven't reached the max number of suppressions, load
1213 the default one. */
floriane6a4ed12012-10-21 02:30:18 +00001214 static const HChar default_supp[] = "default.supp";
sewardj45f4e7c2005-09-27 19:20:21 +00001215 Int len = VG_(strlen)(VG_(libdir)) + 1 + sizeof(default_supp);
florian77eb20b2014-09-11 21:19:17 +00001216 HChar *buf = VG_(malloc)("main.mpclo.3", len);
sewardj45f4e7c2005-09-27 19:20:21 +00001217 VG_(sprintf)(buf, "%s/%s", VG_(libdir), default_supp);
florian79316272014-10-07 18:36:28 +00001218 VG_(addToXA)(VG_(clo_suppressions), &buf);
sewardj45f4e7c2005-09-27 19:20:21 +00001219 }
sewardjde4a1d02002-03-22 01:27:54 +00001220
sewardj738856f2009-07-15 14:48:32 +00001221 *logging_to_fd = log_to == VgLogTo_Fd || log_to == VgLogTo_Socket;
sewardj45f4e7c2005-09-27 19:20:21 +00001222}
1223
sewardj4efbaa72008-06-04 06:51:58 +00001224// Write the name and value of log file qualifiers to the xml file.
florian801c1992014-10-17 21:07:37 +00001225// We can safely assume here that the format string is well-formed.
1226// It has been checked earlier in VG_(expand_file_name) when processing
1227// command line options.
1228static void print_file_vars(const HChar* format)
sewardj4efbaa72008-06-04 06:51:58 +00001229{
1230 Int i = 0;
1231
1232 while (format[i]) {
1233 if (format[i] == '%') {
1234 // We saw a '%'. What's next...
1235 i++;
1236 if ('q' == format[i]) {
1237 i++;
1238 if ('{' == format[i]) {
1239 // Get the env var name, print its contents.
florian19f91bb2012-11-10 22:29:54 +00001240 HChar* qual;
florian801c1992014-10-17 21:07:37 +00001241 Int begin_qualname = ++i;
sewardj4efbaa72008-06-04 06:51:58 +00001242 while (True) {
1243 if ('}' == format[i]) {
florian801c1992014-10-17 21:07:37 +00001244 Int qualname_len = i - begin_qualname;
1245 HChar qualname[qualname_len + 1];
1246 VG_(strncpy)(qualname, format + begin_qualname,
1247 qualname_len);
1248 qualname[qualname_len] = '\0';
sewardj4efbaa72008-06-04 06:51:58 +00001249 qual = VG_(getenv)(qualname);
florian801c1992014-10-17 21:07:37 +00001250 i++;
1251 VG_(printf_xml)("<logfilequalifier> <var>%pS</var> "
1252 "<value>%pS</value> </logfilequalifier>\n",
1253 qualname, qual);
sewardj4efbaa72008-06-04 06:51:58 +00001254 break;
1255 }
1256 i++;
1257 }
sewardj4efbaa72008-06-04 06:51:58 +00001258 }
1259 }
1260 } else {
1261 i++;
1262 }
1263 }
1264}
1265
sewardj45f4e7c2005-09-27 19:20:21 +00001266
1267/*====================================================================*/
1268/*=== Printing the preamble ===*/
1269/*====================================================================*/
1270
barta92677a2011-10-22 08:24:32 +00001271// Print the argument, escaping any chars that require it.
florian19f91bb2012-11-10 22:29:54 +00001272static void umsg_arg(const HChar* arg)
njnf8a11cf2009-08-02 23:03:06 +00001273{
1274 SizeT len = VG_(strlen)(arg);
floriane6a4ed12012-10-21 02:30:18 +00001275 const HChar* special = " \\<>";
njnf8a11cf2009-08-02 23:03:06 +00001276 Int i;
1277 for (i = 0; i < len; i++) {
1278 if (VG_(strchr)(special, arg[i])) {
barta92677a2011-10-22 08:24:32 +00001279 VG_(umsg)("\\"); // escape with a backslash if necessary
njnf8a11cf2009-08-02 23:03:06 +00001280 }
barta92677a2011-10-22 08:24:32 +00001281 VG_(umsg)("%c", arg[i]);
njnf8a11cf2009-08-02 23:03:06 +00001282 }
1283}
1284
barta92677a2011-10-22 08:24:32 +00001285// Send output to the XML-stream and escape any XML meta-characters.
florian19f91bb2012-11-10 22:29:54 +00001286static void xml_arg(const HChar* arg)
barta92677a2011-10-22 08:24:32 +00001287{
1288 VG_(printf_xml)("%pS", arg);
1289}
1290
sewardj45f4e7c2005-09-27 19:20:21 +00001291/* Ok, the logging sink is running now. Print a suitable preamble.
1292 If logging to file or a socket, write details of parent PID and
1293 command line args, to help people trying to interpret the
1294 results of a run which encompasses multiple processes. */
sewardj738856f2009-07-15 14:48:32 +00001295static void print_preamble ( Bool logging_to_fd,
florian801c1992014-10-17 21:07:37 +00001296 const HChar* xml_fname_unexpanded,
sewardj738856f2009-07-15 14:48:32 +00001297 const HChar* toolname )
sewardj45f4e7c2005-09-27 19:20:21 +00001298{
sewardj738856f2009-07-15 14:48:32 +00001299 Int i;
florian6bd9dc12012-11-23 16:17:43 +00001300 const HChar* xpre = VG_(clo_xml) ? " <line>" : "";
1301 const HChar* xpost = VG_(clo_xml) ? "</line>" : "";
sewardj738856f2009-07-15 14:48:32 +00001302 UInt (*umsg_or_xml)( const HChar*, ... )
1303 = VG_(clo_xml) ? VG_(printf_xml) : VG_(umsg);
tom60a4b0b2005-10-12 10:45:27 +00001304
florian19f91bb2012-11-10 22:29:54 +00001305 void (*umsg_or_xml_arg)( const HChar* )
barta92677a2011-10-22 08:24:32 +00001306 = VG_(clo_xml) ? xml_arg : umsg_arg;
1307
sewardj14c7cc52007-02-25 15:08:24 +00001308 vg_assert( VG_(args_for_client) );
1309 vg_assert( VG_(args_for_valgrind) );
sewardj99a2ceb2007-11-09 12:30:36 +00001310 vg_assert( toolname );
sewardj14c7cc52007-02-25 15:08:24 +00001311
sewardj71bc3cb2005-05-19 00:25:45 +00001312 if (VG_(clo_xml)) {
sewardj738856f2009-07-15 14:48:32 +00001313 VG_(printf_xml)("<?xml version=\"1.0\"?>\n");
1314 VG_(printf_xml)("\n");
1315 VG_(printf_xml)("<valgrindoutput>\n");
1316 VG_(printf_xml)("\n");
1317 VG_(printf_xml)("<protocolversion>4</protocolversion>\n");
1318 VG_(printf_xml)("<protocoltool>%s</protocoltool>\n", toolname);
1319 VG_(printf_xml)("\n");
sewardj71bc3cb2005-05-19 00:25:45 +00001320 }
1321
Elliott Hughesa0664b92017-04-18 17:46:52 -07001322 if (VG_(clo_xml) || VG_(clo_verbosity) > 0) {
sewardjd7bddad2005-06-13 16:48:32 +00001323
1324 if (VG_(clo_xml))
sewardj738856f2009-07-15 14:48:32 +00001325 VG_(printf_xml)("<preamble>\n");
sewardjd7bddad2005-06-13 16:48:32 +00001326
nethercote996901a2004-08-03 13:29:09 +00001327 /* Tool details */
bartb3af9cf2011-10-06 19:08:37 +00001328 umsg_or_xml( VG_(clo_xml) ? "%s%pS%pS%pS, %pS%s\n" : "%s%s%s%s, %s%s\n",
sewardj71bc3cb2005-05-19 00:25:45 +00001329 xpre,
njnd04b7c62002-10-03 14:05:52 +00001330 VG_(details).name,
njnb9c427c2004-12-01 14:14:42 +00001331 NULL == VG_(details).version ? "" : "-",
njnd04b7c62002-10-03 14:05:52 +00001332 NULL == VG_(details).version
floriane6a4ed12012-10-21 02:30:18 +00001333 ? "" : VG_(details).version,
sewardj71bc3cb2005-05-19 00:25:45 +00001334 VG_(details).description,
sewardj738856f2009-07-15 14:48:32 +00001335 xpost );
sewardj99a2ceb2007-11-09 12:30:36 +00001336
njn10b9aea2009-07-14 06:55:05 +00001337 if (VG_(strlen)(toolname) >= 4 && VG_STREQN(4, toolname, "exp-")) {
sewardj738856f2009-07-15 14:48:32 +00001338 umsg_or_xml(
njnb6267bd2009-08-12 00:14:16 +00001339 "%sNOTE: This is an Experimental-Class Valgrind Tool%s\n",
sewardj99a2ceb2007-11-09 12:30:36 +00001340 xpre, xpost
1341 );
1342 }
1343
bartb3af9cf2011-10-06 19:08:37 +00001344 umsg_or_xml( VG_(clo_xml) ? "%s%pS%s\n" : "%s%s%s\n",
sewardj743a2082010-07-23 17:03:22 +00001345 xpre, VG_(details).copyright_author, xpost );
sewardj3b2736a2002-03-24 12:18:35 +00001346
njnd04b7c62002-10-03 14:05:52 +00001347 /* Core details */
sewardj738856f2009-07-15 14:48:32 +00001348 umsg_or_xml(
njnf73d87f2009-07-24 04:47:04 +00001349 "%sUsing Valgrind-%s and LibVEX; rerun with -h for copyright info%s\n",
1350 xpre, VERSION, xpost
sewardj738856f2009-07-15 14:48:32 +00001351 );
sewardj45f4e7c2005-09-27 19:20:21 +00001352
njnf3977a32009-08-04 00:27:56 +00001353 // Print the command line. At one point we wrapped at 80 chars and
1354 // printed a '\' as a line joiner, but that makes it hard to cut and
1355 // paste the command line (because of the "==pid==" prefixes), so we now
1356 // favour utility and simplicity over aesthetics.
1357 umsg_or_xml("%sCommand: ", xpre);
florianb16609b2014-08-20 21:04:14 +00001358 umsg_or_xml_arg(VG_(args_the_exename));
barta92677a2011-10-22 08:24:32 +00001359
njn53162bf2009-07-29 23:34:49 +00001360 for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
1361 HChar* s = *(HChar**)VG_(indexXA)( VG_(args_for_client), i );
njnf8a11cf2009-08-02 23:03:06 +00001362 umsg_or_xml(" ");
barta92677a2011-10-22 08:24:32 +00001363 umsg_or_xml_arg(s);
njn53162bf2009-07-29 23:34:49 +00001364 }
njnf3977a32009-08-04 00:27:56 +00001365 umsg_or_xml("%s\n", xpost);
njn53162bf2009-07-29 23:34:49 +00001366
sewardjd7bddad2005-06-13 16:48:32 +00001367 if (VG_(clo_xml))
sewardj738856f2009-07-15 14:48:32 +00001368 VG_(printf_xml)("</preamble>\n");
njnd04b7c62002-10-03 14:05:52 +00001369 }
1370
njnb6267bd2009-08-12 00:14:16 +00001371 // Print the parent PID, and other stuff, if necessary.
sewardj45f4e7c2005-09-27 19:20:21 +00001372 if (!VG_(clo_xml) && VG_(clo_verbosity) > 0 && !logging_to_fd) {
njn305dc002009-07-30 23:36:43 +00001373 VG_(umsg)("Parent PID: %d\n", VG_(getppid)());
sewardj4cf05692002-10-27 20:28:29 +00001374 }
sewardj71bc3cb2005-05-19 00:25:45 +00001375 else
1376 if (VG_(clo_xml)) {
sewardj738856f2009-07-15 14:48:32 +00001377 VG_(printf_xml)("\n");
1378 VG_(printf_xml)("<pid>%d</pid>\n", VG_(getpid)());
1379 VG_(printf_xml)("<ppid>%d</ppid>\n", VG_(getppid)());
bartb3af9cf2011-10-06 19:08:37 +00001380 VG_(printf_xml)("<tool>%pS</tool>\n", toolname);
sewardj738856f2009-07-15 14:48:32 +00001381 if (xml_fname_unexpanded)
1382 print_file_vars(xml_fname_unexpanded);
sewardj768db0e2005-07-19 14:18:56 +00001383 if (VG_(clo_xml_user_comment)) {
1384 /* Note: the user comment itself is XML and is therefore to
1385 be passed through verbatim (%s) rather than escaped
bartb3af9cf2011-10-06 19:08:37 +00001386 (%pS). */
sewardj738856f2009-07-15 14:48:32 +00001387 VG_(printf_xml)("<usercomment>%s</usercomment>\n",
1388 VG_(clo_xml_user_comment));
sewardj768db0e2005-07-19 14:18:56 +00001389 }
sewardj738856f2009-07-15 14:48:32 +00001390 VG_(printf_xml)("\n");
1391 VG_(printf_xml)("<args>\n");
sewardj45f4e7c2005-09-27 19:20:21 +00001392
sewardj738856f2009-07-15 14:48:32 +00001393 VG_(printf_xml)(" <vargv>\n");
sewardj45f4e7c2005-09-27 19:20:21 +00001394 if (VG_(name_of_launcher))
bartb3af9cf2011-10-06 19:08:37 +00001395 VG_(printf_xml)(" <exe>%pS</exe>\n",
sewardj738856f2009-07-15 14:48:32 +00001396 VG_(name_of_launcher));
sewardj125fd4f2007-03-08 19:56:14 +00001397 else
bartb3af9cf2011-10-06 19:08:37 +00001398 VG_(printf_xml)(" <exe>%pS</exe>\n",
njnb1cc5d62010-07-06 04:05:23 +00001399 "(launcher name unknown)");
sewardj14c7cc52007-02-25 15:08:24 +00001400 for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) {
bartb3af9cf2011-10-06 19:08:37 +00001401 VG_(printf_xml)(
1402 " <arg>%pS</arg>\n",
sewardj738856f2009-07-15 14:48:32 +00001403 * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i )
1404 );
sewardjb8a3dac2005-07-19 12:39:11 +00001405 }
sewardj738856f2009-07-15 14:48:32 +00001406 VG_(printf_xml)(" </vargv>\n");
sewardj45f4e7c2005-09-27 19:20:21 +00001407
sewardj738856f2009-07-15 14:48:32 +00001408 VG_(printf_xml)(" <argv>\n");
florianb16609b2014-08-20 21:04:14 +00001409 VG_(printf_xml)(" <exe>%pS</exe>\n",
sewardj738856f2009-07-15 14:48:32 +00001410 VG_(args_the_exename));
sewardj14c7cc52007-02-25 15:08:24 +00001411 for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
bartb3af9cf2011-10-06 19:08:37 +00001412 VG_(printf_xml)(
1413 " <arg>%pS</arg>\n",
sewardj738856f2009-07-15 14:48:32 +00001414 * (HChar**) VG_(indexXA)( VG_(args_for_client), i )
1415 );
sewardj8665d8e2005-06-01 17:35:23 +00001416 }
sewardj738856f2009-07-15 14:48:32 +00001417 VG_(printf_xml)(" </argv>\n");
sewardj45f4e7c2005-09-27 19:20:21 +00001418
sewardj738856f2009-07-15 14:48:32 +00001419 VG_(printf_xml)("</args>\n");
sewardj71bc3cb2005-05-19 00:25:45 +00001420 }
sewardj4cf05692002-10-27 20:28:29 +00001421
njnb6267bd2009-08-12 00:14:16 +00001422 // Last thing in the preamble is a blank line.
sewardj738856f2009-07-15 14:48:32 +00001423 if (VG_(clo_xml))
1424 VG_(printf_xml)("\n");
njnb6267bd2009-08-12 00:14:16 +00001425 else if (VG_(clo_verbosity) > 0)
1426 VG_(umsg)("\n");
sewardj45f4e7c2005-09-27 19:20:21 +00001427
sewardjde4a1d02002-03-22 01:27:54 +00001428 if (VG_(clo_verbosity) > 1) {
sewardj8eb8bab2015-07-21 14:44:28 +00001429# if defined(VGO_linux)
sewardj92645592005-07-23 09:18:34 +00001430 SysRes fd;
rhyskidd05be8f52015-04-01 12:15:49 +00001431# endif
sewardj1f0bbc72005-11-16 03:51:02 +00001432 VexArch vex_arch;
1433 VexArchInfo vex_archinfo;
sewardj45f4e7c2005-09-27 19:20:21 +00001434 if (!logging_to_fd)
sewardj738856f2009-07-15 14:48:32 +00001435 VG_(message)(Vg_DebugMsg, "\n");
njna3311642009-08-10 01:29:14 +00001436 VG_(message)(Vg_DebugMsg, "Valgrind options:\n");
sewardj14c7cc52007-02-25 15:08:24 +00001437 for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) {
1438 VG_(message)(Vg_DebugMsg,
sewardj738856f2009-07-15 14:48:32 +00001439 " %s\n",
sewardj14c7cc52007-02-25 15:08:24 +00001440 * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i ));
sewardjde4a1d02002-03-22 01:27:54 +00001441 }
nethercotea70f7352004-04-18 12:08:46 +00001442
sewardj8eb8bab2015-07-21 14:44:28 +00001443# if defined(VGO_linux)
sewardj738856f2009-07-15 14:48:32 +00001444 VG_(message)(Vg_DebugMsg, "Contents of /proc/version:\n");
nethercotea70f7352004-04-18 12:08:46 +00001445 fd = VG_(open) ( "/proc/version", VKI_O_RDONLY, 0 );
njncda2f0f2009-05-18 02:12:08 +00001446 if (sr_isError(fd)) {
sewardj738856f2009-07-15 14:48:32 +00001447 VG_(message)(Vg_DebugMsg, " can't open /proc/version\n");
nethercotea70f7352004-04-18 12:08:46 +00001448 } else {
florian3b860fc2014-09-28 09:08:59 +00001449 const SizeT bufsiz = 255;
1450 HChar version_buf[bufsiz+1];
1451 VG_(message)(Vg_DebugMsg, " ");
1452 Int n, fdno = sr_Res(fd);
1453 do {
1454 n = VG_(read)(fdno, version_buf, bufsiz);
1455 if (n < 0) {
1456 VG_(message)(Vg_DebugMsg, " error reading /proc/version\n");
1457 break;
1458 }
1459 version_buf[n] = '\0';
1460 VG_(message)(Vg_DebugMsg, "%s", version_buf);
1461 } while (n == bufsiz);
1462 VG_(message)(Vg_DebugMsg, "\n");
1463 VG_(close)(fdno);
nethercotea70f7352004-04-18 12:08:46 +00001464 }
sewardj8eb8bab2015-07-21 14:44:28 +00001465# elif defined(VGO_darwin)
rhyskidd05be8f52015-04-01 12:15:49 +00001466 VG_(message)(Vg_DebugMsg, "Output from sysctl({CTL_KERN,KERN_VERSION}):\n");
1467 /* Note: preferable to use sysctlbyname("kern.version", kernelVersion, &len, NULL, 0)
1468 however that syscall is OS X 10.10+ only. */
1469 Int mib[] = {CTL_KERN, KERN_VERSION};
1470 SizeT len;
1471 VG_(sysctl)(mib, sizeof(mib)/sizeof(Int), NULL, &len, NULL, 0);
1472 HChar *kernelVersion = VG_(malloc)("main.pp.1", len);
1473 VG_(sysctl)(mib, sizeof(mib)/sizeof(Int), kernelVersion, &len, NULL, 0);
1474 VG_(message)(Vg_DebugMsg, " %s\n", kernelVersion);
1475 VG_(free)( kernelVersion );
sewardj8eb8bab2015-07-21 14:44:28 +00001476# elif defined(VGO_solaris)
1477 /* There is no /proc/version file on Solaris so we try to get some
1478 system information using the uname(2) syscall. */
1479 {
1480 struct vki_utsname uts;
1481
1482 VG_(message)(Vg_DebugMsg, "System information:\n");
1483 SysRes res = VG_(do_syscall1)(__NR_uname, (UWord)&uts);
1484 if (sr_isError(res))
1485 VG_(message)(Vg_DebugMsg, " uname() failed\n");
1486 else
1487 VG_(message)(Vg_DebugMsg, " %s %s %s %s\n",
1488 uts.sysname, uts.release, uts.version, uts.machine);
1489 }
rhyskidd05be8f52015-04-01 12:15:49 +00001490# endif
sewardj1f0bbc72005-11-16 03:51:02 +00001491
1492 VG_(machine_get_VexArchInfo)( &vex_arch, &vex_archinfo );
sewardje3121f32006-01-27 21:23:23 +00001493 VG_(message)(
1494 Vg_DebugMsg,
sewardj59731422014-07-24 12:45:24 +00001495 "Arch and hwcaps: %s, %s, %s\n",
1496 LibVEX_ppVexArch ( vex_arch ),
1497 LibVEX_ppVexEndness ( vex_archinfo.endness ),
1498 LibVEX_ppVexHwCaps ( vex_arch, vex_archinfo.hwcaps )
sewardje3121f32006-01-27 21:23:23 +00001499 );
sewardje66f2e02006-12-30 17:45:08 +00001500 VG_(message)(
1501 Vg_DebugMsg,
sewardj738856f2009-07-15 14:48:32 +00001502 "Page sizes: currently %d, max supported %d\n",
sewardje66f2e02006-12-30 17:45:08 +00001503 (Int)VKI_PAGE_SIZE, (Int)VKI_MAX_PAGE_SIZE
1504 );
sewardj738856f2009-07-15 14:48:32 +00001505 VG_(message)(Vg_DebugMsg,
1506 "Valgrind library directory: %s\n", VG_(libdir));
sewardjde4a1d02002-03-22 01:27:54 +00001507 }
nethercotef6a1d502004-08-09 12:21:57 +00001508}
1509
sewardjde4a1d02002-03-22 01:27:54 +00001510
nethercote71980f02004-01-24 18:18:54 +00001511/*====================================================================*/
1512/*=== File descriptor setup ===*/
1513/*====================================================================*/
1514
sewardj5f229e22005-09-28 01:36:01 +00001515/* Number of file descriptors that Valgrind tries to reserve for
Elliott Hughesa0664b92017-04-18 17:46:52 -07001516 its own use - just a small constant. */
1517#define N_RESERVED_FDS (12)
sewardj5f229e22005-09-28 01:36:01 +00001518
nethercote71980f02004-01-24 18:18:54 +00001519static void setup_file_descriptors(void)
1520{
1521 struct vki_rlimit rl;
sewardj17c11042006-10-15 01:26:40 +00001522 Bool show = False;
nethercote71980f02004-01-24 18:18:54 +00001523
1524 /* Get the current file descriptor limits. */
1525 if (VG_(getrlimit)(VKI_RLIMIT_NOFILE, &rl) < 0) {
1526 rl.rlim_cur = 1024;
1527 rl.rlim_max = 1024;
1528 }
1529
njnf76d27a2009-05-28 01:53:07 +00001530# if defined(VGO_darwin)
1531 /* Darwin lies. It reports file max as RLIM_INFINITY but
1532 silently disallows anything bigger than 10240. */
1533 if (rl.rlim_cur >= 10240 && rl.rlim_max == 0x7fffffffffffffffULL) {
1534 rl.rlim_max = 10240;
1535 }
1536# endif
1537
sewardj17c11042006-10-15 01:26:40 +00001538 if (show)
floriana5e06c32015-08-05 21:16:09 +00001539 VG_(printf)("fd limits: host, before: cur %llu max %llu\n",
1540 (ULong)rl.rlim_cur, (ULong)rl.rlim_max);
sewardj17c11042006-10-15 01:26:40 +00001541
nethercote71980f02004-01-24 18:18:54 +00001542 /* Work out where to move the soft limit to. */
njn14319cc2005-03-13 06:26:22 +00001543 if (rl.rlim_cur + N_RESERVED_FDS <= rl.rlim_max) {
1544 rl.rlim_cur = rl.rlim_cur + N_RESERVED_FDS;
nethercote71980f02004-01-24 18:18:54 +00001545 } else {
1546 rl.rlim_cur = rl.rlim_max;
1547 }
1548
1549 /* Reserve some file descriptors for our use. */
njn14319cc2005-03-13 06:26:22 +00001550 VG_(fd_soft_limit) = rl.rlim_cur - N_RESERVED_FDS;
1551 VG_(fd_hard_limit) = rl.rlim_cur - N_RESERVED_FDS;
nethercote71980f02004-01-24 18:18:54 +00001552
1553 /* Update the soft limit. */
1554 VG_(setrlimit)(VKI_RLIMIT_NOFILE, &rl);
1555
sewardj17c11042006-10-15 01:26:40 +00001556 if (show) {
njn8a7b41b2007-09-23 00:51:24 +00001557 VG_(printf)("fd limits: host, after: cur %lu max %lu\n",
sewardja8ffda62008-07-18 18:23:24 +00001558 (UWord)rl.rlim_cur, (UWord)rl.rlim_max);
floriana5e06c32015-08-05 21:16:09 +00001559 VG_(printf)("fd limits: guest : cur %d max %d\n",
sewardj17c11042006-10-15 01:26:40 +00001560 VG_(fd_soft_limit), VG_(fd_hard_limit));
1561 }
1562
sewardj45f4e7c2005-09-27 19:20:21 +00001563 if (VG_(cl_exec_fd) != -1)
1564 VG_(cl_exec_fd) = VG_(safe_fd)( VG_(cl_exec_fd) );
nethercote71980f02004-01-24 18:18:54 +00001565}
1566
sewardjde4a1d02002-03-22 01:27:54 +00001567
njn2da73352005-06-18 01:35:16 +00001568/*====================================================================*/
nethercote71980f02004-01-24 18:18:54 +00001569/*=== main() ===*/
1570/*====================================================================*/
1571
sewardjfdf91b42005-09-28 00:53:09 +00001572/* When main() is entered, we should be on the following stack, not
1573 the one the kernel gave us. We will run on this stack until
1574 simulation of the root thread is started, at which point a transfer
1575 is made to a dynamically allocated stack. This is for the sake of
1576 uniform overflow detection for all Valgrind threads. This is
1577 marked global even though it isn't, because assembly code below
1578 needs to reference the name. */
1579
philipped0720e42015-03-12 20:43:46 +00001580/*static*/ struct {
1581 HChar bytes [VG_STACK_GUARD_SZB + VG_DEFAULT_STACK_ACTIVE_SZB + VG_STACK_GUARD_SZB];
1582} VG_(interim_stack);
sewardjfdf91b42005-09-28 00:53:09 +00001583
sewardjf9d2f9b2006-11-17 20:00:57 +00001584/* These are the structures used to hold info for creating the initial
1585 client image.
1586
1587 'iicii' mostly holds important register state present at system
1588 startup (_start_valgrind). valgrind_main() then fills in the rest
1589 of it and passes it to VG_(ii_create_image)(). That produces
1590 'iifii', which is later handed to VG_(ii_finalise_image). */
1591
1592/* In all OS-instantiations, the_iicii has a field .sp_at_startup.
1593 This should get some address inside the stack on which we gained
sewardjfdf91b42005-09-28 00:53:09 +00001594 control (eg, it could be the SP at startup). It doesn't matter
1595 exactly where in the stack it is. This value is passed to the
sewardjf9d2f9b2006-11-17 20:00:57 +00001596 address space manager at startup. On Linux, aspacem then uses it
1597 to identify the initial stack segment and hence the upper end of
1598 the usable address space. */
sewardjfdf91b42005-09-28 00:53:09 +00001599
sewardjf9d2f9b2006-11-17 20:00:57 +00001600static IICreateImageInfo the_iicii;
1601static IIFinaliseImageInfo the_iifii;
1602
sewardjfdf91b42005-09-28 00:53:09 +00001603
sewardj9c606bd2008-09-18 18:12:50 +00001604/* A simple pair structure, used for conveying debuginfo handles to
1605 calls to VG_TRACK(new_mem_startup, ...). */
1606typedef struct { Addr a; ULong ull; } Addr_n_ULong;
1607
1608
sewardj1ae3f3a2005-09-28 10:47:38 +00001609/* --- Forwards decls to do with shutdown --- */
1610
1611static void final_tidyup(ThreadId tid);
1612
1613/* Do everything which needs doing when the last thread exits */
1614static
1615void shutdown_actions_NORETURN( ThreadId tid,
1616 VgSchedReturnCode tids_schedretcode );
1617
1618/* --- end of Forwards decls to do with shutdown --- */
sewardjfdf91b42005-09-28 00:53:09 +00001619
1620
sewardjf9d2f9b2006-11-17 20:00:57 +00001621/* By the time we get to valgrind_main, the_iicii should already have
1622 been filled in with any important details as required by whatever
1623 OS we have been built for.
1624*/
sewardj17c11042006-10-15 01:26:40 +00001625static
sewardjf9d2f9b2006-11-17 20:00:57 +00001626Int valgrind_main ( Int argc, HChar **argv, HChar **envp )
nethercote71980f02004-01-24 18:18:54 +00001627{
florian19f91bb2012-11-10 22:29:54 +00001628 const HChar* toolname = "memcheck"; // default to Memcheck
sewardj13247ca2005-12-30 22:52:20 +00001629 Int need_help = 0; // 0 = no, 1 = --help, 2 = --help-debug
sewardjde764e82007-11-09 23:13:22 +00001630 ThreadId tid_main = VG_INVALID_THREADID;
sewardj738856f2009-07-15 14:48:32 +00001631 Bool logging_to_fd = False;
florian801c1992014-10-17 21:07:37 +00001632 const HChar* xml_fname_unexpanded = NULL;
sewardj45f4e7c2005-09-27 19:20:21 +00001633 Int loglevel, i;
sewardj9c606bd2008-09-18 18:12:50 +00001634 XArray* addr2dihandle = NULL;
sewardj17c11042006-10-15 01:26:40 +00001635
nethercote71980f02004-01-24 18:18:54 +00001636 //============================================================
nethercote71980f02004-01-24 18:18:54 +00001637 //
sewardj45f4e7c2005-09-27 19:20:21 +00001638 // Nb: startup is complex. Prerequisites are shown at every step.
nethercote71980f02004-01-24 18:18:54 +00001639 // *** Be very careful when messing with the order ***
sewardj45f4e7c2005-09-27 19:20:21 +00001640 //
1641 // The first order of business is to get debug logging, the address
1642 // space manager and the dynamic memory manager up and running.
1643 // Once that's done, we can relax a bit.
1644 //
nethercote71980f02004-01-24 18:18:54 +00001645 //============================================================
sewardj45f4e7c2005-09-27 19:20:21 +00001646
1647 /* This is needed to make VG_(getenv) usable early. */
florian19f91bb2012-11-10 22:29:54 +00001648 VG_(client_envp) = (HChar**)envp;
nethercote71980f02004-01-24 18:18:54 +00001649
sewardj1cf558c2005-04-25 01:36:56 +00001650 //--------------------------------------------------------------
njnf76d27a2009-05-28 01:53:07 +00001651 // Start up Mach kernel interface, if any
1652 // p: none
1653 //--------------------------------------------------------------
1654# if defined(VGO_darwin)
1655 VG_(mach_init)();
1656# endif
1657
1658 //--------------------------------------------------------------
sewardj1cf558c2005-04-25 01:36:56 +00001659 // Start up the logging mechanism
1660 // p: none
1661 //--------------------------------------------------------------
1662 /* Start the debugging-log system ASAP. First find out how many
njn83df0b62009-02-25 01:01:05 +00001663 "-d"s were specified. This is a pre-scan of the command line. Also
philippee4d78122014-04-20 14:20:37 +00001664 get --profile-heap=yes, --core-redzone-size, --redzone-size
1665 --aspace-minaddr which are needed by the time we start up dynamic
1666 memory management. */
sewardj1cf558c2005-04-25 01:36:56 +00001667 loglevel = 0;
1668 for (i = 1; i < argc; i++) {
philippee4d78122014-04-20 14:20:37 +00001669 const HChar* tmp_str;
njn83df0b62009-02-25 01:01:05 +00001670 if (argv[i][0] != '-') break;
1671 if VG_STREQ(argv[i], "--") break;
1672 if VG_STREQ(argv[i], "-d") loglevel++;
1673 if VG_BOOL_CLO(argv[i], "--profile-heap", VG_(clo_profile_heap)) {}
philipped99c26a2012-07-31 22:17:28 +00001674 if VG_BINT_CLO(argv[i], "--core-redzone-size", VG_(clo_core_redzone_size),
1675 0, MAX_CLO_REDZONE_SZB) {}
1676 if VG_BINT_CLO(argv[i], "--redzone-size", VG_(clo_redzone_size),
1677 0, MAX_CLO_REDZONE_SZB) {}
philippee4d78122014-04-20 14:20:37 +00001678 if VG_STR_CLO(argv[i], "--aspace-minaddr", tmp_str) {
philippee4d78122014-04-20 14:20:37 +00001679 Bool ok = VG_(parse_Addr) (&tmp_str, &VG_(clo_aspacem_minAddr));
1680 if (!ok)
1681 VG_(fmsg_bad_option)(argv[i], "Invalid address\n");
florian82e7a542015-03-26 21:55:00 +00001682 const HChar *errmsg;
1683 if (!VG_(am_is_valid_for_aspacem_minAddr)(VG_(clo_aspacem_minAddr),
1684 &errmsg))
1685 VG_(fmsg_bad_option)(argv[i], "%s\n", errmsg);
philippee4d78122014-04-20 14:20:37 +00001686 }
sewardj1cf558c2005-04-25 01:36:56 +00001687 }
1688
1689 /* ... and start the debug logger. Now we can safely emit logging
1690 messages all through startup. */
sewardj10759312005-05-30 23:52:47 +00001691 VG_(debugLog_startup)(loglevel, "Stage 2 (main)");
sewardj45f4e7c2005-09-27 19:20:21 +00001692 VG_(debugLog)(1, "main", "Welcome to Valgrind version "
1693 VERSION " debug logging\n");
1694
1695 //--------------------------------------------------------------
1696 // Ensure we're on a plausible stack.
1697 // p: logging
1698 //--------------------------------------------------------------
1699 VG_(debugLog)(1, "main", "Checking current stack is plausible\n");
sewardjfdf91b42005-09-28 00:53:09 +00001700 { HChar* limLo = (HChar*)(&VG_(interim_stack).bytes[0]);
1701 HChar* limHi = limLo + sizeof(VG_(interim_stack));
sewardjed8b0f62012-09-02 21:17:36 +00001702 HChar* volatile
1703 aLocal = (HChar*)&limLo; /* any auto local will do */
1704 /* Re "volatile": Apple clang version 4.0
1705 (tags/Apple/clang-421.0.57) (based on LLVM 3.1svn)" appeared
1706 to miscompile the following check, causing run to abort at
1707 this point (in 64-bit mode) even though aLocal is within limLo
1708 .. limHi. But in fact clang is within its rights to do
1709 strange things here. "The reason is that the comparisons
1710 aLocal < limLo and aLocal >= limHi cause undefined behaviour
1711 (according to c99 6.5.8) because they compare pointers that do
1712 not point into the same aggregate." Adding "volatile" appears
1713 to fix it because "The compiler would have to prove that there
1714 is undefined behavior in order to exploit it. But as a
1715 volatile variable can change its value in ways invisible to
1716 the compiler, the compiler must make the conservative
1717 assumption that it points into the same aggregate as the other
1718 pointer its compared against. I.e. the behaviour is possibly
1719 defined." (Analysis by Florian Krohm). */
sewardj45f4e7c2005-09-27 19:20:21 +00001720 if (aLocal < limLo || aLocal >= limHi) {
1721 /* something's wrong. Stop. */
1722 VG_(debugLog)(0, "main", "Root stack %p to %p, a local %p\n",
1723 limLo, limHi, aLocal );
1724 VG_(debugLog)(0, "main", "Valgrind: FATAL: "
1725 "Initial stack switched failed.\n");
1726 VG_(debugLog)(0, "main", " Cannot continue. Sorry.\n");
1727 VG_(exit)(1);
1728 }
1729 }
1730
1731 //--------------------------------------------------------------
1732 // Ensure we have a plausible pointer to the stack on which
1733 // we gained control (not the current stack!)
1734 // p: logging
1735 //--------------------------------------------------------------
1736 VG_(debugLog)(1, "main", "Checking initial stack was noted\n");
sewardjf9d2f9b2006-11-17 20:00:57 +00001737 if (the_iicii.sp_at_startup == 0) {
sewardj45f4e7c2005-09-27 19:20:21 +00001738 VG_(debugLog)(0, "main", "Valgrind: FATAL: "
1739 "Initial stack was not noted.\n");
1740 VG_(debugLog)(0, "main", " Cannot continue. Sorry.\n");
1741 VG_(exit)(1);
1742 }
1743
1744 //--------------------------------------------------------------
1745 // Start up the address space manager, and determine the
1746 // approximate location of the client's stack
njnea2d6fd2010-07-01 00:20:20 +00001747 // p: logging, plausible-stack
sewardj45f4e7c2005-09-27 19:20:21 +00001748 //--------------------------------------------------------------
1749 VG_(debugLog)(1, "main", "Starting the address space manager\n");
Elliott Hughesa0664b92017-04-18 17:46:52 -07001750 vg_assert(VKI_PAGE_SIZE == 4096 || VKI_PAGE_SIZE == 8192
1751 || VKI_PAGE_SIZE == 16384 || VKI_PAGE_SIZE == 32768
1752 || VKI_PAGE_SIZE == 65536);
1753 vg_assert(VKI_MAX_PAGE_SIZE == 4096 || VKI_MAX_PAGE_SIZE == 8192
1754 || VKI_MAX_PAGE_SIZE == 16384 || VKI_MAX_PAGE_SIZE == 32768
1755 || VKI_MAX_PAGE_SIZE == 65536);
sewardje66f2e02006-12-30 17:45:08 +00001756 vg_assert(VKI_PAGE_SIZE <= VKI_MAX_PAGE_SIZE);
1757 vg_assert(VKI_PAGE_SIZE == (1 << VKI_PAGE_SHIFT));
1758 vg_assert(VKI_MAX_PAGE_SIZE == (1 << VKI_MAX_PAGE_SHIFT));
philippe38a74d22014-08-29 22:53:19 +00001759 the_iicii.clstack_end = VG_(am_startup)( the_iicii.sp_at_startup );
sewardj45f4e7c2005-09-27 19:20:21 +00001760 VG_(debugLog)(1, "main", "Address space manager is running\n");
1761
1762 //--------------------------------------------------------------
1763 // Start up the dynamic memory manager
1764 // p: address space management
philipped99c26a2012-07-31 22:17:28 +00001765 // p: getting --profile-heap,--core-redzone-size,--redzone-size
sewardj45f4e7c2005-09-27 19:20:21 +00001766 // In fact m_mallocfree is self-initialising, so there's no
1767 // initialisation call to do. Instead, try a simple malloc/
1768 // free pair right now to check that nothing is broken.
1769 //--------------------------------------------------------------
1770 VG_(debugLog)(1, "main", "Starting the dynamic memory manager\n");
sewardj9c606bd2008-09-18 18:12:50 +00001771 { void* p = VG_(malloc)( "main.vm.1", 12345 );
florianf5d8e652014-09-11 22:15:39 +00001772 VG_(free)( p );
sewardj45f4e7c2005-09-27 19:20:21 +00001773 }
1774 VG_(debugLog)(1, "main", "Dynamic memory manager is running\n");
sewardj1cf558c2005-04-25 01:36:56 +00001775
nethercotef4928da2004-06-15 10:54:40 +00001776 //============================================================
sewardj45f4e7c2005-09-27 19:20:21 +00001777 //
1778 // Dynamic memory management is now available.
1779 //
nethercotef4928da2004-06-15 10:54:40 +00001780 //============================================================
1781
sewardj45f4e7c2005-09-27 19:20:21 +00001782 //--------------------------------------------------------------
sewardjf98e1c02008-10-25 16:22:41 +00001783 // Initialise m_debuginfo
1784 // p: dynamic memory allocation
1785 VG_(debugLog)(1, "main", "Initialise m_debuginfo\n");
1786 VG_(di_initialise)();
1787
1788 //--------------------------------------------------------------
sewardj45f4e7c2005-09-27 19:20:21 +00001789 // Look for alternative libdir
1790 { HChar *cp = VG_(getenv)(VALGRIND_LIB);
1791 if (cp != NULL)
1792 VG_(libdir) = cp;
njncde90d32009-07-22 22:41:38 +00001793 VG_(debugLog)(1, "main", "VG_(libdir) = %s\n", VG_(libdir));
sewardj45f4e7c2005-09-27 19:20:21 +00001794 }
1795
1796 //--------------------------------------------------------------
1797 // Extract the launcher name from the environment.
njna842d792009-05-21 01:15:18 +00001798 VG_(debugLog)(1, "main", "Getting launcher's name ...\n");
sewardj45f4e7c2005-09-27 19:20:21 +00001799 VG_(name_of_launcher) = VG_(getenv)(VALGRIND_LAUNCHER);
1800 if (VG_(name_of_launcher) == NULL) {
1801 VG_(printf)("valgrind: You cannot run '%s' directly.\n", argv[0]);
1802 VG_(printf)("valgrind: You should use $prefix/bin/valgrind.\n");
1803 VG_(exit)(1);
1804 }
njna842d792009-05-21 01:15:18 +00001805 VG_(debugLog)(1, "main", "... %s\n", VG_(name_of_launcher));
sewardj45f4e7c2005-09-27 19:20:21 +00001806
1807 //--------------------------------------------------------------
Elliott Hughesa0664b92017-04-18 17:46:52 -07001808 // We used to set the process datasize rlimit to zero to prevent
1809 // any internal use of brk() from having any effect. But later
1810 // linux kernels redefine RLIMIT_DATA as the size of any data
1811 // areas, including some dynamic mmap memory allocations.
1812 // See bug #357833 for the commit that went into linux 4.5
1813 // changing the definition of RLIMIT_DATA. So don't mess with
1814 // RLIMIT_DATA here now anymore. Just remember it for use in
1815 // the syscall wrappers.
fitzhardingeb50068f2004-02-24 23:42:55 +00001816 VG_(getrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data));
thughesc37184f2004-09-11 14:16:57 +00001817
1818 // Get the current process stack rlimit.
1819 VG_(getrlimit)(VKI_RLIMIT_STACK, &VG_(client_rlimit_stack));
1820
sewardje2d1e672005-11-12 23:10:48 +00001821 //--------------------------------------------------------------
1822 // Figure out what sort of CPU we're on, and whether it is
1823 // able to run V.
carll52b284b2015-04-09 16:23:20 +00001824 /* The vex_archinfo structure is passed down later to the client
1825 * to verify the HW info settings are consistent.
1826 */
1827 VexArchInfo vex_archinfo;
sewardje2d1e672005-11-12 23:10:48 +00001828 VG_(debugLog)(1, "main", "Get hardware capabilities ...\n");
1829 { VexArch vex_arch;
sewardje2d1e672005-11-12 23:10:48 +00001830 Bool ok = VG_(machine_get_hwcaps)();
1831 if (!ok) {
1832 VG_(printf)("\n");
1833 VG_(printf)("valgrind: fatal error: unsupported CPU.\n");
1834 VG_(printf)(" Supported CPUs are:\n");
1835 VG_(printf)(" * x86 (practically any; Pentium-I or above), "
1836 "AMD Athlon or above)\n");
1837 VG_(printf)(" * AMD Athlon64/Opteron\n");
philippe3bcd51d2013-06-12 21:45:39 +00001838 VG_(printf)(" * ARM (armv7)\n");
sewardje2d1e672005-11-12 23:10:48 +00001839 VG_(printf)(" * PowerPC (most; ppc405 and above)\n");
florian92b33b52014-12-08 14:03:00 +00001840 VG_(printf)(" * System z (64bit only - s390x; z990 and above)\n");
sewardje2d1e672005-11-12 23:10:48 +00001841 VG_(printf)("\n");
1842 VG_(exit)(1);
1843 }
1844 VG_(machine_get_VexArchInfo)( &vex_arch, &vex_archinfo );
sewardje3121f32006-01-27 21:23:23 +00001845 VG_(debugLog)(
1846 1, "main", "... arch = %s, hwcaps = %s\n",
1847 LibVEX_ppVexArch ( vex_arch ),
1848 LibVEX_ppVexHwCaps ( vex_arch, vex_archinfo.hwcaps )
1849 );
sewardje2d1e672005-11-12 23:10:48 +00001850 }
1851
sewardj198f34f2007-07-09 23:13:07 +00001852 //--------------------------------------------------------------
1853 // Record the working directory at startup
sewardj6e9de462011-06-28 07:25:29 +00001854 // p: none
sewardj198f34f2007-07-09 23:13:07 +00001855 VG_(debugLog)(1, "main", "Getting the working directory at startup\n");
Elliott Hughesa0664b92017-04-18 17:46:52 -07001856 VG_(record_startup_wd)();
1857 const HChar *wd = VG_(get_startup_wd)();
1858 VG_(debugLog)(1, "main", "... %s\n", wd != NULL ? wd : "<NO CWD>" );
sewardj198f34f2007-07-09 23:13:07 +00001859
sewardj45f4e7c2005-09-27 19:20:21 +00001860 //============================================================
1861 // Command line argument handling order:
1862 // * If --help/--help-debug are present, show usage message
1863 // (including the tool-specific usage)
1864 // * (If no --tool option given, default to Memcheck)
1865 // * Then, if client is missing, abort with error msg
1866 // * Then, if any cmdline args are bad, abort with error msg
1867 //============================================================
1868
1869 //--------------------------------------------------------------
1870 // Split up argv into: C args, V args, V extra args, and exename.
1871 // p: dynamic memory allocation
1872 //--------------------------------------------------------------
1873 VG_(debugLog)(1, "main", "Split up command line\n");
1874 VG_(split_up_argv)( argc, argv );
sewardj14c7cc52007-02-25 15:08:24 +00001875 vg_assert( VG_(args_for_valgrind) );
1876 vg_assert( VG_(args_for_client) );
sewardj45f4e7c2005-09-27 19:20:21 +00001877 if (0) {
sewardj14c7cc52007-02-25 15:08:24 +00001878 for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++)
1879 VG_(printf)(
1880 "varg %s\n",
1881 * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i )
1882 );
sewardj45f4e7c2005-09-27 19:20:21 +00001883 VG_(printf)(" exe %s\n", VG_(args_the_exename));
sewardj14c7cc52007-02-25 15:08:24 +00001884 for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++)
1885 VG_(printf)(
1886 "carg %s\n",
1887 * (HChar**) VG_(indexXA)( VG_(args_for_client), i )
1888 );
nethercote71980f02004-01-24 18:18:54 +00001889 }
1890
1891 //--------------------------------------------------------------
sewardj45f4e7c2005-09-27 19:20:21 +00001892 // Extract tool name and whether help has been requested.
1893 // Note we can't print the help message yet, even if requested,
1894 // because the tool has not been initialised.
1895 // p: split_up_argv [for VG_(args_for_valgrind)]
nethercote71980f02004-01-24 18:18:54 +00001896 //--------------------------------------------------------------
sewardj95d86c02007-12-18 01:49:23 +00001897 VG_(debugLog)(1, "main",
1898 "(early_) Process Valgrind's command line options\n");
1899 early_process_cmd_line_options(&need_help, &toolname);
nethercote71980f02004-01-24 18:18:54 +00001900
sewardjdcd1ed42014-09-01 22:32:52 +00001901 // BEGIN HACK
1902 vg_assert(toolname != NULL);
1903 vg_assert(VG_(clo_read_inline_info) == False);
sewardj88f762e2014-09-07 19:53:41 +00001904# if !defined(VGO_darwin)
sewardjdcd1ed42014-09-01 22:32:52 +00001905 if (0 == VG_(strcmp)(toolname, "memcheck")
sewardj88f762e2014-09-07 19:53:41 +00001906 || 0 == VG_(strcmp)(toolname, "helgrind")
1907 || 0 == VG_(strcmp)(toolname, "drd")) {
sewardjdcd1ed42014-09-01 22:32:52 +00001908 /* Change the default setting. Later on (just below)
1909 main_process_cmd_line_options should pick up any
1910 user-supplied setting for it and will override the default
1911 set here. */
1912 VG_(clo_read_inline_info) = True;
1913 }
sewardj88f762e2014-09-07 19:53:41 +00001914# endif
sewardjdcd1ed42014-09-01 22:32:52 +00001915 // END HACK
1916
sewardj8d47a612015-02-05 12:59:46 +00001917 // Set default vex control params.
sewardj45f4e7c2005-09-27 19:20:21 +00001918 LibVEX_default_VexControl(& VG_(clo_vex_control));
nethercote71980f02004-01-24 18:18:54 +00001919
1920 //--------------------------------------------------------------
1921 // Load client executable, finding in $PATH if necessary
njn83df0b62009-02-25 01:01:05 +00001922 // p: early_process_cmd_line_options() [for 'exec', 'need_help',
1923 // clo_max_stackframe,
1924 // clo_main_stacksize]
sewardj95d86c02007-12-18 01:49:23 +00001925 // p: layout_remaining_space [so there's space]
sewardj17c11042006-10-15 01:26:40 +00001926 //
nethercote71980f02004-01-24 18:18:54 +00001927 // Set up client's environment
sewardj95d86c02007-12-18 01:49:23 +00001928 // p: set-libdir [for VG_(libdir)]
1929 // p: early_process_cmd_line_options [for toolname]
sewardj17c11042006-10-15 01:26:40 +00001930 //
nethercote5ee67ca2004-06-22 14:00:09 +00001931 // Setup client stack, eip, and VG_(client_arg[cv])
nethercote71980f02004-01-24 18:18:54 +00001932 // p: load_client() [for 'info']
1933 // p: fix_environment() [for 'env']
sewardj17c11042006-10-15 01:26:40 +00001934 //
sewardj45f4e7c2005-09-27 19:20:21 +00001935 // Setup client data (brk) segment. Initially a 1-page segment
1936 // which abuts a shrinkable reservation.
1937 // p: load_client() [for 'info' and hence VG_(brk_base)]
sewardjf9d2f9b2006-11-17 20:00:57 +00001938 //
1939 // p: _start_in_C (for zeroing out the_iicii and putting some
1940 // initial values into it)
sewardj45f4e7c2005-09-27 19:20:21 +00001941 //--------------------------------------------------------------
sewardj17c11042006-10-15 01:26:40 +00001942 if (!need_help) {
sewardjf9d2f9b2006-11-17 20:00:57 +00001943 VG_(debugLog)(1, "main", "Create initial image\n");
1944
sewardj8eb8bab2015-07-21 14:44:28 +00001945# if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris)
sewardjf9d2f9b2006-11-17 20:00:57 +00001946 the_iicii.argv = argv;
1947 the_iicii.envp = envp;
1948 the_iicii.toolname = toolname;
sewardjf9d2f9b2006-11-17 20:00:57 +00001949# else
njna842d792009-05-21 01:15:18 +00001950# error "Unknown platform"
sewardjf9d2f9b2006-11-17 20:00:57 +00001951# endif
1952
sewardjdc2f79e2007-12-22 14:14:04 +00001953 /* NOTE: this call reads VG_(clo_main_stacksize). */
carll52b284b2015-04-09 16:23:20 +00001954 the_iifii = VG_(ii_create_image)( the_iicii, &vex_archinfo );
sewardj45f4e7c2005-09-27 19:20:21 +00001955 }
nethercote71980f02004-01-24 18:18:54 +00001956
1957 //==============================================================
sewardj45f4e7c2005-09-27 19:20:21 +00001958 //
1959 // Finished loading/setting up the client address space.
1960 //
nethercote71980f02004-01-24 18:18:54 +00001961 //==============================================================
1962
1963 //--------------------------------------------------------------
nethercote71980f02004-01-24 18:18:54 +00001964 // setup file descriptors
1965 // p: n/a
1966 //--------------------------------------------------------------
sewardj1fbc1a52005-04-25 02:05:54 +00001967 VG_(debugLog)(1, "main", "Setup file descriptors\n");
nethercote71980f02004-01-24 18:18:54 +00001968 setup_file_descriptors();
1969
1970 //--------------------------------------------------------------
tom41ad7e72012-10-04 20:27:38 +00001971 // create fake /proc/<pid>/cmdline and /proc/<pid>/auxv files
1972 // and then unlink them, but hold onto the fds, so we can handr
1973 // them out to the client when it tries to open
1974 // /proc/<pid>/cmdline or /proc/<pid>/auxv for itself.
sewardj45f4e7c2005-09-27 19:20:21 +00001975 // p: setup file descriptors
tom41ad7e72012-10-04 20:27:38 +00001976 // p: ii_create_image for VG_(client_auxv) setup.
nethercotec314eba2004-07-15 12:59:41 +00001977 //--------------------------------------------------------------
bart9b533f82009-08-25 20:15:41 +00001978 VG_(cl_cmdline_fd) = -1;
tom41ad7e72012-10-04 20:27:38 +00001979 VG_(cl_auxv_fd) = -1;
sewardj8eb8bab2015-07-21 14:44:28 +00001980#if defined(VGO_solaris)
1981 VG_(cl_psinfo_fd) = -1;
1982#endif
1983
1984#if defined(VGO_linux) || defined(VGO_solaris)
bart9b533f82009-08-25 20:15:41 +00001985 if (!need_help) {
florian87c8abb2014-11-09 16:15:23 +00001986 HChar buf[50]; // large enough
1987 HChar buf2[VG_(mkstemp_fullname_bufsz)(sizeof buf - 1)];
bart9b533f82009-08-25 20:15:41 +00001988 Int fd, r;
sewardj8eb8bab2015-07-21 14:44:28 +00001989
iraisr23e68ca2015-08-14 20:50:11 +00001990#if defined(VGO_linux) || defined(SOLARIS_PROC_CMDLINE)
1991 /* Fake /proc/<pid>/cmdline only on Linux and Solaris if supported. */
sewardj8eb8bab2015-07-21 14:44:28 +00001992 HChar nul[1];
barta3054f52010-06-14 18:12:56 +00001993 const HChar* exename;
nethercotec314eba2004-07-15 12:59:41 +00001994
bart9b533f82009-08-25 20:15:41 +00001995 VG_(debugLog)(1, "main", "Create fake /proc/<pid>/cmdline\n");
sewardj45f4e7c2005-09-27 19:20:21 +00001996
bart9b533f82009-08-25 20:15:41 +00001997 VG_(sprintf)(buf, "proc_%d_cmdline", VG_(getpid)());
1998 fd = VG_(mkstemp)( buf, buf2 );
1999 if (fd == -1)
florian1763e812011-07-12 19:07:05 +00002000 VG_(err_config_error)("Can't create client cmdline file in %s\n", buf2);
sewardj45f4e7c2005-09-27 19:20:21 +00002001
bart9b533f82009-08-25 20:15:41 +00002002 nul[0] = 0;
florianb16609b2014-08-20 21:04:14 +00002003 exename = VG_(args_the_exename);
sewardjc7ffc942011-03-28 16:26:42 +00002004 VG_(write)(fd, exename, VG_(strlen)( exename ));
bart9b533f82009-08-25 20:15:41 +00002005 VG_(write)(fd, nul, 1);
2006
2007 for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
2008 HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i );
2009 VG_(write)(fd, arg, VG_(strlen)( arg ));
sewardj45f4e7c2005-09-27 19:20:21 +00002010 VG_(write)(fd, nul, 1);
2011 }
bart9b533f82009-08-25 20:15:41 +00002012
2013 /* Don't bother to seek the file back to the start; instead do
iraisr23e68ca2015-08-14 20:50:11 +00002014 it every time a copy of it is given out (by PRE(sys_open) or
2015 PRE(sys_openat)). That is probably more robust across fork() etc. */
bart9b533f82009-08-25 20:15:41 +00002016
2017 /* Now delete it, but hang on to the fd. */
2018 r = VG_(unlink)( buf2 );
2019 if (r)
florian1763e812011-07-12 19:07:05 +00002020 VG_(err_config_error)("Can't delete client cmdline file in %s\n", buf2);
bart9b533f82009-08-25 20:15:41 +00002021
2022 VG_(cl_cmdline_fd) = fd;
iraisr23e68ca2015-08-14 20:50:11 +00002023#endif // defined(VGO_linux) || defined(SOLARIS_PROC_CMDLINE)
tom41ad7e72012-10-04 20:27:38 +00002024
sewardj8eb8bab2015-07-21 14:44:28 +00002025 /* Fake /proc/<pid>/auxv on both Linux and Solaris. */
tom41ad7e72012-10-04 20:27:38 +00002026 VG_(debugLog)(1, "main", "Create fake /proc/<pid>/auxv\n");
2027
2028 VG_(sprintf)(buf, "proc_%d_auxv", VG_(getpid)());
2029 fd = VG_(mkstemp)( buf, buf2 );
2030 if (fd == -1)
2031 VG_(err_config_error)("Can't create client auxv file in %s\n", buf2);
2032
2033 UWord *client_auxv = VG_(client_auxv);
2034 unsigned int client_auxv_len = 0;
2035 while (*client_auxv != 0) {
2036 client_auxv++;
2037 client_auxv++;
2038 client_auxv_len += 2 * sizeof(UWord);
2039 }
2040 client_auxv_len += 2 * sizeof(UWord);
2041
2042 VG_(write)(fd, VG_(client_auxv), client_auxv_len);
2043
2044 /* Don't bother to seek the file back to the start; instead do
2045 it every time a copy of it is given out (by PRE(sys_open)).
2046 That is probably more robust across fork() etc. */
2047
2048 /* Now delete it, but hang on to the fd. */
2049 r = VG_(unlink)( buf2 );
2050 if (r)
2051 VG_(err_config_error)("Can't delete client auxv file in %s\n", buf2);
2052
2053 VG_(cl_auxv_fd) = fd;
sewardj8eb8bab2015-07-21 14:44:28 +00002054
2055#if defined(VGO_solaris)
2056 /* Fake /proc/<pid>/psinfo on Solaris.
2057 * Contents will be fetched and partially faked later on the fly. */
2058 VG_(debugLog)(1, "main", "Create fake /proc/<pid>/psinfo\n");
2059
2060 VG_(sprintf)(buf, "proc_%d_psinfo", VG_(getpid)());
2061 fd = VG_(mkstemp)( buf, buf2 );
2062 if (fd == -1)
2063 VG_(err_config_error)("Can't create client psinfo file in %s\n", buf2);
2064
2065 /* Now delete it, but hang on to the fd. */
2066 r = VG_(unlink)( buf2 );
2067 if (r)
2068 VG_(err_config_error)("Can't delete client psinfo file in %s\n", buf2);
2069
2070 VG_(cl_psinfo_fd) = fd;
2071#endif /* VGO_solaris */
sewardj45f4e7c2005-09-27 19:20:21 +00002072 }
bart9b533f82009-08-25 20:15:41 +00002073#endif
nethercotec314eba2004-07-15 12:59:41 +00002074
2075 //--------------------------------------------------------------
sewardj45f4e7c2005-09-27 19:20:21 +00002076 // Init tool part 1: pre_clo_init
nethercotec314eba2004-07-15 12:59:41 +00002077 // p: setup_client_stack() [for 'VG_(client_arg[cv]']
nethercotec314eba2004-07-15 12:59:41 +00002078 // p: setup_file_descriptors() [for 'VG_(fd_xxx_limit)']
nethercote71980f02004-01-24 18:18:54 +00002079 //--------------------------------------------------------------
sewardj7cf4e6b2008-05-01 20:24:26 +00002080 VG_(debugLog)(1, "main", "Initialise the tool part 1 (pre_clo_init)\n");
njn08ce7b32009-02-27 03:38:28 +00002081 VG_(tl_pre_clo_init)();
philippea0a73932014-06-15 15:42:20 +00002082 // Activate var info readers, if the tool asked for it:
2083 if (VG_(needs).var_info)
2084 VG_(clo_read_var_info) = True;
nethercote71980f02004-01-24 18:18:54 +00002085
sewardj45f4e7c2005-09-27 19:20:21 +00002086 //--------------------------------------------------------------
nethercotef4928da2004-06-15 10:54:40 +00002087 // If --tool and --help/--help-debug was given, now give the core+tool
2088 // help message
sewardj95d86c02007-12-18 01:49:23 +00002089 // p: early_process_cmd_line_options() [for 'need_help']
2090 // p: tl_pre_clo_init [for 'VG_(tdict).usage']
sewardj45f4e7c2005-09-27 19:20:21 +00002091 //--------------------------------------------------------------
2092 VG_(debugLog)(1, "main", "Print help and quit, if requested\n");
nethercotef4928da2004-06-15 10:54:40 +00002093 if (need_help) {
njncce38e62010-07-06 04:25:12 +00002094 usage_NORETURN(/*--help-debug?*/need_help >= 2);
nethercotef4928da2004-06-15 10:54:40 +00002095 }
nethercotec314eba2004-07-15 12:59:41 +00002096
sewardj45f4e7c2005-09-27 19:20:21 +00002097 //--------------------------------------------------------------
2098 // Process command line options to Valgrind + tool
2099 // p: setup_client_stack() [for 'VG_(client_arg[cv]']
2100 // p: setup_file_descriptors() [for 'VG_(fd_xxx_limit)']
2101 //--------------------------------------------------------------
sewardj95d86c02007-12-18 01:49:23 +00002102 VG_(debugLog)(1, "main",
2103 "(main_) Process Valgrind's command line options, "
2104 "setup logging\n");
sewardj738856f2009-07-15 14:48:32 +00002105 main_process_cmd_line_options ( &logging_to_fd, &xml_fname_unexpanded,
2106 toolname );
sewardj45f4e7c2005-09-27 19:20:21 +00002107
2108 //--------------------------------------------------------------
sewardj592ae092005-11-08 19:01:44 +00002109 // Zeroise the millisecond counter by doing a first read of it.
2110 // p: none
2111 //--------------------------------------------------------------
2112 (void) VG_(read_millisecond_timer)();
2113
2114 //--------------------------------------------------------------
sewardj45f4e7c2005-09-27 19:20:21 +00002115 // Print the preamble
2116 // p: tl_pre_clo_init [for 'VG_(details).name' and friends]
sewardj738856f2009-07-15 14:48:32 +00002117 // p: main_process_cmd_line_options()
2118 // [for VG_(clo_verbosity), VG_(clo_xml),
2119 // logging_to_fd, xml_fname_unexpanded]
sewardj45f4e7c2005-09-27 19:20:21 +00002120 //--------------------------------------------------------------
2121 VG_(debugLog)(1, "main", "Print the preamble...\n");
sewardj738856f2009-07-15 14:48:32 +00002122 print_preamble(logging_to_fd, xml_fname_unexpanded, toolname);
sewardj45f4e7c2005-09-27 19:20:21 +00002123 VG_(debugLog)(1, "main", "...finished the preamble\n");
2124
2125 //--------------------------------------------------------------
2126 // Init tool part 2: post_clo_init
2127 // p: setup_client_stack() [for 'VG_(client_arg[cv]']
2128 // p: setup_file_descriptors() [for 'VG_(fd_xxx_limit)']
2129 // p: print_preamble() [so any warnings printed in post_clo_init
2130 // are shown after the preamble]
2131 //--------------------------------------------------------------
2132 VG_(debugLog)(1, "main", "Initialise the tool part 2 (post_clo_init)\n");
njn51d827b2005-05-09 01:02:08 +00002133 VG_TDICT_CALL(tool_post_clo_init);
sewardj7cf4e6b2008-05-01 20:24:26 +00002134 {
2135 /* The tool's "needs" will by now be finalised, since it has no
2136 further opportunity to specify them. So now sanity check
2137 them. */
floriane6a4ed12012-10-21 02:30:18 +00002138 const HChar* s;
sewardj7cf4e6b2008-05-01 20:24:26 +00002139 Bool ok;
2140 ok = VG_(sanity_check_needs)( &s );
2141 if (!ok) {
floriana4ca4fe2014-09-16 09:28:12 +00002142 VG_(core_panic)(s);
sewardj7cf4e6b2008-05-01 20:24:26 +00002143 }
2144 }
nethercotef4928da2004-06-15 10:54:40 +00002145
2146 //--------------------------------------------------------------
sewardj45f4e7c2005-09-27 19:20:21 +00002147 // Initialise translation table and translation cache
2148 // p: aspacem [??]
2149 // p: tl_pre_clo_init [for 'VG_(details).avg_translation_sizeB']
nethercote71980f02004-01-24 18:18:54 +00002150 //--------------------------------------------------------------
sewardj45f4e7c2005-09-27 19:20:21 +00002151 VG_(debugLog)(1, "main", "Initialise TT/TC\n");
2152 VG_(init_tt_tc)();
sewardjb5f6f512005-03-10 23:59:00 +00002153
sewardj45f4e7c2005-09-27 19:20:21 +00002154 //--------------------------------------------------------------
2155 // Initialise the redirect table.
2156 // p: init_tt_tc [so it can call VG_(search_transtab) safely]
2157 // p: aspacem [so can change ownership of sysinfo pages]
2158 //--------------------------------------------------------------
2159 VG_(debugLog)(1, "main", "Initialise redirects\n");
sewardj0ec07f32006-01-12 12:32:32 +00002160 VG_(redir_initialise)();
nethercote71980f02004-01-24 18:18:54 +00002161
2162 //--------------------------------------------------------------
2163 // Allow GDB attach
sewardj95d86c02007-12-18 01:49:23 +00002164 // p: main_process_cmd_line_options() [for VG_(clo_wait_for_gdb)]
nethercote71980f02004-01-24 18:18:54 +00002165 //--------------------------------------------------------------
2166 /* Hook to delay things long enough so we can get the pid and
2167 attach GDB in another shell. */
2168 if (VG_(clo_wait_for_gdb)) {
Elliott Hughesa0664b92017-04-18 17:46:52 -07002169 const int ms = 8000; // milliseconds
2170 VG_(debugLog)(1, "main", "Wait for GDB during %d ms\n", ms);
2171 VG_(printf)("pid=%d, entering delay %d ms loop\n", VG_(getpid)(), ms);
2172 VG_(poll)(NULL, 0, ms);
nethercote71980f02004-01-24 18:18:54 +00002173 }
2174
sewardjb5d320c2005-03-13 18:57:15 +00002175 //--------------------------------------------------------------
nethercote71980f02004-01-24 18:18:54 +00002176 // Search for file descriptors that are inherited from our parent
sewardj95d86c02007-12-18 01:49:23 +00002177 // p: main_process_cmd_line_options [for VG_(clo_track_fds)]
nethercote71980f02004-01-24 18:18:54 +00002178 //--------------------------------------------------------------
sewardj1fbc1a52005-04-25 02:05:54 +00002179 if (VG_(clo_track_fds)) {
2180 VG_(debugLog)(1, "main", "Init preopened fds\n");
nethercote71980f02004-01-24 18:18:54 +00002181 VG_(init_preopened_fds)();
sewardj1fbc1a52005-04-25 02:05:54 +00002182 }
nethercote71980f02004-01-24 18:18:54 +00002183
sewardj8eb8bab2015-07-21 14:44:28 +00002184#if defined(VGO_solaris)
2185 VG_(syswrap_init)();
2186#endif
2187
nethercote71980f02004-01-24 18:18:54 +00002188 //--------------------------------------------------------------
sewardj45f4e7c2005-09-27 19:20:21 +00002189 // Load debug info for the existing segments.
2190 // p: setup_code_redirect_table [so that redirs can be recorded]
2191 // p: mallocfree
2192 // p: probably: setup fds and process CLOs, so that logging works
sewardjf98e1c02008-10-25 16:22:41 +00002193 // p: initialise m_debuginfo
sewardj9c606bd2008-09-18 18:12:50 +00002194 //
2195 // While doing this, make a note of the debuginfo-handles that
sewardj6e9de462011-06-28 07:25:29 +00002196 // come back from VG_(di_notify_mmap).
sewardj9c606bd2008-09-18 18:12:50 +00002197 // Later, in "Tell the tool about the initial client memory permissions"
2198 // (just below) we can then hand these handles off to the tool in
2199 // calls to VG_TRACK(new_mem_startup, ...). This gives the tool the
2200 // opportunity to make further queries to m_debuginfo before the
2201 // client is started, if it wants. We put this information into an
2202 // XArray, each handle along with the associated segment start address,
2203 // and search the XArray for the handles later, when calling
2204 // VG_TRACK(new_mem_startup, ...).
sewardj45f4e7c2005-09-27 19:20:21 +00002205 //--------------------------------------------------------------
2206 VG_(debugLog)(1, "main", "Load initial debug info\n");
sewardj9c606bd2008-09-18 18:12:50 +00002207
floriane2800c92014-09-15 20:57:45 +00002208 vg_assert(!addr2dihandle);
sewardj9c606bd2008-09-18 18:12:50 +00002209 addr2dihandle = VG_(newXA)( VG_(malloc), "main.vm.2",
2210 VG_(free), sizeof(Addr_n_ULong) );
sewardj9c606bd2008-09-18 18:12:50 +00002211
sewardj8eb8bab2015-07-21 14:44:28 +00002212# if defined(VGO_linux) || defined(VGO_solaris)
sewardj45f4e7c2005-09-27 19:20:21 +00002213 { Addr* seg_starts;
2214 Int n_seg_starts;
sewardj9c606bd2008-09-18 18:12:50 +00002215 Addr_n_ULong anu;
sewardj45f4e7c2005-09-27 19:20:21 +00002216
florianea8a88c2015-02-20 14:00:23 +00002217 seg_starts = VG_(get_segment_starts)( SkFileC | SkFileV, &n_seg_starts );
sewardj17c11042006-10-15 01:26:40 +00002218 vg_assert(seg_starts && n_seg_starts >= 0);
sewardj45f4e7c2005-09-27 19:20:21 +00002219
sewardjf72cced2005-11-08 00:45:47 +00002220 /* show them all to the debug info reader. allow_SkFileV has to
2221 be True here so that we read info from the valgrind executable
2222 itself. */
sewardj9c606bd2008-09-18 18:12:50 +00002223 for (i = 0; i < n_seg_starts; i++) {
sewardj5f2dcad2011-10-24 08:53:03 +00002224 anu.ull = VG_(di_notify_mmap)( seg_starts[i], True/*allow_SkFileV*/,
2225 -1/*Don't use_fd*/);
sewardj9c606bd2008-09-18 18:12:50 +00002226 /* anu.ull holds the debuginfo handle returned by di_notify_mmap,
2227 if any. */
2228 if (anu.ull > 0) {
2229 anu.a = seg_starts[i];
2230 VG_(addToXA)( addr2dihandle, &anu );
2231 }
2232 }
sewardj45f4e7c2005-09-27 19:20:21 +00002233
2234 VG_(free)( seg_starts );
2235 }
njnf76d27a2009-05-28 01:53:07 +00002236# elif defined(VGO_darwin)
2237 { Addr* seg_starts;
2238 Int n_seg_starts;
florianea8a88c2015-02-20 14:00:23 +00002239 seg_starts = VG_(get_segment_starts)( SkFileC, &n_seg_starts );
njnf76d27a2009-05-28 01:53:07 +00002240 vg_assert(seg_starts && n_seg_starts >= 0);
2241
2242 /* show them all to the debug info reader.
2243 Don't read from V segments (unlike Linux) */
2244 // GrP fixme really?
sewardj5f2dcad2011-10-24 08:53:03 +00002245 for (i = 0; i < n_seg_starts; i++) {
2246 VG_(di_notify_mmap)( seg_starts[i], False/*don't allow_SkFileV*/,
2247 -1/*don't use_fd*/);
2248 }
njnf76d27a2009-05-28 01:53:07 +00002249
2250 VG_(free)( seg_starts );
2251 }
sewardj17c11042006-10-15 01:26:40 +00002252# else
2253# error Unknown OS
2254# endif
sewardj45f4e7c2005-09-27 19:20:21 +00002255
2256 //--------------------------------------------------------------
2257 // Tell aspacem of ownership change of the asm helpers, so that
2258 // m_translate allows them to be translated. However, only do this
2259 // after the initial debug info read, since making a hole in the
2260 // address range for the stage2 binary confuses the debug info reader.
2261 // p: aspacem
2262 //--------------------------------------------------------------
2263 { Bool change_ownership_v_c_OK;
sewardj1a85f4f2006-01-12 21:15:35 +00002264 Addr co_start = VG_PGROUNDDN( (Addr)&VG_(trampoline_stuff_start) );
2265 Addr co_endPlus = VG_PGROUNDUP( (Addr)&VG_(trampoline_stuff_end) );
sewardj45f4e7c2005-09-27 19:20:21 +00002266 VG_(debugLog)(1,"redir",
2267 "transfer ownership V -> C of 0x%llx .. 0x%llx\n",
2268 (ULong)co_start, (ULong)co_endPlus-1 );
2269
2270 change_ownership_v_c_OK
2271 = VG_(am_change_ownership_v_to_c)( co_start, co_endPlus - co_start );
2272 vg_assert(change_ownership_v_c_OK);
2273 }
2274
bart6c63f5c2011-10-11 18:50:14 +00002275 if (VG_(clo_xml)) {
florianf44ff622014-12-20 16:52:08 +00002276 HChar buf[50]; // large enough
florian77dec3a2014-11-14 22:34:40 +00002277 VG_(elapsed_wallclock_time)(buf, sizeof buf);
bart6c63f5c2011-10-11 18:50:14 +00002278 VG_(printf_xml)( "<status>\n"
2279 " <state>RUNNING</state>\n"
2280 " <time>%pS</time>\n"
2281 "</status>\n",
2282 buf );
2283 VG_(printf_xml)( "\n" );
2284 }
2285
bart27233e92012-03-08 14:59:25 +00002286 VG_(init_Threads)();
2287
sewardj45f4e7c2005-09-27 19:20:21 +00002288 //--------------------------------------------------------------
sewardjde764e82007-11-09 23:13:22 +00002289 // Initialise the scheduler (phase 1) [generates tid_main]
2290 // p: none, afaics
2291 //--------------------------------------------------------------
2292 VG_(debugLog)(1, "main", "Initialise scheduler (phase 1)\n");
2293 tid_main = VG_(scheduler_init_phase1)();
2294 vg_assert(tid_main >= 0 && tid_main < VG_N_THREADS
2295 && tid_main != VG_INVALID_THREADID);
2296 /* Tell the tool about tid_main */
2297 VG_TRACK( pre_thread_ll_create, VG_INVALID_THREADID, tid_main );
2298
2299 //--------------------------------------------------------------
sewardj45f4e7c2005-09-27 19:20:21 +00002300 // Tell the tool about the initial client memory permissions
2301 // p: aspacem
2302 // p: mallocfree
2303 // p: setup_client_stack
2304 // p: setup_client_dataseg
sewardj9c606bd2008-09-18 18:12:50 +00002305 //
2306 // For each segment we tell the client about, look up in
2307 // addr2dihandle as created above, to see if there's a debuginfo
2308 // handle associated with the segment, that we can hand along
2309 // to the tool, to be helpful.
sewardj45f4e7c2005-09-27 19:20:21 +00002310 //--------------------------------------------------------------
2311 VG_(debugLog)(1, "main", "Tell tool about initial permissions\n");
2312 { Addr* seg_starts;
2313 Int n_seg_starts;
sewardj45f4e7c2005-09-27 19:20:21 +00002314
floriane2800c92014-09-15 20:57:45 +00002315 vg_assert(addr2dihandle);
sewardj9c606bd2008-09-18 18:12:50 +00002316
tom7c1a19a2008-01-02 10:13:04 +00002317 /* Mark the main thread as running while we tell the tool about
2318 the client memory so that the tool can associate that memory
2319 with the main thread. */
floriane2800c92014-09-15 20:57:45 +00002320 vg_assert(VG_(running_tid) == VG_INVALID_THREADID);
tom7c1a19a2008-01-02 10:13:04 +00002321 VG_(running_tid) = tid_main;
2322
florianea8a88c2015-02-20 14:00:23 +00002323 seg_starts = VG_(get_segment_starts)( SkFileC | SkAnonC | SkShmC,
2324 &n_seg_starts );
sewardj17c11042006-10-15 01:26:40 +00002325 vg_assert(seg_starts && n_seg_starts >= 0);
sewardj45f4e7c2005-09-27 19:20:21 +00002326
florianea8a88c2015-02-20 14:00:23 +00002327 /* Show client segments to the tool */
sewardj45f4e7c2005-09-27 19:20:21 +00002328 for (i = 0; i < n_seg_starts; i++) {
sewardj9c606bd2008-09-18 18:12:50 +00002329 Word j, n;
sewardj12ab7652006-10-17 02:10:42 +00002330 NSegment const* seg
sewardj17c11042006-10-15 01:26:40 +00002331 = VG_(am_find_nsegment)( seg_starts[i] );
sewardj45f4e7c2005-09-27 19:20:21 +00002332 vg_assert(seg);
florianea8a88c2015-02-20 14:00:23 +00002333 vg_assert(seg->kind == SkFileC || seg->kind == SkAnonC ||
2334 seg->kind == SkShmC);
2335 vg_assert(seg->start == seg_starts[i]);
2336 {
sewardj45f4e7c2005-09-27 19:20:21 +00002337 VG_(debugLog)(2, "main",
2338 "tell tool about %010lx-%010lx %c%c%c\n",
2339 seg->start, seg->end,
2340 seg->hasR ? 'r' : '-',
2341 seg->hasW ? 'w' : '-',
2342 seg->hasX ? 'x' : '-' );
sewardj9c606bd2008-09-18 18:12:50 +00002343 /* search addr2dihandle to see if we have an entry
2344 matching seg->start. */
2345 n = VG_(sizeXA)( addr2dihandle );
2346 for (j = 0; j < n; j++) {
2347 Addr_n_ULong* anl = VG_(indexXA)( addr2dihandle, j );
2348 if (anl->a == seg->start) {
floriane2800c92014-09-15 20:57:45 +00002349 vg_assert(anl->ull > 0); /* check it's a valid handle */
sewardj9c606bd2008-09-18 18:12:50 +00002350 break;
2351 }
2352 }
2353 vg_assert(j >= 0 && j <= n);
sewardj45f4e7c2005-09-27 19:20:21 +00002354 VG_TRACK( new_mem_startup, seg->start, seg->end+1-seg->start,
sewardj9c606bd2008-09-18 18:12:50 +00002355 seg->hasR, seg->hasW, seg->hasX,
2356 /* and the retrieved debuginfo handle, if any */
2357 j < n
2358 ? ((Addr_n_ULong*)VG_(indexXA)( addr2dihandle, j ))->ull
2359 : 0 );
sewardj45f4e7c2005-09-27 19:20:21 +00002360 }
2361 }
2362
2363 VG_(free)( seg_starts );
sewardj9c606bd2008-09-18 18:12:50 +00002364 VG_(deleteXA)( addr2dihandle );
sewardj45f4e7c2005-09-27 19:20:21 +00002365
2366 /* Also do the initial stack permissions. */
barte05b3a42010-09-07 16:32:53 +00002367 {
2368 SSizeT inaccessible_len;
2369 NSegment const* seg
sewardjf9d2f9b2006-11-17 20:00:57 +00002370 = VG_(am_find_nsegment)( the_iifii.initial_client_SP );
sewardj17c11042006-10-15 01:26:40 +00002371 vg_assert(seg);
2372 vg_assert(seg->kind == SkAnonC);
sewardjf9d2f9b2006-11-17 20:00:57 +00002373 vg_assert(the_iifii.initial_client_SP >= seg->start);
2374 vg_assert(the_iifii.initial_client_SP <= seg->end);
sewardj45f4e7c2005-09-27 19:20:21 +00002375
sewardj17c11042006-10-15 01:26:40 +00002376 /* Stuff below the initial SP is unaddressable. Take into
2377 account any ABI-mandated space below the stack pointer that
2378 is required (VG_STACK_REDZONE_SZB). setup_client_stack()
2379 will have allocated an extra page if a red zone is required,
2380 to be on the safe side. */
barte05b3a42010-09-07 16:32:53 +00002381 inaccessible_len = the_iifii.initial_client_SP - VG_STACK_REDZONE_SZB
2382 - seg->start;
2383 vg_assert(inaccessible_len >= 0);
2384 if (inaccessible_len > 0)
2385 VG_TRACK( die_mem_stack,
2386 seg->start,
2387 inaccessible_len );
sewardj17c11042006-10-15 01:26:40 +00002388 VG_(debugLog)(2, "main", "mark stack inaccessible %010lx-%010lx\n",
2389 seg->start,
sewardjf9d2f9b2006-11-17 20:00:57 +00002390 the_iifii.initial_client_SP-1 - VG_STACK_REDZONE_SZB);
sewardj17c11042006-10-15 01:26:40 +00002391 }
sewardj45f4e7c2005-09-27 19:20:21 +00002392
2393 /* Also the assembly helpers. */
2394 VG_TRACK( new_mem_startup,
2395 (Addr)&VG_(trampoline_stuff_start),
sewardjc6527d62006-02-13 17:54:31 +00002396 (Addr)&VG_(trampoline_stuff_end)
2397 - (Addr)&VG_(trampoline_stuff_start),
sewardj45f4e7c2005-09-27 19:20:21 +00002398 False, /* readable? */
2399 False, /* writable? */
sewardj9c606bd2008-09-18 18:12:50 +00002400 True /* executable? */,
2401 0 /* di_handle: no associated debug info */ );
tom7c1a19a2008-01-02 10:13:04 +00002402
2403 /* Clear the running thread indicator */
2404 VG_(running_tid) = VG_INVALID_THREADID;
floriane2800c92014-09-15 20:57:45 +00002405 vg_assert(VG_(running_tid) == VG_INVALID_THREADID);
sewardj882a9ec2014-07-08 07:44:07 +00002406
2407 /* Darwin only: tell the tools where the client's kernel commpage
2408 is. It would be better to do this by telling aspacemgr about
2409 it -- see the now disused record_system_memory() in
2410 initimg-darwin.c -- but that causes the sync checker to fail,
2411 since the mapping doesn't appear in the kernel-supplied
2412 process map. So do it here instead. */
2413# if defined(VGP_amd64_darwin)
2414 VG_TRACK( new_mem_startup,
2415 0x7fffffe00000, 0x7ffffffff000-0x7fffffe00000,
2416 True, False, True, /* r-x */
2417 0 /* di_handle: no associated debug info */ );
2418# elif defined(VGP_x86_darwin)
2419 VG_TRACK( new_mem_startup,
2420 0xfffec000, 0xfffff000-0xfffec000,
2421 True, False, True, /* r-x */
2422 0 /* di_handle: no associated debug info */ );
2423# endif
sewardj45f4e7c2005-09-27 19:20:21 +00002424 }
2425
2426 //--------------------------------------------------------------
sewardjde764e82007-11-09 23:13:22 +00002427 // Initialise the scheduler (phase 2)
2428 // p: Initialise the scheduler (phase 1) [for tid_main]
nethercote71980f02004-01-24 18:18:54 +00002429 // p: setup_file_descriptors() [else VG_(safe_fd)() breaks]
sewardj45f4e7c2005-09-27 19:20:21 +00002430 // p: setup_client_stack
nethercote71980f02004-01-24 18:18:54 +00002431 //--------------------------------------------------------------
sewardjde764e82007-11-09 23:13:22 +00002432 VG_(debugLog)(1, "main", "Initialise scheduler (phase 2)\n");
sewardj12ab7652006-10-17 02:10:42 +00002433 { NSegment const* seg
sewardjf9d2f9b2006-11-17 20:00:57 +00002434 = VG_(am_find_nsegment)( the_iifii.initial_client_SP );
sewardj45f4e7c2005-09-27 19:20:21 +00002435 vg_assert(seg);
2436 vg_assert(seg->kind == SkAnonC);
sewardjf9d2f9b2006-11-17 20:00:57 +00002437 vg_assert(the_iifii.initial_client_SP >= seg->start);
2438 vg_assert(the_iifii.initial_client_SP <= seg->end);
sewardjde764e82007-11-09 23:13:22 +00002439 VG_(scheduler_init_phase2)( tid_main,
2440 seg->end, the_iifii.clstack_max_size );
sewardj45f4e7c2005-09-27 19:20:21 +00002441 }
nethercote71980f02004-01-24 18:18:54 +00002442
2443 //--------------------------------------------------------------
sewardj17c11042006-10-15 01:26:40 +00002444 // Set up state for the root thread
sewardjb5f6f512005-03-10 23:59:00 +00002445 // p: ?
sewardj17c11042006-10-15 01:26:40 +00002446 // setup_scheduler() [for sched-specific thread 1 stuff]
sewardjf9d2f9b2006-11-17 20:00:57 +00002447 // VG_(ii_create_image) [for 'the_iicii' initial info]
sewardj2a99cf62004-11-24 10:44:19 +00002448 //--------------------------------------------------------------
sewardjf9d2f9b2006-11-17 20:00:57 +00002449 VG_(debugLog)(1, "main", "Finalise initial image\n");
sewardj8eb8bab2015-07-21 14:44:28 +00002450 { /* Mark the main thread as running while we tell the tool about
2451 the client memory which could be tracked during initial image
2452 finalisation. So the tool can associate that memory with the
2453 main thread. */
2454 vg_assert(VG_(running_tid) == VG_INVALID_THREADID);
2455 VG_(running_tid) = tid_main;
2456
2457 VG_(ii_finalise_image)( the_iifii );
2458
2459 /* Clear the running thread indicator */
2460 VG_(running_tid) = VG_INVALID_THREADID;
2461 vg_assert(VG_(running_tid) == VG_INVALID_THREADID);
2462 }
njnea4b28c2004-11-30 16:04:58 +00002463
sewardj2a99cf62004-11-24 10:44:19 +00002464 //--------------------------------------------------------------
nethercote71980f02004-01-24 18:18:54 +00002465 // Initialise the signal handling subsystem
sewardjb5f6f512005-03-10 23:59:00 +00002466 // p: n/a
nethercote71980f02004-01-24 18:18:54 +00002467 //--------------------------------------------------------------
2468 // Nb: temporarily parks the saved blocking-mask in saved_sigmask.
sewardj1fbc1a52005-04-25 02:05:54 +00002469 VG_(debugLog)(1, "main", "Initialise signal management\n");
njncda2f0f2009-05-18 02:12:08 +00002470 /* Check that the kernel-interface signal definitions look sane */
2471 VG_(vki_do_initial_consistency_checks)();
2472 /* .. and go on to use them. */
nethercote71980f02004-01-24 18:18:54 +00002473 VG_(sigstartup_actions)();
2474
2475 //--------------------------------------------------------------
nethercote71980f02004-01-24 18:18:54 +00002476 // Read suppression file
sewardj95d86c02007-12-18 01:49:23 +00002477 // p: main_process_cmd_line_options() [for VG_(clo_suppressions)]
nethercote71980f02004-01-24 18:18:54 +00002478 //--------------------------------------------------------------
sewardj1fbc1a52005-04-25 02:05:54 +00002479 if (VG_(needs).core_errors || VG_(needs).tool_errors) {
2480 VG_(debugLog)(1, "main", "Load suppressions\n");
nethercote71980f02004-01-24 18:18:54 +00002481 VG_(load_suppressions)();
sewardj1fbc1a52005-04-25 02:05:54 +00002482 }
nethercote71980f02004-01-24 18:18:54 +00002483
2484 //--------------------------------------------------------------
rjwalsh0140af52005-06-04 20:42:33 +00002485 // register client stack
2486 //--------------------------------------------------------------
philippe38a74d22014-08-29 22:53:19 +00002487 VG_(clstk_id) = VG_(register_stack)(VG_(clstk_start_base), VG_(clstk_end));
rjwalsh0140af52005-06-04 20:42:33 +00002488
2489 //--------------------------------------------------------------
sewardj45f4e7c2005-09-27 19:20:21 +00002490 // Show the address space state so far
2491 //--------------------------------------------------------------
2492 VG_(debugLog)(1, "main", "\n");
2493 VG_(debugLog)(1, "main", "\n");
2494 VG_(am_show_nsegments)(1,"Memory layout at client startup");
2495 VG_(debugLog)(1, "main", "\n");
2496 VG_(debugLog)(1, "main", "\n");
2497
2498 //--------------------------------------------------------------
nethercote71980f02004-01-24 18:18:54 +00002499 // Run!
2500 //--------------------------------------------------------------
sewardj1fbc1a52005-04-25 02:05:54 +00002501 VG_(debugLog)(1, "main", "Running thread 1\n");
sewardj1ae3f3a2005-09-28 10:47:38 +00002502
sewardj1d887112005-05-30 21:44:08 +00002503 /* As a result of the following call, the last thread standing
sewardj1ae3f3a2005-09-28 10:47:38 +00002504 eventually winds up running shutdown_actions_NORETURN
2505 just below. Unfortunately, simply exporting said function
2506 causes m_main to be part of a module cycle, which is pretty
2507 nonsensical. So instead of doing that, the address of said
2508 function is stored in a global variable 'owned' by m_syswrap,
2509 and it uses that function pointer to get back here when it needs
2510 to. */
2511
2512 /* Set continuation address. */
2513 VG_(address_of_m_main_shutdown_actions_NORETURN)
2514 = & shutdown_actions_NORETURN;
2515
2516 /* Run the first thread, eventually ending up at the continuation
2517 address. */
njnaf839f52005-06-23 03:27:57 +00002518 VG_(main_thread_wrapper_NORETURN)(1);
nethercote71980f02004-01-24 18:18:54 +00002519
sewardj1d887112005-05-30 21:44:08 +00002520 /*NOTREACHED*/
2521 vg_assert(0);
sewardjb5f6f512005-03-10 23:59:00 +00002522}
2523
sewardj17c11042006-10-15 01:26:40 +00002524/* Do everything which needs doing when the last thread exits or when
sewardj6e9de462011-06-28 07:25:29 +00002525 a thread exits requesting a complete process exit.
sewardj17c11042006-10-15 01:26:40 +00002526
2527 We enter here holding The Lock. For the case VgSrc_ExitProcess we
2528 must never release it, because to do so would allow other threads
2529 to continue after the system is ostensibly shut down. So we must
2530 go to our grave, so to speak, holding the lock.
2531
2532 In fact, there is never any point in releasing the lock at this
2533 point - we have it, we're shutting down the entire system, and
2534 for the case VgSrc_ExitProcess doing so positively causes trouble.
2535 So don't.
2536
2537 The final_tidyup call makes a bit of a nonsense of the ExitProcess
Elliott Hughesa0664b92017-04-18 17:46:52 -07002538 case, since it will run __gnu_cxx::__freeres and libc_freeres functions,
2539 thus allowing other lurking threads to run again. Hmm. */
sewardjb5f6f512005-03-10 23:59:00 +00002540
sewardj1ae3f3a2005-09-28 10:47:38 +00002541static
2542void shutdown_actions_NORETURN( ThreadId tid,
2543 VgSchedReturnCode tids_schedretcode )
sewardjb5f6f512005-03-10 23:59:00 +00002544{
sewardj1d887112005-05-30 21:44:08 +00002545 VG_(debugLog)(1, "main", "entering VG_(shutdown_actions_NORETURN)\n");
sewardj17c11042006-10-15 01:26:40 +00002546 VG_(am_show_nsegments)(1,"Memory layout at client shutdown");
sewardj1d887112005-05-30 21:44:08 +00002547
sewardjb5f6f512005-03-10 23:59:00 +00002548 vg_assert(VG_(is_running_thread)(tid));
sewardj12ab7652006-10-17 02:10:42 +00002549 vg_assert(tids_schedretcode == VgSrc_ExitThread
2550 || tids_schedretcode == VgSrc_ExitProcess
2551 || tids_schedretcode == VgSrc_FatalSig );
sewardjb5f6f512005-03-10 23:59:00 +00002552
sewardj12ab7652006-10-17 02:10:42 +00002553 if (tids_schedretcode == VgSrc_ExitThread) {
sewardjb5f6f512005-03-10 23:59:00 +00002554
sewardj17c11042006-10-15 01:26:40 +00002555 // We are the last surviving thread. Right?
2556 vg_assert( VG_(count_living_threads)() == 1 );
sewardjb5f6f512005-03-10 23:59:00 +00002557
sewardj17c11042006-10-15 01:26:40 +00002558 // Wait for all other threads to exit.
2559 // jrs: Huh? but they surely are already gone
2560 VG_(reap_threads)(tid);
sewardjb5f6f512005-03-10 23:59:00 +00002561
Elliott Hughesa0664b92017-04-18 17:46:52 -07002562 // Clean the client up before the final report.
2563 // This causes __gnu_cxx::__freeres and libc_freeres functions to run.
sewardj17c11042006-10-15 01:26:40 +00002564 final_tidyup(tid);
2565
2566 /* be paranoid */
2567 vg_assert(VG_(is_running_thread)(tid));
2568 vg_assert(VG_(count_living_threads)() == 1);
2569
2570 } else {
2571
2572 // We may not be the last surviving thread. However, we
2573 // want to shut down the entire process. We hold the lock
2574 // and we need to keep hold of it all the way out, in order
2575 // that none of the other threads ever run again.
2576 vg_assert( VG_(count_living_threads)() >= 1 );
2577
Elliott Hughesa0664b92017-04-18 17:46:52 -07002578 // Clean the client up before the final report.
2579 // This causes __gnu_cxx::__freeres and libc_freeres functions to run.
2580 // Perhaps this is unsafe, as per comment above.
sewardj17c11042006-10-15 01:26:40 +00002581 final_tidyup(tid);
2582
2583 /* be paranoid */
2584 vg_assert(VG_(is_running_thread)(tid));
2585 vg_assert(VG_(count_living_threads)() >= 1);
2586 }
sewardjb5f6f512005-03-10 23:59:00 +00002587
philippe180a7502014-04-20 13:41:10 +00002588 /* Final call to gdbserver, if requested. */
2589 if (VG_(gdbserver_stop_at) (VgdbStopAt_Exit)) {
2590 VG_(umsg)("(action at exit) vgdb me ... \n");
2591 VG_(gdbserver) (tid);
2592 }
sewardjb5f6f512005-03-10 23:59:00 +00002593 VG_(threads)[tid].status = VgTs_Empty;
philippe180a7502014-04-20 13:41:10 +00002594
nethercote71980f02004-01-24 18:18:54 +00002595 //--------------------------------------------------------------
sewardj738856f2009-07-15 14:48:32 +00002596 // Finalisation: cleanup, messages, etc. Order not so important, only
nethercote71980f02004-01-24 18:18:54 +00002597 // affects what order the messages come.
2598 //--------------------------------------------------------------
njnb6267bd2009-08-12 00:14:16 +00002599 // First thing in the post-amble is a blank line.
sewardj738856f2009-07-15 14:48:32 +00002600 if (VG_(clo_xml))
2601 VG_(printf_xml)("\n");
njnb6267bd2009-08-12 00:14:16 +00002602 else if (VG_(clo_verbosity) > 0)
2603 VG_(message)(Vg_UserMsg, "\n");
nethercote71980f02004-01-24 18:18:54 +00002604
sewardj71bc3cb2005-05-19 00:25:45 +00002605 if (VG_(clo_xml)) {
florianf44ff622014-12-20 16:52:08 +00002606 HChar buf[50]; // large enough
florian77dec3a2014-11-14 22:34:40 +00002607 VG_(elapsed_wallclock_time)(buf, sizeof buf);
bartb3af9cf2011-10-06 19:08:37 +00002608 VG_(printf_xml)( "<status>\n"
sewardj738856f2009-07-15 14:48:32 +00002609 " <state>FINISHED</state>\n"
bartb3af9cf2011-10-06 19:08:37 +00002610 " <time>%pS</time>\n"
njnb6267bd2009-08-12 00:14:16 +00002611 "</status>\n"
2612 "\n",
sewardj738856f2009-07-15 14:48:32 +00002613 buf);
sewardj71bc3cb2005-05-19 00:25:45 +00002614 }
2615
nethercote71980f02004-01-24 18:18:54 +00002616 /* Print out file descriptor summary and stats. */
2617 if (VG_(clo_track_fds))
philippec3360382012-10-21 14:37:14 +00002618 VG_(show_open_fds)("at exit");
nethercote71980f02004-01-24 18:18:54 +00002619
sewardj2d9e8742009-08-07 15:46:56 +00002620 /* Call the tool's finalisation function. This makes Memcheck's
2621 leak checker run, and possibly chuck a bunch of leak errors into
2622 the error management machinery. */
2623 VG_TDICT_CALL(tool_fini, 0/*exitcode*/);
nethercote71980f02004-01-24 18:18:54 +00002624
sewardj2d9e8742009-08-07 15:46:56 +00002625 /* Show the error counts. */
sewardj7ca100d2009-08-15 23:05:34 +00002626 if (VG_(clo_xml)
2627 && (VG_(needs).core_errors || VG_(needs).tool_errors)) {
sewardj2d9e8742009-08-07 15:46:56 +00002628 VG_(show_error_counts_as_XML)();
sewardj738856f2009-07-15 14:48:32 +00002629 }
sewardj2d9e8742009-08-07 15:46:56 +00002630
2631 /* In XML mode, this merely prints the used suppressions. */
2632 if (VG_(needs).core_errors || VG_(needs).tool_errors)
sewardj3b290482011-05-06 21:02:55 +00002633 VG_(show_all_errors)(VG_(clo_verbosity), VG_(clo_xml));
nethercote71980f02004-01-24 18:18:54 +00002634
sewardj71bc3cb2005-05-19 00:25:45 +00002635 if (VG_(clo_xml)) {
sewardj738856f2009-07-15 14:48:32 +00002636 VG_(printf_xml)("\n");
2637 VG_(printf_xml)("</valgrindoutput>\n");
2638 VG_(printf_xml)("\n");
sewardj71bc3cb2005-05-19 00:25:45 +00002639 }
2640
nethercote885dd912004-08-03 23:14:00 +00002641 VG_(sanity_check_general)( True /*include expensive checks*/ );
nethercote71980f02004-01-24 18:18:54 +00002642
sewardj2d9e8742009-08-07 15:46:56 +00002643 if (VG_(clo_stats))
philippe5af66582015-04-19 17:56:42 +00002644 VG_(print_all_stats)(VG_(clo_verbosity) >= 1, /* Memory stats */
philippe4f6f3362014-04-19 00:25:54 +00002645 False /* tool prints stats in the tool fini */);
nethercote71980f02004-01-24 18:18:54 +00002646
sewardj9c606bd2008-09-18 18:12:50 +00002647 /* Show a profile of the heap(s) at shutdown. Optionally, first
2648 throw away all the debug info, as that makes it easy to spot
2649 leaks in the debuginfo reader. */
2650 if (VG_(clo_profile_heap)) {
2651 if (0) VG_(di_discard_ALL_debuginfo)();
2652 VG_(print_arena_cc_analysis)();
2653 }
2654
sewardj17c5e2e2012-12-28 09:12:14 +00002655 /* If profiling has been requested, but with zero interval, it
2656 means "profile at the end of the run only". In which case we
2657 need to dump the profile now. */
2658 if (VG_(clo_profyle_sbs) && VG_(clo_profyle_interval) == 0) {
2659 VG_(get_and_show_SB_profile)(0/*denoting end-of-run*/);
njn2025cf92005-06-26 20:44:48 +00002660 }
sewardjfa8ec112005-01-19 11:55:34 +00002661
sewardj8b635a42004-11-22 19:01:47 +00002662 /* Print Vex storage stats */
sewardjbf426512005-01-17 18:35:30 +00002663 if (0)
2664 LibVEX_ShowAllocStats();
sewardj1d887112005-05-30 21:44:08 +00002665
sewardj738856f2009-07-15 14:48:32 +00002666 /* Flush any output cached by previous calls to VG_(message). */
2667 VG_(message_flush)();
2668
philippe180a7502014-04-20 13:41:10 +00002669 /* Terminate gdbserver if ever it was started. We terminate it here
sewardj17c5e2e2012-12-28 09:12:14 +00002670 so that it get the output above if output was redirected to
2671 gdb */
philippe0447bbd2012-10-17 21:32:03 +00002672 VG_(gdbserver_exit) (tid, tids_schedretcode);
sewardj3b290482011-05-06 21:02:55 +00002673
njn8aa35852005-06-10 22:59:56 +00002674 /* Ok, finally exit in the os-specific way, according to the scheduler's
2675 return code. In short, if the (last) thread exited by calling
2676 sys_exit, do likewise; if the (last) thread stopped due to a fatal
2677 signal, terminate the entire system with that same fatal signal. */
2678 VG_(debugLog)(1, "core_os",
Elliott Hughesa0664b92017-04-18 17:46:52 -07002679 "VG_(terminate_NORETURN)(tid=%u) schedretcode %s"
2680 " os_state.exit_code %ld fatalsig %d\n",
2681 tid, VG_(name_of_VgSchedReturnCode)(tids_schedretcode),
2682 VG_(threads)[tid].os_state.exitcode,
2683 VG_(threads)[tid].os_state.fatalsig);
njn8aa35852005-06-10 22:59:56 +00002684
njn8aa35852005-06-10 22:59:56 +00002685 switch (tids_schedretcode) {
sewardj8eb8bab2015-07-21 14:44:28 +00002686 case VgSrc_ExitThread: /* the normal way out (Linux, Solaris) */
floriand2190292015-01-08 21:05:03 +00002687 case VgSrc_ExitProcess: /* the normal way out (Darwin) */
sewardjb9779082006-05-12 23:50:15 +00002688 /* Change the application return code to user's return code,
2689 if an error was found */
2690 if (VG_(clo_error_exitcode) > 0
2691 && VG_(get_n_errs_found)() > 0) {
philippe180a7502014-04-20 13:41:10 +00002692 VG_(client_exit)( VG_(clo_error_exitcode) );
sewardjb9779082006-05-12 23:50:15 +00002693 } else {
2694 /* otherwise, return the client's exit code, in the normal
2695 way. */
philippe180a7502014-04-20 13:41:10 +00002696 VG_(client_exit)( VG_(threads)[tid].os_state.exitcode );
sewardjb9779082006-05-12 23:50:15 +00002697 }
njn8aa35852005-06-10 22:59:56 +00002698 /* NOT ALIVE HERE! */
sewardj17c11042006-10-15 01:26:40 +00002699 VG_(core_panic)("entered the afterlife in main() -- ExitT/P");
njn8aa35852005-06-10 22:59:56 +00002700 break; /* what the hell :) */
2701
2702 case VgSrc_FatalSig:
2703 /* We were killed by a fatal signal, so replicate the effect */
2704 vg_assert(VG_(threads)[tid].os_state.fatalsig != 0);
2705 VG_(kill_self)(VG_(threads)[tid].os_state.fatalsig);
njnf76d27a2009-05-28 01:53:07 +00002706 /* we shouldn't be alive at this point. But VG_(kill_self)
2707 sometimes fails with EPERM on Darwin, for unclear reasons. */
2708# if defined(VGO_darwin)
2709 VG_(debugLog)(0, "main", "VG_(kill_self) failed. Exiting normally.\n");
2710 VG_(exit)(0); /* bogus, but we really need to exit now */
2711 /* fall through .. */
2712# endif
njn8aa35852005-06-10 22:59:56 +00002713 VG_(core_panic)("main(): signal was supposed to be fatal");
2714 break;
2715
2716 default:
2717 VG_(core_panic)("main(): unexpected scheduler return code");
2718 }
njne96be672005-05-08 19:08:54 +00002719}
sewardj8b635a42004-11-22 19:01:47 +00002720
sewardj1ae3f3a2005-09-28 10:47:38 +00002721/* -------------------- */
2722
2723/* Final clean-up before terminating the process.
Elliott Hughesa0664b92017-04-18 17:46:52 -07002724 Clean up the client by calling __gnu_cxx::__freeres() (if requested)
2725 and __libc_freeres() (if requested).
sewardj1ae3f3a2005-09-28 10:47:38 +00002726*/
2727static void final_tidyup(ThreadId tid)
2728{
Elliott Hughesa0664b92017-04-18 17:46:52 -07002729#if defined(VGO_linux) || defined(VGO_solaris)
2730 Addr freeres_wrapper = VG_(client_freeres_wrapper);
sewardj1ae3f3a2005-09-28 10:47:38 +00002731
2732 vg_assert(VG_(is_running_thread)(tid));
Elliott Hughesa0664b92017-04-18 17:46:52 -07002733
2734 if (freeres_wrapper == 0) {
2735 return; /* can't do it */
2736 }
2737
2738 Vg_FreeresToRun to_run = 0;
2739 if (VG_(needs).cxx_freeres && VG_(clo_run_cxx_freeres)) {
2740 to_run |= VG_RUN__GNU_CXX__FREERES;
2741 }
2742
2743 if (VG_(needs).libc_freeres && VG_(clo_run_libc_freeres)) {
2744 to_run |= VG_RUN__LIBC_FREERES;
2745 }
2746
2747 if (to_run == 0) {
2748 return; /* won't do it */
2749 }
sewardj1ae3f3a2005-09-28 10:47:38 +00002750
carllcae0cc22014-08-07 23:17:29 +00002751# if defined(VGP_ppc64be_linux)
Elliott Hughesa0664b92017-04-18 17:46:52 -07002752 Addr r2 = VG_(get_tocptr)(freeres_wrapper);
sewardjcf951812006-01-17 02:22:21 +00002753 if (r2 == 0) {
2754 VG_(message)(Vg_UserMsg,
Elliott Hughesa0664b92017-04-18 17:46:52 -07002755 "Caught __NR_exit, but can't run __gnu_cxx::__freeres()\n");
sewardjcf951812006-01-17 02:22:21 +00002756 VG_(message)(Vg_UserMsg,
Elliott Hughesa0664b92017-04-18 17:46:52 -07002757 " or __libc_freeres() since cannot establish TOC pointer "
2758 "for it.\n");
sewardjcf951812006-01-17 02:22:21 +00002759 return;
2760 }
2761# endif
2762
sewardj1ae3f3a2005-09-28 10:47:38 +00002763 if (VG_(clo_verbosity) > 2 ||
2764 VG_(clo_trace_syscalls) ||
Elliott Hughesa0664b92017-04-18 17:46:52 -07002765 VG_(clo_trace_sched)) {
2766
2767 vg_assert(to_run > 0);
2768 vg_assert(to_run <= (VG_RUN__GNU_CXX__FREERES | VG_RUN__LIBC_FREERES));
2769
2770 const HChar *msgs[] = {"__gnu_cxx::__freeres()", "__libc_freeres()",
2771 "__gnu_cxx::__freeres and __libc_freeres()"};
2772 VG_(message)(Vg_DebugMsg,
2773 "Caught __NR_exit; running %s wrapper\n", msgs[to_run - 1]);
2774 }
sewardj1ae3f3a2005-09-28 10:47:38 +00002775
Elliott Hughesa0664b92017-04-18 17:46:52 -07002776 /* set thread context to point to freeres_wrapper */
2777 /* ppc64be-linux note: freeres_wrapper gives us the real
sewardj1a85f4f2006-01-12 21:15:35 +00002778 function entry point, not a fn descriptor, so can use it
2779 directly. However, we need to set R2 (the toc pointer)
2780 appropriately. */
Elliott Hughesa0664b92017-04-18 17:46:52 -07002781 VG_(set_IP)(tid, freeres_wrapper);
carllcae0cc22014-08-07 23:17:29 +00002782# if defined(VGP_ppc64be_linux)
sewardjcf951812006-01-17 02:22:21 +00002783 VG_(threads)[tid].arch.vex.guest_GPR2 = r2;
carll582d5822014-08-07 23:35:54 +00002784# elif defined(VGP_ppc64le_linux)
2785 /* setting GPR2 but not really needed, GPR12 is needed */
Elliott Hughesa0664b92017-04-18 17:46:52 -07002786 VG_(threads)[tid].arch.vex.guest_GPR2 = freeres_wrapper;
2787 VG_(threads)[tid].arch.vex.guest_GPR12 = freeres_wrapper;
sewardjcf951812006-01-17 02:22:21 +00002788# endif
sewardj5db15402012-06-07 09:13:21 +00002789 /* mips-linux note: we need to set t9 */
petarj4df0bfc2013-02-27 23:17:33 +00002790# if defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
Elliott Hughesa0664b92017-04-18 17:46:52 -07002791 VG_(threads)[tid].arch.vex.guest_r25 = freeres_wrapper;
sewardj5db15402012-06-07 09:13:21 +00002792# endif
sewardj1ae3f3a2005-09-28 10:47:38 +00002793
Elliott Hughesa0664b92017-04-18 17:46:52 -07002794 /* Pass a parameter to freeres_wrapper(). */
2795# if defined(VGA_x86)
2796 Addr sp = VG_(threads)[tid].arch.vex.guest_ESP;
2797 *((UWord *) sp) = to_run;
2798 VG_TRACK(post_mem_write, Vg_CoreClientReq, tid, sp, sizeof(UWord));
2799 sp = sp - sizeof(UWord);
2800 VG_(threads)[tid].arch.vex.guest_ESP = sp;
2801 VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2802 offsetof(VexGuestX86State, guest_ESP),
2803 sizeof(VG_(threads)[tid].arch.vex.guest_ESP));
2804# elif defined(VGA_amd64)
2805 VG_(threads)[tid].arch.vex.guest_RDI = to_run;
2806 VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2807 offsetof(VexGuestAMD64State, guest_RDI),
2808 sizeof(VG_(threads)[tid].arch.vex.guest_RDI));
2809# elif defined(VGA_arm)
2810 VG_(threads)[tid].arch.vex.guest_R0 = to_run;
2811 VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2812 offsetof(VexGuestARMState, guest_R0),
2813 sizeof(VG_(threads)[tid].arch.vex.guest_R0));
2814# elif defined(VGA_arm64)
2815 VG_(threads)[tid].arch.vex.guest_X0 = to_run;
2816 VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2817 offsetof(VexGuestARM64State, guest_X0),
2818 sizeof(VG_(threads)[tid].arch.vex.guest_X0));
2819# elif defined(VGA_mips32)
2820 VG_(threads)[tid].arch.vex.guest_r4 = to_run;
2821 VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2822 offsetof(VexGuestMIPS32State, guest_r4),
2823 sizeof(VG_(threads)[tid].arch.vex.guest_r4));
2824# elif defined(VGA_mips64)
2825 VG_(threads)[tid].arch.vex.guest_r4 = to_run;
2826 VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2827 offsetof(VexGuestMIPS64State, guest_r4),
2828 sizeof(VG_(threads)[tid].arch.vex.guest_r4));
2829# elif defined(VGA_ppc32)
2830 VG_(threads)[tid].arch.vex.guest_GPR3 = to_run;
2831 VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2832 offsetof(VexGuestPPC32State, guest_GPR3),
2833 sizeof(VG_(threads)[tid].arch.vex.guest_GPR3));
2834# elif defined(VGA_ppc64be) || defined(VGA_ppc64le)
2835 VG_(threads)[tid].arch.vex.guest_GPR3 = to_run;
2836 VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2837 offsetof(VexGuestPPC64State, guest_GPR3),
2838 sizeof(VG_(threads)[tid].arch.vex.guest_GPR3));
2839# elif defined(VGA_s390x)
2840 VG_(threads)[tid].arch.vex.guest_r2 = to_run;
2841 VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2842 offsetof(VexGuestS390XState, guest_r2),
2843 sizeof(VG_(threads)[tid].arch.vex.guest_r2));
2844# elif defined(VGA_tilegx)
2845 VG_(threads)[tid].arch.vex.guest_r0 = to_run;
2846 VG_TRACK(post_reg_write, Vg_CoreClientReq, tid,
2847 offsetof(VexGuestTILEGXState, guest_r0),
2848 sizeof(VG_(threads)[tid].arch.vex.guest_r0));
2849#else
2850 I_die_here : architecture missing in m_main.c
2851#endif
2852
sewardj1ae3f3a2005-09-28 10:47:38 +00002853 /* Block all blockable signals by copying the real block state into
Elliott Hughesa0664b92017-04-18 17:46:52 -07002854 the thread's block state */
sewardj1ae3f3a2005-09-28 10:47:38 +00002855 VG_(sigprocmask)(VKI_SIG_BLOCK, NULL, &VG_(threads)[tid].sig_mask);
2856 VG_(threads)[tid].tmp_sig_mask = VG_(threads)[tid].sig_mask;
2857
Elliott Hughesa0664b92017-04-18 17:46:52 -07002858 /* and restore handlers to default. */
sewardj1ae3f3a2005-09-28 10:47:38 +00002859 VG_(set_default_handler)(VKI_SIGSEGV);
2860 VG_(set_default_handler)(VKI_SIGBUS);
2861 VG_(set_default_handler)(VKI_SIGILL);
2862 VG_(set_default_handler)(VKI_SIGFPE);
2863
2864 // We were exiting, so assert that...
2865 vg_assert(VG_(is_exiting)(tid));
Elliott Hughesa0664b92017-04-18 17:46:52 -07002866 // ...but now we're not again.
sewardj1ae3f3a2005-09-28 10:47:38 +00002867 VG_(threads)[tid].exitreason = VgSrc_None;
2868
Elliott Hughesa0664b92017-04-18 17:46:52 -07002869 // Run until client thread exits - ideally with FREERES_DONE,
2870 // but exit/exitgroup/signal will do.
sewardj1ae3f3a2005-09-28 10:47:38 +00002871 VG_(scheduler)(tid);
2872
2873 vg_assert(VG_(is_exiting)(tid));
njnf76d27a2009-05-28 01:53:07 +00002874#endif
sewardj1ae3f3a2005-09-28 10:47:38 +00002875}
2876
sewardj45f4e7c2005-09-27 19:20:21 +00002877
2878/*====================================================================*/
njn49f80e82009-05-21 01:25:43 +00002879/*=== Getting to main() alive: LINUX ===*/
sewardj45f4e7c2005-09-27 19:20:21 +00002880/*====================================================================*/
2881
sewardj17c11042006-10-15 01:26:40 +00002882#if defined(VGO_linux)
2883
sewardj45f4e7c2005-09-27 19:20:21 +00002884/* If linking of the final executables is done with glibc present,
2885 then Valgrind starts at main() above as usual, and all of the
2886 following code is irrelevant.
2887
2888 However, this is not the intended mode of use. The plan is to
2889 avoid linking against glibc, by giving gcc the flags
2890 -nodefaultlibs -lgcc -nostartfiles at startup.
2891
2892 From this derive two requirements:
2893
petarje70c45e2013-02-15 03:12:17 +00002894 1. gcc may emit calls to memcpy, memmove and memset to deal with
2895 structure assignments etc. Since we have chosen to ignore all the
sewardj45f4e7c2005-09-27 19:20:21 +00002896 "normal" supporting libraries, we have to provide our own
2897 implementations of them. No problem.
2898
2899 2. We have to provide a symbol "_start", to which the kernel
2900 hands control at startup. Hence the code below.
2901*/
2902
2903/* ---------------- Requirement 1 ---------------- */
2904
sewardj17c11042006-10-15 01:26:40 +00002905void* memcpy(void *dest, const void *src, SizeT n);
2906void* memcpy(void *dest, const void *src, SizeT n) {
sewardj45f4e7c2005-09-27 19:20:21 +00002907 return VG_(memcpy)(dest,src,n);
2908}
petarje70c45e2013-02-15 03:12:17 +00002909void* memmove(void *dest, const void *src, SizeT n);
2910void* memmove(void *dest, const void *src, SizeT n) {
2911 return VG_(memmove)(dest,src,n);
2912}
sewardj17c11042006-10-15 01:26:40 +00002913void* memset(void *s, int c, SizeT n);
2914void* memset(void *s, int c, SizeT n) {
sewardj45f4e7c2005-09-27 19:20:21 +00002915 return VG_(memset)(s,c,n);
2916}
2917
bart82616e12010-06-13 13:46:24 +00002918/* BVA: abort() for those platforms that need it (PPC and ARM). */
2919void abort(void);
2920void abort(void){
2921 VG_(printf)("Something called raise().\n");
2922 vg_assert(0);
2923}
2924
sewardj59570ff2010-01-01 11:59:33 +00002925/* EAZG: ARM's EABI will call floating point exception handlers in
2926 libgcc which boil down to an abort or raise, that's usually defined
2927 in libc. Instead, define them here. */
2928#if defined(VGP_arm_linux)
2929void raise(void);
2930void raise(void){
2931 VG_(printf)("Something called raise().\n");
2932 vg_assert(0);
2933}
2934
sewardj59570ff2010-01-01 11:59:33 +00002935void __aeabi_unwind_cpp_pr0(void);
2936void __aeabi_unwind_cpp_pr0(void){
2937 VG_(printf)("Something called __aeabi_unwind_cpp_pr0()\n");
2938 vg_assert(0);
2939}
sewardj38efe4b2010-08-22 12:23:01 +00002940
2941void __aeabi_unwind_cpp_pr1(void);
2942void __aeabi_unwind_cpp_pr1(void){
2943 VG_(printf)("Something called __aeabi_unwind_cpp_pr1()\n");
2944 vg_assert(0);
2945}
Chih-Hung Hsiehe3972342016-09-28 20:02:14 -07002946
Elliott Hughesa0664b92017-04-18 17:46:52 -07002947#if defined(__ANDROID__) && defined(__clang__)
Chih-Hung Hsiehe3972342016-09-28 20:02:14 -07002948/* Replace __aeabi_memcpy* functions with vgPlain_memcpy. */
2949void* __aeabi_memcpy(void *dest, const void *src, SizeT n);
2950void* __aeabi_memcpy(void *dest, const void *src, SizeT n)
2951{
2952 return VG_(memcpy)(dest, src, n);
2953}
2954
2955void* __aeabi_memcpy4(void *dest, const void *src, SizeT n);
2956void* __aeabi_memcpy4(void *dest, const void *src, SizeT n)
2957{
2958 return VG_(memcpy)(dest, src, n);
2959}
2960
2961void* __aeabi_memcpy8(void *dest, const void *src, SizeT n);
2962void* __aeabi_memcpy8(void *dest, const void *src, SizeT n)
2963{
2964 return VG_(memcpy)(dest, src, n);
2965}
2966
2967/* Replace __aeabi_memclr* functions with vgPlain_memset. */
2968void* __aeabi_memclr(void *dest, SizeT n);
2969void* __aeabi_memclr(void *dest, SizeT n)
2970{
2971 return VG_(memset)(dest, 0, n);
2972}
2973
2974void* __aeabi_memclr4(void *dest, SizeT n);
2975void* __aeabi_memclr4(void *dest, SizeT n)
2976{
2977 return VG_(memset)(dest, 0, n);
2978}
2979
2980void* __aeabi_memclr8(void *dest, SizeT n);
2981void* __aeabi_memclr8(void *dest, SizeT n)
2982{
2983 return VG_(memset)(dest, 0, n);
2984}
Elliott Hughesa0664b92017-04-18 17:46:52 -07002985#endif /* __ANDROID__ __clang__ */
2986
sewardj59570ff2010-01-01 11:59:33 +00002987#endif
2988
sewardj45f4e7c2005-09-27 19:20:21 +00002989/* ---------------- Requirement 2 ---------------- */
2990
2991/* Glibc's sysdeps/i386/elf/start.S has the following gem of a
2992 comment, which explains how the stack looks right at process start
2993 (when _start is jumped to). Hence _start passes %esp to
sewardj17c11042006-10-15 01:26:40 +00002994 _start_in_C_linux, which extracts argc/argv/envp and starts up
sewardj45f4e7c2005-09-27 19:20:21 +00002995 correctly. */
2996
2997/* This is the canonical entry point, usually the first thing in the text
2998 segment. The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry
2999 point runs, most registers' values are unspecified, except for:
3000
3001 %edx Contains a function pointer to be registered with `atexit'.
3002 This is how the dynamic linker arranges to have DT_FINI
3003 functions called for shared libraries that have been loaded
3004 before this code runs.
3005
3006 %esp The stack contains the arguments and environment:
3007 0(%esp) argc
3008 4(%esp) argv[0]
3009 ...
3010 (4*argc)(%esp) NULL
3011 (4*(argc+1))(%esp) envp[0]
3012 ...
3013 NULL
3014*/
3015
3016/* The kernel hands control to _start, which extracts the initial
sewardj17c11042006-10-15 01:26:40 +00003017 stack pointer and calls onwards to _start_in_C_linux. This also switches
sewardja48a4932005-09-29 11:09:56 +00003018 the new stack. */
sewardj45f4e7c2005-09-27 19:20:21 +00003019#if defined(VGP_x86_linux)
3020asm("\n"
sewardjd9fc3822005-11-18 23:50:43 +00003021 ".text\n"
sewardj45f4e7c2005-09-27 19:20:21 +00003022 "\t.globl _start\n"
3023 "\t.type _start,@function\n"
3024 "_start:\n"
3025 /* set up the new stack in %eax */
sewardjfdf91b42005-09-28 00:53:09 +00003026 "\tmovl $vgPlain_interim_stack, %eax\n"
sewardj45f4e7c2005-09-27 19:20:21 +00003027 "\taddl $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n"
philipped0720e42015-03-12 20:43:46 +00003028 "\taddl $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %eax\n"
Chih-Hung Hsieh606dc242016-08-24 19:59:10 -07003029 /* allocate at least 16 bytes on the new stack, and aligned */
sewardj45f4e7c2005-09-27 19:20:21 +00003030 "\tsubl $16, %eax\n"
3031 "\tandl $~15, %eax\n"
3032 /* install it, and collect the original one */
3033 "\txchgl %eax, %esp\n"
sewardj17c11042006-10-15 01:26:40 +00003034 /* call _start_in_C_linux, passing it the startup %esp */
Elliott Hughesa0664b92017-04-18 17:46:52 -07003035 "\tmovl %eax, (%esp)\n"
sewardj17c11042006-10-15 01:26:40 +00003036 "\tcall _start_in_C_linux\n"
sewardj45f4e7c2005-09-27 19:20:21 +00003037 "\thlt\n"
sewardj2fedc642005-11-19 02:02:57 +00003038 ".previous\n"
sewardj45f4e7c2005-09-27 19:20:21 +00003039);
3040#elif defined(VGP_amd64_linux)
3041asm("\n"
sewardjd9fc3822005-11-18 23:50:43 +00003042 ".text\n"
sewardj45f4e7c2005-09-27 19:20:21 +00003043 "\t.globl _start\n"
3044 "\t.type _start,@function\n"
3045 "_start:\n"
3046 /* set up the new stack in %rdi */
sewardjfdf91b42005-09-28 00:53:09 +00003047 "\tmovq $vgPlain_interim_stack, %rdi\n"
sewardj45f4e7c2005-09-27 19:20:21 +00003048 "\taddq $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rdi\n"
philipped0720e42015-03-12 20:43:46 +00003049 "\taddq $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %rdi\n"
sewardj45f4e7c2005-09-27 19:20:21 +00003050 "\tandq $~15, %rdi\n"
3051 /* install it, and collect the original one */
3052 "\txchgq %rdi, %rsp\n"
sewardj17c11042006-10-15 01:26:40 +00003053 /* call _start_in_C_linux, passing it the startup %rsp */
3054 "\tcall _start_in_C_linux\n"
sewardj45f4e7c2005-09-27 19:20:21 +00003055 "\thlt\n"
sewardj2fedc642005-11-19 02:02:57 +00003056 ".previous\n"
sewardj45f4e7c2005-09-27 19:20:21 +00003057);
sewardja48a4932005-09-29 11:09:56 +00003058#elif defined(VGP_ppc32_linux)
3059asm("\n"
sewardjd9fc3822005-11-18 23:50:43 +00003060 ".text\n"
sewardja48a4932005-09-29 11:09:56 +00003061 "\t.globl _start\n"
3062 "\t.type _start,@function\n"
3063 "_start:\n"
3064 /* set up the new stack in r16 */
3065 "\tlis 16,vgPlain_interim_stack@ha\n"
3066 "\tla 16,vgPlain_interim_stack@l(16)\n"
3067 "\tlis 17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" >> 16)\n"
3068 "\tori 17,17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" & 0xFFFF)\n"
philipped0720e42015-03-12 20:43:46 +00003069 "\tlis 18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" >> 16)\n"
3070 "\tori 18,18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" & 0xFFFF)\n"
sewardja48a4932005-09-29 11:09:56 +00003071 "\tadd 16,17,16\n"
3072 "\tadd 16,18,16\n"
3073 "\trlwinm 16,16,0,0,27\n"
3074 /* now r16 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
philipped0720e42015-03-12 20:43:46 +00003075 VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
sewardja48a4932005-09-29 11:09:56 +00003076 boundary. And r1 is the original SP. Set the SP to r16 and
sewardj17c11042006-10-15 01:26:40 +00003077 call _start_in_C_linux, passing it the initial SP. */
sewardja48a4932005-09-29 11:09:56 +00003078 "\tmr 3,1\n"
3079 "\tmr 1,16\n"
sewardj17c11042006-10-15 01:26:40 +00003080 "\tbl _start_in_C_linux\n"
sewardja48a4932005-09-29 11:09:56 +00003081 "\ttrap\n"
sewardj2fedc642005-11-19 02:02:57 +00003082 ".previous\n"
sewardja48a4932005-09-29 11:09:56 +00003083);
carllcae0cc22014-08-07 23:17:29 +00003084#elif defined(VGP_ppc64be_linux)
sewardj2c48c7b2005-11-29 13:05:56 +00003085asm("\n"
cerion21082042005-12-06 19:07:08 +00003086 /* PPC64 ELF ABI says '_start' points to a function descriptor.
3087 So we must have one, and that is what goes into the .opd section. */
cerion297c88f2005-12-22 15:53:12 +00003088 "\t.align 2\n"
cerion21082042005-12-06 19:07:08 +00003089 "\t.global _start\n"
3090 "\t.section \".opd\",\"aw\"\n"
3091 "\t.align 3\n"
sewardj2c48c7b2005-11-29 13:05:56 +00003092 "_start:\n"
cerion21082042005-12-06 19:07:08 +00003093 "\t.quad ._start,.TOC.@tocbase,0\n"
3094 "\t.previous\n"
3095 "\t.type ._start,@function\n"
3096 "\t.global ._start\n"
3097 "._start:\n"
sewardj2c48c7b2005-11-29 13:05:56 +00003098 /* set up the new stack in r16 */
3099 "\tlis 16, vgPlain_interim_stack@highest\n"
3100 "\tori 16,16,vgPlain_interim_stack@higher\n"
3101 "\tsldi 16,16,32\n"
3102 "\toris 16,16,vgPlain_interim_stack@h\n"
3103 "\tori 16,16,vgPlain_interim_stack@l\n"
3104 "\txor 17,17,17\n"
3105 "\tlis 17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" >> 16)\n"
3106 "\tori 17,17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" & 0xFFFF)\n"
3107 "\txor 18,18,18\n"
philipped0720e42015-03-12 20:43:46 +00003108 "\tlis 18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" >> 16)\n"
3109 "\tori 18,18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" & 0xFFFF)\n"
sewardj2c48c7b2005-11-29 13:05:56 +00003110 "\tadd 16,17,16\n"
3111 "\tadd 16,18,16\n"
3112 "\trldicr 16,16,0,59\n"
3113 /* now r16 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
philipped0720e42015-03-12 20:43:46 +00003114 VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
sewardj2c48c7b2005-11-29 13:05:56 +00003115 boundary. And r1 is the original SP. Set the SP to r16 and
sewardj17c11042006-10-15 01:26:40 +00003116 call _start_in_C_linux, passing it the initial SP. */
sewardj2c48c7b2005-11-29 13:05:56 +00003117 "\tmr 3,1\n"
3118 "\tmr 1,16\n"
sewardj5e21d442012-07-21 10:08:29 +00003119 "\tlis 14, _start_in_C_linux@highest\n"
3120 "\tori 14,14,_start_in_C_linux@higher\n"
3121 "\tsldi 14,14,32\n"
3122 "\toris 14,14,_start_in_C_linux@h\n"
3123 "\tori 14,14,_start_in_C_linux@l\n"
3124 "\tld 14,0(14)\n"
3125 "\tmtctr 14\n"
3126 "\tbctrl\n"
cerion21082042005-12-06 19:07:08 +00003127 "\tnop\n"
sewardj2c48c7b2005-11-29 13:05:56 +00003128 "\ttrap\n"
sewardj2c48c7b2005-11-29 13:05:56 +00003129);
carll582d5822014-08-07 23:35:54 +00003130#elif defined(VGP_ppc64le_linux)
3131/* Little Endian uses ELF version 2 but in the future may also
3132 * support other ELF versions.
3133 */
3134asm("\n"
3135 "\t.align 2\n"
3136 "\t.global _start\n"
3137 "\t.type _start,@function\n"
3138 "_start:\n"
3139 "#if _CALL_ELF == 2 \n"
3140 "0: addis 2,12,.TOC.-0b@ha\n"
3141 " addi 2,2,.TOC.-0b@l\n"
3142 " .localentry _start, .-_start\n"
3143 "#endif \n"
3144 /* set up the new stack in r16 */
3145 "\tlis 16, vgPlain_interim_stack@highest\n"
3146 "\tori 16,16,vgPlain_interim_stack@higher\n"
3147 "\tsldi 16,16,32\n"
3148 "\toris 16,16,vgPlain_interim_stack@h\n"
3149 "\tori 16,16,vgPlain_interim_stack@l\n"
3150 "\txor 17,17,17\n"
3151 "\tlis 17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" >> 16)\n"
3152 "\tori 17,17,("VG_STRINGIFY(VG_STACK_GUARD_SZB)" & 0xFFFF)\n"
3153 "\txor 18,18,18\n"
philipped0720e42015-03-12 20:43:46 +00003154 "\tlis 18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" >> 16)\n"
3155 "\tori 18,18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" & 0xFFFF)\n"
carll582d5822014-08-07 23:35:54 +00003156 "\tadd 16,17,16\n"
3157 "\tadd 16,18,16\n"
3158 "\trldicr 16,16,0,59\n"
3159 /* now r16 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
philipped0720e42015-03-12 20:43:46 +00003160 VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
carll582d5822014-08-07 23:35:54 +00003161 boundary. And r1 is the original SP. Set the SP to r16 and
3162 call _start_in_C_linux, passing it the initial SP. */
3163 "\tmr 3,1\n"
3164 "\tmr 1,16\n"
3165 "\tlis 14, _start_in_C_linux@highest\n"
3166 "\tori 14,14,_start_in_C_linux@higher\n"
3167 "\tsldi 14,14,32\n"
3168 "\toris 14,14,_start_in_C_linux@h\n"
3169 "\tori 14,14,_start_in_C_linux@l\n"
3170 "\tmtctr 14\n"
3171 "\tbctrl\n"
3172 "\tnop\n"
3173 "\ttrap\n"
3174);
sewardjb5b87402011-03-07 16:05:35 +00003175#elif defined(VGP_s390x_linux)
3176/*
3177 This is the canonical entry point, usually the first thing in the text
3178 segment. Most registers' values are unspecified, except for:
3179
3180 %r14 Contains a function pointer to be registered with `atexit'.
3181 This is how the dynamic linker arranges to have DT_FINI
3182 functions called for shared libraries that have been loaded
3183 before this code runs.
3184
3185 %r15 The stack contains the arguments and environment:
3186 0(%r15) argc
3187 8(%r15) argv[0]
3188 ...
3189 (8*argc)(%r15) NULL
3190 (8*(argc+1))(%r15) envp[0]
3191 ...
3192 NULL
3193*/
3194asm("\n\t"
3195 ".text\n\t"
3196 ".globl _start\n\t"
3197 ".type _start,@function\n\t"
3198 "_start:\n\t"
3199 /* set up the new stack in %r1 */
3200 "larl %r1, vgPlain_interim_stack\n\t"
3201 "larl %r5, 1f\n\t"
3202 "ag %r1, 0(%r5)\n\t"
3203 "ag %r1, 2f-1f(%r5)\n\t"
3204 "nill %r1, 0xFFF0\n\t"
3205 /* install it, and collect the original one */
3206 "lgr %r2, %r15\n\t"
3207 "lgr %r15, %r1\n\t"
3208 /* call _start_in_C_linux, passing it the startup %r15 */
3209 "brasl %r14, _start_in_C_linux\n\t"
3210 /* trigger execution of an invalid opcode -> halt machine */
3211 "j .+2\n\t"
3212 "1: .quad "VG_STRINGIFY(VG_STACK_GUARD_SZB)"\n\t"
philipped0720e42015-03-12 20:43:46 +00003213 "2: .quad "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n\t"
sewardjb5b87402011-03-07 16:05:35 +00003214 ".previous\n"
3215);
sewardj59570ff2010-01-01 11:59:33 +00003216#elif defined(VGP_arm_linux)
3217asm("\n"
sewardjb51c9262011-05-03 14:24:11 +00003218 "\t.text\n"
3219 "\t.align 4\n"
3220 "\t.type _start,#function\n"
sewardj59570ff2010-01-01 11:59:33 +00003221 "\t.global _start\n"
3222 "_start:\n"
3223 "\tldr r0, [pc, #36]\n"
3224 "\tldr r1, [pc, #36]\n"
3225 "\tadd r0, r1, r0\n"
3226 "\tldr r1, [pc, #32]\n"
3227 "\tadd r0, r1, r0\n"
3228 "\tmvn r1, #15\n"
3229 "\tand r0, r0, r1\n"
3230 "\tmov r1, sp\n"
3231 "\tmov sp, r0\n"
3232 "\tmov r0, r1\n"
3233 "\tb _start_in_C_linux\n"
3234 "\t.word vgPlain_interim_stack\n"
3235 "\t.word "VG_STRINGIFY(VG_STACK_GUARD_SZB)"\n"
philipped0720e42015-03-12 20:43:46 +00003236 "\t.word "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n"
sewardj59570ff2010-01-01 11:59:33 +00003237);
sewardjf0c12502014-01-12 12:54:00 +00003238#elif defined(VGP_arm64_linux)
3239asm("\n"
3240 "\t.text\n"
3241 "\t.align 2\n"
3242 "\t.type _start,#function\n"
3243 "\t.global _start\n"
3244 "_start:\n"
3245 "\tadrp x0, vgPlain_interim_stack\n"
3246 "\tadd x0, x0, :lo12:vgPlain_interim_stack\n"
3247 // The next 2 assume that VG_STACK_GUARD_SZB fits in 32 bits
3248 "\tmov x1, (("VG_STRINGIFY(VG_STACK_GUARD_SZB)") >> 0) & 0xFFFF\n"
3249 "\tmovk x1, (("VG_STRINGIFY(VG_STACK_GUARD_SZB)") >> 16) & 0xFFFF,"
3250 " lsl 16\n"
3251 "\tadd x0, x0, x1\n"
philipped0720e42015-03-12 20:43:46 +00003252 // The next 2 assume that VG_DEFAULT_STACK_ACTIVE_SZB fits in 32 bits
3253 "\tmov x1, (("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)") >> 0) & 0xFFFF\n"
3254 "\tmovk x1, (("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)") >> 16) & 0xFFFF,"
sewardjf0c12502014-01-12 12:54:00 +00003255 " lsl 16\n"
3256 "\tadd x0, x0, x1\n"
3257 "\tand x0, x0, -16\n"
3258 "\tmov x1, sp\n"
3259 "\tmov sp, x0\n"
3260 "\tmov x0, x1\n"
3261 "\tb _start_in_C_linux\n"
3262);
sewardj5db15402012-06-07 09:13:21 +00003263#elif defined(VGP_mips32_linux)
3264asm("\n"
3265 "\t.type _gp_disp,@object\n"
3266 ".text\n"
3267 "\t.globl __start\n"
3268 "\t.type __start,@function\n"
3269 "__start:\n"
3270
3271 "\tbal 1f\n"
3272 "\tnop\n"
3273
3274 "1:\n"
3275
3276 "\tlui $28, %hi(_gp_disp)\n"
3277 "\taddiu $28, $28, %lo(_gp_disp)\n"
3278 "\taddu $28, $28, $31\n"
3279 /* t1/$9 <- Addr(interim_stack) */
3280 "\tlui $9, %hi(vgPlain_interim_stack)\n"
3281 /* t1/$9 <- Addr(interim_stack) */
3282 "\taddiu $9, %lo(vgPlain_interim_stack)\n"
3283
3284
3285 "\tli $10, "VG_STRINGIFY(VG_STACK_GUARD_SZB)"\n"
philipped0720e42015-03-12 20:43:46 +00003286 "\tli $11, "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n"
sewardj5db15402012-06-07 09:13:21 +00003287
3288 "\taddu $9, $9, $10\n"
3289 "\taddu $9, $9, $11\n"
3290 "\tli $12, 0xFFFFFFF0\n"
3291 "\tand $9, $9, $12\n"
3292 /* now t1/$9 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
philipped0720e42015-03-12 20:43:46 +00003293 VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
sewardj5db15402012-06-07 09:13:21 +00003294 boundary. And $29 is the original SP. Set the SP to t1 and
3295 call _start_in_C, passing it the initial SP. */
3296
3297 "\tmove $4, $29\n" // a0 <- $sp (_start_in_C first arg)
3298 "\tmove $29, $9\n" // $sp <- t1 (new sp)
3299
3300 "\tlui $25, %hi(_start_in_C_linux)\n"
3301 "\taddiu $25, %lo(_start_in_C_linux)\n"
3302
3303 "\tbal _start_in_C_linux\n"
3304 "\tbreak 0x7\n"
3305 ".previous\n"
3306);
petarj4df0bfc2013-02-27 23:17:33 +00003307#elif defined(VGP_mips64_linux)
3308asm(
3309".text\n"
3310".globl __start\n"
3311".type __start,@function\n"
3312"__start:\n"
3313 "\t.set noreorder\n"
3314 "\t.cpload $25\n"
3315 "\t.set reorder\n"
3316 "\t.cprestore 16\n"
3317 "\tlui $9, %hi(vgPlain_interim_stack)\n"
3318 /* t1/$9 <- Addr(interim_stack) */
3319 "\tdaddiu $9, %lo(vgPlain_interim_stack)\n"
3320
3321 "\tli $10, "VG_STRINGIFY(VG_STACK_GUARD_SZB)"\n"
philipped0720e42015-03-12 20:43:46 +00003322 "\tli $11, "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n"
petarj4df0bfc2013-02-27 23:17:33 +00003323
3324 "\tdaddu $9, $9, $10\n"
3325 "\tdaddu $9, $9, $11\n"
3326 "\tli $12, 0xFFFFFF00\n"
3327 "\tand $9, $9, $12\n"
3328 /* now t1/$9 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
philipped0720e42015-03-12 20:43:46 +00003329 VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
petarj4df0bfc2013-02-27 23:17:33 +00003330 boundary. And $29 is the original SP. Set the SP to t1 and
3331 call _start_in_C, passing it the initial SP. */
3332
3333 "\tmove $4, $29\n" // a0 <- $sp (_start_in_C first arg)
3334 "\tmove $29, $9\n" // $sp <- t1 (new sp)
3335
3336 "\tlui $9, %highest(_start_in_C_linux)\n"
3337 "\tori $9, %higher(_start_in_C_linux)\n"
3338 "\tdsll32 $9, $9, 0x0\n"
3339 "\tlui $10, %hi(_start_in_C_linux)\n"
3340 "\tdaddiu $10, %lo(_start_in_C_linux)\n"
3341 "\tdaddu $25, $9, $10\n"
3342 "\tjalr $25\n"
3343 "\tnop\n"
dejanj124b9f22013-10-16 14:15:38 +00003344".previous\n"
petarj4df0bfc2013-02-27 23:17:33 +00003345);
sewardj112711a2015-04-10 12:30:09 +00003346#elif defined(VGP_tilegx_linux)
3347asm("\n"
3348 ".text\n"
3349 "\t.align 8\n"
3350 "\t.globl _start\n"
3351 "\t.type _start,@function\n"
3352 "_start:\n"
3353
3354 "\tjal 1f\n"
3355 "1:\n"
3356
3357 /* --FIXME, bundle them :) */
3358 /* r19 <- Addr(interim_stack) */
3359 "\tmoveli r19, hw2_last(vgPlain_interim_stack)\n"
3360 "\tshl16insli r19, r19, hw1(vgPlain_interim_stack)\n"
3361 "\tshl16insli r19, r19, hw0(vgPlain_interim_stack)\n"
3362
3363 "\tmoveli r20, hw1("VG_STRINGIFY(VG_STACK_GUARD_SZB)")\n"
3364 "\tshl16insli r20, r20, hw0("VG_STRINGIFY(VG_STACK_GUARD_SZB)")\n"
3365 "\tmoveli r21, hw1("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)")\n"
3366 "\tshl16insli r21, r21, hw0("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)")\n"
3367 "\tadd r19, r19, r20\n"
3368 "\tadd r19, r19, r21\n"
3369
3370 "\tmovei r12, 0x0F\n"
3371 "\tnor r12, zero, r12\n"
3372
3373 "\tand r19, r19, r12\n"
3374
3375 /* now r19 = &vgPlain_interim_stack + VG_STACK_GUARD_SZB +
3376 VG_STACK_ACTIVE_SZB rounded down to the nearest 16-byte
3377 boundary. And $54 is the original SP. Set the SP to r0 and
3378 call _start_in_C, passing it the initial SP. */
3379
3380 "\tmove r0, r54\n" // r0 <- $sp (_start_in_C first arg)
3381 "\tmove r54, r19\n" // $sp <- r19 (new sp)
3382
3383 "\tjal _start_in_C_linux\n"
3384);
sewardj45f4e7c2005-09-27 19:20:21 +00003385#else
njn49f80e82009-05-21 01:25:43 +00003386# error "Unknown linux platform"
sewardj45f4e7c2005-09-27 19:20:21 +00003387#endif
3388
sewardje66f2e02006-12-30 17:45:08 +00003389/* --- !!! --- EXTERNAL HEADERS start --- !!! --- */
3390#define _GNU_SOURCE
3391#define _FILE_OFFSET_BITS 64
3392/* This is in order to get AT_NULL and AT_PAGESIZE. */
3393#include <elf.h>
3394/* --- !!! --- EXTERNAL HEADERS end --- !!! --- */
3395
sewardj45f4e7c2005-09-27 19:20:21 +00003396/* Avoid compiler warnings: this fn _is_ used, but labelling it
philippe9fdca562012-04-16 22:06:47 +00003397 'static' causes gcc to complain it isn't.
3398 attribute 'used' also ensures the code is not eliminated at link
3399 time */
3400__attribute__ ((used))
sewardj17c11042006-10-15 01:26:40 +00003401void _start_in_C_linux ( UWord* pArgc );
philippe9fdca562012-04-16 22:06:47 +00003402__attribute__ ((used))
sewardj17c11042006-10-15 01:26:40 +00003403void _start_in_C_linux ( UWord* pArgc )
sewardj45f4e7c2005-09-27 19:20:21 +00003404{
3405 Int r;
3406 Word argc = pArgc[0];
3407 HChar** argv = (HChar**)&pArgc[1];
3408 HChar** envp = (HChar**)&pArgc[1+argc+1];
sewardjf9d2f9b2006-11-17 20:00:57 +00003409
philippe854be4c2013-10-22 21:20:14 +00003410 // For an inner Valgrind, register the interim stack asap.
3411 // This is needed to allow the outer valgrind to do stacktraces during init.
3412 // Note that this stack is not unregistered when the main thread
3413 // is switching to the (real) stack. Unregistering this would imply
3414 // to save the stack id in a global variable, and have a "if"
3415 // in run_a_thread_NORETURN to do the unregistration only for the
3416 // main thread. This unregistration is not worth this complexity.
3417 INNER_REQUEST
3418 ((void) VALGRIND_STACK_REGISTER
3419 (&VG_(interim_stack).bytes[0],
3420 &VG_(interim_stack).bytes[0] + sizeof(VG_(interim_stack))));
3421
sewardjf9d2f9b2006-11-17 20:00:57 +00003422 VG_(memset)( &the_iicii, 0, sizeof(the_iicii) );
3423 VG_(memset)( &the_iifii, 0, sizeof(the_iifii) );
3424
3425 the_iicii.sp_at_startup = (Addr)pArgc;
3426
carllcae0cc22014-08-07 23:17:29 +00003427# if defined(VGP_ppc32_linux) || defined(VGP_ppc64be_linux) \
Elliott Hughesa0664b92017-04-18 17:46:52 -07003428 || defined(VGP_ppc64le_linux) || defined(VGP_arm64_linux) \
3429 || defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
sewardje66f2e02006-12-30 17:45:08 +00003430 {
Elliott Hughesa0664b92017-04-18 17:46:52 -07003431 /* ppc32/ppc64, arm64, mips32/64 can be configured with different
3432 page sizes. Determine this early. This is an ugly hack and really
3433 should be moved into valgrind_main. */
sewardje66f2e02006-12-30 17:45:08 +00003434 UWord *sp = &pArgc[1+argc+1];
3435 while (*sp++ != 0)
3436 ;
3437 for (; *sp != AT_NULL && *sp != AT_PAGESZ; sp += 2);
3438 if (*sp == AT_PAGESZ) {
3439 VKI_PAGE_SIZE = sp[1];
3440 for (VKI_PAGE_SHIFT = 12;
3441 VKI_PAGE_SHIFT <= VKI_MAX_PAGE_SHIFT; VKI_PAGE_SHIFT++)
3442 if (VKI_PAGE_SIZE == (1UL << VKI_PAGE_SHIFT))
3443 break;
3444 }
3445 }
3446# endif
3447
sewardjf9d2f9b2006-11-17 20:00:57 +00003448 r = valgrind_main( (Int)argc, argv, envp );
sewardj17c11042006-10-15 01:26:40 +00003449 /* NOTREACHED */
sewardj45f4e7c2005-09-27 19:20:21 +00003450 VG_(exit)(r);
3451}
3452
sewardj17c11042006-10-15 01:26:40 +00003453
3454/*====================================================================*/
njnf76d27a2009-05-28 01:53:07 +00003455/*=== Getting to main() alive: darwin ===*/
3456/*====================================================================*/
3457
3458#elif defined(VGO_darwin)
3459
njnea2d6fd2010-07-01 00:20:20 +00003460/*
3461 Memory layout established by kernel:
3462
3463 0(%esp) argc
3464 4(%esp) argv[0]
3465 ...
3466 argv[argc-1]
3467 NULL
3468 envp[0]
3469 ...
3470 envp[n]
3471 NULL
3472 executable name (presumably, a pointer to it)
3473 NULL
3474
3475 Ditto in the 64-bit case, except all offsets from SP are obviously
3476 twice as large.
3477*/
3478
3479/* The kernel hands control to _start, which extracts the initial
3480 stack pointer and calls onwards to _start_in_C_darwin. This also
3481 switches to the new stack. */
3482#if defined(VGP_x86_darwin)
3483asm("\n"
3484 ".text\n"
3485 ".align 2,0x90\n"
3486 "\t.globl __start\n"
3487 "__start:\n"
3488 /* set up the new stack in %eax */
3489 "\tmovl $_vgPlain_interim_stack, %eax\n"
3490 "\taddl $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n"
philipped0720e42015-03-12 20:43:46 +00003491 "\taddl $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %eax\n"
njnea2d6fd2010-07-01 00:20:20 +00003492 "\tsubl $16, %eax\n"
3493 "\tandl $~15, %eax\n"
3494 /* install it, and collect the original one */
3495 "\txchgl %eax, %esp\n"
sewardj69197362012-03-07 16:38:12 +00003496 "\tsubl $12, %esp\n" // keep stack 16 aligned; see #295428
njnea2d6fd2010-07-01 00:20:20 +00003497 /* call _start_in_C_darwin, passing it the startup %esp */
3498 "\tpushl %eax\n"
3499 "\tcall __start_in_C_darwin\n"
3500 "\tint $3\n"
3501 "\tint $3\n"
3502);
3503#elif defined(VGP_amd64_darwin)
3504asm("\n"
3505 ".text\n"
3506 "\t.globl __start\n"
3507 ".align 3,0x90\n"
3508 "__start:\n"
3509 /* set up the new stack in %rdi */
3510 "\tmovabsq $_vgPlain_interim_stack, %rdi\n"
3511 "\taddq $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rdi\n"
philipped0720e42015-03-12 20:43:46 +00003512 "\taddq $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %rdi\n"
njnea2d6fd2010-07-01 00:20:20 +00003513 "\tandq $~15, %rdi\n"
3514 /* install it, and collect the original one */
3515 "\txchgq %rdi, %rsp\n"
3516 /* call _start_in_C_darwin, passing it the startup %rsp */
3517 "\tcall __start_in_C_darwin\n"
3518 "\tint $3\n"
3519 "\tint $3\n"
3520);
3521#endif
3522
njnf76d27a2009-05-28 01:53:07 +00003523void* __memcpy_chk(void *dest, const void *src, SizeT n, SizeT n2);
3524void* __memcpy_chk(void *dest, const void *src, SizeT n, SizeT n2) {
3525 // skip check
3526 return VG_(memcpy)(dest,src,n);
3527}
3528void* __memset_chk(void *s, int c, SizeT n, SizeT n2);
3529void* __memset_chk(void *s, int c, SizeT n, SizeT n2) {
3530 // skip check
3531 return VG_(memset)(s,c,n);
3532}
3533void bzero(void *s, SizeT n);
3534void bzero(void *s, SizeT n) {
3535 VG_(memset)(s,0,n);
3536}
3537
3538void* memcpy(void *dest, const void *src, SizeT n);
3539void* memcpy(void *dest, const void *src, SizeT n) {
3540 return VG_(memcpy)(dest,src,n);
3541}
3542void* memset(void *s, int c, SizeT n);
3543void* memset(void *s, int c, SizeT n) {
3544 return VG_(memset)(s,c,n);
3545}
3546
njnf76d27a2009-05-28 01:53:07 +00003547/* Avoid compiler warnings: this fn _is_ used, but labelling it
3548 'static' causes gcc to complain it isn't. */
3549void _start_in_C_darwin ( UWord* pArgc );
3550void _start_in_C_darwin ( UWord* pArgc )
3551{
3552 Int r;
njnea2d6fd2010-07-01 00:20:20 +00003553 Int argc = *(Int *)pArgc; // not pArgc[0] on LP64
njnf76d27a2009-05-28 01:53:07 +00003554 HChar** argv = (HChar**)&pArgc[1];
3555 HChar** envp = (HChar**)&pArgc[1+argc+1];
3556
philippe854be4c2013-10-22 21:20:14 +00003557 // See _start_in_C_linux
3558 INNER_REQUEST
3559 ((void) VALGRIND_STACK_REGISTER
3560 (&VG_(interim_stack).bytes[0],
3561 &VG_(interim_stack).bytes[0] + sizeof(VG_(interim_stack))));
3562
njnf76d27a2009-05-28 01:53:07 +00003563 VG_(memset)( &the_iicii, 0, sizeof(the_iicii) );
3564 VG_(memset)( &the_iifii, 0, sizeof(the_iifii) );
3565
3566 the_iicii.sp_at_startup = (Addr)pArgc;
3567
3568 r = valgrind_main( (Int)argc, argv, envp );
3569 /* NOTREACHED */
3570 VG_(exit)(r);
3571}
3572
sewardj8eb8bab2015-07-21 14:44:28 +00003573/*====================================================================*/
3574/*=== Getting to main() alive: Solaris ===*/
3575/*====================================================================*/
3576#elif defined(VGO_solaris)
3577#if defined(VGP_x86_solaris)
3578/* The kernel hands control to _start, which extracts the initial stack
3579 pointer and calls onwards to _start_in_C_solaris. This also switches to
3580 the new stack. */
3581asm("\n"
3582 "\t.text\n"
3583 "\t.globl _start\n"
3584 "\t.type _start, @function\n"
3585 "_start:\n"
3586 /* Set up the new stack in %eax. */
3587 "\tmovl $vgPlain_interim_stack, %eax\n"
3588 "\taddl $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n"
3589 "\taddl $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %eax\n"
3590 "\tandl $~15, %eax\n"
3591 /* Install it, and collect the original one. */
3592 "\txchgl %eax, %esp\n"
3593 "\tsubl $12, %esp\n" /* Keep stack 16-byte aligned. */
3594 /* Call _start_in_C_solaris, passing it the startup %esp. */
3595 "\tpushl %eax\n"
3596 "\tcall _start_in_C_solaris\n"
3597 /* NOTREACHED */
3598 "\thlt\n"
3599 "\t.previous\n"
3600);
3601#elif defined(VGP_amd64_solaris)
3602asm("\n"
3603 ".text\n"
3604 "\t.globl _start\n"
3605 "\t.type _start, @function\n"
3606 "_start:\n"
3607 /* Set up the new stack in %rdi. */
3608 "\tmovq $vgPlain_interim_stack, %rdi\n"
3609 "\taddq $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rdi\n"
3610 "\taddq $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %rdi\n"
3611 "\tandq $~15, %rdi\n"
3612 /* Install it, and collect the original one. */
3613 "\txchgq %rdi, %rsp\n"
3614 /* Call _start_in_C_solaris, passing it the startup %rsp. */
3615 "\tcall _start_in_C_solaris\n"
3616 /* NOTREACHED */
3617 "\thlt\n"
3618 ".previous\n"
3619);
3620#else
3621# error "Unknown Solaris platform"
3622#endif
3623
3624void *memcpy(void *dest, const void *src, size_t n);
3625void *memcpy(void *dest, const void *src, size_t n) {
3626 return VG_(memcpy)(dest, src, n);
3627}
3628
3629__attribute__ ((used))
3630void _start_in_C_solaris ( UWord* pArgc );
3631__attribute__ ((used))
3632void _start_in_C_solaris ( UWord* pArgc )
3633{
3634 Int r;
3635 Word argc = pArgc[0];
3636 HChar** argv = (HChar**)&pArgc[1];
3637 HChar** envp = (HChar**)&pArgc[1 + argc + 1];
3638
3639 VG_(memset)( &the_iicii, 0, sizeof(the_iicii) );
3640 VG_(memset)( &the_iifii, 0, sizeof(the_iifii) );
3641
3642 the_iicii.sp_at_startup = (Addr)pArgc;
3643
3644 r = valgrind_main((Int)argc, argv, envp);
3645 /* NOTREACHED */
3646 VG_(exit)(r);
3647}
njnf76d27a2009-05-28 01:53:07 +00003648
njn49f80e82009-05-21 01:25:43 +00003649#else
njn49f80e82009-05-21 01:25:43 +00003650# error "Unknown OS"
3651#endif
sewardj17c11042006-10-15 01:26:40 +00003652
3653
sewardj8eb8bab2015-07-21 14:44:28 +00003654Addr VG_(get_initial_client_SP)( void )
3655{
3656 return the_iifii.initial_client_SP;
3657}
3658
sewardj0af71bb2010-07-01 14:50:30 +00003659/*====================================================================*/
3660/*=== {u,}{div,mod}di3 replacements ===*/
3661/*====================================================================*/
njnea2d6fd2010-07-01 00:20:20 +00003662
3663/* For static linking on x86-darwin, we need to supply our own 64-bit
3664 integer division code, else the link dies thusly:
3665
3666 ld_classic: Undefined symbols:
3667 ___udivdi3
3668 ___umoddi3
3669*/
3670#if defined(VGP_x86_darwin)
3671
3672/* Routines for doing signed/unsigned 64 x 64 ==> 64 div and mod
3673 (udivdi3, umoddi3, divdi3, moddi3) using only 32 x 32 ==> 32
3674 division. Cobbled together from
3675
3676 http://www.hackersdelight.org/HDcode/divlu.c
3677 http://www.hackersdelight.org/HDcode/divls.c
3678 http://www.hackersdelight.org/HDcode/newCode/divDouble.c
3679
3680 The code from those three files is covered by the following license,
3681 as it appears at:
3682
3683 http://www.hackersdelight.org/permissions.htm
3684
3685 You are free to use, copy, and distribute any of the code on
3686 this web site, whether modified by you or not. You need not give
3687 attribution. This includes the algorithms (some of which appear
3688 in Hacker's Delight), the Hacker's Assistant, and any code
3689 submitted by readers. Submitters implicitly agree to this.
3690*/
3691
3692/* Long division, unsigned (64/32 ==> 32).
3693 This procedure performs unsigned "long division" i.e., division of a
369464-bit unsigned dividend by a 32-bit unsigned divisor, producing a
369532-bit quotient. In the overflow cases (divide by 0, or quotient
3696exceeds 32 bits), it returns a remainder of 0xFFFFFFFF (an impossible
3697value).
3698 The dividend is u1 and u0, with u1 being the most significant word.
3699The divisor is parameter v. The value returned is the quotient.
3700 Max line length is 57, to fit in hacker.book. */
3701
3702static Int nlz32(UInt x)
3703{
3704 Int n;
3705 if (x == 0) return(32);
3706 n = 0;
3707 if (x <= 0x0000FFFF) {n = n +16; x = x <<16;}
3708 if (x <= 0x00FFFFFF) {n = n + 8; x = x << 8;}
3709 if (x <= 0x0FFFFFFF) {n = n + 4; x = x << 4;}
3710 if (x <= 0x3FFFFFFF) {n = n + 2; x = x << 2;}
3711 if (x <= 0x7FFFFFFF) {n = n + 1;}
3712 return n;
3713}
3714
3715/* 64 x 32 ==> 32 unsigned division, using only 32 x 32 ==> 32
3716 division as a primitive. */
3717static UInt divlu2(UInt u1, UInt u0, UInt v, UInt *r)
3718{
3719 const UInt b = 65536; // Number base (16 bits).
3720 UInt un1, un0, // Norm. dividend LSD's.
3721 vn1, vn0, // Norm. divisor digits.
3722 q1, q0, // Quotient digits.
3723 un32, un21, un10, // Dividend digit pairs.
3724 rhat; // A remainder.
3725 Int s; // Shift amount for norm.
3726
3727 if (u1 >= v) { // If overflow, set rem.
3728 if (r != NULL) // to an impossible value,
3729 *r = 0xFFFFFFFF; // and return the largest
3730 return 0xFFFFFFFF;} // possible quotient.
3731
3732 s = nlz32(v); // 0 <= s <= 31.
3733 v = v << s; // Normalize divisor.
3734 vn1 = v >> 16; // Break divisor up into
3735 vn0 = v & 0xFFFF; // two 16-bit digits.
3736
3737 un32 = (u1 << s) | ((u0 >> (32 - s)) & (-s >> 31));
3738 un10 = u0 << s; // Shift dividend left.
3739
3740 un1 = un10 >> 16; // Break right half of
3741 un0 = un10 & 0xFFFF; // dividend into two digits.
3742
3743 q1 = un32/vn1; // Compute the first
3744 rhat = un32 - q1*vn1; // quotient digit, q1.
3745 again1:
3746 if (q1 >= b || q1*vn0 > b*rhat + un1) {
3747 q1 = q1 - 1;
3748 rhat = rhat + vn1;
3749 if (rhat < b) goto again1;}
3750
3751 un21 = un32*b + un1 - q1*v; // Multiply and subtract.
3752
3753 q0 = un21/vn1; // Compute the second
3754 rhat = un21 - q0*vn1; // quotient digit, q0.
3755 again2:
3756 if (q0 >= b || q0*vn0 > b*rhat + un0) {
3757 q0 = q0 - 1;
3758 rhat = rhat + vn1;
3759 if (rhat < b) goto again2;}
3760
3761 if (r != NULL) // If remainder is wanted,
3762 *r = (un21*b + un0 - q0*v) >> s; // return it.
3763 return q1*b + q0;
3764}
3765
3766
3767/* 64 x 32 ==> 32 signed division, using only 32 x 32 ==> 32 division
3768 as a primitive. */
3769static Int divls(Int u1, UInt u0, Int v, Int *r)
3770{
3771 Int q, uneg, vneg, diff, borrow;
3772
3773 uneg = u1 >> 31; // -1 if u < 0.
3774 if (uneg) { // Compute the absolute
3775 u0 = -u0; // value of the dividend u.
3776 borrow = (u0 != 0);
3777 u1 = -u1 - borrow;}
3778
3779 vneg = v >> 31; // -1 if v < 0.
3780 v = (v ^ vneg) - vneg; // Absolute value of v.
3781
3782 if ((UInt)u1 >= (UInt)v) goto overflow;
3783
3784 q = divlu2(u1, u0, v, (UInt *)r);
3785
3786 diff = uneg ^ vneg; // Negate q if signs of
3787 q = (q ^ diff) - diff; // u and v differed.
3788 if (uneg && r != NULL)
3789 *r = -*r;
3790
3791 if ((diff ^ q) < 0 && q != 0) { // If overflow,
3792 overflow: // set remainder
3793 if (r != NULL) // to an impossible value,
3794 *r = 0x80000000; // and return the largest
3795 q = 0x80000000;} // possible neg. quotient.
3796 return q;
3797}
3798
3799
3800
3801/* This file contains a program for doing 64/64 ==> 64 division, on a
3802machine that does not have that instruction but that does have
3803instructions for "long division" (64/32 ==> 32). Code for unsigned
3804division is given first, followed by a simple program for doing the
3805signed version by using the unsigned version.
3806 These programs are useful in implementing "long long" (64-bit)
3807arithmetic on a machine that has the long division instruction. It will
3808work on 64- and 32-bit machines, provided the compiler implements long
3809long's (64-bit integers). It is desirable that the machine have the
3810Count Leading Zeros instruction.
3811 In the GNU world, these programs are known as __divdi3 and __udivdi3,
3812and similar names are used here.
3813 This material is not in HD, but may be in a future edition.
3814Max line length is 57, to fit in hacker.book. */
3815
3816
3817static Int nlz64(ULong x)
3818{
3819 Int n;
3820 if (x == 0) return(64);
3821 n = 0;
3822 if (x <= 0x00000000FFFFFFFFULL) {n = n + 32; x = x << 32;}
3823 if (x <= 0x0000FFFFFFFFFFFFULL) {n = n + 16; x = x << 16;}
3824 if (x <= 0x00FFFFFFFFFFFFFFULL) {n = n + 8; x = x << 8;}
3825 if (x <= 0x0FFFFFFFFFFFFFFFULL) {n = n + 4; x = x << 4;}
3826 if (x <= 0x3FFFFFFFFFFFFFFFULL) {n = n + 2; x = x << 2;}
3827 if (x <= 0x7FFFFFFFFFFFFFFFULL) {n = n + 1;}
3828 return n;
3829}
3830
3831// ---------------------------- udivdi3 --------------------------------
3832
3833 /* The variables u0, u1, etc. take on only 32-bit values, but they
3834 are declared long long to avoid some compiler warning messages and to
3835 avoid some unnecessary EXTRs that the compiler would put in, to
3836 convert long longs to ints.
3837
3838 First the procedure takes care of the case in which the divisor is a
3839 32-bit quantity. There are two subcases: (1) If the left half of the
3840 dividend is less than the divisor, one execution of DIVU is all that
3841 is required (overflow is not possible). (2) Otherwise it does two
3842 divisions, using the grade school method, with variables used as
3843 suggested below.
3844
3845 q1 q0
3846 ________
3847 v) u1 u0
3848 q1*v
3849 ____
3850 k u0 */
3851
3852/* These macros must be used with arguments of the appropriate type
3853(unsigned long long for DIVU and long long for DIVS. They are
3854simulations of the presumed machines ops. I.e., they look at only the
3855low-order 32 bits of the divisor, they return garbage if the division
3856overflows, and they return garbage in the high-order half of the
3857quotient doubleword.
3858 In practice, these would be replaced with uses of the machine's DIVU
3859and DIVS instructions (e.g., by using the GNU "asm" facility). */
3860
3861static UInt DIVU ( ULong u, UInt v )
3862{
3863 UInt uHi = (UInt)(u >> 32);
3864 UInt uLo = (UInt)u;
3865 return divlu2(uHi, uLo, v, NULL);
3866}
3867
3868static Int DIVS ( Long u, Int v )
3869{
3870 Int uHi = (Int)(u >> 32);
3871 UInt uLo = (UInt)u;
3872 return divls(uHi, uLo, v, NULL);
3873}
3874
3875/* 64 x 64 ==> 64 unsigned division, using only 32 x 32 ==> 32
3876 division as a primitive. */
3877static ULong udivdi3(ULong u, ULong v)
3878{
3879 ULong u0, u1, v1, q0, q1, k, n;
3880
3881 if (v >> 32 == 0) { // If v < 2**32:
3882 if (u >> 32 < v) // If u/v cannot overflow,
3883 return DIVU(u, v) // just do one division.
3884 & 0xFFFFFFFF;
3885 else { // If u/v would overflow:
3886 u1 = u >> 32; // Break u up into two
3887 u0 = u & 0xFFFFFFFF; // halves.
3888 q1 = DIVU(u1, v) // First quotient digit.
3889 & 0xFFFFFFFF;
3890 k = u1 - q1*v; // First remainder, < v.
3891 q0 = DIVU((k << 32) + u0, v) // 2nd quot. digit.
3892 & 0xFFFFFFFF;
3893 return (q1 << 32) + q0;
3894 }
3895 }
3896 // Here v >= 2**32.
3897 n = nlz64(v); // 0 <= n <= 31.
3898 v1 = (v << n) >> 32; // Normalize the divisor
3899 // so its MSB is 1.
3900 u1 = u >> 1; // To ensure no overflow.
3901 q1 = DIVU(u1, v1) // Get quotient from
3902 & 0xFFFFFFFF; // divide unsigned insn.
3903 q0 = (q1 << n) >> 31; // Undo normalization and
3904 // division of u by 2.
3905 if (q0 != 0) // Make q0 correct or
3906 q0 = q0 - 1; // too small by 1.
3907 if ((u - q0*v) >= v)
3908 q0 = q0 + 1; // Now q0 is correct.
3909 return q0;
3910}
3911
3912
3913// ----------------------------- divdi3 --------------------------------
3914
3915/* This routine presumes that smallish cases (those which can be done in
3916one execution of DIVS) are common. If this is not the case, the test for
3917this case should be deleted.
3918 Note that the test for when DIVS can be used is not entirely
3919accurate. For example, DIVS is not used if v = 0xFFFFFFFF8000000,
3920whereas if could be (if u is sufficiently small in magnitude). */
3921
3922// ------------------------------ cut ----------------------------------
3923
3924static ULong my_llabs ( Long x )
3925{
3926 ULong t = x >> 63;
3927 return (x ^ t) - t;
3928}
3929
3930/* 64 x 64 ==> 64 signed division, using only 32 x 32 ==> 32 division
3931 as a primitive. */
3932static Long divdi3(Long u, Long v)
3933{
3934 ULong au, av;
3935 Long q, t;
3936 au = my_llabs(u);
3937 av = my_llabs(v);
3938 if (av >> 31 == 0) { // If |v| < 2**31 and
3939 // if (v << 32 >> 32 == v) { // If v is in range and
3940 if (au < av << 31) { // |u|/|v| cannot
3941 q = DIVS(u, v); // overflow, use DIVS.
3942 return (q << 32) >> 32;
3943 }
3944 }
3945 q = udivdi3(au,av); // Invoke udivdi3.
3946 t = (u ^ v) >> 63; // If u, v have different
3947 return (q ^ t) - t; // signs, negate q.
3948}
3949
3950// ---------------------------- end cut --------------------------------
3951
sewardj0af71bb2010-07-01 14:50:30 +00003952ULong __udivdi3 (ULong u, ULong v);
njnea2d6fd2010-07-01 00:20:20 +00003953ULong __udivdi3 (ULong u, ULong v)
3954{
3955 return udivdi3(u,v);
3956}
3957
sewardj0af71bb2010-07-01 14:50:30 +00003958Long __divdi3 (Long u, Long v);
njnea2d6fd2010-07-01 00:20:20 +00003959Long __divdi3 (Long u, Long v)
3960{
3961 return divdi3(u,v);
3962}
3963
sewardj0af71bb2010-07-01 14:50:30 +00003964ULong __umoddi3 (ULong u, ULong v);
njnea2d6fd2010-07-01 00:20:20 +00003965ULong __umoddi3 (ULong u, ULong v)
3966{
3967 ULong q = __udivdi3(u, v);
3968 ULong r = u - q * v;
3969 return r;
3970}
3971
sewardj0af71bb2010-07-01 14:50:30 +00003972Long __moddi3 (Long u, Long v);
njnea2d6fd2010-07-01 00:20:20 +00003973Long __moddi3 (Long u, Long v)
3974{
3975 Long q = __divdi3(u, v);
3976 Long r = u - q * v;
3977 return r;
3978}
3979
sewardj70d71c72011-08-23 07:35:42 +00003980/* ------------------------------------------------
3981 ld_classic: Undefined symbols:
3982 ___fixunsdfdi
3983 ------------------------------------------------
3984*/
3985
3986/* ===-- fixunsdfdi.c - Implement __fixunsdfdi -----------------------------===
3987 *
3988 * The LLVM Compiler Infrastructure
3989 *
3990 * This file is dual licensed under the MIT and the University of Illinois Open
3991 * Source Licenses. See LICENSE.TXT for details.
3992 *
3993 * ===----------------------------------------------------------------------===
3994 *
3995 * This file implements __fixunsdfdi for the compiler_rt library.
3996 *
3997 * ===----------------------------------------------------------------------===
3998 */
3999
4000/* As per http://www.gnu.org/licenses/license-list.html#GPLCompatibleLicenses,
4001
4002 the "NCSA/University of Illinois Open Source License" is compatible
4003 with the GPL (both version 2 and 3). What is claimed to be
4004 compatible is this
4005
4006 http://www.opensource.org/licenses/UoI-NCSA.php
4007
4008 and the LLVM documentation at
4009
4010 http://www.llvm.org/docs/DeveloperPolicy.html#license
4011
4012 says all the code in LLVM is available under the University of
4013 Illinois/NCSA Open Source License, at this URL
4014
4015 http://www.opensource.org/licenses/UoI-NCSA.php
4016
4017 viz, the same one that the FSF pages claim is compatible. So I
4018 think it's OK to include it.
4019*/
4020
4021/* Returns: convert a to a unsigned long long, rounding toward zero.
4022 * Negative values all become zero.
4023 */
4024
4025/* Assumption: double is a IEEE 64 bit floating point type
4026 * du_int is a 64 bit integral type
4027 * value in double is representable in du_int or is negative
4028 * (no range checking performed)
4029 */
4030
4031/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
4032
4033typedef unsigned long long du_int;
4034typedef unsigned su_int;
4035
4036typedef union
4037{
4038 du_int all;
4039 struct
4040 {
4041#if VG_LITTLEENDIAN
4042 su_int low;
4043 su_int high;
4044#else
4045 su_int high;
4046 su_int low;
4047#endif /* VG_LITTLEENDIAN */
4048 }s;
4049} udwords;
4050
4051typedef union
4052{
4053 udwords u;
4054 double f;
4055} double_bits;
4056
4057du_int __fixunsdfdi(double a);
4058
4059du_int
4060__fixunsdfdi(double a)
4061{
4062 double_bits fb;
4063 fb.f = a;
4064 int e = ((fb.u.s.high & 0x7FF00000) >> 20) - 1023;
4065 if (e < 0 || (fb.u.s.high & 0x80000000))
4066 return 0;
4067 udwords r;
4068 r.s.high = (fb.u.s.high & 0x000FFFFF) | 0x00100000;
4069 r.s.low = fb.u.s.low;
4070 if (e > 52)
4071 r.all <<= (e - 52);
4072 else
4073 r.all >>= (52 - e);
4074 return r.all;
4075}
4076
4077
njnea2d6fd2010-07-01 00:20:20 +00004078#endif
4079
4080
sewardjd1678e02014-11-06 20:20:01 +00004081/*====================================================================*/
4082/*=== Dummy _voucher_mach_msg_set for OSX 10.10 ===*/
4083/*====================================================================*/
4084
rhyskidd9090d2f2015-07-08 13:46:57 +00004085#if defined(VGO_darwin) && DARWIN_VERS >= DARWIN_10_10
sewardjd1678e02014-11-06 20:20:01 +00004086
rhyskidd9090d2f2015-07-08 13:46:57 +00004087/* Builds on MacOSX 10.10+ seem to need this for some reason. */
sewardjd1678e02014-11-06 20:20:01 +00004088/* extern boolean_t voucher_mach_msg_set(mach_msg_header_t *msg)
4089 __attribute__((weak_import));
4090 I haven't a clue what the return value means, so just return 0.
4091 Looks like none of the generated uses in the tree look at the
4092 return value anyway.
4093*/
4094UWord voucher_mach_msg_set ( UWord arg1 );
4095UWord voucher_mach_msg_set ( UWord arg1 )
4096{
4097 return 0;
4098}
4099
4100#endif
4101
Elliott Hughesa0664b92017-04-18 17:46:52 -07004102#if defined(VGO_darwin) && DARWIN_VERS == DARWIN_10_10
4103
4104/* This might also be needed for > DARWIN_10_10, but I have no way
4105 to test for that. Hence '==' rather than '>=' in the version
4106 test above. */
4107void __bzero ( void* s, UWord n );
4108void __bzero ( void* s, UWord n )
4109{
4110 (void) VG_(memset)( s, 0, n );
4111}
4112
4113#endif
4114
sewardjd1678e02014-11-06 20:20:01 +00004115
sewardjde4a1d02002-03-22 01:27:54 +00004116/*--------------------------------------------------------------------*/
njn04e16982005-05-31 00:23:43 +00004117/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00004118/*--------------------------------------------------------------------*/