sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 3 | /*--- Startup: the real stuff m_main.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 9 | |
sewardj | b3a1e4b | 2015-08-21 11:32:26 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2015 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 11 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 28 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 31 | #include "pub_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 32 | #include "pub_core_vki.h" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 33 | #include "pub_core_vkiscnums.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 34 | #include "pub_core_threadstate.h" |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 35 | #include "pub_core_xarray.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 36 | #include "pub_core_clientstate.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 37 | #include "pub_core_aspacemgr.h" |
njn | ac1e033 | 2009-05-08 00:39:31 +0000 | [diff] [blame] | 38 | #include "pub_core_aspacehl.h" |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 39 | #include "pub_core_clreq.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 40 | #include "pub_core_commandline.h" |
njn | 2521d32 | 2005-05-08 14:45:13 +0000 | [diff] [blame] | 41 | #include "pub_core_debuglog.h" |
| 42 | #include "pub_core_errormgr.h" |
| 43 | #include "pub_core_execontext.h" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 44 | #include "pub_core_gdbserver.h" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 45 | #include "pub_core_initimg.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 46 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 47 | #include "pub_core_libcassert.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 48 | #include "pub_core_libcfile.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 49 | #include "pub_core_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 50 | #include "pub_core_libcproc.h" |
njn | de62cbf | 2005-06-10 22:08:14 +0000 | [diff] [blame] | 51 | #include "pub_core_libcsignal.h" |
sewardj | 17c5e2e | 2012-12-28 09:12:14 +0000 | [diff] [blame] | 52 | #include "pub_core_sbprofile.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 53 | #include "pub_core_syscall.h" // VG_(strerror) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 54 | #include "pub_core_mach.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 55 | #include "pub_core_machine.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 56 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 57 | #include "pub_core_options.h" |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 58 | #include "pub_core_debuginfo.h" |
njn | d1af003 | 2005-05-29 17:01:48 +0000 | [diff] [blame] | 59 | #include "pub_core_redir.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 60 | #include "pub_core_scheduler.h" |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 61 | #include "pub_core_seqmatch.h" // For VG_(string_match) |
njn | 0c24647 | 2005-05-31 01:00:08 +0000 | [diff] [blame] | 62 | #include "pub_core_signals.h" |
njn | 2025cf9 | 2005-06-26 20:44:48 +0000 | [diff] [blame] | 63 | #include "pub_core_stacks.h" // For VG_(register_stack) |
njn | c1b0181 | 2005-06-17 22:19:06 +0000 | [diff] [blame] | 64 | #include "pub_core_syswrap.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 65 | #include "pub_core_tooliface.h" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 66 | #include "pub_core_translate.h" // For VG_(translate) |
njn | a7598f6 | 2005-06-18 03:27:58 +0000 | [diff] [blame] | 67 | #include "pub_core_trampoline.h" |
njn | 8bddf58 | 2005-05-13 23:40:55 +0000 | [diff] [blame] | 68 | #include "pub_core_transtab.h" |
florian | c91f584 | 2013-09-15 10:42:26 +0000 | [diff] [blame] | 69 | #include "pub_core_inner.h" |
philippe | 14711e8 | 2012-06-14 22:18:50 +0000 | [diff] [blame] | 70 | #if defined(ENABLE_INNER_CLIENT_REQUEST) |
florian | 1a046d5 | 2013-09-16 20:56:35 +0000 | [diff] [blame] | 71 | #include "pub_core_clreq.h" |
philippe | 14711e8 | 2012-06-14 22:18:50 +0000 | [diff] [blame] | 72 | #endif |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 73 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 74 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 75 | /*====================================================================*/ |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 76 | /*=== Command-line: variables, processing, etc ===*/ |
| 77 | /*====================================================================*/ |
| 78 | |
| 79 | // See pub_{core,tool}_options.h for explanations of all these. |
| 80 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 81 | static void usage_NORETURN ( Bool debug_help ) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 82 | { |
florian | 95a128b | 2011-09-29 14:26:38 +0000 | [diff] [blame] | 83 | /* 'usage1' contains a %s |
| 84 | - for the name of the GDB executable |
| 85 | - for the name of vgdb's path prefix |
| 86 | which must be supplied when they are VG_(printf)'d. */ |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 87 | const HChar usage1[] = |
njn | 00cfcfc | 2005-11-12 18:53:50 +0000 | [diff] [blame] | 88 | "usage: valgrind [options] prog-and-args\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 89 | "\n" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 90 | " tool-selection option, with default in [ ]:\n" |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 91 | " --tool=<name> use the Valgrind tool named <name> [memcheck]\n" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 92 | "\n" |
| 93 | " basic user options for all Valgrind tools, with defaults in [ ]:\n" |
nethercote | a76368b | 2004-06-16 11:56:29 +0000 | [diff] [blame] | 94 | " -h --help show this message\n" |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 95 | " --help-debug show this message, plus debugging options\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 96 | " --version show version\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 97 | " -q --quiet run silently; only print error msgs\n" |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 98 | " -v --verbose be more verbose -- show misc extra info\n" |
sewardj | 6e31f80 | 2007-11-17 22:29:25 +0000 | [diff] [blame] | 99 | " --trace-children=no|yes Valgrind-ise child processes (follow execve)? [no]\n" |
sewardj | 0642127 | 2009-11-05 08:55:13 +0000 | [diff] [blame] | 100 | " --trace-children-skip=patt1,patt2,... specifies a list of executables\n" |
| 101 | " that --trace-children=yes should not trace into\n" |
sewardj | 9ab64a4 | 2010-12-06 11:40:04 +0000 | [diff] [blame] | 102 | " --trace-children-skip-by-arg=patt1,patt2,... same as --trace-children-skip=\n" |
| 103 | " but check the argv[] entries for children, rather\n" |
| 104 | " than the exe name, to make a follow/no-follow decision\n" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 105 | " --child-silent-after-fork=no|yes omit child output between fork & exec? [no]\n" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 106 | " --vgdb=no|yes|full activate gdbserver? [yes]\n" |
| 107 | " full is slower but provides precise watchpoint/step\n" |
sewardj | 1568e17 | 2011-06-18 08:28:04 +0000 | [diff] [blame] | 108 | " --vgdb-error=<number> invoke gdbserver after <number> errors [%d]\n" |
| 109 | " to get started quickly, use --vgdb-error=0\n" |
| 110 | " and follow the on-screen directions\n" |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame] | 111 | " --vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none]\n" |
sewardj | 122f6af | 2014-09-03 21:58:54 +0000 | [diff] [blame] | 112 | " where event is one of:\n" |
| 113 | " startup exit valgrindabexit all none\n" |
nethercote | 0d58850 | 2004-06-21 13:27:11 +0000 | [diff] [blame] | 114 | " --track-fds=no|yes track open file descriptors? [no]\n" |
thughes | 6233a38 | 2004-08-21 11:10:44 +0000 | [diff] [blame] | 115 | " --time-stamp=no|yes add timestamps to log messages? [no]\n" |
njn | ce54555 | 2005-07-25 22:36:52 +0000 | [diff] [blame] | 116 | " --log-fd=<number> log messages to file descriptor [2=stderr]\n" |
njn | 374a36d | 2007-11-23 01:41:32 +0000 | [diff] [blame] | 117 | " --log-file=<file> log messages to <file>\n" |
njn | ce54555 | 2005-07-25 22:36:52 +0000 | [diff] [blame] | 118 | " --log-socket=ipaddr:port log messages to socket ipaddr:port\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 119 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 120 | " user options for Valgrind tools that report errors:\n" |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 121 | " --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" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 126 | " --demangle=no|yes automatically demangle C++ names? [yes]\n" |
njn | 20b4a15 | 2005-10-19 22:39:40 +0000 | [diff] [blame] | 127 | " --num-callers=<number> show <number> callers in stack traces [12]\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 128 | " --error-limit=no|yes stop showing new errors if too many? [yes]\n" |
sewardj | b977908 | 2006-05-12 23:50:15 +0000 | [diff] [blame] | 129 | " --error-exitcode=<number> exit code to return if errors found [0=disable]\n" |
philippe | 7b3d356 | 2014-11-12 19:43:29 +0000 | [diff] [blame] | 130 | " --error-markers=<begin>,<end> add lines with begin/end markers before/after\n" |
| 131 | " each error output in plain text mode [none]\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 132 | " --show-below-main=no|yes continue stack traces below main() [no]\n" |
bart | a6efdfa | 2014-06-24 05:08:21 +0000 | [diff] [blame] | 133 | " --default-suppressions=yes|no\n" |
| 134 | " load default suppressions [yes]\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 135 | " --suppressions=<filename> suppress errors described in <filename>\n" |
sewardj | d153fae | 2005-01-10 17:24:47 +0000 | [diff] [blame] | 136 | " --gen-suppressions=no|yes|all print suppressions for errors? [no]\n" |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 137 | " --input-fd=<number> file descriptor for input [0=stdin]\n" |
sewardj | 29dd9e6 | 2015-08-31 14:37:25 +0000 | [diff] [blame] | 138 | " --dsymutil=no|yes run dsymutil on Mac OS X when helpful? [yes]\n" |
sewardj | 97724e5 | 2005-04-02 23:40:59 +0000 | [diff] [blame] | 139 | " --max-stackframe=<number> assume stack switch for SP changes larger\n" |
| 140 | " than <number> bytes [2000000]\n" |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 141 | " --main-stacksize=<number> set size of main thread's stack (in bytes)\n" |
philippe | 3bcd51d | 2013-06-12 21:45:39 +0000 | [diff] [blame] | 142 | " [min(max(current 'ulimit' value,1MB),16MB)]\n" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 143 | "\n" |
| 144 | " user options for Valgrind tools that replace malloc:\n" |
philippe | d99c26a | 2012-07-31 22:17:28 +0000 | [diff] [blame] | 145 | " --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" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 148 | "\n" |
| 149 | " uncommon user options for all Valgrind tools:\n" |
sewardj | 14cdbf8 | 2010-10-12 00:44:05 +0000 | [diff] [blame] | 150 | " --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" |
sewardj | 8b6573d | 2012-12-05 22:15:14 +0000 | [diff] [blame] | 156 | " --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" |
sewardj | 5d616df | 2013-07-02 08:07:15 +0000 | [diff] [blame] | 159 | " --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" |
sewardj | 6d9a338 | 2015-08-31 13:05:35 +0000 | [diff] [blame] | 164 | " --smc-check=none|stack|all|all-non-file [all-non-file]\n" |
sewardj | 6dbcc63 | 2011-06-07 21:39:28 +0000 | [diff] [blame] | 165 | " 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" |
philippe | a0a7393 | 2014-06-15 15:42:20 +0000 | [diff] [blame] | 168 | " --read-inline-info=yes|no read debug info about inlined function calls\n" |
sewardj | 47c6d14 | 2014-09-12 09:22:36 +0000 | [diff] [blame] | 169 | " and use it to do better stack traces. [yes]\n" |
iraisr | 4e1d946 | 2015-09-01 11:51:16 +0000 | [diff] [blame] | 170 | " on Linux/Android/Solaris for Memcheck/Helgrind/DRD\n" |
sewardj | 47c6d14 | 2014-09-12 09:22:36 +0000 | [diff] [blame] | 171 | " only. [no] for all other tools and platforms.\n" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 172 | " --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" |
bart | f6122a0 | 2010-03-27 07:38:39 +0000 | [diff] [blame] | 175 | " DRD) [no]\n" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 176 | " --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" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 179 | " --run-libc-freeres=no|yes free up glibc memory at exit on Linux? [yes]\n" |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 180 | " --run-cxx-freeres=no|yes free up libstdc++ memory at exit on Linux\n" |
| 181 | " and Solaris? [yes]\n" |
philippe | ec905f7 | 2014-08-17 20:03:51 +0000 | [diff] [blame] | 182 | " --sim-hints=hint1,hint2,... activate unusual sim behaviours [none] \n" |
sewardj | 122f6af | 2014-09-03 21:58:54 +0000 | [diff] [blame] | 183 | " where hint is one of:\n" |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 184 | " lax-ioctls lax-doors fuse-compatible enable-outer\n" |
philippe | 9848690 | 2014-08-19 22:46:44 +0000 | [diff] [blame] | 185 | " no-inner-prefix no-nptl-pthread-stackcache none\n" |
bart | 78bfc71 | 2011-12-08 16:14:59 +0000 | [diff] [blame] | 186 | " --fair-sched=no|yes|try schedule threads fairly on multicore systems [no]\n" |
sewardj | 4450a0e | 2014-09-03 15:19:25 +0000 | [diff] [blame] | 187 | " --kernel-variant=variant1,variant2,...\n" |
| 188 | " handle non-standard kernel variants [none]\n" |
| 189 | " where variant is one of:\n" |
sewardj | 124e56d | 2014-09-06 14:45:12 +0000 | [diff] [blame] | 190 | " bproc android-no-hw-tls\n" |
sewardj | 4450a0e | 2014-09-03 15:19:25 +0000 | [diff] [blame] | 191 | " android-gpu-sgx5xx android-gpu-adreno3xx none\n" |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 192 | " --merge-recursive-frames=<number> merge frames between identical\n" |
| 193 | " program counters in max <number> frames) [0]\n" |
philippe | 8e1bee4 | 2013-10-18 00:08:20 +0000 | [diff] [blame] | 194 | " --num-transtab-sectors=<number> size of translated code cache [%d]\n" |
sewardj | a11ec17 | 2013-10-18 11:18:45 +0000 | [diff] [blame] | 195 | " more sectors may increase performance, but use more memory.\n" |
philippe | 924c852 | 2015-03-15 12:24:19 +0000 | [diff] [blame] | 196 | " --avg-transtab-entry-size=<number> avg size in bytes of a translated\n" |
| 197 | " basic block [0, meaning use tool provided default]\n" |
philippe | e4d7812 | 2014-04-20 14:20:37 +0000 | [diff] [blame] | 198 | " --aspace-minaddr=0xPP avoid mapping memory below 0xPP [guessed]\n" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 199 | " --valgrind-stacksize=<number> size of valgrind (host) thread's stack\n" |
| 200 | " (in bytes) [" |
| 201 | VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB) |
| 202 | "]\n" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 203 | " --show-emwarns=no|yes show warnings about emulation limits? [no]\n" |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 204 | " --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" |
philippe | 1e470b5 | 2012-05-11 19:33:46 +0000 | [diff] [blame] | 207 | " --soname-synonyms=syn1=pattern1,syn2=pattern2,... synonym soname\n" |
sewardj | 260c648 | 2012-08-07 14:46:34 +0000 | [diff] [blame] | 208 | " 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" |
sewardj | c30cd9b | 2012-12-06 18:08:54 +0000 | [diff] [blame] | 212 | " --sigill-diagnostics=yes|no warn about illegal instructions? [yes]\n" |
sewardj | 49984ea | 2013-10-18 13:21:26 +0000 | [diff] [blame] | 213 | " --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" |
sewardj | 2f87090 | 2014-10-23 21:49:58 +0000 | [diff] [blame] | 218 | " --resync-filter=no|yes|verbose [yes on MacOS, no on other OSes]\n" |
| 219 | " attempt to avoid expensive address-space-resync operations\n" |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 220 | " --max-threads=<number> maximum number of threads that valgrind can\n" |
| 221 | " handle [%d]\n" |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 222 | "\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 223 | |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 224 | const HChar usage2[] = |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 225 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 226 | " debugging options for all Valgrind tools:\n" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 227 | " -d show verbose debugging output\n" |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 228 | " --stats=no|yes show tool and core statistics [no]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 229 | " --sanity-level=<number> level of sanity checking to do [1]\n" |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 230 | " --trace-flags=<XXXXXXXX> show generated code? (X = 0|1) [00000000]\n" |
| 231 | " --profile-flags=<XXXXXXXX> ditto, but for profiling (X = 0|1) [00000000]\n" |
sewardj | 17c5e2e | 2012-12-28 09:12:14 +0000 | [diff] [blame] | 232 | " --profile-interval=<number> show profile every <number> event checks\n" |
| 233 | " [0, meaning only at the end of the run]\n" |
sewardj | 33afdb5 | 2006-01-17 02:36:40 +0000 | [diff] [blame] | 234 | " --trace-notbelow=<number> only show BBs above <number> [999999999]\n" |
florian | 29e022d | 2012-07-02 21:13:34 +0000 | [diff] [blame] | 235 | " --trace-notabove=<number> only show BBs below <number> [0]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 236 | " --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" |
sewardj | f767d96 | 2007-02-12 17:47:14 +0000 | [diff] [blame] | 239 | " --trace-symtab-patt=<patt> limit debuginfo tracing to obj name <patt>\n" |
sewardj | ce058b0 | 2005-05-01 08:55:38 +0000 | [diff] [blame] | 240 | " --trace-cfi=no|yes show call-frame-info details? [no]\n" |
sewardj | f767d96 | 2007-02-12 17:47:14 +0000 | [diff] [blame] | 241 | " --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" |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 244 | " --trace-redir=no|yes show redirection details? [no]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 245 | " --trace-sched=no|yes show thread scheduler details? [no]\n" |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 246 | " --profile-heap=no|yes profile Valgrind's own space use\n" |
philippe | 0644437 | 2012-10-12 21:46:55 +0000 | [diff] [blame] | 247 | " --core-redzone-size=<number> set minimum size of redzones added before/after\n" |
philippe | d99c26a | 2012-07-31 22:17:28 +0000 | [diff] [blame] | 248 | " heap blocks allocated for Valgrind internal use (in bytes) [4]\n" |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 249 | " --wait-for-gdb=yes|no pause on startup to wait for gdb attach\n" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 250 | " --sym-offsets=yes|no show syms in form 'name+offset' ? [no]\n" |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 251 | " --command-line-only=no|yes only use command line options [no]\n" |
njn | 613812e | 2005-03-11 04:57:30 +0000 | [diff] [blame] | 252 | "\n" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 253 | " Vex options for all Valgrind tools:\n" |
| 254 | " --vex-iropt-verbosity=<0..9> [0]\n" |
| 255 | " --vex-iropt-level=<0..2> [2]\n" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 256 | " --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" |
sewardj | 540cc4a | 2010-01-15 10:57:57 +0000 | [diff] [blame] | 259 | " --vex-guest-chase-cond=no|yes [no]\n" |
sewardj | 8d47a61 | 2015-02-05 12:59:46 +0000 | [diff] [blame] | 260 | " 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" |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 284 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 285 | " 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" |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 288 | "\n" |
| 289 | " debugging options for Valgrind tools that replace malloc:\n" |
| 290 | " --trace-malloc=no|yes show client malloc details? [no]\n" |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 291 | "\n"; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 292 | |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 293 | const HChar usage3[] = |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 294 | "\n" |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 295 | " Extra options read from ~/.valgrindrc, $VALGRIND_OPTS, ./.valgrindrc\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 296 | "\n" |
njn | 10b9aea | 2009-07-14 06:55:05 +0000 | [diff] [blame] | 297 | " %s is %s\n" |
sewardj | b3a1e4b | 2015-08-21 11:32:26 +0000 | [diff] [blame] | 298 | " 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" |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 300 | "\n" |
njn | 10b9aea | 2009-07-14 06:55:05 +0000 | [diff] [blame] | 301 | " Bug reports, feedback, admiration, abuse, etc, to: %s.\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 302 | "\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 303 | |
florian | f44ff62 | 2014-12-20 16:52:08 +0000 | [diff] [blame] | 304 | HChar default_alignment[30]; // large enough |
| 305 | HChar default_redzone_size[30]; // large enough |
sewardj | 12373b1 | 2007-11-20 21:38:14 +0000 | [diff] [blame] | 306 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 307 | // Ensure the message goes to stdout |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 308 | VG_(log_output_sink).fd = 1; |
| 309 | VG_(log_output_sink).is_socket = False; |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 310 | |
philippe | d99c26a | 2012-07-31 22:17:28 +0000 | [diff] [blame] | 311 | 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. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 319 | VG_(printf)(usage1, |
philippe | d99c26a | 2012-07-31 22:17:28 +0000 | [diff] [blame] | 320 | VG_(clo_vgdb_error) /* int */, |
philippe | d99c26a | 2012-07-31 22:17:28 +0000 | [diff] [blame] | 321 | default_alignment /* char* */, |
| 322 | default_redzone_size /* char* */, |
| 323 | VG_(clo_vgdb_poll) /* int */, |
philippe | 8e1bee4 | 2013-10-18 00:08:20 +0000 | [diff] [blame] | 324 | VG_(vgdb_prefix_default)() /* char* */, |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 325 | N_SECTORS_DEFAULT /* int */, |
| 326 | MAX_THREADS_DEFAULT /* int */ |
philippe | d99c26a | 2012-07-31 22:17:28 +0000 | [diff] [blame] | 327 | ); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 328 | if (VG_(details).name) { |
| 329 | VG_(printf)(" user options for %s:\n", VG_(details).name); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 330 | if (VG_(needs).command_line_options) |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 331 | VG_TDICT_CALL(tool_print_usage); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 332 | else |
| 333 | VG_(printf)(" (none)\n"); |
| 334 | } |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 335 | if (debug_help) { |
sewardj | bbaef87 | 2008-11-01 23:55:32 +0000 | [diff] [blame] | 336 | VG_(printf)("%s", usage2); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 337 | |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 338 | if (VG_(details).name) { |
| 339 | VG_(printf)(" debugging options for %s:\n", VG_(details).name); |
| 340 | |
| 341 | if (VG_(needs).command_line_options) |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 342 | VG_TDICT_CALL(tool_print_debug_usage); |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 343 | else |
| 344 | VG_(printf)(" (none)\n"); |
| 345 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 346 | } |
njn | 10b9aea | 2009-07-14 06:55:05 +0000 | [diff] [blame] | 347 | VG_(printf)(usage3, VG_(details).name, VG_(details).copyright_author, |
| 348 | VG_BUGS_TO); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 349 | VG_(exit)(0); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 350 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 351 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 352 | |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 353 | /* Peer at previously set up VG_(args_for_valgrind) and do some |
| 354 | minimal command line processing that must happen early on: |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 355 | |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 356 | - 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=) |
philippe | 72faf10 | 2012-03-11 22:24:03 +0000 | [diff] [blame] | 361 | - set VG_(clo_sim_hints) (--sim-hints=) |
florian | e13efb8 | 2015-03-11 13:46:14 +0000 | [diff] [blame] | 362 | - set VG_(clo_max_threads) (--max-threads) |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 363 | |
| 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 | */ |
| 369 | static void early_process_cmd_line_options ( /*OUT*/Int* need_help, |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 370 | /*OUT*/const HChar** tool ) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 371 | { |
| 372 | UInt i; |
| 373 | HChar* str; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 374 | |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 375 | vg_assert( VG_(args_for_valgrind) ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 376 | |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 377 | /* 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 ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 381 | vg_assert(str); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 382 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 383 | // Nb: the version string goes to stdout. |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 384 | if VG_XACT_CLO(str, "--version", VG_(log_output_sink).fd, 1) { |
| 385 | VG_(log_output_sink).is_socket = False; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 386 | VG_(printf)("valgrind-" VERSION "\n"); |
| 387 | VG_(exit)(0); |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 388 | } |
njn | cce38e6 | 2010-07-06 04:25:12 +0000 | [diff] [blame] | 389 | 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) {} |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 391 | |
njn | cce38e6 | 2010-07-06 04:25:12 +0000 | [diff] [blame] | 392 | else if VG_XACT_CLO(str, "--help-debug", *need_help, *need_help+2) {} |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 393 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 394 | // The tool has already been determined, but we need to know the name |
| 395 | // here. |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 396 | else if VG_STR_CLO(str, "--tool", *tool) {} |
sewardj | 5bdfbd2 | 2007-12-15 22:13:05 +0000 | [diff] [blame] | 397 | |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 398 | // 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(). |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 401 | 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)) {} |
philippe | 72faf10 | 2012-03-11 22:24:03 +0000 | [diff] [blame] | 403 | |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 404 | // 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 | |
philippe | 72faf10 | 2012-03-11 22:24:03 +0000 | [diff] [blame] | 407 | // 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. |
philippe | ec905f7 | 2014-08-17 20:03:51 +0000 | [diff] [blame] | 410 | else if VG_USETX_CLO (str, "--sim-hints", |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 411 | "lax-ioctls,lax-doors,fuse-compatible," |
philippe | b071b71 | 2014-08-24 11:24:10 +0000 | [diff] [blame] | 412 | "enable-outer,no-inner-prefix," |
philippe | 9848690 | 2014-08-19 22:46:44 +0000 | [diff] [blame] | 413 | "no-nptl-pthread-stackcache", |
philippe | ec905f7 | 2014-08-17 20:03:51 +0000 | [diff] [blame] | 414 | VG_(clo_sim_hints)) {} |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 415 | } |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 416 | |
| 417 | /* For convenience */ |
| 418 | VG_N_THREADS = VG_(clo_max_threads); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 419 | } |
| 420 | |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 421 | /* The main processing for command line options. See comments above |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 422 | 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. |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 444 | */ |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 445 | static |
| 446 | void main_process_cmd_line_options ( /*OUT*/Bool* logging_to_fd, |
florian | 801c199 | 2014-10-17 21:07:37 +0000 | [diff] [blame] | 447 | /*OUT*/const HChar** xml_fname_unexpanded, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 448 | const HChar* toolname ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 449 | { |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 450 | // 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. |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 453 | SysRes sres; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 454 | Int i, tmp_log_fd, tmp_xml_fd; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 455 | Int toolname_len = VG_(strlen)(toolname); |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 456 | const HChar* tmp_str; // Used in a couple of places. |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 457 | enum { |
| 458 | VgLogTo_Fd, |
| 459 | VgLogTo_File, |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 460 | VgLogTo_Socket |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 461 | } log_to = VgLogTo_Fd, // Where is logging output to be sent? |
| 462 | xml_to = VgLogTo_Fd; // Where is XML output to be sent? |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 463 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 464 | /* Temporarily holds the string STR specified with |
| 465 | --{log,xml}-{name,socket}=STR. 'fs' stands for |
| 466 | file-or-socket. */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 467 | const HChar* log_fsname_unexpanded = NULL; |
| 468 | const HChar* xml_fsname_unexpanded = NULL; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 469 | |
sewardj | c30cd9b | 2012-12-06 18:08:54 +0000 | [diff] [blame] | 470 | /* 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 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 474 | /* Log to stderr by default, but usage message goes to stdout. XML |
| 475 | output is initially disabled. */ |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 476 | tmp_log_fd = 2; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 477 | tmp_xml_fd = -1; |
| 478 | |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 479 | /* Check for sane path in ./configure --prefix=... */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 480 | if (VG_LIBDIR[0] != '/') |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 481 | VG_(err_config_error)("Please use absolute paths in " |
florian | 1763e81 | 2011-07-12 19:07:05 +0000 | [diff] [blame] | 482 | "./configure --prefix=... or --libdir=...\n"); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 483 | |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 484 | vg_assert( VG_(args_for_valgrind) ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 485 | |
florian | 7931627 | 2014-10-07 18:36:28 +0000 | [diff] [blame] | 486 | 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 | |
sewardj | 8d47a61 | 2015-02-05 12:59:46 +0000 | [diff] [blame] | 493 | /* 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 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 501 | /* BEGIN command-line processing loop */ |
| 502 | |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 503 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) { |
| 504 | |
| 505 | HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 506 | HChar* colon = arg; |
sewardj | 8d47a61 | 2015-02-05 12:59:46 +0000 | [diff] [blame] | 507 | UInt ix = 0; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 508 | |
njn | 1274d24 | 2007-03-26 23:38:42 +0000 | [diff] [blame] | 509 | // Look for a colon in the option name. |
thughes | 3bfd5a0 | 2004-07-18 08:05:44 +0000 | [diff] [blame] | 510 | while (*colon && *colon != ':' && *colon != '=') |
| 511 | colon++; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 512 | |
njn | 1274d24 | 2007-03-26 23:38:42 +0000 | [diff] [blame] | 513 | // 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". |
thughes | 3bfd5a0 | 2004-07-18 08:05:44 +0000 | [diff] [blame] | 516 | if (*colon == ':') { |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 517 | if (VG_STREQN(2, arg, "--") && |
| 518 | VG_STREQN(toolname_len, arg+2, toolname) && |
| 519 | VG_STREQN(1, arg+2+toolname_len, ":")) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 520 | { |
njn | 1274d24 | 2007-03-26 23:38:42 +0000 | [diff] [blame] | 521 | // 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.) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 536 | if (0) |
| 537 | VG_(printf)("tool-specific arg: %s\n", arg); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 538 | arg = VG_(strdup)("main.mpclo.1", arg + toolname_len + 1); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 539 | arg[0] = '-'; |
| 540 | arg[1] = '-'; |
| 541 | |
| 542 | } else { |
| 543 | // prefix doesn't match, skip to next arg |
| 544 | continue; |
| 545 | } |
| 546 | } |
| 547 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 548 | /* Ignore these options - they've already been handled */ |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 549 | 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") {} |
philippe | 0644437 | 2012-10-12 21:46:55 +0000 | [diff] [blame] | 553 | else if VG_STREQN(17, arg, "--max-stackframe=") {} |
| 554 | else if VG_STREQN(17, arg, "--main-stacksize=") {} |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 555 | else if VG_STREQN(14, arg, "--max-threads=") {} |
philippe | e4d7812 | 2014-04-20 14:20:37 +0000 | [diff] [blame] | 556 | else if VG_STREQN(12, arg, "--sim-hints=") {} |
philippe | 0644437 | 2012-10-12 21:46:55 +0000 | [diff] [blame] | 557 | 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=") {} |
philippe | e4d7812 | 2014-04-20 14:20:37 +0000 | [diff] [blame] | 560 | else if VG_STREQN(17, arg, "--aspace-minaddr=") {} |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 561 | |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 562 | 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 | |
philippe | 0c0291a | 2012-08-01 22:03:12 +0000 | [diff] [blame] | 568 | /* 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 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 583 | // These options are new. |
| 584 | else if (VG_STREQ(arg, "-v") || |
| 585 | VG_STREQ(arg, "--verbose")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 586 | VG_(clo_verbosity)++; |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 587 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 588 | else if (VG_STREQ(arg, "-q") || |
| 589 | VG_STREQ(arg, "--quiet")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 590 | VG_(clo_verbosity)--; |
| 591 | |
sewardj | c30cd9b | 2012-12-06 18:08:54 +0000 | [diff] [blame] | 592 | else if VG_BOOL_CLO(arg, "--sigill-diagnostics", VG_(clo_sigill_diag)) |
| 593 | sigill_diag_set = True; |
| 594 | |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 595 | else if VG_BOOL_CLO(arg, "--stats", VG_(clo_stats)) {} |
bart | db4384e | 2011-10-11 18:49:35 +0000 | [diff] [blame] | 596 | else if VG_BOOL_CLO(arg, "--xml", VG_(clo_xml)) |
| 597 | VG_(debugLog_setXml)(VG_(clo_xml)); |
| 598 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 599 | 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) {} |
philippe | 0c0291a | 2012-08-01 22:03:12 +0000 | [diff] [blame] | 601 | 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 */ |
sewardj | 8d47a61 | 2015-02-05 12:59:46 +0000 | [diff] [blame] | 604 | VG_(clo_vex_control).iropt_register_updates_default |
| 605 | = VG_(clo_px_file_backed) |
philippe | 0c0291a | 2012-08-01 22:03:12 +0000 | [diff] [blame] | 606 | = VexRegUpdAllregsAtEachInsn; |
| 607 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 608 | 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)) {} |
philippe | ec905f7 | 2014-08-17 20:03:51 +0000 | [diff] [blame] | 610 | else if VG_USET_CLO (arg, "--vgdb-stop-at", |
| 611 | "startup,exit,valgrindabexit", |
| 612 | VG_(clo_vgdb_stop_at)) {} |
philippe | cffe2a5 | 2014-01-11 13:56:48 +0000 | [diff] [blame] | 613 | else if VG_STR_CLO (arg, "--vgdb-prefix", VG_(clo_vgdb_prefix)) { |
| 614 | VG_(arg_vgdb_prefix) = arg; |
| 615 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 616 | else if VG_BOOL_CLO(arg, "--vgdb-shadow-registers", |
| 617 | VG_(clo_vgdb_shadow_registers)) {} |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 618 | else if VG_BOOL_CLO(arg, "--demangle", VG_(clo_demangle)) {} |
philippe | 1e470b5 | 2012-05-11 19:33:46 +0000 | [diff] [blame] | 619 | else if VG_STR_CLO (arg, "--soname-synonyms",VG_(clo_soname_synonyms)) {} |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 620 | 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)) {} |
philippe | 7b3d356 | 2014-11-12 19:43:29 +0000 | [diff] [blame] | 622 | 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] |
philippe | 8948817 | 2014-11-12 21:10:37 +0000 | [diff] [blame] | 639 | = VG_(malloc)("main.mpclo.2", nextpos - startpos + 1); |
philippe | 7b3d356 | 2014-11-12 19:43:29 +0000 | [diff] [blame] | 640 | 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 | } |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 647 | else if VG_BOOL_CLO(arg, "--show-emwarns", VG_(clo_show_emwarns)) {} |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 648 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 649 | else if VG_BOOL_CLO(arg, "--run-libc-freeres", VG_(clo_run_libc_freeres)) {} |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 650 | else if VG_BOOL_CLO(arg, "--run-cxx-freeres", VG_(clo_run_cxx_freeres)) {} |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 651 | 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)) {} |
bart | 78bfc71 | 2011-12-08 16:14:59 +0000 | [diff] [blame] | 657 | 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 |
florian | 5b99e66 | 2014-11-29 14:41:32 +0000 | [diff] [blame] | 665 | VG_(fmsg_bad_option)(arg, |
| 666 | "Bad argument, should be 'yes', 'try' or 'no'\n"); |
bart | 78bfc71 | 2011-12-08 16:14:59 +0000 | [diff] [blame] | 667 | } |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 668 | 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)) {} |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 680 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 681 | 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)) {} |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 683 | else if VG_BOOL_CLO(arg, "--sym-offsets", VG_(clo_sym_offsets)) {} |
philippe | a0a7393 | 2014-06-15 15:42:20 +0000 | [diff] [blame] | 684 | else if VG_BOOL_CLO(arg, "--read-inline-info", VG_(clo_read_inline_info)) {} |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 685 | else if VG_BOOL_CLO(arg, "--read-var-info", VG_(clo_read_var_info)) {} |
sewardj | f767d96 | 2007-02-12 17:47:14 +0000 | [diff] [blame] | 686 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 687 | 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) {} |
philippe | 8e1bee4 | 2013-10-18 00:08:20 +0000 | [diff] [blame] | 692 | else if VG_BINT_CLO(arg, "--num-transtab-sectors", |
| 693 | VG_(clo_num_transtab_sectors), |
| 694 | MIN_N_SECTORS, MAX_N_SECTORS) {} |
philippe | 924c852 | 2015-03-15 12:24:19 +0000 | [diff] [blame] | 695 | else if VG_BINT_CLO(arg, "--avg-transtab-entry-size", |
| 696 | VG_(clo_avg_transtab_entry_size), |
| 697 | 50, 5000) {} |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 698 | else if VG_BINT_CLO(arg, "--merge-recursive-frames", |
| 699 | VG_(clo_merge_recursive_frames), 0, |
| 700 | VG_DEEPEST_BACKTRACE) {} |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 701 | |
sewardj | 13e3f1b | 2015-02-05 10:05:43 +0000 | [diff] [blame] | 702 | 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) {} |
sewardj | 6dbcc63 | 2011-06-07 21:39:28 +0000 | [diff] [blame] | 708 | else if VG_XACT_CLO(arg, "--smc-check=all-non-file", |
sewardj | 13e3f1b | 2015-02-05 10:05:43 +0000 | [diff] [blame] | 709 | VG_(clo_smc_check), Vg_SmcAllNonFile) {} |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 710 | |
sewardj | 4450a0e | 2014-09-03 15:19:25 +0000 | [diff] [blame] | 711 | else if VG_USETX_CLO (arg, "--kernel-variant", |
| 712 | "bproc," |
sewardj | 124e56d | 2014-09-06 14:45:12 +0000 | [diff] [blame] | 713 | "android-no-hw-tls," |
sewardj | 4450a0e | 2014-09-03 15:19:25 +0000 | [diff] [blame] | 714 | "android-gpu-sgx5xx," |
| 715 | "android-gpu-adreno3xx", |
philippe | ec905f7 | 2014-08-17 20:03:51 +0000 | [diff] [blame] | 716 | VG_(clo_kernel_variant)) {} |
sewardj | 26412bd | 2005-07-07 10:05:05 +0000 | [diff] [blame] | 717 | |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 718 | else if VG_BOOL_CLO(arg, "--dsymutil", VG_(clo_dsymutil)) {} |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 719 | |
sewardj | 9ab64a4 | 2010-12-06 11:40:04 +0000 | [diff] [blame] | 720 | 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)) {} |
sewardj | 0642127 | 2009-11-05 08:55:13 +0000 | [diff] [blame] | 724 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 725 | 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) {} |
sewardj | 13e3f1b | 2015-02-05 10:05:43 +0000 | [diff] [blame] | 729 | |
sewardj | 8d47a61 | 2015-02-05 12:59:46 +0000 | [diff] [blame] | 730 | 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 | } |
sewardj | 13e3f1b | 2015-02-05 10:05:43 +0000 | [diff] [blame] | 753 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 754 | 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) {} |
sewardj | 540cc4a | 2010-01-15 10:57:57 +0000 | [diff] [blame] | 760 | else if VG_BOOL_CLO(arg, "--vex-guest-chase-cond", |
| 761 | VG_(clo_vex_control).guest_chase_cond) {} |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame] | 762 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 763 | else if VG_INT_CLO(arg, "--log-fd", tmp_log_fd) { |
| 764 | log_to = VgLogTo_Fd; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 765 | 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; |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 770 | } |
| 771 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 772 | else if VG_STR_CLO(arg, "--log-file", log_fsname_unexpanded) { |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 773 | log_to = VgLogTo_File; |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 774 | } |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 775 | 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) { |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 780 | log_to = VgLogTo_Socket; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 781 | } |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 782 | else if VG_STR_CLO(arg, "--xml-socket", xml_fsname_unexpanded) { |
| 783 | xml_to = VgLogTo_Socket; |
| 784 | } |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 785 | |
sewardj | 5d616df | 2013-07-02 08:07:15 +0000 | [diff] [blame] | 786 | 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 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 792 | else if VG_STR_CLO(arg, "--xml-user-comment", |
| 793 | VG_(clo_xml_user_comment)) {} |
sewardj | 768db0e | 2005-07-19 14:18:56 +0000 | [diff] [blame] | 794 | |
bart | a6efdfa | 2014-06-24 05:08:21 +0000 | [diff] [blame] | 795 | else if VG_BOOL_CLO(arg, "--default-suppressions", |
sewardj | 13e3f1b | 2015-02-05 10:05:43 +0000 | [diff] [blame] | 796 | VG_(clo_default_supp)) {} |
bart | 2c68e3e | 2014-06-22 10:11:59 +0000 | [diff] [blame] | 797 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 798 | else if VG_STR_CLO(arg, "--suppressions", tmp_str) { |
florian | 7931627 | 2014-10-07 18:36:28 +0000 | [diff] [blame] | 799 | VG_(addToXA)(VG_(clo_suppressions), &tmp_str); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 800 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 801 | |
sewardj | 14cdbf8 | 2010-10-12 00:44:05 +0000 | [diff] [blame] | 802 | else if VG_STR_CLO (arg, "--fullpath-after", tmp_str) { |
florian | 7931627 | 2014-10-07 18:36:28 +0000 | [diff] [blame] | 803 | VG_(addToXA)(VG_(clo_fullpath_after), &tmp_str); |
sewardj | 14cdbf8 | 2010-10-12 00:44:05 +0000 | [diff] [blame] | 804 | } |
| 805 | |
sewardj | 8b6573d | 2012-12-05 22:15:14 +0000 | [diff] [blame] | 806 | else if VG_STR_CLO (arg, "--extra-debuginfo-path", |
| 807 | VG_(clo_extra_debuginfo_path)) {} |
| 808 | |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 809 | else if VG_STR_CLO(arg, "--require-text-symbol", tmp_str) { |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 810 | /* 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. */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 816 | HChar patt[7]; |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 817 | 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) { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 827 | VG_(fmsg_bad_option)(arg, |
| 828 | "Invalid --require-text-symbol= specification.\n"); |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 829 | } |
florian | 7931627 | 2014-10-07 18:36:28 +0000 | [diff] [blame] | 830 | VG_(addToXA)(VG_(clo_req_tsyms), &tmp_str); |
sewardj | f9ebc39 | 2010-05-09 22:30:43 +0000 | [diff] [blame] | 831 | } |
| 832 | |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 833 | /* "stuvwxyz" --> stuvwxyz (binary) */ |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 834 | else if VG_STR_CLO(arg, "--trace-flags", tmp_str) { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 835 | Int j; |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 836 | if (8 != VG_(strlen)(tmp_str)) { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 837 | VG_(fmsg_bad_option)(arg, |
| 838 | "--trace-flags argument must have 8 digits\n"); |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 839 | } |
| 840 | for (j = 0; j < 8; j++) { |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 841 | if ('0' == tmp_str[j]) { /* do nothing */ } |
| 842 | else if ('1' == tmp_str[j]) VG_(clo_trace_flags) |= (1 << (7-j)); |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 843 | else { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 844 | VG_(fmsg_bad_option)(arg, |
| 845 | "--trace-flags argument can only contain 0s and 1s\n"); |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
sewardj | 17c5e2e | 2012-12-28 09:12:14 +0000 | [diff] [blame] | 850 | 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 | |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 854 | /* "stuvwxyz" --> stuvwxyz (binary) */ |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 855 | else if VG_STR_CLO(arg, "--profile-flags", tmp_str) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 856 | Int j; |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 857 | if (8 != VG_(strlen)(tmp_str)) { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 858 | VG_(fmsg_bad_option)(arg, |
| 859 | "--profile-flags argument must have 8 digits\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 860 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 861 | for (j = 0; j < 8; j++) { |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 862 | if ('0' == tmp_str[j]) { /* do nothing */ } |
sewardj | 17c5e2e | 2012-12-28 09:12:14 +0000 | [diff] [blame] | 863 | else if ('1' == tmp_str[j]) VG_(clo_profyle_flags) |= (1 << (7-j)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 864 | else { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 865 | VG_(fmsg_bad_option)(arg, |
| 866 | "--profile-flags argument can only contain 0s and 1s\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
sewardj | 17c5e2e | 2012-12-28 09:12:14 +0000 | [diff] [blame] | 869 | VG_(clo_profyle_sbs) = True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 870 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 871 | |
sewardj | 17c5e2e | 2012-12-28 09:12:14 +0000 | [diff] [blame] | 872 | else if VG_INT_CLO (arg, "--profile-interval", |
| 873 | VG_(clo_profyle_interval)) {} |
florian | 29e022d | 2012-07-02 21:13:34 +0000 | [diff] [blame] | 874 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 875 | 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) {} |
sewardj | d153fae | 2005-01-10 17:24:47 +0000 | [diff] [blame] | 881 | |
sewardj | 49984ea | 2013-10-18 13:21:26 +0000 | [diff] [blame] | 882 | 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 | |
sewardj | 67f7c03 | 2014-10-23 19:48:01 +0000 | [diff] [blame] | 887 | 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 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 894 | else if ( ! VG_(needs).command_line_options |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 895 | || ! VG_TDICT_CALL(tool_process_cmd_line_option, arg) ) { |
florian | 5b99e66 | 2014-11-29 14:41:32 +0000 | [diff] [blame] | 896 | VG_(fmsg_unknown_option)(arg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 897 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 898 | } |
| 899 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 900 | /* END command-line processing loop */ |
| 901 | |
florian | b9749a5 | 2015-07-24 11:50:12 +0000 | [diff] [blame] | 902 | /* Notify about deprecated features here. */ |
florian | 882038d | 2014-09-01 06:37:07 +0000 | [diff] [blame] | 903 | |
florian | b985e2d | 2011-09-29 03:03:45 +0000 | [diff] [blame] | 904 | /* Determine the path prefix for vgdb */ |
| 905 | if (VG_(clo_vgdb_prefix) == NULL) |
| 906 | VG_(clo_vgdb_prefix) = VG_(vgdb_prefix_default)(); |
| 907 | |
sewardj | 998d40d | 2004-12-06 14:24:52 +0000 | [diff] [blame] | 908 | /* 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 */ |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 919 | |
njn | f9ebf67 | 2003-05-12 21:41:30 +0000 | [diff] [blame] | 920 | if (VG_(clo_verbosity) < 0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 921 | VG_(clo_verbosity) = 0; |
| 922 | |
sewardj | c30cd9b | 2012-12-06 18:08:54 +0000 | [diff] [blame] | 923 | if (!sigill_diag_set) |
| 924 | VG_(clo_sigill_diag) = (VG_(clo_verbosity) > 0); |
| 925 | |
florian | 29e022d | 2012-07-02 21:13:34 +0000 | [diff] [blame] | 926 | 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 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 944 | VG_(dyn_vgdb_error) = VG_(clo_vgdb_error); |
| 945 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 946 | if (VG_(clo_gen_suppressions) > 0 && |
| 947 | !VG_(needs).core_errors && !VG_(needs).tool_errors) { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 948 | 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); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 951 | } |
| 952 | |
sewardj | 67f7c03 | 2014-10-23 19:48:01 +0000 | [diff] [blame] | 953 | # 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 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 961 | /* 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; |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 965 | VG_(fmsg_bad_option)("--xml=yes", |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 966 | "%s does not support XML output.\n", VG_(details).name); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 967 | /*NOTREACHED*/ |
| 968 | } |
| 969 | |
| 970 | vg_assert( VG_(clo_gen_suppressions) >= 0 ); |
| 971 | vg_assert( VG_(clo_gen_suppressions) <= 2 ); |
| 972 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 973 | /* If we've been asked to emit XML, mash around various other |
| 974 | options so as to constrain the output somewhat, and to remove |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 975 | any need for user input during the run. |
| 976 | */ |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 977 | if (VG_(clo_xml)) { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 978 | |
| 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) { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 987 | 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 " |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 991 | "--gen-suppressions=yes.\n"); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 992 | } |
| 993 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 994 | /* 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) { |
florian | 5b99e66 | 2014-11-29 14:41:32 +0000 | [diff] [blame] | 998 | VG_(fmsg_bad_option)("--xml=yes", |
| 999 | "Cannot be used together with --dump-error"); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1002 | /* Disable error limits (this might be a bad idea!) */ |
| 1003 | VG_(clo_error_limit) = False; |
| 1004 | /* Disable emulation warnings */ |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1005 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1006 | /* 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 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1011 | /* 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 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1018 | 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. */ |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1023 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1024 | 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 --- */ |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1033 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1034 | switch (log_to) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1035 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1036 | case VgLogTo_Fd: |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1037 | vg_assert(log_fsname_unexpanded == NULL); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1038 | break; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1039 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1040 | case VgLogTo_File: { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1041 | HChar* logfilename; |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1042 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1043 | vg_assert(log_fsname_unexpanded != NULL); |
| 1044 | vg_assert(VG_(strlen)(log_fsname_unexpanded) <= 900); /* paranoia */ |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1045 | |
njn | 374a36d | 2007-11-23 01:41:32 +0000 | [diff] [blame] | 1046 | // Nb: we overwrite an existing file of this name without asking |
| 1047 | // any questions. |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1048 | logfilename = VG_(expand_file_name)("--log-file", |
| 1049 | log_fsname_unexpanded); |
njn | 374a36d | 2007-11-23 01:41:32 +0000 | [diff] [blame] | 1050 | sres = VG_(open)(logfilename, |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 1051 | VKI_O_CREAT|VKI_O_WRONLY|VKI_O_TRUNC, |
florian | 204197a | 2015-01-07 14:47:15 +0000 | [diff] [blame] | 1052 | VKI_S_IRUSR|VKI_S_IWUSR|VKI_S_IRGRP|VKI_S_IROTH); |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1053 | if (!sr_isError(sres)) { |
| 1054 | tmp_log_fd = sr_Res(sres); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1055 | VG_(clo_log_fname_expanded) = logfilename; |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1056 | } else { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 1057 | VG_(fmsg)("can't create log file '%s': %s\n", |
| 1058 | logfilename, VG_(strerror)(sr_Err(sres))); |
| 1059 | VG_(exit)(1); |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1060 | /*NOTREACHED*/ |
njn | 374a36d | 2007-11-23 01:41:32 +0000 | [diff] [blame] | 1061 | } |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1062 | break; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | case VgLogTo_Socket: { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1066 | 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 ); |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 1069 | if (tmp_log_fd == -1) { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 1070 | VG_(fmsg)("Invalid --log-socket spec of '%s'\n", |
| 1071 | log_fsname_unexpanded); |
| 1072 | VG_(exit)(1); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1073 | /*NOTREACHED*/ |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1074 | } |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 1075 | if (tmp_log_fd == -2) { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 1076 | VG_(umsg)("failed to connect to logging server '%s'.\n" |
| 1077 | "Log messages will sent to stderr instead.\n", |
| 1078 | log_fsname_unexpanded ); |
| 1079 | |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1080 | /* We don't change anything here. */ |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1081 | vg_assert(VG_(log_output_sink).fd == 2); |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 1082 | tmp_log_fd = 2; |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1083 | } else { |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 1084 | vg_assert(tmp_log_fd > 0); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1085 | VG_(log_output_sink).is_socket = True; |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1086 | } |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1087 | break; |
| 1088 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1091 | /* --- set up the XML output channel --- */ |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1092 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1093 | switch (xml_to) { |
| 1094 | |
| 1095 | case VgLogTo_Fd: |
| 1096 | vg_assert(xml_fsname_unexpanded == NULL); |
| 1097 | break; |
| 1098 | |
| 1099 | case VgLogTo_File: { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1100 | HChar* xmlfilename; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1101 | |
| 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, |
florian | 204197a | 2015-01-07 14:47:15 +0000 | [diff] [blame] | 1111 | VKI_S_IRUSR|VKI_S_IWUSR|VKI_S_IRGRP|VKI_S_IROTH); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1112 | if (!sr_isError(sres)) { |
| 1113 | tmp_xml_fd = sr_Res(sres); |
| 1114 | VG_(clo_xml_fname_expanded) = xmlfilename; |
florian | 801c199 | 2014-10-17 21:07:37 +0000 | [diff] [blame] | 1115 | *xml_fname_unexpanded = xml_fsname_unexpanded; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1116 | } else { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 1117 | VG_(fmsg)("can't create XML file '%s': %s\n", |
| 1118 | xmlfilename, VG_(strerror)(sr_Err(sres))); |
| 1119 | VG_(exit)(1); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1120 | /*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) { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 1130 | VG_(fmsg)("Invalid --xml-socket spec of '%s'\n", |
| 1131 | xml_fsname_unexpanded ); |
| 1132 | VG_(exit)(1); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1133 | /*NOTREACHED*/ |
| 1134 | } |
| 1135 | if (tmp_xml_fd == -2) { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 1136 | VG_(umsg)("failed to connect to XML logging server '%s'.\n" |
| 1137 | "XML output will sent to stderr instead.\n", |
| 1138 | xml_fsname_unexpanded); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1139 | /* 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 | } |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1150 | /* 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) { |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 1156 | VG_(fmsg_bad_option)( |
| 1157 | "--xml=yes, but no XML destination specified", |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1158 | "--xml=yes has been specified, but there is no XML output\n" |
| 1159 | "destination. You must specify an XML output destination\n" |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 1160 | "using --xml-fd, --xml-file or --xml-socket.\n" |
| 1161 | ); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | // Finalise the output fds: the log fd .. |
| 1165 | |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 1166 | if (tmp_log_fd >= 0) { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1167 | // Move log_fd into the safe range, so it doesn't conflict with |
| 1168 | // any app fds. |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 1169 | tmp_log_fd = VG_(fcntl)(tmp_log_fd, VKI_F_DUPFD, VG_(fd_hard_limit)); |
| 1170 | if (tmp_log_fd < 0) { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1171 | 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; |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 1175 | } else { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1176 | VG_(log_output_sink).fd = tmp_log_fd; |
| 1177 | VG_(fcntl)(VG_(log_output_sink).fd, VKI_F_SETFD, VKI_FD_CLOEXEC); |
njn | da033f5 | 2005-12-19 21:27:58 +0000 | [diff] [blame] | 1178 | } |
| 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. |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1182 | VG_(log_output_sink).fd = -1; |
| 1183 | VG_(log_output_sink).is_socket = False; |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1186 | // 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 | |
bart | 2c68e3e | 2014-06-22 10:11:59 +0000 | [diff] [blame] | 1210 | if (VG_(clo_default_supp) && |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1211 | (VG_(needs).core_errors || VG_(needs).tool_errors)) { |
| 1212 | /* If we haven't reached the max number of suppressions, load |
| 1213 | the default one. */ |
florian | e6a4ed1 | 2012-10-21 02:30:18 +0000 | [diff] [blame] | 1214 | static const HChar default_supp[] = "default.supp"; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1215 | Int len = VG_(strlen)(VG_(libdir)) + 1 + sizeof(default_supp); |
florian | 77eb20b | 2014-09-11 21:19:17 +0000 | [diff] [blame] | 1216 | HChar *buf = VG_(malloc)("main.mpclo.3", len); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1217 | VG_(sprintf)(buf, "%s/%s", VG_(libdir), default_supp); |
florian | 7931627 | 2014-10-07 18:36:28 +0000 | [diff] [blame] | 1218 | VG_(addToXA)(VG_(clo_suppressions), &buf); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1219 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1220 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1221 | *logging_to_fd = log_to == VgLogTo_Fd || log_to == VgLogTo_Socket; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
sewardj | 4efbaa7 | 2008-06-04 06:51:58 +0000 | [diff] [blame] | 1224 | // Write the name and value of log file qualifiers to the xml file. |
florian | 801c199 | 2014-10-17 21:07:37 +0000 | [diff] [blame] | 1225 | // 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. |
| 1228 | static void print_file_vars(const HChar* format) |
sewardj | 4efbaa7 | 2008-06-04 06:51:58 +0000 | [diff] [blame] | 1229 | { |
| 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. |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1240 | HChar* qual; |
florian | 801c199 | 2014-10-17 21:07:37 +0000 | [diff] [blame] | 1241 | Int begin_qualname = ++i; |
sewardj | 4efbaa7 | 2008-06-04 06:51:58 +0000 | [diff] [blame] | 1242 | while (True) { |
| 1243 | if ('}' == format[i]) { |
florian | 801c199 | 2014-10-17 21:07:37 +0000 | [diff] [blame] | 1244 | 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'; |
sewardj | 4efbaa7 | 2008-06-04 06:51:58 +0000 | [diff] [blame] | 1249 | qual = VG_(getenv)(qualname); |
florian | 801c199 | 2014-10-17 21:07:37 +0000 | [diff] [blame] | 1250 | i++; |
| 1251 | VG_(printf_xml)("<logfilequalifier> <var>%pS</var> " |
| 1252 | "<value>%pS</value> </logfilequalifier>\n", |
| 1253 | qualname, qual); |
sewardj | 4efbaa7 | 2008-06-04 06:51:58 +0000 | [diff] [blame] | 1254 | break; |
| 1255 | } |
| 1256 | i++; |
| 1257 | } |
sewardj | 4efbaa7 | 2008-06-04 06:51:58 +0000 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | } else { |
| 1261 | i++; |
| 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1266 | |
| 1267 | /*====================================================================*/ |
| 1268 | /*=== Printing the preamble ===*/ |
| 1269 | /*====================================================================*/ |
| 1270 | |
bart | a92677a | 2011-10-22 08:24:32 +0000 | [diff] [blame] | 1271 | // Print the argument, escaping any chars that require it. |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1272 | static void umsg_arg(const HChar* arg) |
njn | f8a11cf | 2009-08-02 23:03:06 +0000 | [diff] [blame] | 1273 | { |
| 1274 | SizeT len = VG_(strlen)(arg); |
florian | e6a4ed1 | 2012-10-21 02:30:18 +0000 | [diff] [blame] | 1275 | const HChar* special = " \\<>"; |
njn | f8a11cf | 2009-08-02 23:03:06 +0000 | [diff] [blame] | 1276 | Int i; |
| 1277 | for (i = 0; i < len; i++) { |
| 1278 | if (VG_(strchr)(special, arg[i])) { |
bart | a92677a | 2011-10-22 08:24:32 +0000 | [diff] [blame] | 1279 | VG_(umsg)("\\"); // escape with a backslash if necessary |
njn | f8a11cf | 2009-08-02 23:03:06 +0000 | [diff] [blame] | 1280 | } |
bart | a92677a | 2011-10-22 08:24:32 +0000 | [diff] [blame] | 1281 | VG_(umsg)("%c", arg[i]); |
njn | f8a11cf | 2009-08-02 23:03:06 +0000 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | |
bart | a92677a | 2011-10-22 08:24:32 +0000 | [diff] [blame] | 1285 | // Send output to the XML-stream and escape any XML meta-characters. |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1286 | static void xml_arg(const HChar* arg) |
bart | a92677a | 2011-10-22 08:24:32 +0000 | [diff] [blame] | 1287 | { |
| 1288 | VG_(printf_xml)("%pS", arg); |
| 1289 | } |
| 1290 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1291 | /* 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. */ |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1295 | static void print_preamble ( Bool logging_to_fd, |
florian | 801c199 | 2014-10-17 21:07:37 +0000 | [diff] [blame] | 1296 | const HChar* xml_fname_unexpanded, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1297 | const HChar* toolname ) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1298 | { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1299 | Int i; |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 1300 | const HChar* xpre = VG_(clo_xml) ? " <line>" : ""; |
| 1301 | const HChar* xpost = VG_(clo_xml) ? "</line>" : ""; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1302 | UInt (*umsg_or_xml)( const HChar*, ... ) |
| 1303 | = VG_(clo_xml) ? VG_(printf_xml) : VG_(umsg); |
tom | 60a4b0b | 2005-10-12 10:45:27 +0000 | [diff] [blame] | 1304 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1305 | void (*umsg_or_xml_arg)( const HChar* ) |
bart | a92677a | 2011-10-22 08:24:32 +0000 | [diff] [blame] | 1306 | = VG_(clo_xml) ? xml_arg : umsg_arg; |
| 1307 | |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1308 | vg_assert( VG_(args_for_client) ); |
| 1309 | vg_assert( VG_(args_for_valgrind) ); |
sewardj | 99a2ceb | 2007-11-09 12:30:36 +0000 | [diff] [blame] | 1310 | vg_assert( toolname ); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1311 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1312 | if (VG_(clo_xml)) { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1313 | 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"); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1322 | if (VG_(clo_xml) || VG_(clo_verbosity) > 0) { |
sewardj | d7bddad | 2005-06-13 16:48:32 +0000 | [diff] [blame] | 1323 | |
| 1324 | if (VG_(clo_xml)) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1325 | VG_(printf_xml)("<preamble>\n"); |
sewardj | d7bddad | 2005-06-13 16:48:32 +0000 | [diff] [blame] | 1326 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 1327 | /* Tool details */ |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 1328 | umsg_or_xml( VG_(clo_xml) ? "%s%pS%pS%pS, %pS%s\n" : "%s%s%s%s, %s%s\n", |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1329 | xpre, |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1330 | VG_(details).name, |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 1331 | NULL == VG_(details).version ? "" : "-", |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1332 | NULL == VG_(details).version |
florian | e6a4ed1 | 2012-10-21 02:30:18 +0000 | [diff] [blame] | 1333 | ? "" : VG_(details).version, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1334 | VG_(details).description, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1335 | xpost ); |
sewardj | 99a2ceb | 2007-11-09 12:30:36 +0000 | [diff] [blame] | 1336 | |
njn | 10b9aea | 2009-07-14 06:55:05 +0000 | [diff] [blame] | 1337 | if (VG_(strlen)(toolname) >= 4 && VG_STREQN(4, toolname, "exp-")) { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1338 | umsg_or_xml( |
njn | b6267bd | 2009-08-12 00:14:16 +0000 | [diff] [blame] | 1339 | "%sNOTE: This is an Experimental-Class Valgrind Tool%s\n", |
sewardj | 99a2ceb | 2007-11-09 12:30:36 +0000 | [diff] [blame] | 1340 | xpre, xpost |
| 1341 | ); |
| 1342 | } |
| 1343 | |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 1344 | umsg_or_xml( VG_(clo_xml) ? "%s%pS%s\n" : "%s%s%s\n", |
sewardj | 743a208 | 2010-07-23 17:03:22 +0000 | [diff] [blame] | 1345 | xpre, VG_(details).copyright_author, xpost ); |
sewardj | 3b2736a | 2002-03-24 12:18:35 +0000 | [diff] [blame] | 1346 | |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1347 | /* Core details */ |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1348 | umsg_or_xml( |
njn | f73d87f | 2009-07-24 04:47:04 +0000 | [diff] [blame] | 1349 | "%sUsing Valgrind-%s and LibVEX; rerun with -h for copyright info%s\n", |
| 1350 | xpre, VERSION, xpost |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1351 | ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1352 | |
njn | f3977a3 | 2009-08-04 00:27:56 +0000 | [diff] [blame] | 1353 | // 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); |
florian | b16609b | 2014-08-20 21:04:14 +0000 | [diff] [blame] | 1358 | umsg_or_xml_arg(VG_(args_the_exename)); |
bart | a92677a | 2011-10-22 08:24:32 +0000 | [diff] [blame] | 1359 | |
njn | 53162bf | 2009-07-29 23:34:49 +0000 | [diff] [blame] | 1360 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) { |
| 1361 | HChar* s = *(HChar**)VG_(indexXA)( VG_(args_for_client), i ); |
njn | f8a11cf | 2009-08-02 23:03:06 +0000 | [diff] [blame] | 1362 | umsg_or_xml(" "); |
bart | a92677a | 2011-10-22 08:24:32 +0000 | [diff] [blame] | 1363 | umsg_or_xml_arg(s); |
njn | 53162bf | 2009-07-29 23:34:49 +0000 | [diff] [blame] | 1364 | } |
njn | f3977a3 | 2009-08-04 00:27:56 +0000 | [diff] [blame] | 1365 | umsg_or_xml("%s\n", xpost); |
njn | 53162bf | 2009-07-29 23:34:49 +0000 | [diff] [blame] | 1366 | |
sewardj | d7bddad | 2005-06-13 16:48:32 +0000 | [diff] [blame] | 1367 | if (VG_(clo_xml)) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1368 | VG_(printf_xml)("</preamble>\n"); |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
njn | b6267bd | 2009-08-12 00:14:16 +0000 | [diff] [blame] | 1371 | // Print the parent PID, and other stuff, if necessary. |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1372 | if (!VG_(clo_xml) && VG_(clo_verbosity) > 0 && !logging_to_fd) { |
njn | 305dc00 | 2009-07-30 23:36:43 +0000 | [diff] [blame] | 1373 | VG_(umsg)("Parent PID: %d\n", VG_(getppid)()); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1374 | } |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1375 | else |
| 1376 | if (VG_(clo_xml)) { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1377 | VG_(printf_xml)("\n"); |
| 1378 | VG_(printf_xml)("<pid>%d</pid>\n", VG_(getpid)()); |
| 1379 | VG_(printf_xml)("<ppid>%d</ppid>\n", VG_(getppid)()); |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 1380 | VG_(printf_xml)("<tool>%pS</tool>\n", toolname); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1381 | if (xml_fname_unexpanded) |
| 1382 | print_file_vars(xml_fname_unexpanded); |
sewardj | 768db0e | 2005-07-19 14:18:56 +0000 | [diff] [blame] | 1383 | 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 |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 1386 | (%pS). */ |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1387 | VG_(printf_xml)("<usercomment>%s</usercomment>\n", |
| 1388 | VG_(clo_xml_user_comment)); |
sewardj | 768db0e | 2005-07-19 14:18:56 +0000 | [diff] [blame] | 1389 | } |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1390 | VG_(printf_xml)("\n"); |
| 1391 | VG_(printf_xml)("<args>\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1392 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1393 | VG_(printf_xml)(" <vargv>\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1394 | if (VG_(name_of_launcher)) |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 1395 | VG_(printf_xml)(" <exe>%pS</exe>\n", |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1396 | VG_(name_of_launcher)); |
sewardj | 125fd4f | 2007-03-08 19:56:14 +0000 | [diff] [blame] | 1397 | else |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 1398 | VG_(printf_xml)(" <exe>%pS</exe>\n", |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 1399 | "(launcher name unknown)"); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1400 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) { |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 1401 | VG_(printf_xml)( |
| 1402 | " <arg>%pS</arg>\n", |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1403 | * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i ) |
| 1404 | ); |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 1405 | } |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1406 | VG_(printf_xml)(" </vargv>\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1407 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1408 | VG_(printf_xml)(" <argv>\n"); |
florian | b16609b | 2014-08-20 21:04:14 +0000 | [diff] [blame] | 1409 | VG_(printf_xml)(" <exe>%pS</exe>\n", |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1410 | VG_(args_the_exename)); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1411 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) { |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 1412 | VG_(printf_xml)( |
| 1413 | " <arg>%pS</arg>\n", |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1414 | * (HChar**) VG_(indexXA)( VG_(args_for_client), i ) |
| 1415 | ); |
sewardj | 8665d8e | 2005-06-01 17:35:23 +0000 | [diff] [blame] | 1416 | } |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1417 | VG_(printf_xml)(" </argv>\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1418 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1419 | VG_(printf_xml)("</args>\n"); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1420 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1421 | |
njn | b6267bd | 2009-08-12 00:14:16 +0000 | [diff] [blame] | 1422 | // Last thing in the preamble is a blank line. |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1423 | if (VG_(clo_xml)) |
| 1424 | VG_(printf_xml)("\n"); |
njn | b6267bd | 2009-08-12 00:14:16 +0000 | [diff] [blame] | 1425 | else if (VG_(clo_verbosity) > 0) |
| 1426 | VG_(umsg)("\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1427 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1428 | if (VG_(clo_verbosity) > 1) { |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1429 | # if defined(VGO_linux) |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1430 | SysRes fd; |
rhyskidd | 05be8f5 | 2015-04-01 12:15:49 +0000 | [diff] [blame] | 1431 | # endif |
sewardj | 1f0bbc7 | 2005-11-16 03:51:02 +0000 | [diff] [blame] | 1432 | VexArch vex_arch; |
| 1433 | VexArchInfo vex_archinfo; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1434 | if (!logging_to_fd) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1435 | VG_(message)(Vg_DebugMsg, "\n"); |
njn | a331164 | 2009-08-10 01:29:14 +0000 | [diff] [blame] | 1436 | VG_(message)(Vg_DebugMsg, "Valgrind options:\n"); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1437 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_valgrind) ); i++) { |
| 1438 | VG_(message)(Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1439 | " %s\n", |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1440 | * (HChar**) VG_(indexXA)( VG_(args_for_valgrind), i )); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1441 | } |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1442 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1443 | # if defined(VGO_linux) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1444 | VG_(message)(Vg_DebugMsg, "Contents of /proc/version:\n"); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1445 | fd = VG_(open) ( "/proc/version", VKI_O_RDONLY, 0 ); |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1446 | if (sr_isError(fd)) { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1447 | VG_(message)(Vg_DebugMsg, " can't open /proc/version\n"); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1448 | } else { |
florian | 3b860fc | 2014-09-28 09:08:59 +0000 | [diff] [blame] | 1449 | 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); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1464 | } |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1465 | # elif defined(VGO_darwin) |
rhyskidd | 05be8f5 | 2015-04-01 12:15:49 +0000 | [diff] [blame] | 1466 | 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 ); |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1476 | # 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 | } |
rhyskidd | 05be8f5 | 2015-04-01 12:15:49 +0000 | [diff] [blame] | 1490 | # endif |
sewardj | 1f0bbc7 | 2005-11-16 03:51:02 +0000 | [diff] [blame] | 1491 | |
| 1492 | VG_(machine_get_VexArchInfo)( &vex_arch, &vex_archinfo ); |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 1493 | VG_(message)( |
| 1494 | Vg_DebugMsg, |
sewardj | 5973142 | 2014-07-24 12:45:24 +0000 | [diff] [blame] | 1495 | "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 ) |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 1499 | ); |
sewardj | e66f2e0 | 2006-12-30 17:45:08 +0000 | [diff] [blame] | 1500 | VG_(message)( |
| 1501 | Vg_DebugMsg, |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1502 | "Page sizes: currently %d, max supported %d\n", |
sewardj | e66f2e0 | 2006-12-30 17:45:08 +0000 | [diff] [blame] | 1503 | (Int)VKI_PAGE_SIZE, (Int)VKI_MAX_PAGE_SIZE |
| 1504 | ); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1505 | VG_(message)(Vg_DebugMsg, |
| 1506 | "Valgrind library directory: %s\n", VG_(libdir)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1507 | } |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1510 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1511 | /*====================================================================*/ |
| 1512 | /*=== File descriptor setup ===*/ |
| 1513 | /*====================================================================*/ |
| 1514 | |
sewardj | 5f229e2 | 2005-09-28 01:36:01 +0000 | [diff] [blame] | 1515 | /* Number of file descriptors that Valgrind tries to reserve for |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1516 | its own use - just a small constant. */ |
| 1517 | #define N_RESERVED_FDS (12) |
sewardj | 5f229e2 | 2005-09-28 01:36:01 +0000 | [diff] [blame] | 1518 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1519 | static void setup_file_descriptors(void) |
| 1520 | { |
| 1521 | struct vki_rlimit rl; |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1522 | Bool show = False; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1523 | |
| 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 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1530 | # 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 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1538 | if (show) |
florian | a5e06c3 | 2015-08-05 21:16:09 +0000 | [diff] [blame] | 1539 | VG_(printf)("fd limits: host, before: cur %llu max %llu\n", |
| 1540 | (ULong)rl.rlim_cur, (ULong)rl.rlim_max); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1541 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1542 | /* Work out where to move the soft limit to. */ |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 1543 | if (rl.rlim_cur + N_RESERVED_FDS <= rl.rlim_max) { |
| 1544 | rl.rlim_cur = rl.rlim_cur + N_RESERVED_FDS; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1545 | } else { |
| 1546 | rl.rlim_cur = rl.rlim_max; |
| 1547 | } |
| 1548 | |
| 1549 | /* Reserve some file descriptors for our use. */ |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 1550 | VG_(fd_soft_limit) = rl.rlim_cur - N_RESERVED_FDS; |
| 1551 | VG_(fd_hard_limit) = rl.rlim_cur - N_RESERVED_FDS; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1552 | |
| 1553 | /* Update the soft limit. */ |
| 1554 | VG_(setrlimit)(VKI_RLIMIT_NOFILE, &rl); |
| 1555 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1556 | if (show) { |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 1557 | VG_(printf)("fd limits: host, after: cur %lu max %lu\n", |
sewardj | a8ffda6 | 2008-07-18 18:23:24 +0000 | [diff] [blame] | 1558 | (UWord)rl.rlim_cur, (UWord)rl.rlim_max); |
florian | a5e06c3 | 2015-08-05 21:16:09 +0000 | [diff] [blame] | 1559 | VG_(printf)("fd limits: guest : cur %d max %d\n", |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1560 | VG_(fd_soft_limit), VG_(fd_hard_limit)); |
| 1561 | } |
| 1562 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1563 | if (VG_(cl_exec_fd) != -1) |
| 1564 | VG_(cl_exec_fd) = VG_(safe_fd)( VG_(cl_exec_fd) ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1567 | |
njn | 2da7335 | 2005-06-18 01:35:16 +0000 | [diff] [blame] | 1568 | /*====================================================================*/ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1569 | /*=== main() ===*/ |
| 1570 | /*====================================================================*/ |
| 1571 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1572 | /* 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 | |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 1580 | /*static*/ struct { |
| 1581 | HChar bytes [VG_STACK_GUARD_SZB + VG_DEFAULT_STACK_ACTIVE_SZB + VG_STACK_GUARD_SZB]; |
| 1582 | } VG_(interim_stack); |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1583 | |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1584 | /* 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 |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1594 | 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 |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1596 | 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. */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1599 | |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1600 | static IICreateImageInfo the_iicii; |
| 1601 | static IIFinaliseImageInfo the_iifii; |
| 1602 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1603 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1604 | /* A simple pair structure, used for conveying debuginfo handles to |
| 1605 | calls to VG_TRACK(new_mem_startup, ...). */ |
| 1606 | typedef struct { Addr a; ULong ull; } Addr_n_ULong; |
| 1607 | |
| 1608 | |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 1609 | /* --- Forwards decls to do with shutdown --- */ |
| 1610 | |
| 1611 | static void final_tidyup(ThreadId tid); |
| 1612 | |
| 1613 | /* Do everything which needs doing when the last thread exits */ |
| 1614 | static |
| 1615 | void shutdown_actions_NORETURN( ThreadId tid, |
| 1616 | VgSchedReturnCode tids_schedretcode ); |
| 1617 | |
| 1618 | /* --- end of Forwards decls to do with shutdown --- */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1619 | |
| 1620 | |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1621 | /* 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 | */ |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1625 | static |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1626 | Int valgrind_main ( Int argc, HChar **argv, HChar **envp ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1627 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1628 | const HChar* toolname = "memcheck"; // default to Memcheck |
sewardj | 13247ca | 2005-12-30 22:52:20 +0000 | [diff] [blame] | 1629 | Int need_help = 0; // 0 = no, 1 = --help, 2 = --help-debug |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 1630 | ThreadId tid_main = VG_INVALID_THREADID; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 1631 | Bool logging_to_fd = False; |
florian | 801c199 | 2014-10-17 21:07:37 +0000 | [diff] [blame] | 1632 | const HChar* xml_fname_unexpanded = NULL; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1633 | Int loglevel, i; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1634 | XArray* addr2dihandle = NULL; |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1635 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1636 | //============================================================ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1637 | // |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1638 | // Nb: startup is complex. Prerequisites are shown at every step. |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1639 | // *** Be very careful when messing with the order *** |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1640 | // |
| 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 | // |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1645 | //============================================================ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1646 | |
| 1647 | /* This is needed to make VG_(getenv) usable early. */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1648 | VG_(client_envp) = (HChar**)envp; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1649 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1650 | //-------------------------------------------------------------- |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1651 | // Start up Mach kernel interface, if any |
| 1652 | // p: none |
| 1653 | //-------------------------------------------------------------- |
| 1654 | # if defined(VGO_darwin) |
| 1655 | VG_(mach_init)(); |
| 1656 | # endif |
| 1657 | |
| 1658 | //-------------------------------------------------------------- |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1659 | // Start up the logging mechanism |
| 1660 | // p: none |
| 1661 | //-------------------------------------------------------------- |
| 1662 | /* Start the debugging-log system ASAP. First find out how many |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 1663 | "-d"s were specified. This is a pre-scan of the command line. Also |
philippe | e4d7812 | 2014-04-20 14:20:37 +0000 | [diff] [blame] | 1664 | 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. */ |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1667 | loglevel = 0; |
| 1668 | for (i = 1; i < argc; i++) { |
philippe | e4d7812 | 2014-04-20 14:20:37 +0000 | [diff] [blame] | 1669 | const HChar* tmp_str; |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 1670 | 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)) {} |
philippe | d99c26a | 2012-07-31 22:17:28 +0000 | [diff] [blame] | 1674 | 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) {} |
philippe | e4d7812 | 2014-04-20 14:20:37 +0000 | [diff] [blame] | 1678 | if VG_STR_CLO(argv[i], "--aspace-minaddr", tmp_str) { |
philippe | e4d7812 | 2014-04-20 14:20:37 +0000 | [diff] [blame] | 1679 | Bool ok = VG_(parse_Addr) (&tmp_str, &VG_(clo_aspacem_minAddr)); |
| 1680 | if (!ok) |
| 1681 | VG_(fmsg_bad_option)(argv[i], "Invalid address\n"); |
florian | 82e7a54 | 2015-03-26 21:55:00 +0000 | [diff] [blame] | 1682 | 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); |
philippe | e4d7812 | 2014-04-20 14:20:37 +0000 | [diff] [blame] | 1686 | } |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | /* ... and start the debug logger. Now we can safely emit logging |
| 1690 | messages all through startup. */ |
sewardj | 1075931 | 2005-05-30 23:52:47 +0000 | [diff] [blame] | 1691 | VG_(debugLog_startup)(loglevel, "Stage 2 (main)"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1692 | 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"); |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1700 | { HChar* limLo = (HChar*)(&VG_(interim_stack).bytes[0]); |
| 1701 | HChar* limHi = limLo + sizeof(VG_(interim_stack)); |
sewardj | ed8b0f6 | 2012-09-02 21:17:36 +0000 | [diff] [blame] | 1702 | 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). */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1720 | 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"); |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1737 | if (the_iicii.sp_at_startup == 0) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1738 | 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 |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 1747 | // p: logging, plausible-stack |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1748 | //-------------------------------------------------------------- |
| 1749 | VG_(debugLog)(1, "main", "Starting the address space manager\n"); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1750 | 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); |
sewardj | e66f2e0 | 2006-12-30 17:45:08 +0000 | [diff] [blame] | 1756 | 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)); |
philippe | 38a74d2 | 2014-08-29 22:53:19 +0000 | [diff] [blame] | 1759 | the_iicii.clstack_end = VG_(am_startup)( the_iicii.sp_at_startup ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1760 | VG_(debugLog)(1, "main", "Address space manager is running\n"); |
| 1761 | |
| 1762 | //-------------------------------------------------------------- |
| 1763 | // Start up the dynamic memory manager |
| 1764 | // p: address space management |
philippe | d99c26a | 2012-07-31 22:17:28 +0000 | [diff] [blame] | 1765 | // p: getting --profile-heap,--core-redzone-size,--redzone-size |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1766 | // 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"); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1771 | { void* p = VG_(malloc)( "main.vm.1", 12345 ); |
florian | f5d8e65 | 2014-09-11 22:15:39 +0000 | [diff] [blame] | 1772 | VG_(free)( p ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1773 | } |
| 1774 | VG_(debugLog)(1, "main", "Dynamic memory manager is running\n"); |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1775 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1776 | //============================================================ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1777 | // |
| 1778 | // Dynamic memory management is now available. |
| 1779 | // |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1780 | //============================================================ |
| 1781 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1782 | //-------------------------------------------------------------- |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1783 | // Initialise m_debuginfo |
| 1784 | // p: dynamic memory allocation |
| 1785 | VG_(debugLog)(1, "main", "Initialise m_debuginfo\n"); |
| 1786 | VG_(di_initialise)(); |
| 1787 | |
| 1788 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1789 | // Look for alternative libdir |
| 1790 | { HChar *cp = VG_(getenv)(VALGRIND_LIB); |
| 1791 | if (cp != NULL) |
| 1792 | VG_(libdir) = cp; |
njn | cde90d3 | 2009-07-22 22:41:38 +0000 | [diff] [blame] | 1793 | VG_(debugLog)(1, "main", "VG_(libdir) = %s\n", VG_(libdir)); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | //-------------------------------------------------------------- |
| 1797 | // Extract the launcher name from the environment. |
njn | a842d79 | 2009-05-21 01:15:18 +0000 | [diff] [blame] | 1798 | VG_(debugLog)(1, "main", "Getting launcher's name ...\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1799 | 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 | } |
njn | a842d79 | 2009-05-21 01:15:18 +0000 | [diff] [blame] | 1805 | VG_(debugLog)(1, "main", "... %s\n", VG_(name_of_launcher)); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1806 | |
| 1807 | //-------------------------------------------------------------- |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1808 | // 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. |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 1816 | VG_(getrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data)); |
thughes | c37184f | 2004-09-11 14:16:57 +0000 | [diff] [blame] | 1817 | |
| 1818 | // Get the current process stack rlimit. |
| 1819 | VG_(getrlimit)(VKI_RLIMIT_STACK, &VG_(client_rlimit_stack)); |
| 1820 | |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1821 | //-------------------------------------------------------------- |
| 1822 | // Figure out what sort of CPU we're on, and whether it is |
| 1823 | // able to run V. |
carll | 52b284b | 2015-04-09 16:23:20 +0000 | [diff] [blame] | 1824 | /* 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; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1828 | VG_(debugLog)(1, "main", "Get hardware capabilities ...\n"); |
| 1829 | { VexArch vex_arch; |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1830 | 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"); |
philippe | 3bcd51d | 2013-06-12 21:45:39 +0000 | [diff] [blame] | 1838 | VG_(printf)(" * ARM (armv7)\n"); |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1839 | VG_(printf)(" * PowerPC (most; ppc405 and above)\n"); |
florian | 92b33b5 | 2014-12-08 14:03:00 +0000 | [diff] [blame] | 1840 | VG_(printf)(" * System z (64bit only - s390x; z990 and above)\n"); |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1841 | VG_(printf)("\n"); |
| 1842 | VG_(exit)(1); |
| 1843 | } |
| 1844 | VG_(machine_get_VexArchInfo)( &vex_arch, &vex_archinfo ); |
sewardj | e3121f3 | 2006-01-27 21:23:23 +0000 | [diff] [blame] | 1845 | VG_(debugLog)( |
| 1846 | 1, "main", "... arch = %s, hwcaps = %s\n", |
| 1847 | LibVEX_ppVexArch ( vex_arch ), |
| 1848 | LibVEX_ppVexHwCaps ( vex_arch, vex_archinfo.hwcaps ) |
| 1849 | ); |
sewardj | e2d1e67 | 2005-11-12 23:10:48 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
sewardj | 198f34f | 2007-07-09 23:13:07 +0000 | [diff] [blame] | 1852 | //-------------------------------------------------------------- |
| 1853 | // Record the working directory at startup |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 1854 | // p: none |
sewardj | 198f34f | 2007-07-09 23:13:07 +0000 | [diff] [blame] | 1855 | VG_(debugLog)(1, "main", "Getting the working directory at startup\n"); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 1856 | VG_(record_startup_wd)(); |
| 1857 | const HChar *wd = VG_(get_startup_wd)(); |
| 1858 | VG_(debugLog)(1, "main", "... %s\n", wd != NULL ? wd : "<NO CWD>" ); |
sewardj | 198f34f | 2007-07-09 23:13:07 +0000 | [diff] [blame] | 1859 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1860 | //============================================================ |
| 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 ); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1875 | vg_assert( VG_(args_for_valgrind) ); |
| 1876 | vg_assert( VG_(args_for_client) ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1877 | if (0) { |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1878 | 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 | ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1883 | VG_(printf)(" exe %s\n", VG_(args_the_exename)); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1884 | 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 | ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1892 | // 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)] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1896 | //-------------------------------------------------------------- |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 1897 | VG_(debugLog)(1, "main", |
| 1898 | "(early_) Process Valgrind's command line options\n"); |
| 1899 | early_process_cmd_line_options(&need_help, &toolname); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1900 | |
sewardj | dcd1ed4 | 2014-09-01 22:32:52 +0000 | [diff] [blame] | 1901 | // BEGIN HACK |
| 1902 | vg_assert(toolname != NULL); |
| 1903 | vg_assert(VG_(clo_read_inline_info) == False); |
sewardj | 88f762e | 2014-09-07 19:53:41 +0000 | [diff] [blame] | 1904 | # if !defined(VGO_darwin) |
sewardj | dcd1ed4 | 2014-09-01 22:32:52 +0000 | [diff] [blame] | 1905 | if (0 == VG_(strcmp)(toolname, "memcheck") |
sewardj | 88f762e | 2014-09-07 19:53:41 +0000 | [diff] [blame] | 1906 | || 0 == VG_(strcmp)(toolname, "helgrind") |
| 1907 | || 0 == VG_(strcmp)(toolname, "drd")) { |
sewardj | dcd1ed4 | 2014-09-01 22:32:52 +0000 | [diff] [blame] | 1908 | /* 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 | } |
sewardj | 88f762e | 2014-09-07 19:53:41 +0000 | [diff] [blame] | 1914 | # endif |
sewardj | dcd1ed4 | 2014-09-01 22:32:52 +0000 | [diff] [blame] | 1915 | // END HACK |
| 1916 | |
sewardj | 8d47a61 | 2015-02-05 12:59:46 +0000 | [diff] [blame] | 1917 | // Set default vex control params. |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1918 | LibVEX_default_VexControl(& VG_(clo_vex_control)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1919 | |
| 1920 | //-------------------------------------------------------------- |
| 1921 | // Load client executable, finding in $PATH if necessary |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 1922 | // p: early_process_cmd_line_options() [for 'exec', 'need_help', |
| 1923 | // clo_max_stackframe, |
| 1924 | // clo_main_stacksize] |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 1925 | // p: layout_remaining_space [so there's space] |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1926 | // |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1927 | // Set up client's environment |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 1928 | // p: set-libdir [for VG_(libdir)] |
| 1929 | // p: early_process_cmd_line_options [for toolname] |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1930 | // |
nethercote | 5ee67ca | 2004-06-22 14:00:09 +0000 | [diff] [blame] | 1931 | // Setup client stack, eip, and VG_(client_arg[cv]) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1932 | // p: load_client() [for 'info'] |
| 1933 | // p: fix_environment() [for 'env'] |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1934 | // |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1935 | // 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)] |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1938 | // |
| 1939 | // p: _start_in_C (for zeroing out the_iicii and putting some |
| 1940 | // initial values into it) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1941 | //-------------------------------------------------------------- |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 1942 | if (!need_help) { |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1943 | VG_(debugLog)(1, "main", "Create initial image\n"); |
| 1944 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1945 | # if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_solaris) |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1946 | the_iicii.argv = argv; |
| 1947 | the_iicii.envp = envp; |
| 1948 | the_iicii.toolname = toolname; |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1949 | # else |
njn | a842d79 | 2009-05-21 01:15:18 +0000 | [diff] [blame] | 1950 | # error "Unknown platform" |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 1951 | # endif |
| 1952 | |
sewardj | dc2f79e | 2007-12-22 14:14:04 +0000 | [diff] [blame] | 1953 | /* NOTE: this call reads VG_(clo_main_stacksize). */ |
carll | 52b284b | 2015-04-09 16:23:20 +0000 | [diff] [blame] | 1954 | the_iifii = VG_(ii_create_image)( the_iicii, &vex_archinfo ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1955 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1956 | |
| 1957 | //============================================================== |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1958 | // |
| 1959 | // Finished loading/setting up the client address space. |
| 1960 | // |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1961 | //============================================================== |
| 1962 | |
| 1963 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1964 | // setup file descriptors |
| 1965 | // p: n/a |
| 1966 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 1967 | VG_(debugLog)(1, "main", "Setup file descriptors\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1968 | setup_file_descriptors(); |
| 1969 | |
| 1970 | //-------------------------------------------------------------- |
tom | 41ad7e7 | 2012-10-04 20:27:38 +0000 | [diff] [blame] | 1971 | // 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. |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1975 | // p: setup file descriptors |
tom | 41ad7e7 | 2012-10-04 20:27:38 +0000 | [diff] [blame] | 1976 | // p: ii_create_image for VG_(client_auxv) setup. |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 1977 | //-------------------------------------------------------------- |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 1978 | VG_(cl_cmdline_fd) = -1; |
tom | 41ad7e7 | 2012-10-04 20:27:38 +0000 | [diff] [blame] | 1979 | VG_(cl_auxv_fd) = -1; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1980 | #if defined(VGO_solaris) |
| 1981 | VG_(cl_psinfo_fd) = -1; |
| 1982 | #endif |
| 1983 | |
| 1984 | #if defined(VGO_linux) || defined(VGO_solaris) |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 1985 | if (!need_help) { |
florian | 87c8abb | 2014-11-09 16:15:23 +0000 | [diff] [blame] | 1986 | HChar buf[50]; // large enough |
| 1987 | HChar buf2[VG_(mkstemp_fullname_bufsz)(sizeof buf - 1)]; |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 1988 | Int fd, r; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1989 | |
iraisr | 23e68ca | 2015-08-14 20:50:11 +0000 | [diff] [blame] | 1990 | #if defined(VGO_linux) || defined(SOLARIS_PROC_CMDLINE) |
| 1991 | /* Fake /proc/<pid>/cmdline only on Linux and Solaris if supported. */ |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 1992 | HChar nul[1]; |
bart | a3054f5 | 2010-06-14 18:12:56 +0000 | [diff] [blame] | 1993 | const HChar* exename; |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 1994 | |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 1995 | VG_(debugLog)(1, "main", "Create fake /proc/<pid>/cmdline\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1996 | |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 1997 | VG_(sprintf)(buf, "proc_%d_cmdline", VG_(getpid)()); |
| 1998 | fd = VG_(mkstemp)( buf, buf2 ); |
| 1999 | if (fd == -1) |
florian | 1763e81 | 2011-07-12 19:07:05 +0000 | [diff] [blame] | 2000 | VG_(err_config_error)("Can't create client cmdline file in %s\n", buf2); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2001 | |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 2002 | nul[0] = 0; |
florian | b16609b | 2014-08-20 21:04:14 +0000 | [diff] [blame] | 2003 | exename = VG_(args_the_exename); |
sewardj | c7ffc94 | 2011-03-28 16:26:42 +0000 | [diff] [blame] | 2004 | VG_(write)(fd, exename, VG_(strlen)( exename )); |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 2005 | 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 )); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2010 | VG_(write)(fd, nul, 1); |
| 2011 | } |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 2012 | |
| 2013 | /* Don't bother to seek the file back to the start; instead do |
iraisr | 23e68ca | 2015-08-14 20:50:11 +0000 | [diff] [blame] | 2014 | 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. */ |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 2016 | |
| 2017 | /* Now delete it, but hang on to the fd. */ |
| 2018 | r = VG_(unlink)( buf2 ); |
| 2019 | if (r) |
florian | 1763e81 | 2011-07-12 19:07:05 +0000 | [diff] [blame] | 2020 | VG_(err_config_error)("Can't delete client cmdline file in %s\n", buf2); |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 2021 | |
| 2022 | VG_(cl_cmdline_fd) = fd; |
iraisr | 23e68ca | 2015-08-14 20:50:11 +0000 | [diff] [blame] | 2023 | #endif // defined(VGO_linux) || defined(SOLARIS_PROC_CMDLINE) |
tom | 41ad7e7 | 2012-10-04 20:27:38 +0000 | [diff] [blame] | 2024 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 2025 | /* Fake /proc/<pid>/auxv on both Linux and Solaris. */ |
tom | 41ad7e7 | 2012-10-04 20:27:38 +0000 | [diff] [blame] | 2026 | 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; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 2054 | |
| 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 */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2072 | } |
bart | 9b533f8 | 2009-08-25 20:15:41 +0000 | [diff] [blame] | 2073 | #endif |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2074 | |
| 2075 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2076 | // Init tool part 1: pre_clo_init |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2077 | // p: setup_client_stack() [for 'VG_(client_arg[cv]'] |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2078 | // p: setup_file_descriptors() [for 'VG_(fd_xxx_limit)'] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2079 | //-------------------------------------------------------------- |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2080 | VG_(debugLog)(1, "main", "Initialise the tool part 1 (pre_clo_init)\n"); |
njn | 08ce7b3 | 2009-02-27 03:38:28 +0000 | [diff] [blame] | 2081 | VG_(tl_pre_clo_init)(); |
philippe | a0a7393 | 2014-06-15 15:42:20 +0000 | [diff] [blame] | 2082 | // Activate var info readers, if the tool asked for it: |
| 2083 | if (VG_(needs).var_info) |
| 2084 | VG_(clo_read_var_info) = True; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2085 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2086 | //-------------------------------------------------------------- |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2087 | // If --tool and --help/--help-debug was given, now give the core+tool |
| 2088 | // help message |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 2089 | // p: early_process_cmd_line_options() [for 'need_help'] |
| 2090 | // p: tl_pre_clo_init [for 'VG_(tdict).usage'] |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2091 | //-------------------------------------------------------------- |
| 2092 | VG_(debugLog)(1, "main", "Print help and quit, if requested\n"); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2093 | if (need_help) { |
njn | cce38e6 | 2010-07-06 04:25:12 +0000 | [diff] [blame] | 2094 | usage_NORETURN(/*--help-debug?*/need_help >= 2); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2095 | } |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2096 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2097 | //-------------------------------------------------------------- |
| 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 | //-------------------------------------------------------------- |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 2102 | VG_(debugLog)(1, "main", |
| 2103 | "(main_) Process Valgrind's command line options, " |
| 2104 | "setup logging\n"); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 2105 | main_process_cmd_line_options ( &logging_to_fd, &xml_fname_unexpanded, |
| 2106 | toolname ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2107 | |
| 2108 | //-------------------------------------------------------------- |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 2109 | // Zeroise the millisecond counter by doing a first read of it. |
| 2110 | // p: none |
| 2111 | //-------------------------------------------------------------- |
| 2112 | (void) VG_(read_millisecond_timer)(); |
| 2113 | |
| 2114 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2115 | // Print the preamble |
| 2116 | // p: tl_pre_clo_init [for 'VG_(details).name' and friends] |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 2117 | // p: main_process_cmd_line_options() |
| 2118 | // [for VG_(clo_verbosity), VG_(clo_xml), |
| 2119 | // logging_to_fd, xml_fname_unexpanded] |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2120 | //-------------------------------------------------------------- |
| 2121 | VG_(debugLog)(1, "main", "Print the preamble...\n"); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 2122 | print_preamble(logging_to_fd, xml_fname_unexpanded, toolname); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2123 | 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"); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2133 | VG_TDICT_CALL(tool_post_clo_init); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2134 | { |
| 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. */ |
florian | e6a4ed1 | 2012-10-21 02:30:18 +0000 | [diff] [blame] | 2138 | const HChar* s; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2139 | Bool ok; |
| 2140 | ok = VG_(sanity_check_needs)( &s ); |
| 2141 | if (!ok) { |
florian | a4ca4fe | 2014-09-16 09:28:12 +0000 | [diff] [blame] | 2142 | VG_(core_panic)(s); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2143 | } |
| 2144 | } |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2145 | |
| 2146 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2147 | // Initialise translation table and translation cache |
| 2148 | // p: aspacem [??] |
| 2149 | // p: tl_pre_clo_init [for 'VG_(details).avg_translation_sizeB'] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2150 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2151 | VG_(debugLog)(1, "main", "Initialise TT/TC\n"); |
| 2152 | VG_(init_tt_tc)(); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2153 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2154 | //-------------------------------------------------------------- |
| 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"); |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 2160 | VG_(redir_initialise)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2161 | |
| 2162 | //-------------------------------------------------------------- |
| 2163 | // Allow GDB attach |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 2164 | // p: main_process_cmd_line_options() [for VG_(clo_wait_for_gdb)] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2165 | //-------------------------------------------------------------- |
| 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 Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2169 | 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); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
sewardj | b5d320c | 2005-03-13 18:57:15 +0000 | [diff] [blame] | 2175 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2176 | // Search for file descriptors that are inherited from our parent |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 2177 | // p: main_process_cmd_line_options [for VG_(clo_track_fds)] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2178 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2179 | if (VG_(clo_track_fds)) { |
| 2180 | VG_(debugLog)(1, "main", "Init preopened fds\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2181 | VG_(init_preopened_fds)(); |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2182 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2183 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 2184 | #if defined(VGO_solaris) |
| 2185 | VG_(syswrap_init)(); |
| 2186 | #endif |
| 2187 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2188 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2189 | // 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 |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2193 | // p: initialise m_debuginfo |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2194 | // |
| 2195 | // While doing this, make a note of the debuginfo-handles that |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 2196 | // come back from VG_(di_notify_mmap). |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2197 | // 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, ...). |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2205 | //-------------------------------------------------------------- |
| 2206 | VG_(debugLog)(1, "main", "Load initial debug info\n"); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2207 | |
florian | e2800c9 | 2014-09-15 20:57:45 +0000 | [diff] [blame] | 2208 | vg_assert(!addr2dihandle); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2209 | addr2dihandle = VG_(newXA)( VG_(malloc), "main.vm.2", |
| 2210 | VG_(free), sizeof(Addr_n_ULong) ); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2211 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 2212 | # if defined(VGO_linux) || defined(VGO_solaris) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2213 | { Addr* seg_starts; |
| 2214 | Int n_seg_starts; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2215 | Addr_n_ULong anu; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2216 | |
florian | ea8a88c | 2015-02-20 14:00:23 +0000 | [diff] [blame] | 2217 | seg_starts = VG_(get_segment_starts)( SkFileC | SkFileV, &n_seg_starts ); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2218 | vg_assert(seg_starts && n_seg_starts >= 0); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2219 | |
sewardj | f72cced | 2005-11-08 00:45:47 +0000 | [diff] [blame] | 2220 | /* 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. */ |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2223 | for (i = 0; i < n_seg_starts; i++) { |
sewardj | 5f2dcad | 2011-10-24 08:53:03 +0000 | [diff] [blame] | 2224 | anu.ull = VG_(di_notify_mmap)( seg_starts[i], True/*allow_SkFileV*/, |
| 2225 | -1/*Don't use_fd*/); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2226 | /* 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 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2233 | |
| 2234 | VG_(free)( seg_starts ); |
| 2235 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2236 | # elif defined(VGO_darwin) |
| 2237 | { Addr* seg_starts; |
| 2238 | Int n_seg_starts; |
florian | ea8a88c | 2015-02-20 14:00:23 +0000 | [diff] [blame] | 2239 | seg_starts = VG_(get_segment_starts)( SkFileC, &n_seg_starts ); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2240 | 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? |
sewardj | 5f2dcad | 2011-10-24 08:53:03 +0000 | [diff] [blame] | 2245 | 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 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2249 | |
| 2250 | VG_(free)( seg_starts ); |
| 2251 | } |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2252 | # else |
| 2253 | # error Unknown OS |
| 2254 | # endif |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2255 | |
| 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; |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 2264 | Addr co_start = VG_PGROUNDDN( (Addr)&VG_(trampoline_stuff_start) ); |
| 2265 | Addr co_endPlus = VG_PGROUNDUP( (Addr)&VG_(trampoline_stuff_end) ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2266 | 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 | |
bart | 6c63f5c | 2011-10-11 18:50:14 +0000 | [diff] [blame] | 2275 | if (VG_(clo_xml)) { |
florian | f44ff62 | 2014-12-20 16:52:08 +0000 | [diff] [blame] | 2276 | HChar buf[50]; // large enough |
florian | 77dec3a | 2014-11-14 22:34:40 +0000 | [diff] [blame] | 2277 | VG_(elapsed_wallclock_time)(buf, sizeof buf); |
bart | 6c63f5c | 2011-10-11 18:50:14 +0000 | [diff] [blame] | 2278 | 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 | |
bart | 27233e9 | 2012-03-08 14:59:25 +0000 | [diff] [blame] | 2286 | VG_(init_Threads)(); |
| 2287 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2288 | //-------------------------------------------------------------- |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 2289 | // 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 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2300 | // 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 |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2305 | // |
| 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. |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2310 | //-------------------------------------------------------------- |
| 2311 | VG_(debugLog)(1, "main", "Tell tool about initial permissions\n"); |
| 2312 | { Addr* seg_starts; |
| 2313 | Int n_seg_starts; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2314 | |
florian | e2800c9 | 2014-09-15 20:57:45 +0000 | [diff] [blame] | 2315 | vg_assert(addr2dihandle); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2316 | |
tom | 7c1a19a | 2008-01-02 10:13:04 +0000 | [diff] [blame] | 2317 | /* 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. */ |
florian | e2800c9 | 2014-09-15 20:57:45 +0000 | [diff] [blame] | 2320 | vg_assert(VG_(running_tid) == VG_INVALID_THREADID); |
tom | 7c1a19a | 2008-01-02 10:13:04 +0000 | [diff] [blame] | 2321 | VG_(running_tid) = tid_main; |
| 2322 | |
florian | ea8a88c | 2015-02-20 14:00:23 +0000 | [diff] [blame] | 2323 | seg_starts = VG_(get_segment_starts)( SkFileC | SkAnonC | SkShmC, |
| 2324 | &n_seg_starts ); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2325 | vg_assert(seg_starts && n_seg_starts >= 0); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2326 | |
florian | ea8a88c | 2015-02-20 14:00:23 +0000 | [diff] [blame] | 2327 | /* Show client segments to the tool */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2328 | for (i = 0; i < n_seg_starts; i++) { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2329 | Word j, n; |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 2330 | NSegment const* seg |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2331 | = VG_(am_find_nsegment)( seg_starts[i] ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2332 | vg_assert(seg); |
florian | ea8a88c | 2015-02-20 14:00:23 +0000 | [diff] [blame] | 2333 | vg_assert(seg->kind == SkFileC || seg->kind == SkAnonC || |
| 2334 | seg->kind == SkShmC); |
| 2335 | vg_assert(seg->start == seg_starts[i]); |
| 2336 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2337 | 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' : '-' ); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2343 | /* 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) { |
florian | e2800c9 | 2014-09-15 20:57:45 +0000 | [diff] [blame] | 2349 | vg_assert(anl->ull > 0); /* check it's a valid handle */ |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2350 | break; |
| 2351 | } |
| 2352 | } |
| 2353 | vg_assert(j >= 0 && j <= n); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2354 | VG_TRACK( new_mem_startup, seg->start, seg->end+1-seg->start, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2355 | 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 ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | VG_(free)( seg_starts ); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2364 | VG_(deleteXA)( addr2dihandle ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2365 | |
| 2366 | /* Also do the initial stack permissions. */ |
bart | e05b3a4 | 2010-09-07 16:32:53 +0000 | [diff] [blame] | 2367 | { |
| 2368 | SSizeT inaccessible_len; |
| 2369 | NSegment const* seg |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2370 | = VG_(am_find_nsegment)( the_iifii.initial_client_SP ); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2371 | vg_assert(seg); |
| 2372 | vg_assert(seg->kind == SkAnonC); |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2373 | vg_assert(the_iifii.initial_client_SP >= seg->start); |
| 2374 | vg_assert(the_iifii.initial_client_SP <= seg->end); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2375 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2376 | /* 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. */ |
bart | e05b3a4 | 2010-09-07 16:32:53 +0000 | [diff] [blame] | 2381 | 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 ); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2388 | VG_(debugLog)(2, "main", "mark stack inaccessible %010lx-%010lx\n", |
| 2389 | seg->start, |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2390 | the_iifii.initial_client_SP-1 - VG_STACK_REDZONE_SZB); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2391 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2392 | |
| 2393 | /* Also the assembly helpers. */ |
| 2394 | VG_TRACK( new_mem_startup, |
| 2395 | (Addr)&VG_(trampoline_stuff_start), |
sewardj | c6527d6 | 2006-02-13 17:54:31 +0000 | [diff] [blame] | 2396 | (Addr)&VG_(trampoline_stuff_end) |
| 2397 | - (Addr)&VG_(trampoline_stuff_start), |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2398 | False, /* readable? */ |
| 2399 | False, /* writable? */ |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2400 | True /* executable? */, |
| 2401 | 0 /* di_handle: no associated debug info */ ); |
tom | 7c1a19a | 2008-01-02 10:13:04 +0000 | [diff] [blame] | 2402 | |
| 2403 | /* Clear the running thread indicator */ |
| 2404 | VG_(running_tid) = VG_INVALID_THREADID; |
florian | e2800c9 | 2014-09-15 20:57:45 +0000 | [diff] [blame] | 2405 | vg_assert(VG_(running_tid) == VG_INVALID_THREADID); |
sewardj | 882a9ec | 2014-07-08 07:44:07 +0000 | [diff] [blame] | 2406 | |
| 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 |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | //-------------------------------------------------------------- |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 2427 | // Initialise the scheduler (phase 2) |
| 2428 | // p: Initialise the scheduler (phase 1) [for tid_main] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2429 | // p: setup_file_descriptors() [else VG_(safe_fd)() breaks] |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2430 | // p: setup_client_stack |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2431 | //-------------------------------------------------------------- |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 2432 | VG_(debugLog)(1, "main", "Initialise scheduler (phase 2)\n"); |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 2433 | { NSegment const* seg |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2434 | = VG_(am_find_nsegment)( the_iifii.initial_client_SP ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2435 | vg_assert(seg); |
| 2436 | vg_assert(seg->kind == SkAnonC); |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2437 | vg_assert(the_iifii.initial_client_SP >= seg->start); |
| 2438 | vg_assert(the_iifii.initial_client_SP <= seg->end); |
sewardj | de764e8 | 2007-11-09 23:13:22 +0000 | [diff] [blame] | 2439 | VG_(scheduler_init_phase2)( tid_main, |
| 2440 | seg->end, the_iifii.clstack_max_size ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2441 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2442 | |
| 2443 | //-------------------------------------------------------------- |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2444 | // Set up state for the root thread |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2445 | // p: ? |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2446 | // setup_scheduler() [for sched-specific thread 1 stuff] |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2447 | // VG_(ii_create_image) [for 'the_iicii' initial info] |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2448 | //-------------------------------------------------------------- |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 2449 | VG_(debugLog)(1, "main", "Finalise initial image\n"); |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 2450 | { /* 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 | } |
njn | ea4b28c | 2004-11-30 16:04:58 +0000 | [diff] [blame] | 2463 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2464 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2465 | // Initialise the signal handling subsystem |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2466 | // p: n/a |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2467 | //-------------------------------------------------------------- |
| 2468 | // Nb: temporarily parks the saved blocking-mask in saved_sigmask. |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2469 | VG_(debugLog)(1, "main", "Initialise signal management\n"); |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 2470 | /* Check that the kernel-interface signal definitions look sane */ |
| 2471 | VG_(vki_do_initial_consistency_checks)(); |
| 2472 | /* .. and go on to use them. */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2473 | VG_(sigstartup_actions)(); |
| 2474 | |
| 2475 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2476 | // Read suppression file |
sewardj | 95d86c0 | 2007-12-18 01:49:23 +0000 | [diff] [blame] | 2477 | // p: main_process_cmd_line_options() [for VG_(clo_suppressions)] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2478 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2479 | if (VG_(needs).core_errors || VG_(needs).tool_errors) { |
| 2480 | VG_(debugLog)(1, "main", "Load suppressions\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2481 | VG_(load_suppressions)(); |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2482 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2483 | |
| 2484 | //-------------------------------------------------------------- |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 2485 | // register client stack |
| 2486 | //-------------------------------------------------------------- |
philippe | 38a74d2 | 2014-08-29 22:53:19 +0000 | [diff] [blame] | 2487 | VG_(clstk_id) = VG_(register_stack)(VG_(clstk_start_base), VG_(clstk_end)); |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 2488 | |
| 2489 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2490 | // 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 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2499 | // Run! |
| 2500 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2501 | VG_(debugLog)(1, "main", "Running thread 1\n"); |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2502 | |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2503 | /* As a result of the following call, the last thread standing |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2504 | 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. */ |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 2518 | VG_(main_thread_wrapper_NORETURN)(1); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2519 | |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2520 | /*NOTREACHED*/ |
| 2521 | vg_assert(0); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2524 | /* Do everything which needs doing when the last thread exits or when |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 2525 | a thread exits requesting a complete process exit. |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2526 | |
| 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 Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2538 | case, since it will run __gnu_cxx::__freeres and libc_freeres functions, |
| 2539 | thus allowing other lurking threads to run again. Hmm. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2540 | |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2541 | static |
| 2542 | void shutdown_actions_NORETURN( ThreadId tid, |
| 2543 | VgSchedReturnCode tids_schedretcode ) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2544 | { |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2545 | VG_(debugLog)(1, "main", "entering VG_(shutdown_actions_NORETURN)\n"); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2546 | VG_(am_show_nsegments)(1,"Memory layout at client shutdown"); |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2547 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2548 | vg_assert(VG_(is_running_thread)(tid)); |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 2549 | vg_assert(tids_schedretcode == VgSrc_ExitThread |
| 2550 | || tids_schedretcode == VgSrc_ExitProcess |
| 2551 | || tids_schedretcode == VgSrc_FatalSig ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2552 | |
sewardj | 12ab765 | 2006-10-17 02:10:42 +0000 | [diff] [blame] | 2553 | if (tids_schedretcode == VgSrc_ExitThread) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2554 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2555 | // We are the last surviving thread. Right? |
| 2556 | vg_assert( VG_(count_living_threads)() == 1 ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2557 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2558 | // Wait for all other threads to exit. |
| 2559 | // jrs: Huh? but they surely are already gone |
| 2560 | VG_(reap_threads)(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2561 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2562 | // Clean the client up before the final report. |
| 2563 | // This causes __gnu_cxx::__freeres and libc_freeres functions to run. |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2564 | 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 Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2578 | // 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. |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2581 | final_tidyup(tid); |
| 2582 | |
| 2583 | /* be paranoid */ |
| 2584 | vg_assert(VG_(is_running_thread)(tid)); |
| 2585 | vg_assert(VG_(count_living_threads)() >= 1); |
| 2586 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2587 | |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame] | 2588 | /* 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 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2593 | VG_(threads)[tid].status = VgTs_Empty; |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame] | 2594 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2595 | //-------------------------------------------------------------- |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 2596 | // Finalisation: cleanup, messages, etc. Order not so important, only |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2597 | // affects what order the messages come. |
| 2598 | //-------------------------------------------------------------- |
njn | b6267bd | 2009-08-12 00:14:16 +0000 | [diff] [blame] | 2599 | // First thing in the post-amble is a blank line. |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 2600 | if (VG_(clo_xml)) |
| 2601 | VG_(printf_xml)("\n"); |
njn | b6267bd | 2009-08-12 00:14:16 +0000 | [diff] [blame] | 2602 | else if (VG_(clo_verbosity) > 0) |
| 2603 | VG_(message)(Vg_UserMsg, "\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2604 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2605 | if (VG_(clo_xml)) { |
florian | f44ff62 | 2014-12-20 16:52:08 +0000 | [diff] [blame] | 2606 | HChar buf[50]; // large enough |
florian | 77dec3a | 2014-11-14 22:34:40 +0000 | [diff] [blame] | 2607 | VG_(elapsed_wallclock_time)(buf, sizeof buf); |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 2608 | VG_(printf_xml)( "<status>\n" |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 2609 | " <state>FINISHED</state>\n" |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 2610 | " <time>%pS</time>\n" |
njn | b6267bd | 2009-08-12 00:14:16 +0000 | [diff] [blame] | 2611 | "</status>\n" |
| 2612 | "\n", |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 2613 | buf); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2616 | /* Print out file descriptor summary and stats. */ |
| 2617 | if (VG_(clo_track_fds)) |
philippe | c336038 | 2012-10-21 14:37:14 +0000 | [diff] [blame] | 2618 | VG_(show_open_fds)("at exit"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2619 | |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 2620 | /* 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*/); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2624 | |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 2625 | /* Show the error counts. */ |
sewardj | 7ca100d | 2009-08-15 23:05:34 +0000 | [diff] [blame] | 2626 | if (VG_(clo_xml) |
| 2627 | && (VG_(needs).core_errors || VG_(needs).tool_errors)) { |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 2628 | VG_(show_error_counts_as_XML)(); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 2629 | } |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 2630 | |
| 2631 | /* In XML mode, this merely prints the used suppressions. */ |
| 2632 | if (VG_(needs).core_errors || VG_(needs).tool_errors) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2633 | VG_(show_all_errors)(VG_(clo_verbosity), VG_(clo_xml)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2634 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2635 | if (VG_(clo_xml)) { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 2636 | VG_(printf_xml)("\n"); |
| 2637 | VG_(printf_xml)("</valgrindoutput>\n"); |
| 2638 | VG_(printf_xml)("\n"); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 2641 | VG_(sanity_check_general)( True /*include expensive checks*/ ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2642 | |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 2643 | if (VG_(clo_stats)) |
philippe | 5af6658 | 2015-04-19 17:56:42 +0000 | [diff] [blame] | 2644 | VG_(print_all_stats)(VG_(clo_verbosity) >= 1, /* Memory stats */ |
philippe | 4f6f336 | 2014-04-19 00:25:54 +0000 | [diff] [blame] | 2645 | False /* tool prints stats in the tool fini */); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2646 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2647 | /* 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 | |
sewardj | 17c5e2e | 2012-12-28 09:12:14 +0000 | [diff] [blame] | 2655 | /* 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*/); |
njn | 2025cf9 | 2005-06-26 20:44:48 +0000 | [diff] [blame] | 2660 | } |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 2661 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 2662 | /* Print Vex storage stats */ |
sewardj | bf42651 | 2005-01-17 18:35:30 +0000 | [diff] [blame] | 2663 | if (0) |
| 2664 | LibVEX_ShowAllocStats(); |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2665 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 2666 | /* Flush any output cached by previous calls to VG_(message). */ |
| 2667 | VG_(message_flush)(); |
| 2668 | |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame] | 2669 | /* Terminate gdbserver if ever it was started. We terminate it here |
sewardj | 17c5e2e | 2012-12-28 09:12:14 +0000 | [diff] [blame] | 2670 | so that it get the output above if output was redirected to |
| 2671 | gdb */ |
philippe | 0447bbd | 2012-10-17 21:32:03 +0000 | [diff] [blame] | 2672 | VG_(gdbserver_exit) (tid, tids_schedretcode); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2673 | |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2674 | /* 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 Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2679 | "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); |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2684 | |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2685 | switch (tids_schedretcode) { |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 2686 | case VgSrc_ExitThread: /* the normal way out (Linux, Solaris) */ |
florian | d219029 | 2015-01-08 21:05:03 +0000 | [diff] [blame] | 2687 | case VgSrc_ExitProcess: /* the normal way out (Darwin) */ |
sewardj | b977908 | 2006-05-12 23:50:15 +0000 | [diff] [blame] | 2688 | /* 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) { |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame] | 2692 | VG_(client_exit)( VG_(clo_error_exitcode) ); |
sewardj | b977908 | 2006-05-12 23:50:15 +0000 | [diff] [blame] | 2693 | } else { |
| 2694 | /* otherwise, return the client's exit code, in the normal |
| 2695 | way. */ |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame] | 2696 | VG_(client_exit)( VG_(threads)[tid].os_state.exitcode ); |
sewardj | b977908 | 2006-05-12 23:50:15 +0000 | [diff] [blame] | 2697 | } |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2698 | /* NOT ALIVE HERE! */ |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2699 | VG_(core_panic)("entered the afterlife in main() -- ExitT/P"); |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2700 | 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); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2706 | /* 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 |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2713 | 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 | } |
njn | e96be67 | 2005-05-08 19:08:54 +0000 | [diff] [blame] | 2719 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 2720 | |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2721 | /* -------------------- */ |
| 2722 | |
| 2723 | /* Final clean-up before terminating the process. |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2724 | Clean up the client by calling __gnu_cxx::__freeres() (if requested) |
| 2725 | and __libc_freeres() (if requested). |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2726 | */ |
| 2727 | static void final_tidyup(ThreadId tid) |
| 2728 | { |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2729 | #if defined(VGO_linux) || defined(VGO_solaris) |
| 2730 | Addr freeres_wrapper = VG_(client_freeres_wrapper); |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2731 | |
| 2732 | vg_assert(VG_(is_running_thread)(tid)); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2733 | |
| 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 | } |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2750 | |
carll | cae0cc2 | 2014-08-07 23:17:29 +0000 | [diff] [blame] | 2751 | # if defined(VGP_ppc64be_linux) |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2752 | Addr r2 = VG_(get_tocptr)(freeres_wrapper); |
sewardj | cf95181 | 2006-01-17 02:22:21 +0000 | [diff] [blame] | 2753 | if (r2 == 0) { |
| 2754 | VG_(message)(Vg_UserMsg, |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2755 | "Caught __NR_exit, but can't run __gnu_cxx::__freeres()\n"); |
sewardj | cf95181 | 2006-01-17 02:22:21 +0000 | [diff] [blame] | 2756 | VG_(message)(Vg_UserMsg, |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2757 | " or __libc_freeres() since cannot establish TOC pointer " |
| 2758 | "for it.\n"); |
sewardj | cf95181 | 2006-01-17 02:22:21 +0000 | [diff] [blame] | 2759 | return; |
| 2760 | } |
| 2761 | # endif |
| 2762 | |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2763 | if (VG_(clo_verbosity) > 2 || |
| 2764 | VG_(clo_trace_syscalls) || |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2765 | 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 | } |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2775 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2776 | /* set thread context to point to freeres_wrapper */ |
| 2777 | /* ppc64be-linux note: freeres_wrapper gives us the real |
sewardj | 1a85f4f | 2006-01-12 21:15:35 +0000 | [diff] [blame] | 2778 | 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 Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2781 | VG_(set_IP)(tid, freeres_wrapper); |
carll | cae0cc2 | 2014-08-07 23:17:29 +0000 | [diff] [blame] | 2782 | # if defined(VGP_ppc64be_linux) |
sewardj | cf95181 | 2006-01-17 02:22:21 +0000 | [diff] [blame] | 2783 | VG_(threads)[tid].arch.vex.guest_GPR2 = r2; |
carll | 582d582 | 2014-08-07 23:35:54 +0000 | [diff] [blame] | 2784 | # elif defined(VGP_ppc64le_linux) |
| 2785 | /* setting GPR2 but not really needed, GPR12 is needed */ |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2786 | VG_(threads)[tid].arch.vex.guest_GPR2 = freeres_wrapper; |
| 2787 | VG_(threads)[tid].arch.vex.guest_GPR12 = freeres_wrapper; |
sewardj | cf95181 | 2006-01-17 02:22:21 +0000 | [diff] [blame] | 2788 | # endif |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 2789 | /* mips-linux note: we need to set t9 */ |
petarj | 4df0bfc | 2013-02-27 23:17:33 +0000 | [diff] [blame] | 2790 | # if defined(VGP_mips32_linux) || defined(VGP_mips64_linux) |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2791 | VG_(threads)[tid].arch.vex.guest_r25 = freeres_wrapper; |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 2792 | # endif |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2793 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2794 | /* 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 | |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2853 | /* Block all blockable signals by copying the real block state into |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2854 | the thread's block state */ |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2855 | 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 Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2858 | /* and restore handlers to default. */ |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2859 | 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 Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2866 | // ...but now we're not again. |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2867 | VG_(threads)[tid].exitreason = VgSrc_None; |
| 2868 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2869 | // Run until client thread exits - ideally with FREERES_DONE, |
| 2870 | // but exit/exitgroup/signal will do. |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2871 | VG_(scheduler)(tid); |
| 2872 | |
| 2873 | vg_assert(VG_(is_exiting)(tid)); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 2874 | #endif |
sewardj | 1ae3f3a | 2005-09-28 10:47:38 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2877 | |
| 2878 | /*====================================================================*/ |
njn | 49f80e8 | 2009-05-21 01:25:43 +0000 | [diff] [blame] | 2879 | /*=== Getting to main() alive: LINUX ===*/ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2880 | /*====================================================================*/ |
| 2881 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2882 | #if defined(VGO_linux) |
| 2883 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2884 | /* 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 | |
petarj | e70c45e | 2013-02-15 03:12:17 +0000 | [diff] [blame] | 2894 | 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 |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2896 | "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 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2905 | void* memcpy(void *dest, const void *src, SizeT n); |
| 2906 | void* memcpy(void *dest, const void *src, SizeT n) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2907 | return VG_(memcpy)(dest,src,n); |
| 2908 | } |
petarj | e70c45e | 2013-02-15 03:12:17 +0000 | [diff] [blame] | 2909 | void* memmove(void *dest, const void *src, SizeT n); |
| 2910 | void* memmove(void *dest, const void *src, SizeT n) { |
| 2911 | return VG_(memmove)(dest,src,n); |
| 2912 | } |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2913 | void* memset(void *s, int c, SizeT n); |
| 2914 | void* memset(void *s, int c, SizeT n) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2915 | return VG_(memset)(s,c,n); |
| 2916 | } |
| 2917 | |
bart | 82616e1 | 2010-06-13 13:46:24 +0000 | [diff] [blame] | 2918 | /* BVA: abort() for those platforms that need it (PPC and ARM). */ |
| 2919 | void abort(void); |
| 2920 | void abort(void){ |
| 2921 | VG_(printf)("Something called raise().\n"); |
| 2922 | vg_assert(0); |
| 2923 | } |
| 2924 | |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 2925 | /* 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) |
| 2929 | void raise(void); |
| 2930 | void raise(void){ |
| 2931 | VG_(printf)("Something called raise().\n"); |
| 2932 | vg_assert(0); |
| 2933 | } |
| 2934 | |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 2935 | void __aeabi_unwind_cpp_pr0(void); |
| 2936 | void __aeabi_unwind_cpp_pr0(void){ |
| 2937 | VG_(printf)("Something called __aeabi_unwind_cpp_pr0()\n"); |
| 2938 | vg_assert(0); |
| 2939 | } |
sewardj | 38efe4b | 2010-08-22 12:23:01 +0000 | [diff] [blame] | 2940 | |
| 2941 | void __aeabi_unwind_cpp_pr1(void); |
| 2942 | void __aeabi_unwind_cpp_pr1(void){ |
| 2943 | VG_(printf)("Something called __aeabi_unwind_cpp_pr1()\n"); |
| 2944 | vg_assert(0); |
| 2945 | } |
Chih-Hung Hsieh | e397234 | 2016-09-28 20:02:14 -0700 | [diff] [blame] | 2946 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2947 | #if defined(__ANDROID__) && defined(__clang__) |
Chih-Hung Hsieh | e397234 | 2016-09-28 20:02:14 -0700 | [diff] [blame] | 2948 | /* Replace __aeabi_memcpy* functions with vgPlain_memcpy. */ |
| 2949 | void* __aeabi_memcpy(void *dest, const void *src, SizeT n); |
| 2950 | void* __aeabi_memcpy(void *dest, const void *src, SizeT n) |
| 2951 | { |
| 2952 | return VG_(memcpy)(dest, src, n); |
| 2953 | } |
| 2954 | |
| 2955 | void* __aeabi_memcpy4(void *dest, const void *src, SizeT n); |
| 2956 | void* __aeabi_memcpy4(void *dest, const void *src, SizeT n) |
| 2957 | { |
| 2958 | return VG_(memcpy)(dest, src, n); |
| 2959 | } |
| 2960 | |
| 2961 | void* __aeabi_memcpy8(void *dest, const void *src, SizeT n); |
| 2962 | void* __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. */ |
| 2968 | void* __aeabi_memclr(void *dest, SizeT n); |
| 2969 | void* __aeabi_memclr(void *dest, SizeT n) |
| 2970 | { |
| 2971 | return VG_(memset)(dest, 0, n); |
| 2972 | } |
| 2973 | |
| 2974 | void* __aeabi_memclr4(void *dest, SizeT n); |
| 2975 | void* __aeabi_memclr4(void *dest, SizeT n) |
| 2976 | { |
| 2977 | return VG_(memset)(dest, 0, n); |
| 2978 | } |
| 2979 | |
| 2980 | void* __aeabi_memclr8(void *dest, SizeT n); |
| 2981 | void* __aeabi_memclr8(void *dest, SizeT n) |
| 2982 | { |
| 2983 | return VG_(memset)(dest, 0, n); |
| 2984 | } |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 2985 | #endif /* __ANDROID__ __clang__ */ |
| 2986 | |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 2987 | #endif |
| 2988 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2989 | /* ---------------- 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 |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 2994 | _start_in_C_linux, which extracts argc/argv/envp and starts up |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2995 | 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 |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3017 | stack pointer and calls onwards to _start_in_C_linux. This also switches |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 3018 | the new stack. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3019 | #if defined(VGP_x86_linux) |
| 3020 | asm("\n" |
sewardj | d9fc382 | 2005-11-18 23:50:43 +0000 | [diff] [blame] | 3021 | ".text\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3022 | "\t.globl _start\n" |
| 3023 | "\t.type _start,@function\n" |
| 3024 | "_start:\n" |
| 3025 | /* set up the new stack in %eax */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 3026 | "\tmovl $vgPlain_interim_stack, %eax\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3027 | "\taddl $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3028 | "\taddl $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %eax\n" |
Chih-Hung Hsieh | 606dc24 | 2016-08-24 19:59:10 -0700 | [diff] [blame] | 3029 | /* allocate at least 16 bytes on the new stack, and aligned */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3030 | "\tsubl $16, %eax\n" |
| 3031 | "\tandl $~15, %eax\n" |
| 3032 | /* install it, and collect the original one */ |
| 3033 | "\txchgl %eax, %esp\n" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3034 | /* call _start_in_C_linux, passing it the startup %esp */ |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3035 | "\tmovl %eax, (%esp)\n" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3036 | "\tcall _start_in_C_linux\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3037 | "\thlt\n" |
sewardj | 2fedc64 | 2005-11-19 02:02:57 +0000 | [diff] [blame] | 3038 | ".previous\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3039 | ); |
| 3040 | #elif defined(VGP_amd64_linux) |
| 3041 | asm("\n" |
sewardj | d9fc382 | 2005-11-18 23:50:43 +0000 | [diff] [blame] | 3042 | ".text\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3043 | "\t.globl _start\n" |
| 3044 | "\t.type _start,@function\n" |
| 3045 | "_start:\n" |
| 3046 | /* set up the new stack in %rdi */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 3047 | "\tmovq $vgPlain_interim_stack, %rdi\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3048 | "\taddq $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rdi\n" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3049 | "\taddq $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %rdi\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3050 | "\tandq $~15, %rdi\n" |
| 3051 | /* install it, and collect the original one */ |
| 3052 | "\txchgq %rdi, %rsp\n" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3053 | /* call _start_in_C_linux, passing it the startup %rsp */ |
| 3054 | "\tcall _start_in_C_linux\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3055 | "\thlt\n" |
sewardj | 2fedc64 | 2005-11-19 02:02:57 +0000 | [diff] [blame] | 3056 | ".previous\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3057 | ); |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 3058 | #elif defined(VGP_ppc32_linux) |
| 3059 | asm("\n" |
sewardj | d9fc382 | 2005-11-18 23:50:43 +0000 | [diff] [blame] | 3060 | ".text\n" |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 3061 | "\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" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3069 | "\tlis 18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" >> 16)\n" |
| 3070 | "\tori 18,18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" & 0xFFFF)\n" |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 3071 | "\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 + |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3075 | VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 3076 | boundary. And r1 is the original SP. Set the SP to r16 and |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3077 | call _start_in_C_linux, passing it the initial SP. */ |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 3078 | "\tmr 3,1\n" |
| 3079 | "\tmr 1,16\n" |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3080 | "\tbl _start_in_C_linux\n" |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 3081 | "\ttrap\n" |
sewardj | 2fedc64 | 2005-11-19 02:02:57 +0000 | [diff] [blame] | 3082 | ".previous\n" |
sewardj | a48a493 | 2005-09-29 11:09:56 +0000 | [diff] [blame] | 3083 | ); |
carll | cae0cc2 | 2014-08-07 23:17:29 +0000 | [diff] [blame] | 3084 | #elif defined(VGP_ppc64be_linux) |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 3085 | asm("\n" |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 3086 | /* 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. */ |
cerion | 297c88f | 2005-12-22 15:53:12 +0000 | [diff] [blame] | 3088 | "\t.align 2\n" |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 3089 | "\t.global _start\n" |
| 3090 | "\t.section \".opd\",\"aw\"\n" |
| 3091 | "\t.align 3\n" |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 3092 | "_start:\n" |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 3093 | "\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" |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 3098 | /* 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" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3108 | "\tlis 18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" >> 16)\n" |
| 3109 | "\tori 18,18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" & 0xFFFF)\n" |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 3110 | "\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 + |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3114 | VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 3115 | boundary. And r1 is the original SP. Set the SP to r16 and |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3116 | call _start_in_C_linux, passing it the initial SP. */ |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 3117 | "\tmr 3,1\n" |
| 3118 | "\tmr 1,16\n" |
sewardj | 5e21d44 | 2012-07-21 10:08:29 +0000 | [diff] [blame] | 3119 | "\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" |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 3127 | "\tnop\n" |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 3128 | "\ttrap\n" |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 3129 | ); |
carll | 582d582 | 2014-08-07 23:35:54 +0000 | [diff] [blame] | 3130 | #elif defined(VGP_ppc64le_linux) |
| 3131 | /* Little Endian uses ELF version 2 but in the future may also |
| 3132 | * support other ELF versions. |
| 3133 | */ |
| 3134 | asm("\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" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3154 | "\tlis 18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" >> 16)\n" |
| 3155 | "\tori 18,18,("VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)" & 0xFFFF)\n" |
carll | 582d582 | 2014-08-07 23:35:54 +0000 | [diff] [blame] | 3156 | "\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 + |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3160 | VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte |
carll | 582d582 | 2014-08-07 23:35:54 +0000 | [diff] [blame] | 3161 | 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 | ); |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 3175 | #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 | */ |
| 3194 | asm("\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" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3213 | "2: .quad "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n\t" |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 3214 | ".previous\n" |
| 3215 | ); |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 3216 | #elif defined(VGP_arm_linux) |
| 3217 | asm("\n" |
sewardj | b51c926 | 2011-05-03 14:24:11 +0000 | [diff] [blame] | 3218 | "\t.text\n" |
| 3219 | "\t.align 4\n" |
| 3220 | "\t.type _start,#function\n" |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 3221 | "\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" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3236 | "\t.word "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n" |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 3237 | ); |
sewardj | f0c1250 | 2014-01-12 12:54:00 +0000 | [diff] [blame] | 3238 | #elif defined(VGP_arm64_linux) |
| 3239 | asm("\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" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3252 | // 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," |
sewardj | f0c1250 | 2014-01-12 12:54:00 +0000 | [diff] [blame] | 3255 | " 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 | ); |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 3263 | #elif defined(VGP_mips32_linux) |
| 3264 | asm("\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" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3286 | "\tli $11, "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n" |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 3287 | |
| 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 + |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3293 | VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 3294 | 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 | ); |
petarj | 4df0bfc | 2013-02-27 23:17:33 +0000 | [diff] [blame] | 3307 | #elif defined(VGP_mips64_linux) |
| 3308 | asm( |
| 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" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3322 | "\tli $11, "VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)"\n" |
petarj | 4df0bfc | 2013-02-27 23:17:33 +0000 | [diff] [blame] | 3323 | |
| 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 + |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3329 | VG_DEFAULT_STACK_ACTIVE_SZB rounded down to the nearest 16-byte |
petarj | 4df0bfc | 2013-02-27 23:17:33 +0000 | [diff] [blame] | 3330 | 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" |
dejanj | 124b9f2 | 2013-10-16 14:15:38 +0000 | [diff] [blame] | 3344 | ".previous\n" |
petarj | 4df0bfc | 2013-02-27 23:17:33 +0000 | [diff] [blame] | 3345 | ); |
sewardj | 112711a | 2015-04-10 12:30:09 +0000 | [diff] [blame] | 3346 | #elif defined(VGP_tilegx_linux) |
| 3347 | asm("\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 | ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3385 | #else |
njn | 49f80e8 | 2009-05-21 01:25:43 +0000 | [diff] [blame] | 3386 | # error "Unknown linux platform" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3387 | #endif |
| 3388 | |
sewardj | e66f2e0 | 2006-12-30 17:45:08 +0000 | [diff] [blame] | 3389 | /* --- !!! --- 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 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3396 | /* Avoid compiler warnings: this fn _is_ used, but labelling it |
philippe | 9fdca56 | 2012-04-16 22:06:47 +0000 | [diff] [blame] | 3397 | '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)) |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3401 | void _start_in_C_linux ( UWord* pArgc ); |
philippe | 9fdca56 | 2012-04-16 22:06:47 +0000 | [diff] [blame] | 3402 | __attribute__ ((used)) |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3403 | void _start_in_C_linux ( UWord* pArgc ) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3404 | { |
| 3405 | Int r; |
| 3406 | Word argc = pArgc[0]; |
| 3407 | HChar** argv = (HChar**)&pArgc[1]; |
| 3408 | HChar** envp = (HChar**)&pArgc[1+argc+1]; |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 3409 | |
philippe | 854be4c | 2013-10-22 21:20:14 +0000 | [diff] [blame] | 3410 | // 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 | |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 3422 | 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 | |
carll | cae0cc2 | 2014-08-07 23:17:29 +0000 | [diff] [blame] | 3427 | # if defined(VGP_ppc32_linux) || defined(VGP_ppc64be_linux) \ |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3428 | || defined(VGP_ppc64le_linux) || defined(VGP_arm64_linux) \ |
| 3429 | || defined(VGP_mips32_linux) || defined(VGP_mips64_linux) |
sewardj | e66f2e0 | 2006-12-30 17:45:08 +0000 | [diff] [blame] | 3430 | { |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 3431 | /* 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. */ |
sewardj | e66f2e0 | 2006-12-30 17:45:08 +0000 | [diff] [blame] | 3434 | 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 | |
sewardj | f9d2f9b | 2006-11-17 20:00:57 +0000 | [diff] [blame] | 3448 | r = valgrind_main( (Int)argc, argv, envp ); |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3449 | /* NOTREACHED */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 3450 | VG_(exit)(r); |
| 3451 | } |
| 3452 | |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3453 | |
| 3454 | /*====================================================================*/ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3455 | /*=== Getting to main() alive: darwin ===*/ |
| 3456 | /*====================================================================*/ |
| 3457 | |
| 3458 | #elif defined(VGO_darwin) |
| 3459 | |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 3460 | /* |
| 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) |
| 3483 | asm("\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" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3491 | "\taddl $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %eax\n" |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 3492 | "\tsubl $16, %eax\n" |
| 3493 | "\tandl $~15, %eax\n" |
| 3494 | /* install it, and collect the original one */ |
| 3495 | "\txchgl %eax, %esp\n" |
sewardj | 6919736 | 2012-03-07 16:38:12 +0000 | [diff] [blame] | 3496 | "\tsubl $12, %esp\n" // keep stack 16 aligned; see #295428 |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 3497 | /* 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) |
| 3504 | asm("\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" |
philippe | d0720e4 | 2015-03-12 20:43:46 +0000 | [diff] [blame] | 3512 | "\taddq $"VG_STRINGIFY(VG_DEFAULT_STACK_ACTIVE_SZB)", %rdi\n" |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 3513 | "\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 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3523 | void* __memcpy_chk(void *dest, const void *src, SizeT n, SizeT n2); |
| 3524 | void* __memcpy_chk(void *dest, const void *src, SizeT n, SizeT n2) { |
| 3525 | // skip check |
| 3526 | return VG_(memcpy)(dest,src,n); |
| 3527 | } |
| 3528 | void* __memset_chk(void *s, int c, SizeT n, SizeT n2); |
| 3529 | void* __memset_chk(void *s, int c, SizeT n, SizeT n2) { |
| 3530 | // skip check |
| 3531 | return VG_(memset)(s,c,n); |
| 3532 | } |
| 3533 | void bzero(void *s, SizeT n); |
| 3534 | void bzero(void *s, SizeT n) { |
| 3535 | VG_(memset)(s,0,n); |
| 3536 | } |
| 3537 | |
| 3538 | void* memcpy(void *dest, const void *src, SizeT n); |
| 3539 | void* memcpy(void *dest, const void *src, SizeT n) { |
| 3540 | return VG_(memcpy)(dest,src,n); |
| 3541 | } |
| 3542 | void* memset(void *s, int c, SizeT n); |
| 3543 | void* memset(void *s, int c, SizeT n) { |
| 3544 | return VG_(memset)(s,c,n); |
| 3545 | } |
| 3546 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3547 | /* Avoid compiler warnings: this fn _is_ used, but labelling it |
| 3548 | 'static' causes gcc to complain it isn't. */ |
| 3549 | void _start_in_C_darwin ( UWord* pArgc ); |
| 3550 | void _start_in_C_darwin ( UWord* pArgc ) |
| 3551 | { |
| 3552 | Int r; |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 3553 | Int argc = *(Int *)pArgc; // not pArgc[0] on LP64 |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3554 | HChar** argv = (HChar**)&pArgc[1]; |
| 3555 | HChar** envp = (HChar**)&pArgc[1+argc+1]; |
| 3556 | |
philippe | 854be4c | 2013-10-22 21:20:14 +0000 | [diff] [blame] | 3557 | // 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 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3563 | 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 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 3573 | /*====================================================================*/ |
| 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. */ |
| 3581 | asm("\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) |
| 3602 | asm("\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 | |
| 3624 | void *memcpy(void *dest, const void *src, size_t n); |
| 3625 | void *memcpy(void *dest, const void *src, size_t n) { |
| 3626 | return VG_(memcpy)(dest, src, n); |
| 3627 | } |
| 3628 | |
| 3629 | __attribute__ ((used)) |
| 3630 | void _start_in_C_solaris ( UWord* pArgc ); |
| 3631 | __attribute__ ((used)) |
| 3632 | void _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 | } |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 3648 | |
njn | 49f80e8 | 2009-05-21 01:25:43 +0000 | [diff] [blame] | 3649 | #else |
njn | 49f80e8 | 2009-05-21 01:25:43 +0000 | [diff] [blame] | 3650 | # error "Unknown OS" |
| 3651 | #endif |
sewardj | 17c1104 | 2006-10-15 01:26:40 +0000 | [diff] [blame] | 3652 | |
| 3653 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 3654 | Addr VG_(get_initial_client_SP)( void ) |
| 3655 | { |
| 3656 | return the_iifii.initial_client_SP; |
| 3657 | } |
| 3658 | |
sewardj | 0af71bb | 2010-07-01 14:50:30 +0000 | [diff] [blame] | 3659 | /*====================================================================*/ |
| 3660 | /*=== {u,}{div,mod}di3 replacements ===*/ |
| 3661 | /*====================================================================*/ |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 3662 | |
| 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 |
| 3694 | 64-bit unsigned dividend by a 32-bit unsigned divisor, producing a |
| 3695 | 32-bit quotient. In the overflow cases (divide by 0, or quotient |
| 3696 | exceeds 32 bits), it returns a remainder of 0xFFFFFFFF (an impossible |
| 3697 | value). |
| 3698 | The dividend is u1 and u0, with u1 being the most significant word. |
| 3699 | The divisor is parameter v. The value returned is the quotient. |
| 3700 | Max line length is 57, to fit in hacker.book. */ |
| 3701 | |
| 3702 | static 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. */ |
| 3717 | static 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. */ |
| 3769 | static 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 |
| 3802 | machine that does not have that instruction but that does have |
| 3803 | instructions for "long division" (64/32 ==> 32). Code for unsigned |
| 3804 | division is given first, followed by a simple program for doing the |
| 3805 | signed version by using the unsigned version. |
| 3806 | These programs are useful in implementing "long long" (64-bit) |
| 3807 | arithmetic on a machine that has the long division instruction. It will |
| 3808 | work on 64- and 32-bit machines, provided the compiler implements long |
| 3809 | long's (64-bit integers). It is desirable that the machine have the |
| 3810 | Count Leading Zeros instruction. |
| 3811 | In the GNU world, these programs are known as __divdi3 and __udivdi3, |
| 3812 | and similar names are used here. |
| 3813 | This material is not in HD, but may be in a future edition. |
| 3814 | Max line length is 57, to fit in hacker.book. */ |
| 3815 | |
| 3816 | |
| 3817 | static 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 |
| 3854 | simulations of the presumed machines ops. I.e., they look at only the |
| 3855 | low-order 32 bits of the divisor, they return garbage if the division |
| 3856 | overflows, and they return garbage in the high-order half of the |
| 3857 | quotient doubleword. |
| 3858 | In practice, these would be replaced with uses of the machine's DIVU |
| 3859 | and DIVS instructions (e.g., by using the GNU "asm" facility). */ |
| 3860 | |
| 3861 | static 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 | |
| 3868 | static 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. */ |
| 3877 | static 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 |
| 3916 | one execution of DIVS) are common. If this is not the case, the test for |
| 3917 | this case should be deleted. |
| 3918 | Note that the test for when DIVS can be used is not entirely |
| 3919 | accurate. For example, DIVS is not used if v = 0xFFFFFFFF8000000, |
| 3920 | whereas if could be (if u is sufficiently small in magnitude). */ |
| 3921 | |
| 3922 | // ------------------------------ cut ---------------------------------- |
| 3923 | |
| 3924 | static 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. */ |
| 3932 | static 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 | |
sewardj | 0af71bb | 2010-07-01 14:50:30 +0000 | [diff] [blame] | 3952 | ULong __udivdi3 (ULong u, ULong v); |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 3953 | ULong __udivdi3 (ULong u, ULong v) |
| 3954 | { |
| 3955 | return udivdi3(u,v); |
| 3956 | } |
| 3957 | |
sewardj | 0af71bb | 2010-07-01 14:50:30 +0000 | [diff] [blame] | 3958 | Long __divdi3 (Long u, Long v); |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 3959 | Long __divdi3 (Long u, Long v) |
| 3960 | { |
| 3961 | return divdi3(u,v); |
| 3962 | } |
| 3963 | |
sewardj | 0af71bb | 2010-07-01 14:50:30 +0000 | [diff] [blame] | 3964 | ULong __umoddi3 (ULong u, ULong v); |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 3965 | ULong __umoddi3 (ULong u, ULong v) |
| 3966 | { |
| 3967 | ULong q = __udivdi3(u, v); |
| 3968 | ULong r = u - q * v; |
| 3969 | return r; |
| 3970 | } |
| 3971 | |
sewardj | 0af71bb | 2010-07-01 14:50:30 +0000 | [diff] [blame] | 3972 | Long __moddi3 (Long u, Long v); |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 3973 | Long __moddi3 (Long u, Long v) |
| 3974 | { |
| 3975 | Long q = __divdi3(u, v); |
| 3976 | Long r = u - q * v; |
| 3977 | return r; |
| 3978 | } |
| 3979 | |
sewardj | 70d71c7 | 2011-08-23 07:35:42 +0000 | [diff] [blame] | 3980 | /* ------------------------------------------------ |
| 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 | |
| 4033 | typedef unsigned long long du_int; |
| 4034 | typedef unsigned su_int; |
| 4035 | |
| 4036 | typedef 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 | |
| 4051 | typedef union |
| 4052 | { |
| 4053 | udwords u; |
| 4054 | double f; |
| 4055 | } double_bits; |
| 4056 | |
| 4057 | du_int __fixunsdfdi(double a); |
| 4058 | |
| 4059 | du_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 | |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 4078 | #endif |
| 4079 | |
| 4080 | |
sewardj | d1678e0 | 2014-11-06 20:20:01 +0000 | [diff] [blame] | 4081 | /*====================================================================*/ |
| 4082 | /*=== Dummy _voucher_mach_msg_set for OSX 10.10 ===*/ |
| 4083 | /*====================================================================*/ |
| 4084 | |
rhyskidd | 9090d2f | 2015-07-08 13:46:57 +0000 | [diff] [blame] | 4085 | #if defined(VGO_darwin) && DARWIN_VERS >= DARWIN_10_10 |
sewardj | d1678e0 | 2014-11-06 20:20:01 +0000 | [diff] [blame] | 4086 | |
rhyskidd | 9090d2f | 2015-07-08 13:46:57 +0000 | [diff] [blame] | 4087 | /* Builds on MacOSX 10.10+ seem to need this for some reason. */ |
sewardj | d1678e0 | 2014-11-06 20:20:01 +0000 | [diff] [blame] | 4088 | /* 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 | */ |
| 4094 | UWord voucher_mach_msg_set ( UWord arg1 ); |
| 4095 | UWord voucher_mach_msg_set ( UWord arg1 ) |
| 4096 | { |
| 4097 | return 0; |
| 4098 | } |
| 4099 | |
| 4100 | #endif |
| 4101 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 4102 | #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. */ |
| 4107 | void __bzero ( void* s, UWord n ); |
| 4108 | void __bzero ( void* s, UWord n ) |
| 4109 | { |
| 4110 | (void) VG_(memset)( s, 0, n ); |
| 4111 | } |
| 4112 | |
| 4113 | #endif |
| 4114 | |
sewardj | d1678e0 | 2014-11-06 20:20:01 +0000 | [diff] [blame] | 4115 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4116 | /*--------------------------------------------------------------------*/ |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 4117 | /*--- end ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4118 | /*--------------------------------------------------------------------*/ |