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