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 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2005 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" |
| 32 | #include "pub_core_threadstate.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 33 | #include "pub_core_clientstate.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 34 | #include "pub_core_aspacemgr.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 35 | #include "pub_core_commandline.h" |
njn | 2521d32 | 2005-05-08 14:45:13 +0000 | [diff] [blame] | 36 | #include "pub_core_debuglog.h" |
| 37 | #include "pub_core_errormgr.h" |
| 38 | #include "pub_core_execontext.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 39 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 40 | #include "pub_core_libcassert.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 41 | #include "pub_core_libcfile.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 42 | #include "pub_core_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 43 | #include "pub_core_libcproc.h" |
njn | de62cbf | 2005-06-10 22:08:14 +0000 | [diff] [blame] | 44 | #include "pub_core_libcsignal.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 45 | #include "pub_core_syscall.h" // VG_(strerror) |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 46 | #include "pub_core_machine.h" |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 47 | #include "pub_core_main.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 48 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 49 | #include "pub_core_options.h" |
njn | 31513b4 | 2005-06-01 03:09:59 +0000 | [diff] [blame] | 50 | #include "pub_core_profile.h" |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 51 | #include "pub_core_debuginfo.h" |
njn | d1af003 | 2005-05-29 17:01:48 +0000 | [diff] [blame] | 52 | #include "pub_core_redir.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 53 | #include "pub_core_scheduler.h" |
njn | 0c24647 | 2005-05-31 01:00:08 +0000 | [diff] [blame] | 54 | #include "pub_core_signals.h" |
njn | 2025cf9 | 2005-06-26 20:44:48 +0000 | [diff] [blame] | 55 | #include "pub_core_stacks.h" // For VG_(register_stack) |
njn | c1b0181 | 2005-06-17 22:19:06 +0000 | [diff] [blame] | 56 | #include "pub_core_syswrap.h" |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 57 | #include "pub_core_translate.h" // For VG_(translate) |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 58 | #include "pub_core_tooliface.h" |
njn | a7598f6 | 2005-06-18 03:27:58 +0000 | [diff] [blame] | 59 | #include "pub_core_trampoline.h" |
njn | 8bddf58 | 2005-05-13 23:40:55 +0000 | [diff] [blame] | 60 | #include "pub_core_transtab.h" |
njn | 08a2e17 | 2005-06-21 22:47:54 +0000 | [diff] [blame] | 61 | #include "pub_core_ume.h" |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 62 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 63 | |
thughes | 74b8de2 | 2004-04-22 18:12:31 +0000 | [diff] [blame] | 64 | #ifndef AT_DCACHEBSIZE |
| 65 | #define AT_DCACHEBSIZE 19 |
| 66 | #endif /* AT_DCACHEBSIZE */ |
| 67 | |
| 68 | #ifndef AT_ICACHEBSIZE |
| 69 | #define AT_ICACHEBSIZE 20 |
| 70 | #endif /* AT_ICACHEBSIZE */ |
| 71 | |
| 72 | #ifndef AT_UCACHEBSIZE |
| 73 | #define AT_UCACHEBSIZE 21 |
| 74 | #endif /* AT_UCACHEBSIZE */ |
| 75 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 76 | #ifndef AT_SYSINFO |
| 77 | #define AT_SYSINFO 32 |
| 78 | #endif /* AT_SYSINFO */ |
| 79 | |
| 80 | #ifndef AT_SYSINFO_EHDR |
| 81 | #define AT_SYSINFO_EHDR 33 |
| 82 | #endif /* AT_SYSINFO_EHDR */ |
| 83 | |
| 84 | #ifndef AT_SECURE |
| 85 | #define AT_SECURE 23 /* secure mode boolean */ |
| 86 | #endif /* AT_SECURE */ |
| 87 | |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 88 | /* Number of file descriptors that Valgrind tries to reserve for |
| 89 | it's own use - just a small constant. */ |
| 90 | #define N_RESERVED_FDS (10) |
| 91 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 92 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 93 | /*====================================================================*/ |
| 94 | /*=== Counters, for profiling purposes only ===*/ |
| 95 | /*====================================================================*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 96 | |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 97 | static void print_all_stats ( void ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 98 | { |
nethercote | 92e7b7f | 2004-08-07 17:52:25 +0000 | [diff] [blame] | 99 | VG_(print_tt_tc_stats)(); |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 100 | VG_(print_scheduler_stats)(); |
njn | 9271cbc | 2005-03-13 05:38:25 +0000 | [diff] [blame] | 101 | VG_(print_ExeContext_stats)(); |
| 102 | |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 103 | // Memory stats |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 104 | if (VG_(clo_verbosity) > 2) { |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 105 | VG_(message)(Vg_DebugMsg, ""); |
| 106 | VG_(message)(Vg_DebugMsg, |
| 107 | "------ Valgrind's internal memory use stats follow ------" ); |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 108 | VG_(sanity_check_malloc_all)(); |
njn | 11adeb2 | 2005-07-03 20:22:39 +0000 | [diff] [blame] | 109 | VG_(message)(Vg_DebugMsg, "------" ); |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 110 | VG_(print_all_arena_stats)(); |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 111 | VG_(message)(Vg_DebugMsg, ""); |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 112 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | /*====================================================================*/ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 117 | /*=== Environment and stack setup ===*/ |
| 118 | /*====================================================================*/ |
| 119 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 120 | /* Prepare the client's environment. This is basically a copy of our |
| 121 | environment, except: |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 122 | |
| 123 | LD_PRELOAD=$VALGRIND_LIB/vg_preload_core.so: |
| 124 | ($VALGRIND_LIB/vgpreload_TOOL.so:)? |
| 125 | $LD_PRELOAD |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 126 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 127 | If this is missing, then it is added. |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 128 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 129 | Also, remove any binding for VALGRIND_LAUNCHER=. The client should |
| 130 | not be able to see this. |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 131 | |
| 132 | If this needs to handle any more variables it should be hacked |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 133 | into something table driven. The copy is VG_(malloc)'d space. |
| 134 | */ |
| 135 | static HChar** setup_client_env ( HChar** origenv, const HChar* toolname) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 136 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 137 | HChar* preload_core_so = "vg_preload_core.so"; |
| 138 | HChar* ld_preload = "LD_PRELOAD="; |
| 139 | HChar* v_launcher = VALGRIND_LAUNCHER "="; |
| 140 | Int ld_preload_len = VG_(strlen)( ld_preload ); |
| 141 | Int v_launcher_len = VG_(strlen)( v_launcher ); |
| 142 | Bool ld_preload_done = False; |
| 143 | Int vglib_len = VG_(strlen)(VG_(libdir)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 144 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 145 | HChar** cpp; |
| 146 | HChar** ret; |
| 147 | HChar* preload_tool_path;; |
| 148 | Int envc, i; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 149 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 150 | /* Alloc space for the vgpreload_core.so path and vgpreload_<tool>.so |
| 151 | paths. We might not need the space for vgpreload_<tool>.so, but it |
| 152 | doesn't hurt to over-allocate briefly. The 16s are just cautious |
| 153 | slop. */ |
| 154 | Int preload_core_path_len = vglib_len + sizeof(preload_core_so) + 16; |
| 155 | Int preload_tool_path_len = vglib_len + VG_(strlen)(toolname) + 16; |
| 156 | Int preload_string_len = preload_core_path_len + preload_tool_path_len; |
| 157 | HChar* preload_string = VG_(malloc)(preload_string_len); |
| 158 | vg_assert(preload_string); |
| 159 | |
| 160 | /* Determine if there's a vgpreload_<tool>.so file, and setup |
| 161 | preload_string. */ |
| 162 | preload_tool_path = VG_(malloc)(preload_tool_path_len); |
| 163 | vg_assert(preload_tool_path); |
| 164 | VG_(snprintf)(preload_tool_path, preload_tool_path_len, |
| 165 | "%s/vgpreload_%s.so", VG_(libdir), toolname); |
| 166 | if (VG_(access)(preload_tool_path, True/*r*/, False/*w*/, False/*x*/) == 0) { |
| 167 | VG_(snprintf)(preload_string, preload_string_len, "%s/%s:%s", |
| 168 | VG_(libdir), preload_core_so, preload_tool_path); |
| 169 | } else { |
| 170 | VG_(snprintf)(preload_string, preload_string_len, "%s/%s", |
| 171 | VG_(libdir), preload_core_so); |
| 172 | } |
| 173 | VG_(free)(preload_tool_path); |
| 174 | |
| 175 | VG_(debugLog)(1, "main", "preload_string = %s\n", preload_string); |
| 176 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 177 | /* Count the original size of the env */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 178 | envc = 0; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 179 | for (cpp = origenv; cpp && *cpp; cpp++) |
| 180 | envc++; |
| 181 | |
| 182 | /* Allocate a new space */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 183 | ret = VG_(malloc) (sizeof(HChar *) * (envc+1+1)); /* 1 new entry + NULL */ |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 184 | vg_assert(ret); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 185 | |
| 186 | /* copy it over */ |
| 187 | for (cpp = ret; *origenv; ) |
| 188 | *cpp++ = *origenv++; |
| 189 | *cpp = NULL; |
| 190 | |
| 191 | vg_assert(envc == (cpp - ret)); |
| 192 | |
| 193 | /* Walk over the new environment, mashing as we go */ |
| 194 | for (cpp = ret; cpp && *cpp; cpp++) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 195 | if (VG_(memcmp)(*cpp, ld_preload, ld_preload_len) == 0) { |
| 196 | Int len = VG_(strlen)(*cpp) + preload_string_len; |
| 197 | HChar *cp = VG_(malloc)(len); |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 198 | vg_assert(cp); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 199 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 200 | VG_(snprintf)(cp, len, "%s%s:%s", |
| 201 | ld_preload, preload_string, (*cpp)+ld_preload_len); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 202 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 203 | *cpp = cp; |
| 204 | |
| 205 | ld_preload_done = True; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | /* Add the missing bits */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 210 | if (!ld_preload_done) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 211 | Int len = ld_preload_len + preload_string_len; |
| 212 | HChar *cp = VG_(malloc) (len); |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 213 | vg_assert(cp); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 214 | |
| 215 | VG_(snprintf)(cp, len, "%s%s", ld_preload, preload_string); |
| 216 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 217 | ret[envc++] = cp; |
| 218 | } |
| 219 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 220 | /* ret[0 .. envc-1] is live now. */ |
| 221 | /* Find and remove a binding for VALGRIND_LAUNCHER. */ |
| 222 | for (i = 0; i < envc; i++) |
| 223 | if (0 == VG_(memcmp(ret[i], v_launcher, v_launcher_len))) |
| 224 | break; |
| 225 | |
| 226 | if (i < envc) { |
| 227 | for (; i < envc-1; i++) |
| 228 | ret[i] = ret[i+1]; |
| 229 | envc--; |
| 230 | } |
| 231 | |
| 232 | VG_(free)(preload_string); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 233 | ret[envc] = NULL; |
| 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 238 | |
| 239 | /* Add a string onto the string table, and return its address */ |
| 240 | static char *copy_str(char **tab, const char *str) |
| 241 | { |
| 242 | char *cp = *tab; |
| 243 | char *orig = cp; |
| 244 | |
| 245 | while(*str) |
| 246 | *cp++ = *str++; |
| 247 | *cp++ = '\0'; |
| 248 | |
| 249 | if (0) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 250 | VG_(printf)("copied %p \"%s\" len %lld\n", orig, orig, (Long)(cp-orig)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 251 | |
| 252 | *tab = cp; |
| 253 | |
| 254 | return orig; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | This sets up the client's initial stack, containing the args, |
| 259 | environment and aux vector. |
| 260 | |
| 261 | The format of the stack is: |
| 262 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 263 | higher address +-----------------+ <- clstack_end |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 264 | | | |
| 265 | : string table : |
| 266 | | | |
| 267 | +-----------------+ |
| 268 | | AT_NULL | |
| 269 | - - |
| 270 | | auxv | |
| 271 | +-----------------+ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 272 | | NULL | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 273 | - - |
| 274 | | envp | |
| 275 | +-----------------+ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 276 | | NULL | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 277 | - - |
| 278 | | argv | |
| 279 | +-----------------+ |
| 280 | | argc | |
nethercote | 4ad7431 | 2004-10-26 09:59:49 +0000 | [diff] [blame] | 281 | lower address +-----------------+ <- sp |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 282 | | undefined | |
| 283 | : : |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 284 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 285 | Allocate and create the initial client stack. It is allocated down |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 286 | from clstack_end, which was previously determined by the address |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 287 | space manager. The returned value is the SP value for the client. |
| 288 | |
| 289 | The client's auxv is created by copying and modifying our own one. |
| 290 | As a side effect of scanning our own auxv, some important bits of |
| 291 | info are collected: |
| 292 | |
| 293 | VG_(cache_line_size_ppc32) // ppc32 only -- cache line size |
| 294 | VG_(have_altivec_ppc32) // ppc32 only -- is Altivec supported? |
| 295 | */ |
| 296 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 297 | static |
| 298 | Addr setup_client_stack( void* init_sp, |
| 299 | char** orig_envp, |
| 300 | const struct exeinfo *info, |
| 301 | UInt** client_auxv, |
| 302 | Addr clstack_end, |
| 303 | SizeT clstack_max_size ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 304 | { |
njn | 8340749 | 2005-06-19 16:10:47 +0000 | [diff] [blame] | 305 | SysRes res; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 306 | char **cpp; |
| 307 | char *strtab; /* string table */ |
| 308 | char *stringbase; |
nethercote | a3c3cf2 | 2004-11-01 18:38:00 +0000 | [diff] [blame] | 309 | Addr *ptr; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 310 | struct ume_auxv *auxv; |
| 311 | const struct ume_auxv *orig_auxv; |
| 312 | const struct ume_auxv *cauxv; |
| 313 | unsigned stringsize; /* total size of strings in bytes */ |
| 314 | unsigned auxsize; /* total size of auxv in bytes */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 315 | Int argc; /* total argc */ |
| 316 | Int envc; /* total number of env vars */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 317 | unsigned stacksize; /* total client stack size */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 318 | Addr client_SP; /* client stack base (initial SP) */ |
| 319 | Addr clstack_start; |
| 320 | Int i; |
| 321 | Bool have_exename; |
| 322 | |
| 323 | vg_assert(VG_IS_PAGE_ALIGNED(clstack_end+1)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 324 | |
| 325 | /* use our own auxv as a prototype */ |
njn | 62ff0f2 | 2005-06-21 23:03:36 +0000 | [diff] [blame] | 326 | orig_auxv = VG_(find_auxv)(init_sp); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 327 | |
| 328 | /* ==================== compute sizes ==================== */ |
| 329 | |
| 330 | /* first of all, work out how big the client stack will be */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 331 | stringsize = 0; |
| 332 | have_exename = VG_(args_the_exename) != NULL; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 333 | |
| 334 | /* paste on the extra args if the loader needs them (ie, the #! |
| 335 | interpreter and its argument) */ |
| 336 | argc = 0; |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 337 | if (info->interp_name != NULL) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 338 | argc++; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 339 | stringsize += VG_(strlen)(info->interp_name) + 1; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 340 | } |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 341 | if (info->interp_args != NULL) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 342 | argc++; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 343 | stringsize += VG_(strlen)(info->interp_args) + 1; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /* now scan the args we're given... */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 347 | if (have_exename) |
| 348 | stringsize += VG_(strlen)( VG_(args_the_exename) ) + 1; |
| 349 | |
| 350 | for (i = 0; i < VG_(args_for_client).used; i++) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 351 | argc++; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 352 | stringsize += VG_(strlen)( VG_(args_for_client).strs[i] ) + 1; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 353 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 354 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 355 | /* ...and the environment */ |
| 356 | envc = 0; |
| 357 | for (cpp = orig_envp; cpp && *cpp; cpp++) { |
| 358 | envc++; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 359 | stringsize += VG_(strlen)(*cpp) + 1; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* now, how big is the auxv? */ |
| 363 | auxsize = sizeof(*auxv); /* there's always at least one entry: AT_NULL */ |
| 364 | for (cauxv = orig_auxv; cauxv->a_type != AT_NULL; cauxv++) { |
| 365 | if (cauxv->a_type == AT_PLATFORM) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 366 | stringsize += VG_(strlen)(cauxv->u.a_ptr) + 1; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 367 | auxsize += sizeof(*cauxv); |
| 368 | } |
| 369 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 370 | # if defined(VGP_ppc32_linux) |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 371 | auxsize += 2 * sizeof(*cauxv); |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 372 | # endif |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 373 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 374 | /* OK, now we know how big the client stack is */ |
| 375 | stacksize = |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 376 | sizeof(Word) + /* argc */ |
| 377 | (have_exename ? sizeof(char **) : 0) + /* argc[0] == exename */ |
| 378 | sizeof(char **)*argc + /* argv */ |
| 379 | sizeof(char **) + /* terminal NULL */ |
| 380 | sizeof(char **)*envc + /* envp */ |
| 381 | sizeof(char **) + /* terminal NULL */ |
| 382 | auxsize + /* auxv */ |
sewardj | 90d3854 | 2005-09-27 23:53:09 +0000 | [diff] [blame] | 383 | VG_ROUNDUP(stringsize, sizeof(Word)); /* strings (aligned) */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 384 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 385 | if (0) VG_(printf)("stacksize = %d\n", stacksize); |
| 386 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 387 | /* client_SP is the client's stack pointer */ |
| 388 | client_SP = clstack_end - stacksize; |
| 389 | client_SP = VG_ROUNDDN(client_SP, 16); /* make stack 16 byte aligned */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 390 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 391 | /* base of the string table (aligned) */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 392 | stringbase = strtab = (char *)clstack_end |
| 393 | - VG_ROUNDUP(stringsize, sizeof(int)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 394 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 395 | clstack_start = VG_PGROUNDDN(client_SP); |
| 396 | |
| 397 | /* The max stack size */ |
| 398 | clstack_max_size = VG_PGROUNDUP(clstack_max_size); |
| 399 | |
| 400 | /* Record stack extent -- needed for stack-change code. */ |
| 401 | VG_(clstk_base) = clstack_start; |
| 402 | VG_(clstk_end) = clstack_end; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 403 | |
sewardj | 79048ce | 2005-02-18 08:28:32 +0000 | [diff] [blame] | 404 | if (0) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 405 | VG_(printf)("stringsize=%d auxsize=%d stacksize=%d maxsize=0x%x\n" |
| 406 | "clstack_start %p\n" |
| 407 | "clstack_end %p\n", |
| 408 | stringsize, auxsize, stacksize, (Int)clstack_max_size, |
| 409 | (void*)clstack_start, (void*)clstack_end); |
nethercote | 5ee67ca | 2004-06-22 14:00:09 +0000 | [diff] [blame] | 410 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 411 | /* ==================== allocate space ==================== */ |
| 412 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 413 | { SizeT anon_size = clstack_end - clstack_start + 1; |
| 414 | SizeT resvn_size = clstack_max_size - anon_size; |
| 415 | Addr anon_start = clstack_start; |
| 416 | Addr resvn_start = anon_start - resvn_size; |
| 417 | SizeT inner_HACK = 0; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 418 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 419 | vg_assert(VG_IS_PAGE_ALIGNED(anon_size)); |
| 420 | vg_assert(VG_IS_PAGE_ALIGNED(resvn_size)); |
| 421 | vg_assert(VG_IS_PAGE_ALIGNED(anon_start)); |
| 422 | vg_assert(VG_IS_PAGE_ALIGNED(resvn_start)); |
| 423 | vg_assert(resvn_start == clstack_end + 1 - clstack_max_size); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 424 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 425 | # ifdef ENABLE_INNER |
| 426 | inner_HACK = 1024*1024; // create 1M non-fault-extending stack |
| 427 | # endif |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 428 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 429 | if (0) |
| 430 | VG_(printf)("%p 0x%x %p 0x%x\n", |
| 431 | resvn_start, resvn_size, anon_start, anon_size); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 432 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 433 | /* Create a shrinkable reservation followed by an anonymous |
| 434 | segment. Together these constitute a growdown stack. */ |
| 435 | Bool ok = VG_(am_create_reservation)( |
| 436 | resvn_start, |
| 437 | resvn_size -inner_HACK, |
| 438 | SmUpper, |
| 439 | anon_size +inner_HACK |
| 440 | ); |
| 441 | vg_assert(ok); |
| 442 | /* allocate a stack - mmap enough space for the stack */ |
| 443 | res = VG_(am_mmap_anon_fixed_client)( |
| 444 | anon_start -inner_HACK, |
| 445 | anon_size +inner_HACK, |
| 446 | VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC |
| 447 | ); |
| 448 | vg_assert(!res.isError); |
| 449 | } |
| 450 | |
| 451 | /* ==================== create client stack ==================== */ |
| 452 | |
| 453 | ptr = (Addr*)client_SP; |
| 454 | |
| 455 | /* --- client argc --- */ |
| 456 | *ptr++ = argc + (have_exename ? 1 : 0); |
| 457 | |
| 458 | /* --- client argv --- */ |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 459 | if (info->interp_name) { |
nethercote | a3c3cf2 | 2004-11-01 18:38:00 +0000 | [diff] [blame] | 460 | *ptr++ = (Addr)copy_str(&strtab, info->interp_name); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 461 | VG_(free)(info->interp_name); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 462 | } |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 463 | if (info->interp_args) { |
nethercote | a3c3cf2 | 2004-11-01 18:38:00 +0000 | [diff] [blame] | 464 | *ptr++ = (Addr)copy_str(&strtab, info->interp_args); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 465 | VG_(free)(info->interp_args); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 466 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 467 | |
| 468 | if (have_exename) |
| 469 | *ptr++ = (Addr)copy_str(&strtab, VG_(args_the_exename)); |
| 470 | |
| 471 | for (i = 0; i < VG_(args_for_client).used; i++) { |
| 472 | *ptr++ = (Addr)copy_str(&strtab, VG_(args_for_client).strs[i]); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 473 | } |
| 474 | *ptr++ = 0; |
| 475 | |
| 476 | /* --- envp --- */ |
| 477 | VG_(client_envp) = (Char **)ptr; |
| 478 | for (cpp = orig_envp; cpp && *cpp; ptr++, cpp++) |
nethercote | a3c3cf2 | 2004-11-01 18:38:00 +0000 | [diff] [blame] | 479 | *ptr = (Addr)copy_str(&strtab, *cpp); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 480 | *ptr++ = 0; |
| 481 | |
| 482 | /* --- auxv --- */ |
| 483 | auxv = (struct ume_auxv *)ptr; |
| 484 | *client_auxv = (UInt *)auxv; |
| 485 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 486 | # if defined(VGP_ppc32_linux) |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 487 | auxv[0].a_type = AT_IGNOREPPC; |
| 488 | auxv[0].u.a_val = AT_IGNOREPPC; |
| 489 | auxv[1].a_type = AT_IGNOREPPC; |
| 490 | auxv[1].u.a_val = AT_IGNOREPPC; |
| 491 | auxv += 2; |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 492 | # endif |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 493 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 494 | for (; orig_auxv->a_type != AT_NULL; auxv++, orig_auxv++) { |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 495 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 496 | /* copy the entry... */ |
| 497 | *auxv = *orig_auxv; |
| 498 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 499 | /* ...and fix up / examine the copy */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 500 | switch(auxv->a_type) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 501 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 502 | case AT_IGNORE: |
| 503 | case AT_PHENT: |
| 504 | case AT_PAGESZ: |
| 505 | case AT_FLAGS: |
| 506 | case AT_NOTELF: |
| 507 | case AT_UID: |
| 508 | case AT_EUID: |
| 509 | case AT_GID: |
| 510 | case AT_EGID: |
| 511 | case AT_CLKTCK: |
| 512 | case AT_FPUCW: |
| 513 | case AT_SYSINFO: |
| 514 | /* All these are pointerless, so we don't need to do |
| 515 | anything about them. */ |
| 516 | break; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 517 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 518 | case AT_PHDR: |
| 519 | if (info->phdr == 0) |
| 520 | auxv->a_type = AT_IGNORE; |
| 521 | else |
| 522 | auxv->u.a_val = info->phdr; |
| 523 | break; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 524 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 525 | case AT_PHNUM: |
| 526 | if (info->phdr == 0) |
| 527 | auxv->a_type = AT_IGNORE; |
| 528 | else |
| 529 | auxv->u.a_val = info->phnum; |
| 530 | break; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 531 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 532 | case AT_BASE: |
| 533 | auxv->u.a_val = info->interp_base; |
| 534 | break; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 535 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 536 | case AT_PLATFORM: |
| 537 | /* points to a platform description string */ |
| 538 | auxv->u.a_ptr = copy_str(&strtab, orig_auxv->u.a_ptr); |
| 539 | break; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 540 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 541 | case AT_ENTRY: |
| 542 | auxv->u.a_val = info->entry; |
| 543 | break; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 544 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 545 | case AT_HWCAP: |
| 546 | # if defined(VGP_ppc32_linux) |
| 547 | /* Acquire altivecness info */ |
| 548 | VG_(debugLog)(1, "main", "PPC32 hwcaps: 0x%x\n", |
| 549 | (UInt)auxv->u.a_val); |
| 550 | if (auxv->u.a_val & 0x10000000) |
| 551 | VG_(have_altivec_ppc32) = 1; |
| 552 | VG_(debugLog)(1, "main", "PPC32 AltiVec support: %u\n", |
| 553 | VG_(have_altivec_ppc32)); |
| 554 | # endif |
| 555 | break; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 556 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 557 | case AT_DCACHEBSIZE: |
| 558 | case AT_ICACHEBSIZE: |
| 559 | case AT_UCACHEBSIZE: |
| 560 | # if defined(VGP_ppc32_linux) |
| 561 | /* acquire cache info */ |
| 562 | if (auxv->u.a_val > 0) { |
| 563 | VG_(cache_line_size_ppc32) = auxv->u.a_val; |
| 564 | VG_(debugLog)(1, "main", |
| 565 | "PPC32 cache line size %u (type %u)\n", |
| 566 | (UInt)auxv->u.a_val, (UInt)auxv->a_type ); |
| 567 | } |
| 568 | # endif |
| 569 | break; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 570 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 571 | # if defined(VGP_ppc32_linux) |
| 572 | case AT_IGNOREPPC: |
| 573 | break; |
| 574 | # endif |
| 575 | |
| 576 | case AT_SECURE: |
| 577 | /* If this is 1, then it means that this program is |
| 578 | running suid, and therefore the dynamic linker should |
| 579 | be careful about LD_PRELOAD, etc. However, since |
| 580 | stage1 (the thing the kernel actually execve's) should |
| 581 | never be SUID, and we need LD_PRELOAD to work for the |
| 582 | client, we set AT_SECURE to 0. */ |
| 583 | auxv->u.a_val = 0; |
| 584 | break; |
| 585 | |
| 586 | # if !defined(VGP_ppc32_linux) |
| 587 | case AT_SYSINFO_EHDR: |
| 588 | /* Trash this, because we don't reproduce it */ |
| 589 | auxv->a_type = AT_IGNORE; |
| 590 | break; |
| 591 | # endif |
| 592 | |
| 593 | default: |
| 594 | /* stomp out anything we don't know about */ |
| 595 | VG_(debugLog)(2, "main", |
| 596 | "stomping auxv entry %lld\n", |
| 597 | (ULong)auxv->a_type); |
| 598 | auxv->a_type = AT_IGNORE; |
| 599 | break; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | *auxv = *orig_auxv; |
| 603 | vg_assert(auxv->a_type == AT_NULL); |
| 604 | |
| 605 | vg_assert((strtab-stringbase) == stringsize); |
| 606 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 607 | /* client_SP is pointing at client's argc/argv */ |
nethercote | 5ee67ca | 2004-06-22 14:00:09 +0000 | [diff] [blame] | 608 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 609 | if (0) VG_(printf)("startup SP = %p\n", client_SP); |
| 610 | return client_SP; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 611 | } |
| 612 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 613 | |
| 614 | /* Allocate the client data segment. It is an expandable anonymous |
| 615 | mapping abutting a shrinkable reservation of size max_dseg_size. |
| 616 | The data segment starts at VG_(brk_base), which is page-aligned, |
| 617 | and runs up to VG_(brk_limit), which isn't. */ |
| 618 | |
| 619 | static void setup_client_dataseg ( SizeT max_size ) |
| 620 | { |
| 621 | Bool ok; |
| 622 | SysRes sres; |
| 623 | Addr anon_start = VG_(brk_base); |
| 624 | SizeT anon_size = VKI_PAGE_SIZE; |
| 625 | Addr resvn_start = anon_start + anon_size; |
| 626 | SizeT resvn_size = max_size - anon_size; |
| 627 | |
| 628 | vg_assert(VG_IS_PAGE_ALIGNED(anon_size)); |
| 629 | vg_assert(VG_IS_PAGE_ALIGNED(resvn_size)); |
| 630 | vg_assert(VG_IS_PAGE_ALIGNED(anon_start)); |
| 631 | vg_assert(VG_IS_PAGE_ALIGNED(resvn_start)); |
| 632 | |
| 633 | /* Because there's been no brk activity yet: */ |
| 634 | vg_assert(VG_(brk_base) == VG_(brk_limit)); |
| 635 | |
| 636 | /* Try to create the data seg and associated reservation where |
| 637 | VG_(brk_base) says. */ |
| 638 | ok = VG_(am_create_reservation)( |
| 639 | resvn_start, |
| 640 | resvn_size, |
| 641 | SmLower, |
| 642 | anon_size |
| 643 | ); |
| 644 | |
| 645 | if (!ok) { |
| 646 | /* Hmm, that didn't work. Well, let aspacem suggest an address |
| 647 | it likes better, and try again with that. */ |
| 648 | anon_start = VG_(am_get_advisory_client_simple) |
| 649 | ( 0/*floating*/, anon_size+resvn_size, &ok ); |
| 650 | if (ok) { |
| 651 | resvn_start = anon_start + anon_size; |
| 652 | ok = VG_(am_create_reservation)( |
| 653 | resvn_start, |
| 654 | resvn_size, |
| 655 | SmLower, |
| 656 | anon_size |
| 657 | ); |
| 658 | if (ok) |
| 659 | VG_(brk_base) = VG_(brk_limit) = anon_start; |
| 660 | } |
| 661 | /* that too might have failed, but if it has, we're hosed: there |
| 662 | is no Plan C. */ |
| 663 | } |
| 664 | vg_assert(ok); |
| 665 | |
| 666 | sres = VG_(am_mmap_anon_fixed_client)( |
| 667 | anon_start, |
| 668 | anon_size, |
| 669 | VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC |
| 670 | ); |
| 671 | vg_assert(!sres.isError); |
| 672 | vg_assert(sres.val == anon_start); |
| 673 | } |
| 674 | |
| 675 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 676 | /*====================================================================*/ |
| 677 | /*=== Find executable ===*/ |
| 678 | /*====================================================================*/ |
| 679 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 680 | /* Scan a colon-separated list, and call a function on each element. |
| 681 | The string must be mutable, because we insert a temporary '\0', but |
| 682 | the string will end up unmodified. (*func) should return True if it |
| 683 | doesn't need to see any more. |
| 684 | |
| 685 | This routine will return True if (*func) returns True and False if |
| 686 | it reaches the end of the list without that happening. |
| 687 | */ |
| 688 | static Bool scan_colsep(char *colsep, Bool (*func)(const char *)) |
| 689 | { |
| 690 | char *cp, *entry; |
| 691 | int end; |
| 692 | |
| 693 | if (colsep == NULL || |
| 694 | *colsep == '\0') |
| 695 | return False; |
| 696 | |
| 697 | entry = cp = colsep; |
| 698 | |
| 699 | do { |
| 700 | end = (*cp == '\0'); |
| 701 | |
| 702 | if (*cp == ':' || *cp == '\0') { |
| 703 | char save = *cp; |
| 704 | |
| 705 | *cp = '\0'; |
| 706 | if ((*func)(entry)) { |
| 707 | *cp = save; |
| 708 | return True; |
| 709 | } |
| 710 | *cp = save; |
| 711 | entry = cp+1; |
| 712 | } |
| 713 | cp++; |
| 714 | } while(!end); |
| 715 | |
| 716 | return False; |
| 717 | } |
| 718 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 719 | /* Need a static copy because can't use dynamic mem allocation yet */ |
| 720 | static HChar executable_name[VKI_PATH_MAX]; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 721 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 722 | static Bool match_executable(const char *entry) |
| 723 | { |
| 724 | HChar buf[VG_(strlen)(entry) + VG_(strlen)(executable_name) + 3]; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 725 | |
| 726 | /* empty PATH element means . */ |
| 727 | if (*entry == '\0') |
| 728 | entry = "."; |
| 729 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 730 | VG_(snprintf)(buf, sizeof(buf), "%s/%s", entry, executable_name); |
| 731 | if (VG_(access)(buf, True/*r*/, False/*w*/, True/*x*/) == 0) { |
| 732 | VG_(strncpy)( executable_name, buf, VKI_PATH_MAX-1 ); |
| 733 | executable_name[VKI_PATH_MAX-1] = 0; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 734 | return True; |
| 735 | } |
| 736 | return False; |
| 737 | } |
| 738 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 739 | static HChar* find_executable ( HChar* exec ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 740 | { |
| 741 | vg_assert(NULL != exec); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 742 | VG_(strncpy)( executable_name, exec, VKI_PATH_MAX-1 ); |
| 743 | executable_name[VKI_PATH_MAX-1] = 0; |
| 744 | |
| 745 | if (VG_(strchr)(executable_name, '/') == NULL) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 746 | /* no '/' - we need to search the path */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 747 | HChar *path = VG_(getenv)("PATH"); |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 748 | scan_colsep(path, match_executable); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 749 | } |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 750 | return executable_name; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | |
| 754 | /*====================================================================*/ |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 755 | /*=== Command line errors ===*/ |
| 756 | /*====================================================================*/ |
| 757 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 758 | static void revert_to_stderr ( void ) |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 759 | { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 760 | vg_assert( !VG_(logging_to_socket) ); |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 761 | VG_(clo_log_fd) = 2; /* stderr */ |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | void VG_(bad_option) ( Char* opt ) |
| 765 | { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 766 | revert_to_stderr(); |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 767 | VG_(printf)("valgrind: Bad option '%s'; aborting.\n", opt); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 768 | VG_(printf)("valgrind: Use --help for more information.\n"); |
| 769 | VG_(exit)(1); |
| 770 | } |
| 771 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 772 | static void missing_prog ( void ) |
| 773 | { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 774 | revert_to_stderr(); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 775 | VG_(printf)("valgrind: no program specified\n"); |
| 776 | VG_(printf)("valgrind: Use --help for more information.\n"); |
| 777 | VG_(exit)(1); |
| 778 | } |
| 779 | |
| 780 | static void config_error ( Char* msg ) |
| 781 | { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 782 | revert_to_stderr(); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 783 | VG_(printf)("valgrind: Startup or configuration error:\n %s\n", msg); |
| 784 | VG_(printf)("valgrind: Unable to start up properly. Giving up.\n"); |
| 785 | VG_(exit)(1); |
| 786 | } |
| 787 | |
| 788 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 789 | /*====================================================================*/ |
| 790 | /*=== Loading the client ===*/ |
| 791 | /*====================================================================*/ |
| 792 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 793 | /* Load the client whose name is VG_(argv_the_exename). */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 794 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 795 | static void load_client ( /*OUT*/struct exeinfo* info, |
| 796 | /*OUT*/Addr* client_eip) |
| 797 | { |
| 798 | HChar* exec; |
| 799 | Int ret; |
| 800 | SysRes res; |
| 801 | |
| 802 | vg_assert( VG_(args_the_exename) != NULL); |
| 803 | exec = find_executable( VG_(args_the_exename) ); |
| 804 | |
| 805 | VG_(memset)(info, 0, sizeof(*info)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 806 | info->exe_base = VG_(client_base); |
| 807 | info->exe_end = VG_(client_end); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 808 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 809 | ret = VG_(do_exec)(exec, info); |
| 810 | if (ret != 0) { |
| 811 | VG_(printf)("valgrind: do_exec(%s) failed: %s\n", |
| 812 | exec, VG_(strerror)(ret)); |
| 813 | VG_(exit)(127); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 814 | } |
| 815 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 816 | /* Get hold of a file descriptor which refers to the client |
| 817 | executable. This is needed for attaching to GDB. */ |
| 818 | res = VG_(open)(exec, VKI_O_RDONLY, VKI_S_IRUSR); |
| 819 | if (!res.isError) |
| 820 | VG_(cl_exec_fd) = res.val; |
| 821 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 822 | /* Copy necessary bits of 'info' that were filled in */ |
| 823 | *client_eip = info->init_eip; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 824 | VG_(brk_base) = VG_(brk_limit) = VG_PGROUNDUP(info->brkbase); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 825 | } |
| 826 | |
nethercote | 969ecf1 | 2004-10-13 17:29:01 +0000 | [diff] [blame] | 827 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 828 | /*====================================================================*/ |
| 829 | /*=== Command-line: variables, processing, etc ===*/ |
| 830 | /*====================================================================*/ |
| 831 | |
| 832 | // See pub_{core,tool}_options.h for explanations of all these. |
| 833 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 834 | static void usage_NORETURN ( Bool debug_help ) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 835 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 836 | Char* usage1 = |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 837 | "usage: valgrind --tool=<toolname> [options] prog-and-args\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 838 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 839 | " common user options for all Valgrind tools, with defaults in [ ]:\n" |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 840 | " --tool=<name> use the Valgrind tool named <name> [memcheck]\n" |
nethercote | a76368b | 2004-06-16 11:56:29 +0000 | [diff] [blame] | 841 | " -h --help show this message\n" |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 842 | " --help-debug show this message, plus debugging options\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 843 | " --version show version\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 844 | " -q --quiet run silently; only print error msgs\n" |
| 845 | " -v --verbose be more verbose, incl counts of errors\n" |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 846 | " --trace-children=no|yes Valgrind-ise child processes? [no]\n" |
nethercote | 0d58850 | 2004-06-21 13:27:11 +0000 | [diff] [blame] | 847 | " --track-fds=no|yes track open file descriptors? [no]\n" |
thughes | 6233a38 | 2004-08-21 11:10:44 +0000 | [diff] [blame] | 848 | " --time-stamp=no|yes add timestamps to log messages? [no]\n" |
njn | ce54555 | 2005-07-25 22:36:52 +0000 | [diff] [blame] | 849 | " --log-fd=<number> log messages to file descriptor [2=stderr]\n" |
| 850 | " --log-file=<file> log messages to <file>.pid<pid>\n" |
| 851 | " --log-file-exactly=<file> log messages to <file>\n" |
| 852 | " --log-file-qualifier=<VAR> incorporate $VAR in logfile name [none]\n" |
| 853 | " --log-socket=ipaddr:port log messages to socket ipaddr:port\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 854 | "\n" |
| 855 | " uncommon user options for all Valgrind tools:\n" |
nethercote | 0d58850 | 2004-06-21 13:27:11 +0000 | [diff] [blame] | 856 | " --run-libc-freeres=no|yes free up glibc memory at exit? [yes]\n" |
sewardj | a0461a6 | 2005-09-28 01:14:32 +0000 | [diff] [blame^] | 857 | " --weird-hacks=hack1,hack2,... known hacks: lax-ioctls\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 858 | " enable-outer [none]\n" |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 859 | " --pointercheck=no|yes enforce client address space limits [yes]\n" |
sewardj | b1131a8 | 2005-03-19 15:12:21 +0000 | [diff] [blame] | 860 | " --show-emwarns=no|yes show warnings about emulation limits? [no]\n" |
sewardj | 6c3a219 | 2005-07-24 07:00:45 +0000 | [diff] [blame] | 861 | " --smc-check=none|stack|all checks for self-modifying code: none,\n" |
| 862 | " only for code found in stacks, or all [stack]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 863 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 864 | " user options for Valgrind tools that report errors:\n" |
njn | ce54555 | 2005-07-25 22:36:52 +0000 | [diff] [blame] | 865 | " --xml=yes all output is in XML (Memcheck/Nulgrind only)\n" |
njn | c8ec9f8 | 2005-07-20 03:03:28 +0000 | [diff] [blame] | 866 | " --xml-user-comment=STR copy STR verbatim to XML output\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 867 | " --demangle=no|yes automatically demangle C++ names? [yes]\n" |
njn | cc6cc14 | 2005-03-12 20:59:29 +0000 | [diff] [blame] | 868 | " --num-callers=<number> show <num> callers in stack traces [12]\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 869 | " --error-limit=no|yes stop showing new errors if too many? [yes]\n" |
| 870 | " --show-below-main=no|yes continue stack traces below main() [no]\n" |
| 871 | " --suppressions=<filename> suppress errors described in <filename>\n" |
sewardj | d153fae | 2005-01-10 17:24:47 +0000 | [diff] [blame] | 872 | " --gen-suppressions=no|yes|all print suppressions for errors? [no]\n" |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 873 | " --db-attach=no|yes start debugger when errors detected? [no]\n" |
| 874 | " --db-command=<command> command to start debugger [gdb -nw %%f %%p]\n" |
| 875 | " --input-fd=<number> file descriptor for input [0=stdin]\n" |
sewardj | 97724e5 | 2005-04-02 23:40:59 +0000 | [diff] [blame] | 876 | " --max-stackframe=<number> assume stack switch for SP changes larger\n" |
| 877 | " than <number> bytes [2000000]\n" |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 878 | "\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 879 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 880 | Char* usage2 = |
| 881 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 882 | " debugging options for all Valgrind tools:\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 883 | " --sanity-level=<number> level of sanity checking to do [1]\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 884 | " --profile=no|yes profile? (tool must be built for it) [no]\n" |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 885 | " --trace-flags=<XXXXXXXX> show generated code? (X = 0|1) [00000000]\n" |
| 886 | " --profile-flags=<XXXXXXXX> ditto, but for profiling (X = 0|1) [00000000]\n" |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 887 | " --trace-notbelow=<number> only show BBs above <number> [0]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 888 | " --trace-syscalls=no|yes show all system calls? [no]\n" |
| 889 | " --trace-signals=no|yes show signal handling details? [no]\n" |
| 890 | " --trace-symtab=no|yes show symbol table details? [no]\n" |
sewardj | ce058b0 | 2005-05-01 08:55:38 +0000 | [diff] [blame] | 891 | " --trace-cfi=no|yes show call-frame-info details? [no]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 892 | " --trace-sched=no|yes show thread scheduler details? [no]\n" |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 893 | " --wait-for-gdb=yes|no pause on startup to wait for gdb attach\n" |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 894 | #if 0 |
| 895 | " --model-pthreads=yes|no model the pthreads library [no]\n" |
| 896 | #endif |
| 897 | " --command-line-only=no|yes only use command line options [no]\n" |
njn | 613812e | 2005-03-11 04:57:30 +0000 | [diff] [blame] | 898 | "\n" |
| 899 | " --vex-iropt-verbosity 0 .. 9 [0]\n" |
| 900 | " --vex-iropt-level 0 .. 2 [2]\n" |
| 901 | " --vex-iropt-precise-memory-exns [no]\n" |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 902 | " --vex-iropt-unroll-thresh 0 .. 400 [120]\n" |
| 903 | " --vex-guest-max-insns 1 .. 100 [50]\n" |
| 904 | " --vex-guest-chase-thresh 0 .. 99 [10]\n" |
| 905 | "\n" |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 906 | " --trace-flags and --profile-flags values (omit the middle space):\n" |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 907 | " 1000 0000 show conversion into IR\n" |
| 908 | " 0100 0000 show after initial opt\n" |
| 909 | " 0010 0000 show after instrumentation\n" |
| 910 | " 0001 0000 show after second opt\n" |
| 911 | " 0000 1000 show after tree building\n" |
| 912 | " 0000 0100 show selecting insns\n" |
| 913 | " 0000 0010 show after reg-alloc\n" |
| 914 | " 0000 0001 show final assembly\n" |
| 915 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 916 | " debugging options for Valgrind tools that report errors\n" |
| 917 | " --dump-error=<number> show translation for basic block associated\n" |
| 918 | " with <number>'th error context [0=show none]\n" |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 919 | "\n"; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 920 | |
| 921 | Char* usage3 = |
| 922 | "\n" |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 923 | " Extra options read from ~/.valgrindrc, $VALGRIND_OPTS, ./.valgrindrc\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 924 | "\n" |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 925 | " Valgrind is Copyright (C) 2000-2005 Julian Seward et al.\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 926 | " and licensed under the GNU General Public License, version 2.\n" |
| 927 | " Bug reports, feedback, admiration, abuse, etc, to: %s.\n" |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 928 | "\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 929 | " Tools are copyright and licensed by their authors. See each\n" |
| 930 | " tool's start-up message for more information.\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 931 | "\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 932 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 933 | // Ensure the message goes to stdout |
| 934 | VG_(clo_log_fd) = 1; |
| 935 | vg_assert( !VG_(logging_to_socket) ); |
| 936 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 937 | VG_(printf)(usage1); |
| 938 | if (VG_(details).name) { |
| 939 | VG_(printf)(" user options for %s:\n", VG_(details).name); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 940 | if (VG_(needs).command_line_options) |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 941 | VG_TDICT_CALL(tool_print_usage); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 942 | else |
| 943 | VG_(printf)(" (none)\n"); |
| 944 | } |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 945 | if (debug_help) { |
| 946 | VG_(printf)(usage2); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 947 | |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 948 | if (VG_(details).name) { |
| 949 | VG_(printf)(" debugging options for %s:\n", VG_(details).name); |
| 950 | |
| 951 | if (VG_(needs).command_line_options) |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 952 | VG_TDICT_CALL(tool_print_debug_usage); |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 953 | else |
| 954 | VG_(printf)(" (none)\n"); |
| 955 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 956 | } |
nethercote | 421281e | 2003-11-20 16:20:55 +0000 | [diff] [blame] | 957 | VG_(printf)(usage3, VG_BUGS_TO); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 958 | VG_(exit)(0); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 959 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 960 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 961 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 962 | /* Peer at previously set up VG_(args_for_valgrind) and extract any |
| 963 | request for help and also the tool name. */ |
| 964 | |
| 965 | static void get_helprequest_and_toolname ( Int* need_help, HChar** tool ) |
| 966 | { |
| 967 | UInt i; |
| 968 | HChar* str; |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 969 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 970 | /* parse the options we have (only the options we care about now) */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 971 | for (i = 0; i < VG_(args_for_valgrind).used; i++) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 972 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 973 | str = VG_(args_for_valgrind).strs[i]; |
| 974 | vg_assert(str); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 975 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 976 | if (VG_STREQ(str, "--version")) { |
| 977 | VG_(printf)("valgrind-" VERSION "\n"); |
| 978 | VG_(exit)(0); |
| 979 | |
| 980 | } else if (VG_CLO_STREQ(str, "--help") || |
| 981 | VG_CLO_STREQ(str, "-h")) { |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 982 | *need_help = 1; |
| 983 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 984 | } else if (VG_CLO_STREQ(str, "--help-debug")) { |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 985 | *need_help = 2; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 986 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 987 | // The tool has already been determined, but we need to know the name |
| 988 | // here. |
| 989 | } else if (VG_CLO_STREQN(7, str, "--tool=")) { |
| 990 | *tool = &str[7]; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 993 | } |
| 994 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 995 | static Bool process_cmd_line_options( UInt* client_auxv, const char* toolname ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 996 | { |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 997 | SysRes sres; |
| 998 | Int i, eventually_log_fd; |
| 999 | Int toolname_len = VG_(strlen)(toolname); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1000 | enum { |
| 1001 | VgLogTo_Fd, |
| 1002 | VgLogTo_File, |
| 1003 | VgLogTo_FileExactly, |
| 1004 | VgLogTo_Socket |
| 1005 | } log_to = VgLogTo_Fd; // Where is logging output to be sent? |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1006 | |
nethercote | e173069 | 2003-11-20 10:38:07 +0000 | [diff] [blame] | 1007 | /* log to stderr by default, but usage message goes to stdout */ |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1008 | eventually_log_fd = 2; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1009 | |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 1010 | /* Check for sane path in ./configure --prefix=... */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1011 | if (VG_LIBDIR[0] != '/') |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 1012 | config_error("Please use absolute paths in " |
| 1013 | "./configure --prefix=... or --libdir=..."); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 1014 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1015 | for (i = 0; i < VG_(args_for_valgrind).used; i++) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1016 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1017 | HChar* arg = VG_(args_for_valgrind).strs[i]; |
| 1018 | HChar* colon = arg; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1019 | |
thughes | 3bfd5a0 | 2004-07-18 08:05:44 +0000 | [diff] [blame] | 1020 | /* Look for a colon in the switch name */ |
| 1021 | while (*colon && *colon != ':' && *colon != '=') |
| 1022 | colon++; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1023 | |
| 1024 | /* Look for matching "--toolname:foo" */ |
thughes | 3bfd5a0 | 2004-07-18 08:05:44 +0000 | [diff] [blame] | 1025 | if (*colon == ':') { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1026 | if (VG_CLO_STREQN(2, arg, "--") && |
| 1027 | VG_CLO_STREQN(toolname_len, arg+2, toolname) && |
| 1028 | VG_CLO_STREQN(1, arg+2+toolname_len, ":")) |
| 1029 | { |
| 1030 | // prefix matches, convert "--toolname:foo" to "--foo" |
| 1031 | if (0) |
| 1032 | VG_(printf)("tool-specific arg: %s\n", arg); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1033 | arg = VG_(strdup)(arg + toolname_len + 1); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1034 | arg[0] = '-'; |
| 1035 | arg[1] = '-'; |
| 1036 | |
| 1037 | } else { |
| 1038 | // prefix doesn't match, skip to next arg |
| 1039 | continue; |
| 1040 | } |
| 1041 | } |
| 1042 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1043 | /* Ignore these options - they've already been handled */ |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1044 | if (VG_CLO_STREQN( 7, arg, "--tool=")) goto skip_arg; |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1045 | if (VG_CLO_STREQN(20, arg, "--command-line-only=")) goto skip_arg; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1046 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1047 | if ( VG_CLO_STREQ(arg, "--")) goto skip_arg; |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 1048 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1049 | else if (VG_CLO_STREQ(arg, "-v") || |
| 1050 | VG_CLO_STREQ(arg, "--verbose")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1051 | VG_(clo_verbosity)++; |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 1052 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1053 | else if (VG_CLO_STREQ(arg, "-q") || |
| 1054 | VG_CLO_STREQ(arg, "--quiet")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1055 | VG_(clo_verbosity)--; |
| 1056 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1057 | else if (VG_CLO_STREQ(arg, "-d")) { |
| 1058 | /* do nothing */ |
| 1059 | } |
| 1060 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1061 | else VG_BOOL_CLO(arg, "--xml", VG_(clo_xml)) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1062 | else VG_BOOL_CLO(arg, "--db-attach", VG_(clo_db_attach)) |
| 1063 | else VG_BOOL_CLO(arg, "--demangle", VG_(clo_demangle)) |
| 1064 | else VG_BOOL_CLO(arg, "--error-limit", VG_(clo_error_limit)) |
| 1065 | else VG_BOOL_CLO(arg, "--pointercheck", VG_(clo_pointercheck)) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1066 | else VG_BOOL_CLO(arg, "--show-emwarns", VG_(clo_show_emwarns)) |
sewardj | 97724e5 | 2005-04-02 23:40:59 +0000 | [diff] [blame] | 1067 | else VG_NUM_CLO (arg, "--max-stackframe", VG_(clo_max_stackframe)) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1068 | else VG_BOOL_CLO(arg, "--profile", VG_(clo_profile)) |
| 1069 | else VG_BOOL_CLO(arg, "--run-libc-freeres", VG_(clo_run_libc_freeres)) |
| 1070 | else VG_BOOL_CLO(arg, "--show-below-main", VG_(clo_show_below_main)) |
| 1071 | else VG_BOOL_CLO(arg, "--time-stamp", VG_(clo_time_stamp)) |
| 1072 | else VG_BOOL_CLO(arg, "--track-fds", VG_(clo_track_fds)) |
| 1073 | else VG_BOOL_CLO(arg, "--trace-children", VG_(clo_trace_children)) |
| 1074 | else VG_BOOL_CLO(arg, "--trace-sched", VG_(clo_trace_sched)) |
| 1075 | else VG_BOOL_CLO(arg, "--trace-signals", VG_(clo_trace_signals)) |
| 1076 | else VG_BOOL_CLO(arg, "--trace-symtab", VG_(clo_trace_symtab)) |
sewardj | ce058b0 | 2005-05-01 08:55:38 +0000 | [diff] [blame] | 1077 | else VG_BOOL_CLO(arg, "--trace-cfi", VG_(clo_trace_cfi)) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1078 | else VG_BOOL_CLO(arg, "--trace-redir", VG_(clo_trace_redir)) |
| 1079 | else VG_BOOL_CLO(arg, "--trace-syscalls", VG_(clo_trace_syscalls)) |
| 1080 | else VG_BOOL_CLO(arg, "--trace-pthreads", VG_(clo_trace_pthreads)) |
| 1081 | else VG_BOOL_CLO(arg, "--wait-for-gdb", VG_(clo_wait_for_gdb)) |
| 1082 | else VG_BOOL_CLO(arg, "--model-pthreads", VG_(clo_model_pthreads)) |
sewardj | 72f98ff | 2002-06-13 17:23:38 +0000 | [diff] [blame] | 1083 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1084 | else VG_STR_CLO (arg, "--db-command", VG_(clo_db_command)) |
| 1085 | else VG_STR_CLO (arg, "--weird-hacks", VG_(clo_weird_hacks)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1086 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1087 | else VG_NUM_CLO (arg, "--dump-error", VG_(clo_dump_error)) |
| 1088 | else VG_NUM_CLO (arg, "--input-fd", VG_(clo_input_fd)) |
| 1089 | else VG_NUM_CLO (arg, "--sanity-level", VG_(clo_sanity_level)) |
| 1090 | else VG_BNUM_CLO(arg, "--num-callers", VG_(clo_backtrace_size), 1, |
| 1091 | VG_DEEPEST_BACKTRACE) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1092 | |
sewardj | 6c3a219 | 2005-07-24 07:00:45 +0000 | [diff] [blame] | 1093 | else if (VG_CLO_STREQ(arg, "--smc-check=none")) |
| 1094 | VG_(clo_smc_check) = Vg_SmcNone; |
| 1095 | else if (VG_CLO_STREQ(arg, "--smc-check=stack")) |
| 1096 | VG_(clo_smc_check) = Vg_SmcStack; |
| 1097 | else if (VG_CLO_STREQ(arg, "--smc-check=all")) |
| 1098 | VG_(clo_smc_check) = Vg_SmcAll; |
sewardj | 26412bd | 2005-07-07 10:05:05 +0000 | [diff] [blame] | 1099 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1100 | else VG_BNUM_CLO(arg, "--vex-iropt-verbosity", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1101 | VG_(clo_vex_control).iropt_verbosity, 0, 10) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1102 | else VG_BNUM_CLO(arg, "--vex-iropt-level", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1103 | VG_(clo_vex_control).iropt_level, 0, 2) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1104 | else VG_BOOL_CLO(arg, "--vex-iropt-precise-memory-exns", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1105 | VG_(clo_vex_control).iropt_precise_memory_exns) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1106 | else VG_BNUM_CLO(arg, "--vex-iropt-unroll-thresh", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1107 | VG_(clo_vex_control).iropt_unroll_thresh, 0, 400) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1108 | else VG_BNUM_CLO(arg, "--vex-guest-max-insns", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1109 | VG_(clo_vex_control).guest_max_insns, 1, 100) |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1110 | else VG_BNUM_CLO(arg, "--vex-guest-chase-thresh", |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1111 | VG_(clo_vex_control).guest_chase_thresh, 0, 99) |
| 1112 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1113 | else if (VG_CLO_STREQN(9, arg, "--log-fd=")) { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1114 | log_to = VgLogTo_Fd; |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1115 | VG_(clo_log_name) = NULL; |
| 1116 | eventually_log_fd = (Int)VG_(atoll)(&arg[9]); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1119 | else if (VG_CLO_STREQN(11, arg, "--log-file=")) { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1120 | log_to = VgLogTo_File; |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1121 | VG_(clo_log_name) = &arg[11]; |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1122 | } |
njn | d6bc3c3 | 2005-03-27 00:44:31 +0000 | [diff] [blame] | 1123 | |
sewardj | ad31116 | 2005-07-19 11:25:02 +0000 | [diff] [blame] | 1124 | else if (VG_CLO_STREQN(11, arg, "--log-file=")) { |
| 1125 | log_to = VgLogTo_File; |
| 1126 | VG_(clo_log_name) = &arg[11]; |
| 1127 | } |
| 1128 | |
| 1129 | else if (VG_CLO_STREQN(21, arg, "--log-file-qualifier=")) { |
| 1130 | VG_(clo_log_file_qualifier) = &arg[21]; |
| 1131 | } |
| 1132 | |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1133 | else if (VG_CLO_STREQN(19, arg, "--log-file-exactly=")) { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1134 | log_to = VgLogTo_FileExactly; |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1135 | VG_(clo_log_name) = &arg[19]; |
| 1136 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1137 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1138 | else if (VG_CLO_STREQN(13, arg, "--log-socket=")) { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1139 | log_to = VgLogTo_Socket; |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1140 | VG_(clo_log_name) = &arg[13]; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
sewardj | 768db0e | 2005-07-19 14:18:56 +0000 | [diff] [blame] | 1143 | else if (VG_CLO_STREQN(19, arg, "--xml-user-comment=")) { |
| 1144 | VG_(clo_xml_user_comment) = &arg[19]; |
| 1145 | } |
| 1146 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1147 | else if (VG_CLO_STREQN(15, arg, "--suppressions=")) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1148 | if (VG_(clo_n_suppressions) >= VG_CLO_MAX_SFILES) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1149 | VG_(message)(Vg_UserMsg, "Too many suppression files specified."); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1150 | VG_(message)(Vg_UserMsg, |
| 1151 | "Increase VG_CLO_MAX_SFILES and recompile."); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1152 | VG_(bad_option)(arg); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1153 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1154 | VG_(clo_suppressions)[VG_(clo_n_suppressions)] = &arg[15]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1155 | VG_(clo_n_suppressions)++; |
| 1156 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1157 | |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1158 | /* "stuvwxyz" --> stuvwxyz (binary) */ |
| 1159 | else if (VG_CLO_STREQN(14, arg, "--trace-flags=")) { |
| 1160 | Int j; |
| 1161 | char* opt = & arg[14]; |
| 1162 | |
| 1163 | if (8 != VG_(strlen)(opt)) { |
| 1164 | VG_(message)(Vg_UserMsg, |
| 1165 | "--trace-flags argument must have 8 digits"); |
| 1166 | VG_(bad_option)(arg); |
| 1167 | } |
| 1168 | for (j = 0; j < 8; j++) { |
| 1169 | if ('0' == opt[j]) { /* do nothing */ } |
| 1170 | else if ('1' == opt[j]) VG_(clo_trace_flags) |= (1 << (7-j)); |
| 1171 | else { |
| 1172 | VG_(message)(Vg_UserMsg, "--trace-flags argument can only " |
| 1173 | "contain 0s and 1s"); |
| 1174 | VG_(bad_option)(arg); |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | /* "stuvwxyz" --> stuvwxyz (binary) */ |
| 1180 | else if (VG_CLO_STREQN(16, arg, "--profile-flags=")) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1181 | Int j; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1182 | char* opt = & arg[16]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1183 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 1184 | if (8 != VG_(strlen)(opt)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1185 | VG_(message)(Vg_UserMsg, |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1186 | "--profile-flags argument must have 8 digits"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1187 | VG_(bad_option)(arg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1188 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 1189 | for (j = 0; j < 8; j++) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1190 | if ('0' == opt[j]) { /* do nothing */ } |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1191 | else if ('1' == opt[j]) VG_(clo_profile_flags) |= (1 << (7-j)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1192 | else { |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 1193 | VG_(message)(Vg_UserMsg, "--profile-flags argument can only " |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1194 | "contain 0s and 1s"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1195 | VG_(bad_option)(arg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1199 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 1200 | else VG_NUM_CLO (arg, "--trace-notbelow", VG_(clo_trace_notbelow)) |
sewardj | c771b29 | 2004-11-30 18:55:21 +0000 | [diff] [blame] | 1201 | |
sewardj | d153fae | 2005-01-10 17:24:47 +0000 | [diff] [blame] | 1202 | else if (VG_CLO_STREQ(arg, "--gen-suppressions=no")) |
| 1203 | VG_(clo_gen_suppressions) = 0; |
| 1204 | else if (VG_CLO_STREQ(arg, "--gen-suppressions=yes")) |
| 1205 | VG_(clo_gen_suppressions) = 1; |
| 1206 | else if (VG_CLO_STREQ(arg, "--gen-suppressions=all")) |
| 1207 | VG_(clo_gen_suppressions) = 2; |
| 1208 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1209 | else if ( ! VG_(needs).command_line_options |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1210 | || ! VG_TDICT_CALL(tool_process_cmd_line_option, arg) ) { |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1211 | VG_(bad_option)(arg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1212 | } |
njn | 8c0b3bb | 2005-03-12 21:20:39 +0000 | [diff] [blame] | 1213 | skip_arg: |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1214 | if (arg != VG_(args_for_valgrind).strs[i]) { |
| 1215 | VG_(free)(arg); |
| 1216 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
sewardj | 998d40d | 2004-12-06 14:24:52 +0000 | [diff] [blame] | 1219 | /* Make VEX control parameters sane */ |
| 1220 | |
| 1221 | if (VG_(clo_vex_control).guest_chase_thresh |
| 1222 | >= VG_(clo_vex_control).guest_max_insns) |
| 1223 | VG_(clo_vex_control).guest_chase_thresh |
| 1224 | = VG_(clo_vex_control).guest_max_insns - 1; |
| 1225 | |
| 1226 | if (VG_(clo_vex_control).guest_chase_thresh < 0) |
| 1227 | VG_(clo_vex_control).guest_chase_thresh = 0; |
| 1228 | |
| 1229 | /* Check various option values */ |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 1230 | |
njn | f9ebf67 | 2003-05-12 21:41:30 +0000 | [diff] [blame] | 1231 | if (VG_(clo_verbosity) < 0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1232 | VG_(clo_verbosity) = 0; |
| 1233 | |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 1234 | if (VG_(clo_db_attach) && VG_(clo_trace_children)) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1235 | VG_(message)(Vg_UserMsg, ""); |
| 1236 | VG_(message)(Vg_UserMsg, |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 1237 | "--db-attach=yes conflicts with --trace-children=yes"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1238 | VG_(message)(Vg_UserMsg, |
| 1239 | "Please choose one or the other, but not both."); |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 1240 | VG_(bad_option)("--db-attach=yes and --trace-children=yes"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1243 | if (VG_(clo_gen_suppressions) > 0 && |
| 1244 | !VG_(needs).core_errors && !VG_(needs).tool_errors) { |
| 1245 | VG_(message)(Vg_UserMsg, |
| 1246 | "Can't use --gen-suppressions= with this tool,"); |
| 1247 | VG_(message)(Vg_UserMsg, |
| 1248 | "as it doesn't generate errors."); |
| 1249 | VG_(bad_option)("--gen-suppressions="); |
| 1250 | } |
| 1251 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1252 | /* If we've been asked to emit XML, mash around various other |
| 1253 | options so as to constrain the output somewhat, and to remove |
| 1254 | any need for user input during the run. */ |
| 1255 | if (VG_(clo_xml)) { |
| 1256 | /* Disable suppression generation (requires user input) */ |
| 1257 | VG_(clo_gen_suppressions) = 0; |
| 1258 | /* Disable attaching to GDB (requires user input) */ |
| 1259 | VG_(clo_db_attach) = False; |
| 1260 | /* Set a known verbosity level */ |
| 1261 | VG_(clo_verbosity) = 1; |
| 1262 | /* Disable error limits (this might be a bad idea!) */ |
| 1263 | VG_(clo_error_limit) = False; |
| 1264 | /* Disable emulation warnings */ |
| 1265 | VG_(clo_show_emwarns) = False; |
| 1266 | /* Disable waiting for GDB to debug Valgrind */ |
| 1267 | VG_(clo_wait_for_gdb) = False; |
| 1268 | /* No file-descriptor leak checking yet */ |
| 1269 | VG_(clo_track_fds) = False; |
sewardj | 21f7f0c | 2005-07-06 19:46:48 +0000 | [diff] [blame] | 1270 | /* Disable timestamped output */ |
| 1271 | VG_(clo_time_stamp) = False; |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1272 | /* Also, we want to set options for the leak checker, but that |
| 1273 | will have to be done in Memcheck's flag-handling code, not |
| 1274 | here. */ |
| 1275 | } |
| 1276 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1277 | /* All non-logging-related options have been checked. If the logging |
| 1278 | option specified is ok, we can switch to it, as we know we won't |
| 1279 | have to generate any other command-line-related error messages. |
| 1280 | (So far we should be still attached to stderr, so we can show on |
| 1281 | the terminal any problems to do with processing command line |
| 1282 | opts.) |
| 1283 | |
| 1284 | So set up logging now. After this is done, VG_(clo_log_fd) |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1285 | should be connected to whatever sink has been selected, and we |
| 1286 | indiscriminately chuck stuff into it without worrying what the |
| 1287 | nature of it is. Oh the wonder of Unix streams. */ |
| 1288 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1289 | vg_assert(VG_(clo_log_fd) == 2 /* stderr */); |
| 1290 | vg_assert(VG_(logging_to_socket) == False); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1291 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1292 | switch (log_to) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1293 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1294 | case VgLogTo_Fd: |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1295 | vg_assert(VG_(clo_log_name) == NULL); |
| 1296 | VG_(clo_log_fd) = eventually_log_fd; |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1297 | break; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1298 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1299 | case VgLogTo_File: { |
sewardj | ad31116 | 2005-07-19 11:25:02 +0000 | [diff] [blame] | 1300 | HChar logfilename[1000]; |
| 1301 | Int seq = 0; |
| 1302 | Int pid = VG_(getpid)(); |
| 1303 | HChar* qual = NULL; |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1304 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1305 | vg_assert(VG_(clo_log_name) != NULL); |
| 1306 | vg_assert(VG_(strlen)(VG_(clo_log_name)) <= 900); /* paranoia */ |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1307 | |
sewardj | ad31116 | 2005-07-19 11:25:02 +0000 | [diff] [blame] | 1308 | if (VG_(clo_log_file_qualifier)) { |
| 1309 | qual = VG_(getenv)(VG_(clo_log_file_qualifier)); |
| 1310 | } |
| 1311 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1312 | for (;;) { |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1313 | HChar pidtxt[20], seqtxt[20]; |
| 1314 | |
| 1315 | VG_(sprintf)(pidtxt, "%d", pid); |
| 1316 | |
| 1317 | if (seq == 0) |
| 1318 | seqtxt[0] = 0; |
| 1319 | else |
| 1320 | VG_(sprintf)(seqtxt, ".%d", seq); |
| 1321 | |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1322 | seq++; |
| 1323 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1324 | /* Result: |
| 1325 | if (qual) base_name ++ "." ++ qual ++ seqtxt |
| 1326 | if (not qual) base_name ++ "." ++ pid ++ seqtxt |
| 1327 | */ |
| 1328 | VG_(sprintf)( logfilename, |
| 1329 | "%s.%s%s", |
| 1330 | VG_(clo_log_name), |
| 1331 | qual ? qual : pidtxt, |
| 1332 | seqtxt ); |
| 1333 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1334 | // EXCL: it will fail with EEXIST if the file already exists. |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1335 | sres |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1336 | = VG_(open)(logfilename, |
| 1337 | VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC, |
| 1338 | VKI_S_IRUSR|VKI_S_IWUSR); |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1339 | if (!sres.isError) { |
| 1340 | eventually_log_fd = sres.val; |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1341 | VG_(clo_log_fd) = VG_(safe_fd)(eventually_log_fd); |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1342 | break; /* for (;;) */ |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1343 | } else { |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1344 | // If the file already existed, we try the next name. If it |
| 1345 | // was some other file error, we give up. |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1346 | if (sres.val != VKI_EEXIST) { |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1347 | VG_(message)(Vg_UserMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 1348 | "Can't create/open log file '%s.pid%d'; giving up!", |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1349 | VG_(clo_log_name), pid); |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1350 | VG_(bad_option)( |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1351 | "--log-file=<file> (didn't work out for some reason.)"); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1352 | /*NOTREACHED*/ |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | } |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1356 | break; /* switch (VG_(clo_log_to)) */ |
| 1357 | } |
| 1358 | |
| 1359 | case VgLogTo_FileExactly: { |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1360 | vg_assert(VG_(clo_log_name) != NULL); |
| 1361 | vg_assert(VG_(strlen)(VG_(clo_log_name)) <= 900); /* paranoia */ |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1362 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1363 | sres |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1364 | = VG_(open)(VG_(clo_log_name), |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1365 | VKI_O_CREAT|VKI_O_WRONLY|VKI_O_TRUNC, |
| 1366 | VKI_S_IRUSR|VKI_S_IWUSR); |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1367 | if (!sres.isError) { |
| 1368 | eventually_log_fd = sres.val; |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1369 | VG_(clo_log_fd) = VG_(safe_fd)(eventually_log_fd); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1370 | } else { |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1371 | VG_(message)(Vg_UserMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 1372 | "Can't create/open log file '%s'; giving up!", |
sewardj | 603d410 | 2005-01-11 14:01:02 +0000 | [diff] [blame] | 1373 | VG_(clo_log_name)); |
| 1374 | VG_(bad_option)( |
| 1375 | "--log-file-exactly=<file> (didn't work out for some reason.)"); |
| 1376 | /*NOTREACHED*/ |
| 1377 | } |
| 1378 | break; /* switch (VG_(clo_log_to)) */ |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | case VgLogTo_Socket: { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1382 | vg_assert(VG_(clo_log_name) != NULL); |
| 1383 | vg_assert(VG_(strlen)(VG_(clo_log_name)) <= 900); /* paranoia */ |
| 1384 | eventually_log_fd = VG_(connect_via_socket)( VG_(clo_log_name) ); |
| 1385 | if (eventually_log_fd == -1) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1386 | VG_(message)(Vg_UserMsg, |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1387 | "Invalid --log-socket=ipaddr or --log-socket=ipaddr:port spec"); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1388 | VG_(message)(Vg_UserMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 1389 | "of '%s'; giving up!", VG_(clo_log_name) ); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1390 | VG_(bad_option)( |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1391 | "--log-socket="); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1392 | /*NOTREACHED*/ |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1393 | } |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1394 | if (eventually_log_fd == -2) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1395 | VG_(message)(Vg_UserMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 1396 | "valgrind: failed to connect to logging server '%s'.", |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1397 | VG_(clo_log_name) ); |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1398 | VG_(message)(Vg_UserMsg, |
| 1399 | "Log messages will sent to stderr instead." ); |
| 1400 | VG_(message)(Vg_UserMsg, |
| 1401 | "" ); |
| 1402 | /* We don't change anything here. */ |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1403 | vg_assert(VG_(clo_log_fd) == 2); |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1404 | } else { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1405 | vg_assert(eventually_log_fd > 0); |
| 1406 | VG_(clo_log_fd) = eventually_log_fd; |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1407 | VG_(logging_to_socket) = True; |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1408 | } |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1409 | break; |
| 1410 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1413 | |
| 1414 | /* Check that the requested tool actually supports XML output. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1415 | if (VG_(clo_xml) && !VG_STREQ(toolname, "memcheck") |
| 1416 | && !VG_STREQ(toolname, "none")) { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1417 | VG_(clo_xml) = False; |
| 1418 | VG_(message)(Vg_UserMsg, |
cerion | 3965520 | 2005-07-13 14:18:24 +0000 | [diff] [blame] | 1419 | "Currently only Memcheck|None supports XML output."); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1420 | VG_(bad_option)("--xml=yes"); |
| 1421 | /*NOTREACHED*/ |
| 1422 | } |
| 1423 | |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1424 | // Move log_fd into the safe range, so it doesn't conflict with any app fds. |
| 1425 | // XXX: this is more or less duplicating the behaviour of the calls to |
| 1426 | // VG_(safe_fd)() above, although this does not close the original fd. |
| 1427 | // Perhaps the VG_(safe_fd)() calls above should be removed, and this |
| 1428 | // code should be replaced with a call to VG_(safe_fd)(). --njn |
thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 1429 | eventually_log_fd = VG_(fcntl)(VG_(clo_log_fd), VKI_F_DUPFD, VG_(fd_hard_limit)); |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1430 | if (eventually_log_fd < 0) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1431 | VG_(message)(Vg_UserMsg, "valgrind: failed to move logfile fd into safe range"); |
| 1432 | else { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1433 | VG_(clo_log_fd) = eventually_log_fd; |
| 1434 | VG_(fcntl)(VG_(clo_log_fd), VKI_F_SETFD, VKI_FD_CLOEXEC); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1437 | if (VG_(clo_n_suppressions) < VG_CLO_MAX_SFILES-1 && |
| 1438 | (VG_(needs).core_errors || VG_(needs).tool_errors)) { |
| 1439 | /* If we haven't reached the max number of suppressions, load |
| 1440 | the default one. */ |
| 1441 | static const Char default_supp[] = "default.supp"; |
| 1442 | Int len = VG_(strlen)(VG_(libdir)) + 1 + sizeof(default_supp); |
| 1443 | Char *buf = VG_(arena_malloc)(VG_AR_CORE, len); |
| 1444 | VG_(sprintf)(buf, "%s/%s", VG_(libdir), default_supp); |
| 1445 | VG_(clo_suppressions)[VG_(clo_n_suppressions)] = buf; |
| 1446 | VG_(clo_n_suppressions)++; |
| 1447 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1448 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1449 | return (log_to == VgLogTo_Fd); |
| 1450 | } |
| 1451 | |
| 1452 | |
| 1453 | /*====================================================================*/ |
| 1454 | /*=== Printing the preamble ===*/ |
| 1455 | /*====================================================================*/ |
| 1456 | |
| 1457 | /* Ok, the logging sink is running now. Print a suitable preamble. |
| 1458 | If logging to file or a socket, write details of parent PID and |
| 1459 | command line args, to help people trying to interpret the |
| 1460 | results of a run which encompasses multiple processes. */ |
| 1461 | static void print_preamble(Bool logging_to_fd, const char* toolname) |
| 1462 | { |
| 1463 | Int i; |
| 1464 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1465 | if (VG_(clo_xml)) { |
sewardj | 8665d8e | 2005-06-01 17:35:23 +0000 | [diff] [blame] | 1466 | VG_(message)(Vg_UserMsg, "<?xml version=\"1.0\"?>"); |
| 1467 | VG_(message)(Vg_UserMsg, ""); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1468 | VG_(message)(Vg_UserMsg, "<valgrindoutput>"); |
| 1469 | VG_(message)(Vg_UserMsg, ""); |
| 1470 | VG_(message)(Vg_UserMsg, "<protocolversion>1</protocolversion>"); |
| 1471 | VG_(message)(Vg_UserMsg, ""); |
| 1472 | } |
| 1473 | |
sewardj | d7bddad | 2005-06-13 16:48:32 +0000 | [diff] [blame] | 1474 | HChar* xpre = VG_(clo_xml) ? " <line>" : ""; |
| 1475 | HChar* xpost = VG_(clo_xml) ? "</line>" : ""; |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1476 | |
sewardj | 83adf41 | 2002-05-01 01:25:45 +0000 | [diff] [blame] | 1477 | if (VG_(clo_verbosity > 0)) { |
sewardj | d7bddad | 2005-06-13 16:48:32 +0000 | [diff] [blame] | 1478 | |
| 1479 | if (VG_(clo_xml)) |
| 1480 | VG_(message)(Vg_UserMsg, "<preamble>"); |
| 1481 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 1482 | /* Tool details */ |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1483 | VG_(message)(Vg_UserMsg, "%s%s%s%s, %s.%s", |
| 1484 | xpre, |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1485 | VG_(details).name, |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 1486 | NULL == VG_(details).version ? "" : "-", |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1487 | NULL == VG_(details).version |
| 1488 | ? (Char*)"" : VG_(details).version, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1489 | VG_(details).description, |
| 1490 | xpost); |
| 1491 | VG_(message)(Vg_UserMsg, "%s%s%s", |
| 1492 | xpre, VG_(details).copyright_author, xpost); |
sewardj | 3b2736a | 2002-03-24 12:18:35 +0000 | [diff] [blame] | 1493 | |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1494 | /* Core details */ |
| 1495 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1496 | "%sUsing LibVEX rev %s, a library for dynamic binary translation.%s", |
| 1497 | xpre, LibVEX_Version(), xpost ); |
sewardj | c702533 | 2004-12-13 18:30:39 +0000 | [diff] [blame] | 1498 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1499 | "%sCopyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.%s", |
| 1500 | xpre, xpost ); |
sewardj | c702533 | 2004-12-13 18:30:39 +0000 | [diff] [blame] | 1501 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1502 | "%sUsing valgrind-%s, a dynamic binary instrumentation framework.%s", |
| 1503 | xpre, VERSION, xpost); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1504 | VG_(message)(Vg_UserMsg, |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1505 | "%sCopyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.%s", |
| 1506 | xpre, xpost ); |
sewardj | d7bddad | 2005-06-13 16:48:32 +0000 | [diff] [blame] | 1507 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1508 | if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) |
| 1509 | VG_(message)(Vg_UserMsg, "For more details, rerun with: -v"); |
| 1510 | |
sewardj | d7bddad | 2005-06-13 16:48:32 +0000 | [diff] [blame] | 1511 | if (VG_(clo_xml)) |
| 1512 | VG_(message)(Vg_UserMsg, "</preamble>"); |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1515 | if (!VG_(clo_xml) && VG_(clo_verbosity) > 0 && !logging_to_fd) { |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1516 | VG_(message)(Vg_UserMsg, ""); |
| 1517 | VG_(message)(Vg_UserMsg, |
| 1518 | "My PID = %d, parent PID = %d. Prog and args are:", |
| 1519 | VG_(getpid)(), VG_(getppid)() ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1520 | for (i = 0; i < VG_(args_for_client).used; i++) |
| 1521 | VG_(message)(Vg_UserMsg, " %s", VG_(args_for_client).strs[i]); |
sewardj | 5b742c3 | 2005-07-26 23:43:26 +0000 | [diff] [blame] | 1522 | if (VG_(clo_log_file_qualifier)) { |
| 1523 | HChar* val = VG_(getenv)(VG_(clo_log_file_qualifier)); |
| 1524 | VG_(message)(Vg_UserMsg, ""); |
| 1525 | VG_(message)(Vg_UserMsg, "Log file qualifier: var %s, value %s.", |
| 1526 | VG_(clo_log_file_qualifier), |
| 1527 | val ? val : ""); |
| 1528 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1529 | } |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1530 | else |
| 1531 | if (VG_(clo_xml)) { |
| 1532 | VG_(message)(Vg_UserMsg, ""); |
| 1533 | VG_(message)(Vg_UserMsg, "<pid>%d</pid>", VG_(getpid)()); |
| 1534 | VG_(message)(Vg_UserMsg, "<ppid>%d</ppid>", VG_(getppid)()); |
sewardj | 97f7e0c | 2005-07-19 15:00:25 +0000 | [diff] [blame] | 1535 | VG_(message)(Vg_UserMsg, "<tool>%t</tool>", toolname); |
sewardj | ad31116 | 2005-07-19 11:25:02 +0000 | [diff] [blame] | 1536 | if (VG_(clo_log_file_qualifier)) { |
| 1537 | HChar* val = VG_(getenv)(VG_(clo_log_file_qualifier)); |
sewardj | 97f7e0c | 2005-07-19 15:00:25 +0000 | [diff] [blame] | 1538 | VG_(message)(Vg_UserMsg, "<logfilequalifier> <var>%t</var> " |
| 1539 | "<value>%t</value> </logfilequalifier>", |
sewardj | ad31116 | 2005-07-19 11:25:02 +0000 | [diff] [blame] | 1540 | VG_(clo_log_file_qualifier), |
| 1541 | val ? val : ""); |
| 1542 | } |
sewardj | 768db0e | 2005-07-19 14:18:56 +0000 | [diff] [blame] | 1543 | if (VG_(clo_xml_user_comment)) { |
| 1544 | /* Note: the user comment itself is XML and is therefore to |
| 1545 | be passed through verbatim (%s) rather than escaped |
| 1546 | (%t). */ |
| 1547 | VG_(message)(Vg_UserMsg, "<usercomment>%s</usercomment>", |
| 1548 | VG_(clo_xml_user_comment)); |
| 1549 | } |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1550 | VG_(message)(Vg_UserMsg, ""); |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 1551 | VG_(message)(Vg_UserMsg, "<args>"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1552 | |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 1553 | VG_(message)(Vg_UserMsg, " <vargv>"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1554 | if (VG_(name_of_launcher)) |
| 1555 | VG_(message)(Vg_UserMsg, " <exe>%t</exe>", |
| 1556 | VG_(name_of_launcher)); |
| 1557 | for (i = 0; i < VG_(args_for_valgrind).used; i++) { |
| 1558 | VG_(message)(Vg_UserMsg, |
| 1559 | " <arg>%t</arg>", |
| 1560 | VG_(args_for_valgrind).strs[i]); |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 1561 | } |
| 1562 | VG_(message)(Vg_UserMsg, " </vargv>"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1563 | |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 1564 | VG_(message)(Vg_UserMsg, " <argv>"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1565 | if (VG_(args_the_exename)) |
| 1566 | VG_(message)(Vg_UserMsg, " <exe>%t</exe>", |
| 1567 | VG_(args_the_exename)); |
| 1568 | for (i = 0; i < VG_(args_for_client).used; i++) { |
| 1569 | VG_(message)(Vg_UserMsg, " <arg>%t</arg>", |
| 1570 | VG_(args_for_client).strs[i]); |
sewardj | 8665d8e | 2005-06-01 17:35:23 +0000 | [diff] [blame] | 1571 | } |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 1572 | VG_(message)(Vg_UserMsg, " </argv>"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1573 | |
sewardj | b8a3dac | 2005-07-19 12:39:11 +0000 | [diff] [blame] | 1574 | VG_(message)(Vg_UserMsg, "</args>"); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1575 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1576 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1577 | // Empty line after the preamble |
| 1578 | if (VG_(clo_verbosity) > 0) |
| 1579 | VG_(message)(Vg_UserMsg, ""); |
| 1580 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1581 | if (VG_(clo_verbosity) > 1) { |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1582 | SysRes fd; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1583 | if (!logging_to_fd) |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 1584 | VG_(message)(Vg_DebugMsg, ""); |
| 1585 | VG_(message)(Vg_DebugMsg, "Valgrind library directory: %s", VG_(libdir)); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1586 | |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 1587 | VG_(message)(Vg_DebugMsg, "Command line"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1588 | if (VG_(args_the_exename)) |
| 1589 | VG_(message)(Vg_DebugMsg, " %s", VG_(args_the_exename)); |
| 1590 | for (i = 0; i < VG_(args_for_client).used; i++) |
| 1591 | VG_(message)(Vg_DebugMsg, " %s", VG_(args_for_client).strs[i]); |
njn | 86dc2bc | 2003-09-09 07:26:21 +0000 | [diff] [blame] | 1592 | |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 1593 | VG_(message)(Vg_DebugMsg, "Startup, with flags:"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1594 | for (i = 0; i < VG_(args_for_valgrind).used; i++) { |
| 1595 | VG_(message)(Vg_DebugMsg, " %s", VG_(args_for_valgrind).strs[i]); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1596 | } |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1597 | |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 1598 | VG_(message)(Vg_DebugMsg, "Contents of /proc/version:"); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1599 | fd = VG_(open) ( "/proc/version", VKI_O_RDONLY, 0 ); |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1600 | if (fd.isError) { |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 1601 | VG_(message)(Vg_DebugMsg, " can't open /proc/version"); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1602 | } else { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1603 | # define BUF_LEN 256 |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1604 | Char version_buf[BUF_LEN]; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1605 | Int n = VG_(read) ( fd.val, version_buf, BUF_LEN ); |
njn | be9b47b | 2005-05-15 16:22:58 +0000 | [diff] [blame] | 1606 | vg_assert(n <= BUF_LEN); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1607 | if (n > 0) { |
| 1608 | version_buf[n-1] = '\0'; |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 1609 | VG_(message)(Vg_DebugMsg, " %s", version_buf); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1610 | } else { |
njn | 1fd5eb2 | 2005-03-13 05:43:23 +0000 | [diff] [blame] | 1611 | VG_(message)(Vg_DebugMsg, " (empty?)"); |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1612 | } |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1613 | VG_(close)(fd.val); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1614 | # undef BUF_LEN |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1615 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1616 | } |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1619 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1620 | /*====================================================================*/ |
| 1621 | /*=== File descriptor setup ===*/ |
| 1622 | /*====================================================================*/ |
| 1623 | |
| 1624 | static void setup_file_descriptors(void) |
| 1625 | { |
| 1626 | struct vki_rlimit rl; |
| 1627 | |
| 1628 | /* Get the current file descriptor limits. */ |
| 1629 | if (VG_(getrlimit)(VKI_RLIMIT_NOFILE, &rl) < 0) { |
| 1630 | rl.rlim_cur = 1024; |
| 1631 | rl.rlim_max = 1024; |
| 1632 | } |
| 1633 | |
| 1634 | /* Work out where to move the soft limit to. */ |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 1635 | if (rl.rlim_cur + N_RESERVED_FDS <= rl.rlim_max) { |
| 1636 | rl.rlim_cur = rl.rlim_cur + N_RESERVED_FDS; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1637 | } else { |
| 1638 | rl.rlim_cur = rl.rlim_max; |
| 1639 | } |
| 1640 | |
| 1641 | /* Reserve some file descriptors for our use. */ |
njn | 14319cc | 2005-03-13 06:26:22 +0000 | [diff] [blame] | 1642 | VG_(fd_soft_limit) = rl.rlim_cur - N_RESERVED_FDS; |
| 1643 | VG_(fd_hard_limit) = rl.rlim_cur - N_RESERVED_FDS; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1644 | |
| 1645 | /* Update the soft limit. */ |
| 1646 | VG_(setrlimit)(VKI_RLIMIT_NOFILE, &rl); |
| 1647 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1648 | if (VG_(cl_exec_fd) != -1) |
| 1649 | VG_(cl_exec_fd) = VG_(safe_fd)( VG_(cl_exec_fd) ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1652 | |
njn | 2da7335 | 2005-06-18 01:35:16 +0000 | [diff] [blame] | 1653 | /*====================================================================*/ |
| 1654 | /*=== Initialise the first thread. ===*/ |
| 1655 | /*====================================================================*/ |
| 1656 | |
| 1657 | /* Given a pointer to the ThreadArchState for thread 1 (the root |
| 1658 | thread), initialise the VEX guest state, and copy in essential |
| 1659 | starting values. |
| 1660 | */ |
| 1661 | static void init_thread1state ( Addr client_ip, |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1662 | Addr client_sp, |
njn | 2da7335 | 2005-06-18 01:35:16 +0000 | [diff] [blame] | 1663 | /*inout*/ ThreadArchState* arch ) |
| 1664 | { |
| 1665 | #if defined(VGA_x86) |
| 1666 | vg_assert(0 == sizeof(VexGuestX86State) % 8); |
| 1667 | |
| 1668 | /* Zero out the initial state, and set up the simulated FPU in a |
| 1669 | sane way. */ |
| 1670 | LibVEX_GuestX86_initialise(&arch->vex); |
| 1671 | |
| 1672 | /* Zero out the shadow area. */ |
| 1673 | VG_(memset)(&arch->vex_shadow, 0, sizeof(VexGuestX86State)); |
| 1674 | |
| 1675 | /* Put essential stuff into the new state. */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1676 | arch->vex.guest_ESP = client_sp; |
njn | 2da7335 | 2005-06-18 01:35:16 +0000 | [diff] [blame] | 1677 | arch->vex.guest_EIP = client_ip; |
| 1678 | |
| 1679 | /* initialise %cs, %ds and %ss to point at the operating systems |
| 1680 | default code, data and stack segments */ |
| 1681 | asm volatile("movw %%cs, %0" : : "m" (arch->vex.guest_CS)); |
| 1682 | asm volatile("movw %%ds, %0" : : "m" (arch->vex.guest_DS)); |
| 1683 | asm volatile("movw %%ss, %0" : : "m" (arch->vex.guest_SS)); |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 1684 | |
njn | 2da7335 | 2005-06-18 01:35:16 +0000 | [diff] [blame] | 1685 | #elif defined(VGA_amd64) |
| 1686 | vg_assert(0 == sizeof(VexGuestAMD64State) % 8); |
| 1687 | |
| 1688 | /* Zero out the initial state, and set up the simulated FPU in a |
| 1689 | sane way. */ |
| 1690 | LibVEX_GuestAMD64_initialise(&arch->vex); |
| 1691 | |
| 1692 | /* Zero out the shadow area. */ |
| 1693 | VG_(memset)(&arch->vex_shadow, 0, sizeof(VexGuestAMD64State)); |
| 1694 | |
| 1695 | /* Put essential stuff into the new state. */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1696 | arch->vex.guest_RSP = client_sp; |
njn | 2da7335 | 2005-06-18 01:35:16 +0000 | [diff] [blame] | 1697 | arch->vex.guest_RIP = client_ip; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 1698 | |
| 1699 | #elif defined(VGA_ppc32) |
| 1700 | vg_assert(0 == sizeof(VexGuestPPC32State) % 8); |
| 1701 | |
| 1702 | /* Zero out the initial state, and set up the simulated FPU in a |
| 1703 | sane way. */ |
| 1704 | LibVEX_GuestPPC32_initialise(&arch->vex); |
| 1705 | |
| 1706 | /* Zero out the shadow area. */ |
| 1707 | VG_(memset)(&arch->vex_shadow, 0, sizeof(VexGuestPPC32State)); |
| 1708 | |
| 1709 | /* Put essential stuff into the new state. */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1710 | arch->vex.guest_GPR1 = client_sp; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 1711 | arch->vex.guest_CIA = client_ip; |
| 1712 | |
njn | 2da7335 | 2005-06-18 01:35:16 +0000 | [diff] [blame] | 1713 | #else |
| 1714 | # error Unknown arch |
| 1715 | #endif |
| 1716 | // Tell the tool that we just wrote to the registers. |
| 1717 | VG_TRACK( post_reg_write, Vg_CoreStartup, /*tid*/1, /*offset*/0, |
| 1718 | sizeof(VexGuestArchState)); |
| 1719 | } |
| 1720 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1721 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1722 | /*====================================================================*/ |
njn | 2025cf9 | 2005-06-26 20:44:48 +0000 | [diff] [blame] | 1723 | /*=== BB profiling ===*/ |
| 1724 | /*====================================================================*/ |
| 1725 | |
| 1726 | static |
| 1727 | void show_BB_profile ( BBProfEntry tops[], UInt n_tops, ULong score_total ) |
| 1728 | { |
| 1729 | ULong score_cumul, score_here; |
| 1730 | Char buf_cumul[10], buf_here[10]; |
| 1731 | Char name[64]; |
| 1732 | Int r; |
| 1733 | |
| 1734 | VG_(printf)("\n"); |
| 1735 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1736 | VG_(printf)("--- BEGIN BB Profile (summary of scores) ---\n"); |
| 1737 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1738 | VG_(printf)("\n"); |
| 1739 | |
| 1740 | VG_(printf)("Total score = %lld\n\n", score_total); |
| 1741 | |
| 1742 | score_cumul = 0; |
| 1743 | for (r = 0; r < n_tops; r++) { |
| 1744 | if (tops[r].addr == 0) |
| 1745 | continue; |
| 1746 | name[0] = 0; |
| 1747 | VG_(get_fnname_w_offset)(tops[r].addr, name, 64); |
| 1748 | name[63] = 0; |
| 1749 | score_here = tops[r].score; |
| 1750 | score_cumul += score_here; |
| 1751 | VG_(percentify)(score_cumul, score_total, 2, 6, buf_cumul); |
| 1752 | VG_(percentify)(score_here, score_total, 2, 6, buf_here); |
| 1753 | VG_(printf)("%3d: (%9lld %s) %9lld %s 0x%llx %s\n", |
| 1754 | r, |
| 1755 | score_cumul, buf_cumul, |
| 1756 | score_here, buf_here, tops[r].addr, name ); |
| 1757 | } |
| 1758 | |
| 1759 | VG_(printf)("\n"); |
| 1760 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1761 | VG_(printf)("--- BB Profile (BB details) ---\n"); |
| 1762 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1763 | VG_(printf)("\n"); |
| 1764 | |
| 1765 | score_cumul = 0; |
| 1766 | for (r = 0; r < n_tops; r++) { |
| 1767 | if (tops[r].addr == 0) |
| 1768 | continue; |
| 1769 | name[0] = 0; |
| 1770 | VG_(get_fnname_w_offset)(tops[r].addr, name, 64); |
| 1771 | name[63] = 0; |
| 1772 | score_here = tops[r].score; |
| 1773 | score_cumul += score_here; |
| 1774 | VG_(percentify)(score_cumul, score_total, 2, 6, buf_cumul); |
| 1775 | VG_(percentify)(score_here, score_total, 2, 6, buf_here); |
| 1776 | VG_(printf)("\n"); |
| 1777 | VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= begin BB rank %d " |
| 1778 | "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r); |
| 1779 | VG_(printf)("%3d: (%9lld %s) %9lld %s 0x%llx %s\n", |
| 1780 | r, |
| 1781 | score_cumul, buf_cumul, |
| 1782 | score_here, buf_here, tops[r].addr, name ); |
| 1783 | VG_(printf)("\n"); |
| 1784 | VG_(translate)(0, tops[r].addr, True, VG_(clo_profile_flags), 0); |
| 1785 | VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= end BB rank %d " |
| 1786 | "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r); |
| 1787 | } |
| 1788 | |
| 1789 | VG_(printf)("\n"); |
| 1790 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1791 | VG_(printf)("--- END BB Profile ---\n"); |
| 1792 | VG_(printf)("-----------------------------------------------------------\n"); |
| 1793 | VG_(printf)("\n"); |
| 1794 | } |
| 1795 | |
| 1796 | |
| 1797 | /*====================================================================*/ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1798 | /*=== main() ===*/ |
| 1799 | /*====================================================================*/ |
| 1800 | |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1801 | /* When main() is entered, we should be on the following stack, not |
| 1802 | the one the kernel gave us. We will run on this stack until |
| 1803 | simulation of the root thread is started, at which point a transfer |
| 1804 | is made to a dynamically allocated stack. This is for the sake of |
| 1805 | uniform overflow detection for all Valgrind threads. This is |
| 1806 | marked global even though it isn't, because assembly code below |
| 1807 | needs to reference the name. */ |
| 1808 | |
| 1809 | /*static*/ VgStack VG_(interim_stack); |
| 1810 | |
| 1811 | /* This should get some address inside the stack on which we gained |
| 1812 | control (eg, it could be the SP at startup). It doesn't matter |
| 1813 | exactly where in the stack it is. This value is passed to the |
| 1814 | address space manager at startup, which uses it to identify the |
| 1815 | initial stack segment and hence the upper end of the usable address |
| 1816 | space. */ |
| 1817 | |
| 1818 | static Addr sp_at_startup = 0; |
| 1819 | |
| 1820 | |
| 1821 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1822 | /* TODO: GIVE THIS A PROPER HOME |
| 1823 | TODO: MERGE THIS WITH DUPLICATE IN mac_leakcheck.c |
| 1824 | Extract from aspacem a vector of the current segment start |
| 1825 | addresses. The vector is dynamically allocated and should be freed |
| 1826 | by the caller when done. REQUIRES m_mallocfree to be running. |
| 1827 | Writes the number of addresses required into *n_acquired. */ |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 1828 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1829 | static Addr* get_seg_starts ( /*OUT*/Int* n_acquired ) |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1830 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1831 | Addr* starts; |
| 1832 | Int n_starts, r; |
| 1833 | |
| 1834 | n_starts = 1; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1835 | while (True) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1836 | starts = VG_(malloc)( n_starts * sizeof(Addr) ); |
| 1837 | if (starts == NULL) |
| 1838 | break; |
| 1839 | r = VG_(am_get_segment_starts)( starts, n_starts ); |
| 1840 | if (r >= 0) |
| 1841 | break; |
| 1842 | VG_(free)(starts); |
| 1843 | n_starts *= 2; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1844 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1845 | |
| 1846 | if (starts == NULL) { |
| 1847 | *n_acquired = 0; |
| 1848 | return NULL; |
| 1849 | } |
| 1850 | |
| 1851 | *n_acquired = r; |
| 1852 | return starts; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1856 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1857 | |
| 1858 | |
| 1859 | Int main(Int argc, HChar **argv, HChar **envp) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1860 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1861 | HChar* toolname = "memcheck"; // default to Memcheck |
| 1862 | HChar** env = NULL; |
| 1863 | Int need_help = 0; // 0 = no, 1 = --help, 2 = --help-debug |
| 1864 | Addr initial_client_IP = 0; |
| 1865 | Addr initial_client_SP = 0; |
| 1866 | Addr clstack_top = 0; |
| 1867 | SizeT clstack_max_size = 0; |
| 1868 | UInt* client_auxv; |
| 1869 | Int loglevel, i; |
| 1870 | Bool logging_to_fd; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 1871 | struct vki_rlimit zero = { 0, 0 }; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1872 | struct exeinfo info; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1873 | |
| 1874 | //============================================================ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1875 | // |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1876 | // Nb: startup is complex. Prerequisites are shown at every step. |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1877 | // *** Be very careful when messing with the order *** |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1878 | // |
| 1879 | // The first order of business is to get debug logging, the address |
| 1880 | // space manager and the dynamic memory manager up and running. |
| 1881 | // Once that's done, we can relax a bit. |
| 1882 | // |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1883 | //============================================================ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1884 | |
| 1885 | /* This is needed to make VG_(getenv) usable early. */ |
| 1886 | VG_(client_envp) = (Char**)envp; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1887 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1888 | //-------------------------------------------------------------- |
| 1889 | // Start up the logging mechanism |
| 1890 | // p: none |
| 1891 | //-------------------------------------------------------------- |
| 1892 | /* Start the debugging-log system ASAP. First find out how many |
| 1893 | "-d"s were specified. This is a pre-scan of the command line. */ |
| 1894 | loglevel = 0; |
| 1895 | for (i = 1; i < argc; i++) { |
sewardj | 1075931 | 2005-05-30 23:52:47 +0000 | [diff] [blame] | 1896 | if (argv[i][0] != '-') |
| 1897 | break; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1898 | if (VG_STREQ(argv[i], "--")) |
sewardj | 1075931 | 2005-05-30 23:52:47 +0000 | [diff] [blame] | 1899 | break; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1900 | if (VG_STREQ(argv[i], "-d")) |
sewardj | 1075931 | 2005-05-30 23:52:47 +0000 | [diff] [blame] | 1901 | loglevel++; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | /* ... and start the debug logger. Now we can safely emit logging |
| 1905 | messages all through startup. */ |
sewardj | 1075931 | 2005-05-30 23:52:47 +0000 | [diff] [blame] | 1906 | VG_(debugLog_startup)(loglevel, "Stage 2 (main)"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1907 | VG_(debugLog)(1, "main", "Welcome to Valgrind version " |
| 1908 | VERSION " debug logging\n"); |
| 1909 | |
| 1910 | //-------------------------------------------------------------- |
| 1911 | // Ensure we're on a plausible stack. |
| 1912 | // p: logging |
| 1913 | //-------------------------------------------------------------- |
| 1914 | VG_(debugLog)(1, "main", "Checking current stack is plausible\n"); |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1915 | { HChar* limLo = (HChar*)(&VG_(interim_stack).bytes[0]); |
| 1916 | HChar* limHi = limLo + sizeof(VG_(interim_stack)); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1917 | HChar* aLocal = (HChar*)&zero; /* any auto local will do */ |
| 1918 | if (aLocal < limLo || aLocal >= limHi) { |
| 1919 | /* something's wrong. Stop. */ |
| 1920 | VG_(debugLog)(0, "main", "Root stack %p to %p, a local %p\n", |
| 1921 | limLo, limHi, aLocal ); |
| 1922 | VG_(debugLog)(0, "main", "Valgrind: FATAL: " |
| 1923 | "Initial stack switched failed.\n"); |
| 1924 | VG_(debugLog)(0, "main", " Cannot continue. Sorry.\n"); |
| 1925 | VG_(exit)(1); |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | //-------------------------------------------------------------- |
| 1930 | // Ensure we have a plausible pointer to the stack on which |
| 1931 | // we gained control (not the current stack!) |
| 1932 | // p: logging |
| 1933 | //-------------------------------------------------------------- |
| 1934 | VG_(debugLog)(1, "main", "Checking initial stack was noted\n"); |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1935 | if (sp_at_startup == 0) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1936 | VG_(debugLog)(0, "main", "Valgrind: FATAL: " |
| 1937 | "Initial stack was not noted.\n"); |
| 1938 | VG_(debugLog)(0, "main", " Cannot continue. Sorry.\n"); |
| 1939 | VG_(exit)(1); |
| 1940 | } |
| 1941 | |
| 1942 | //-------------------------------------------------------------- |
| 1943 | // Start up the address space manager, and determine the |
| 1944 | // approximate location of the client's stack |
| 1945 | // p: logging, plausible-stack |
| 1946 | //-------------------------------------------------------------- |
| 1947 | VG_(debugLog)(1, "main", "Starting the address space manager\n"); |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 1948 | clstack_top = VG_(am_startup)( sp_at_startup ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1949 | VG_(debugLog)(1, "main", "Address space manager is running\n"); |
| 1950 | |
| 1951 | //-------------------------------------------------------------- |
| 1952 | // Start up the dynamic memory manager |
| 1953 | // p: address space management |
| 1954 | // In fact m_mallocfree is self-initialising, so there's no |
| 1955 | // initialisation call to do. Instead, try a simple malloc/ |
| 1956 | // free pair right now to check that nothing is broken. |
| 1957 | //-------------------------------------------------------------- |
| 1958 | VG_(debugLog)(1, "main", "Starting the dynamic memory manager\n"); |
| 1959 | { void* p = VG_(malloc)( 12345 ); |
| 1960 | if (p) VG_(free)( p ); |
| 1961 | } |
| 1962 | VG_(debugLog)(1, "main", "Dynamic memory manager is running\n"); |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1963 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1964 | //============================================================ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1965 | // |
| 1966 | // Dynamic memory management is now available. |
| 1967 | // |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1968 | //============================================================ |
| 1969 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1970 | //-------------------------------------------------------------- |
| 1971 | // Look for alternative libdir |
| 1972 | { HChar *cp = VG_(getenv)(VALGRIND_LIB); |
| 1973 | if (cp != NULL) |
| 1974 | VG_(libdir) = cp; |
| 1975 | } |
| 1976 | |
| 1977 | //-------------------------------------------------------------- |
| 1978 | // Extract the launcher name from the environment. |
| 1979 | VG_(debugLog)(1, "main", "Getting stage1's name\n"); |
| 1980 | VG_(name_of_launcher) = VG_(getenv)(VALGRIND_LAUNCHER); |
| 1981 | if (VG_(name_of_launcher) == NULL) { |
| 1982 | VG_(printf)("valgrind: You cannot run '%s' directly.\n", argv[0]); |
| 1983 | VG_(printf)("valgrind: You should use $prefix/bin/valgrind.\n"); |
| 1984 | VG_(exit)(1); |
| 1985 | } |
| 1986 | |
| 1987 | //-------------------------------------------------------------- |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 1988 | // Get the current process datasize rlimit, and set it to zero. |
| 1989 | // This prevents any internal uses of brk() from having any effect. |
| 1990 | // We remember the old value so we can restore it on exec, so that |
| 1991 | // child processes will have a reasonable brk value. |
| 1992 | VG_(getrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data)); |
| 1993 | zero.rlim_max = VG_(client_rlimit_data).rlim_max; |
| 1994 | VG_(setrlimit)(VKI_RLIMIT_DATA, &zero); |
thughes | c37184f | 2004-09-11 14:16:57 +0000 | [diff] [blame] | 1995 | |
| 1996 | // Get the current process stack rlimit. |
| 1997 | VG_(getrlimit)(VKI_RLIMIT_STACK, &VG_(client_rlimit_stack)); |
| 1998 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1999 | //============================================================ |
| 2000 | // Command line argument handling order: |
| 2001 | // * If --help/--help-debug are present, show usage message |
| 2002 | // (including the tool-specific usage) |
| 2003 | // * (If no --tool option given, default to Memcheck) |
| 2004 | // * Then, if client is missing, abort with error msg |
| 2005 | // * Then, if any cmdline args are bad, abort with error msg |
| 2006 | //============================================================ |
| 2007 | |
| 2008 | //-------------------------------------------------------------- |
| 2009 | // Split up argv into: C args, V args, V extra args, and exename. |
| 2010 | // p: dynamic memory allocation |
| 2011 | //-------------------------------------------------------------- |
| 2012 | VG_(debugLog)(1, "main", "Split up command line\n"); |
| 2013 | VG_(split_up_argv)( argc, argv ); |
| 2014 | if (0) { |
| 2015 | for (i = 0; i < VG_(args_for_valgrind).used; i++) |
| 2016 | VG_(printf)("varg %s\n", VG_(args_for_valgrind).strs[i]); |
| 2017 | VG_(printf)(" exe %s\n", VG_(args_the_exename)); |
| 2018 | for (i = 0; i < VG_(args_for_client).used; i++) |
| 2019 | VG_(printf)("carg %s\n", VG_(args_for_client).strs[i]); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2023 | // Extract tool name and whether help has been requested. |
| 2024 | // Note we can't print the help message yet, even if requested, |
| 2025 | // because the tool has not been initialised. |
| 2026 | // p: split_up_argv [for VG_(args_for_valgrind)] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2027 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2028 | VG_(debugLog)(1, "main", "Preprocess command line opts\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2029 | get_helprequest_and_toolname(&need_help, &toolname); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2030 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2031 | // Set default vex control params |
| 2032 | LibVEX_default_VexControl(& VG_(clo_vex_control)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2033 | |
| 2034 | //-------------------------------------------------------------- |
| 2035 | // Load client executable, finding in $PATH if necessary |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2036 | // p: get_helprequest_and_toolname() [for 'exec', 'need_help'] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2037 | // p: layout_remaining_space [so there's space] |
| 2038 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2039 | if (!need_help) { |
| 2040 | VG_(debugLog)(1, "main", "Loading client\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2041 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2042 | if (VG_(args_the_exename) == NULL) |
| 2043 | missing_prog(); |
| 2044 | |
| 2045 | load_client(&info, &initial_client_IP); |
| 2046 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2047 | |
| 2048 | //-------------------------------------------------------------- |
| 2049 | // Set up client's environment |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2050 | // p: set-libdir [for VG_(libdir)] |
| 2051 | // p: get_helprequest_and_toolname [for toolname] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2052 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2053 | if (!need_help) { |
| 2054 | VG_(debugLog)(1, "main", "Setup client env\n"); |
| 2055 | env = setup_client_env(envp, toolname); |
| 2056 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2057 | |
| 2058 | //-------------------------------------------------------------- |
nethercote | 5ee67ca | 2004-06-22 14:00:09 +0000 | [diff] [blame] | 2059 | // Setup client stack, eip, and VG_(client_arg[cv]) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2060 | // p: load_client() [for 'info'] |
| 2061 | // p: fix_environment() [for 'env'] |
| 2062 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2063 | if (!need_help) { |
| 2064 | void* init_sp = argv - 1; |
| 2065 | SizeT m1 = 1024 * 1024; |
| 2066 | SizeT m8 = 8 * m1; |
| 2067 | VG_(debugLog)(1, "main", "Setup client stack\n"); |
| 2068 | clstack_max_size = (SizeT)VG_(client_rlimit_stack).rlim_cur; |
| 2069 | if (clstack_max_size < m1) clstack_max_size = m1; |
| 2070 | if (clstack_max_size > m8) clstack_max_size = m8; |
| 2071 | clstack_max_size = VG_PGROUNDUP(clstack_max_size); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2072 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2073 | initial_client_SP |
| 2074 | = setup_client_stack( init_sp, env, |
| 2075 | &info, &client_auxv, |
| 2076 | clstack_top, clstack_max_size ); |
| 2077 | |
| 2078 | VG_(free)(env); |
| 2079 | |
| 2080 | VG_(debugLog)(2, "main", |
| 2081 | "Client info: " |
| 2082 | "entry=%p client_SP=%p brkbase=%p\n", |
| 2083 | (void*)initial_client_IP, |
| 2084 | (void*)initial_client_SP, |
| 2085 | (void*)VG_(brk_base) ); |
nethercote | c25c449 | 2004-10-18 11:52:17 +0000 | [diff] [blame] | 2086 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2087 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2088 | //-------------------------------------------------------------- |
| 2089 | // Setup client data (brk) segment. Initially a 1-page segment |
| 2090 | // which abuts a shrinkable reservation. |
| 2091 | // p: load_client() [for 'info' and hence VG_(brk_base)] |
| 2092 | //-------------------------------------------------------------- |
| 2093 | if (!need_help) { |
| 2094 | SizeT m1 = 1024 * 1024; |
| 2095 | SizeT m8 = 8 * m1; |
| 2096 | SizeT dseg_max_size = (SizeT)VG_(client_rlimit_data).rlim_cur; |
| 2097 | VG_(debugLog)(1, "main", "Setup client data (brk) segment\n"); |
| 2098 | if (dseg_max_size < m1) dseg_max_size = m1; |
| 2099 | if (dseg_max_size > m8) dseg_max_size = m8; |
| 2100 | dseg_max_size = VG_PGROUNDUP(dseg_max_size); |
| 2101 | |
| 2102 | setup_client_dataseg( dseg_max_size ); |
| 2103 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2104 | |
| 2105 | //============================================================== |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2106 | // |
| 2107 | // Finished loading/setting up the client address space. |
| 2108 | // |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2109 | //============================================================== |
| 2110 | |
| 2111 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2112 | // setup file descriptors |
| 2113 | // p: n/a |
| 2114 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2115 | VG_(debugLog)(1, "main", "Setup file descriptors\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2116 | setup_file_descriptors(); |
| 2117 | |
| 2118 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2119 | // create the fake /proc/<pid>/cmdline file and then unlink it, |
| 2120 | // but hold onto the fd, so we can hand it out to the client |
| 2121 | // when it tries to open /proc/<pid>/cmdline for itself. |
| 2122 | // p: setup file descriptors |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2123 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2124 | if (!need_help) { |
| 2125 | HChar buf[50], buf2[50+64]; |
| 2126 | HChar nul[1]; |
| 2127 | Int fd, r; |
| 2128 | HChar* exename; |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2129 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2130 | VG_(debugLog)(1, "main", "Create fake /proc/<pid>/cmdline\n"); |
| 2131 | |
| 2132 | VG_(sprintf)(buf, "proc_%d_cmdline", VG_(getpid)()); |
| 2133 | fd = VG_(mkstemp)( buf, buf2 ); |
| 2134 | if (fd == -1) |
| 2135 | config_error("Can't create client cmdline file in /tmp."); |
| 2136 | |
| 2137 | nul[0] = 0; |
| 2138 | exename = VG_(args_the_exename) ? VG_(args_the_exename) |
| 2139 | : "unknown_exename"; |
| 2140 | |
| 2141 | VG_(write)(fd, VG_(args_the_exename), |
| 2142 | VG_(strlen)( VG_(args_the_exename) )); |
| 2143 | VG_(write)(fd, nul, 1); |
| 2144 | |
| 2145 | for (i = 0; i < VG_(args_for_client).used; i++) { |
| 2146 | VG_(write)(fd, VG_(args_for_client).strs[i], |
| 2147 | VG_(strlen)( VG_(args_for_client).strs[i] )); |
| 2148 | VG_(write)(fd, nul, 1); |
| 2149 | } |
| 2150 | |
| 2151 | /* Don't bother to seek the file back to the start; instead do |
| 2152 | it every time a copy of it is given out (by PRE(sys_open)). |
| 2153 | That is probably more robust across fork() etc. */ |
| 2154 | |
| 2155 | /* Now delete it, but hang on to the fd. */ |
| 2156 | r = VG_(unlink)( buf2 ); |
| 2157 | if (r) |
| 2158 | config_error("Can't delete client cmdline file in /tmp."); |
| 2159 | |
| 2160 | VG_(cl_cmdline_fd) = fd; |
| 2161 | } |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2162 | |
| 2163 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2164 | // Init tool part 1: pre_clo_init |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2165 | // p: setup_client_stack() [for 'VG_(client_arg[cv]'] |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2166 | // p: setup_file_descriptors() [for 'VG_(fd_xxx_limit)'] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2167 | //-------------------------------------------------------------- |
njn | 64c8324 | 2005-06-21 01:54:38 +0000 | [diff] [blame] | 2168 | { |
| 2169 | Char* s; |
| 2170 | Bool ok; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2171 | VG_(debugLog)(1, "main", "Initialise the tool part 1 (pre_clo_init)\n"); |
| 2172 | (VG_(tool_info).tl_pre_clo_init)(); |
| 2173 | ok = VG_(sanity_check_needs)( &s ); |
njn | 64c8324 | 2005-06-21 01:54:38 +0000 | [diff] [blame] | 2174 | if (!ok) { |
| 2175 | VG_(tool_panic)(s); |
| 2176 | } |
| 2177 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2178 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2179 | //-------------------------------------------------------------- |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2180 | // If --tool and --help/--help-debug was given, now give the core+tool |
| 2181 | // help message |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2182 | // p: get_helprequest_and_toolname() [for 'need_help'] |
| 2183 | // p: tl_pre_clo_init [for 'VG_(tdict).usage'] |
| 2184 | //-------------------------------------------------------------- |
| 2185 | VG_(debugLog)(1, "main", "Print help and quit, if requested\n"); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2186 | if (need_help) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2187 | usage_NORETURN(/*--help-debug?*/2 == need_help); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2188 | } |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2189 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2190 | //-------------------------------------------------------------- |
| 2191 | // Process command line options to Valgrind + tool |
| 2192 | // p: setup_client_stack() [for 'VG_(client_arg[cv]'] |
| 2193 | // p: setup_file_descriptors() [for 'VG_(fd_xxx_limit)'] |
| 2194 | //-------------------------------------------------------------- |
| 2195 | VG_(debugLog)(1, "main", "Process Valgrind's command line options, " |
| 2196 | " setup logging\n"); |
| 2197 | logging_to_fd = process_cmd_line_options(client_auxv, toolname); |
| 2198 | |
| 2199 | //-------------------------------------------------------------- |
| 2200 | // Print the preamble |
| 2201 | // p: tl_pre_clo_init [for 'VG_(details).name' and friends] |
| 2202 | // p: process_cmd_line_options() [for VG_(clo_verbosity), VG_(clo_xml), |
| 2203 | // VG_(clo_log_file_qualifier), |
| 2204 | // logging_to_fd] |
| 2205 | //-------------------------------------------------------------- |
| 2206 | VG_(debugLog)(1, "main", "Print the preamble...\n"); |
| 2207 | print_preamble(logging_to_fd, toolname); |
| 2208 | VG_(debugLog)(1, "main", "...finished the preamble\n"); |
| 2209 | |
| 2210 | //-------------------------------------------------------------- |
| 2211 | // Init tool part 2: post_clo_init |
| 2212 | // p: setup_client_stack() [for 'VG_(client_arg[cv]'] |
| 2213 | // p: setup_file_descriptors() [for 'VG_(fd_xxx_limit)'] |
| 2214 | // p: print_preamble() [so any warnings printed in post_clo_init |
| 2215 | // are shown after the preamble] |
| 2216 | //-------------------------------------------------------------- |
| 2217 | VG_(debugLog)(1, "main", "Initialise the tool part 2 (post_clo_init)\n"); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2218 | VG_TDICT_CALL(tool_post_clo_init); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2219 | |
| 2220 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2221 | // Initialise translation table and translation cache |
| 2222 | // p: aspacem [??] |
| 2223 | // p: tl_pre_clo_init [for 'VG_(details).avg_translation_sizeB'] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2224 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2225 | VG_(debugLog)(1, "main", "Initialise TT/TC\n"); |
| 2226 | VG_(init_tt_tc)(); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2227 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2228 | //-------------------------------------------------------------- |
| 2229 | // Initialise the redirect table. |
| 2230 | // p: init_tt_tc [so it can call VG_(search_transtab) safely] |
| 2231 | // p: aspacem [so can change ownership of sysinfo pages] |
| 2232 | //-------------------------------------------------------------- |
| 2233 | VG_(debugLog)(1, "main", "Initialise redirects\n"); |
| 2234 | VG_(setup_code_redirect_table)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2235 | |
| 2236 | //-------------------------------------------------------------- |
| 2237 | // Allow GDB attach |
| 2238 | // p: process_cmd_line_options() [for VG_(clo_wait_for_gdb)] |
| 2239 | //-------------------------------------------------------------- |
| 2240 | /* Hook to delay things long enough so we can get the pid and |
| 2241 | attach GDB in another shell. */ |
| 2242 | if (VG_(clo_wait_for_gdb)) { |
sewardj | 8211a57 | 2005-06-23 21:37:47 +0000 | [diff] [blame] | 2243 | Long q, iters; |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2244 | VG_(debugLog)(1, "main", "Wait for GDB\n"); |
sewardj | 93ab857 | 2005-02-06 14:10:40 +0000 | [diff] [blame] | 2245 | VG_(printf)("pid=%d, entering delay loop\n", VG_(getpid)()); |
| 2246 | /* jrs 20050206: I don't understand why this works on x86. On |
| 2247 | amd64 the obvious analogues (jump *$rip or jump *$rcx) don't |
| 2248 | work. */ |
| 2249 | /* do "jump *$eip" to skip this in gdb (x86) */ |
| 2250 | //VG_(do_syscall0)(__NR_pause); |
sewardj | 8211a57 | 2005-06-23 21:37:47 +0000 | [diff] [blame] | 2251 | |
| 2252 | # if defined(VGP_x86_linux) |
| 2253 | iters = 5; |
| 2254 | # elif defined(VGP_amd64_linux) |
| 2255 | iters = 10; |
| 2256 | # elif defined(VGP_ppc32_linux) |
sewardj | d714d2e | 2005-07-08 18:24:04 +0000 | [diff] [blame] | 2257 | iters = 5; |
sewardj | 8211a57 | 2005-06-23 21:37:47 +0000 | [diff] [blame] | 2258 | # else |
| 2259 | # error "Unknown plat" |
| 2260 | # endif |
| 2261 | |
| 2262 | iters *= 1000*1000*1000; |
| 2263 | for (q = 0; q < iters; q++) |
| 2264 | ; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
sewardj | b5d320c | 2005-03-13 18:57:15 +0000 | [diff] [blame] | 2267 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2268 | // Search for file descriptors that are inherited from our parent |
| 2269 | // p: process_cmd_line_options [for VG_(clo_track_fds)] |
| 2270 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2271 | if (VG_(clo_track_fds)) { |
| 2272 | VG_(debugLog)(1, "main", "Init preopened fds\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2273 | VG_(init_preopened_fds)(); |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2274 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2275 | |
| 2276 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2277 | // Load debug info for the existing segments. |
| 2278 | // p: setup_code_redirect_table [so that redirs can be recorded] |
| 2279 | // p: mallocfree |
| 2280 | // p: probably: setup fds and process CLOs, so that logging works |
| 2281 | //-------------------------------------------------------------- |
| 2282 | VG_(debugLog)(1, "main", "Load initial debug info\n"); |
| 2283 | { Addr* seg_starts; |
| 2284 | Int n_seg_starts; |
| 2285 | |
| 2286 | seg_starts = get_seg_starts( &n_seg_starts ); |
| 2287 | vg_assert(seg_starts && n_seg_starts > 0); |
| 2288 | |
| 2289 | /* show them all to the debug info reader */ |
| 2290 | for (i = 0; i < n_seg_starts; i++) |
| 2291 | VG_(di_notify_mmap)( seg_starts[i] ); |
| 2292 | |
| 2293 | VG_(free)( seg_starts ); |
| 2294 | } |
| 2295 | |
| 2296 | //-------------------------------------------------------------- |
| 2297 | // Tell aspacem of ownership change of the asm helpers, so that |
| 2298 | // m_translate allows them to be translated. However, only do this |
| 2299 | // after the initial debug info read, since making a hole in the |
| 2300 | // address range for the stage2 binary confuses the debug info reader. |
| 2301 | // p: aspacem |
| 2302 | //-------------------------------------------------------------- |
| 2303 | { Bool change_ownership_v_c_OK; |
| 2304 | Addr co_start = VG_PGROUNDDN( (Addr)&VG_(trampoline_stuff_start) ); |
| 2305 | Addr co_endPlus = VG_PGROUNDUP( (Addr)&VG_(trampoline_stuff_end) ); |
| 2306 | VG_(debugLog)(1,"redir", |
| 2307 | "transfer ownership V -> C of 0x%llx .. 0x%llx\n", |
| 2308 | (ULong)co_start, (ULong)co_endPlus-1 ); |
| 2309 | |
| 2310 | change_ownership_v_c_OK |
| 2311 | = VG_(am_change_ownership_v_to_c)( co_start, co_endPlus - co_start ); |
| 2312 | vg_assert(change_ownership_v_c_OK); |
| 2313 | } |
| 2314 | |
| 2315 | //-------------------------------------------------------------- |
| 2316 | // Tell the tool about the initial client memory permissions |
| 2317 | // p: aspacem |
| 2318 | // p: mallocfree |
| 2319 | // p: setup_client_stack |
| 2320 | // p: setup_client_dataseg |
| 2321 | //-------------------------------------------------------------- |
| 2322 | VG_(debugLog)(1, "main", "Tell tool about initial permissions\n"); |
| 2323 | { Addr* seg_starts; |
| 2324 | Int n_seg_starts; |
| 2325 | NSegment* seg; |
| 2326 | |
| 2327 | seg_starts = get_seg_starts( &n_seg_starts ); |
| 2328 | vg_assert(seg_starts && n_seg_starts > 0); |
| 2329 | |
| 2330 | /* show interesting ones to the tool */ |
| 2331 | for (i = 0; i < n_seg_starts; i++) { |
| 2332 | seg = VG_(am_find_nsegment)( seg_starts[i] ); |
| 2333 | vg_assert(seg); |
| 2334 | if (seg->kind == SkFileC || seg->kind == SkAnonC) { |
| 2335 | VG_(debugLog)(2, "main", |
| 2336 | "tell tool about %010lx-%010lx %c%c%c\n", |
| 2337 | seg->start, seg->end, |
| 2338 | seg->hasR ? 'r' : '-', |
| 2339 | seg->hasW ? 'w' : '-', |
| 2340 | seg->hasX ? 'x' : '-' ); |
| 2341 | VG_TRACK( new_mem_startup, seg->start, seg->end+1-seg->start, |
| 2342 | seg->hasR, seg->hasW, seg->hasX ); |
| 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | VG_(free)( seg_starts ); |
| 2347 | |
| 2348 | /* Also do the initial stack permissions. */ |
| 2349 | seg = VG_(am_find_nsegment)( initial_client_SP ); |
| 2350 | vg_assert(seg); |
| 2351 | vg_assert(seg->kind == SkAnonC); |
| 2352 | vg_assert(initial_client_SP >= seg->start); |
| 2353 | vg_assert(initial_client_SP <= seg->end); |
| 2354 | |
| 2355 | /* Stuff below the initial SP is unaddressable. */ |
| 2356 | /* NB: shouldn't this take into account the VG_STACK_REDZONE_SZB |
| 2357 | bytes below SP? */ |
| 2358 | VG_TRACK( die_mem_stack, seg->start, initial_client_SP - seg->start ); |
| 2359 | VG_(debugLog)(2, "main", "mark stack inaccessible %010lx-%010lx\n", |
| 2360 | seg->start, initial_client_SP-1 ); |
| 2361 | |
| 2362 | /* Also the assembly helpers. */ |
| 2363 | VG_TRACK( new_mem_startup, |
| 2364 | (Addr)&VG_(trampoline_stuff_start), |
| 2365 | &VG_(trampoline_stuff_end) - &VG_(trampoline_stuff_start), |
| 2366 | False, /* readable? */ |
| 2367 | False, /* writable? */ |
| 2368 | True /* executable? */ ); |
| 2369 | } |
| 2370 | |
| 2371 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2372 | // Initialise the scheduler |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2373 | // p: setup_file_descriptors() [else VG_(safe_fd)() breaks] |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2374 | // p: setup_client_stack |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2375 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2376 | VG_(debugLog)(1, "main", "Initialise scheduler\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2377 | { NSegment* seg = VG_(am_find_nsegment)( initial_client_SP ); |
| 2378 | vg_assert(seg); |
| 2379 | vg_assert(seg->kind == SkAnonC); |
| 2380 | vg_assert(initial_client_SP >= seg->start); |
| 2381 | vg_assert(initial_client_SP <= seg->end); |
| 2382 | VG_(scheduler_init)( seg->end, clstack_max_size ); |
| 2383 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2384 | |
| 2385 | //-------------------------------------------------------------- |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2386 | // Initialise the pthread model |
| 2387 | // p: ? |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2388 | // load_client() [for 'client_eip'] |
| 2389 | // setup_client_stack() [for 'sp_at_startup'] |
| 2390 | // setup_scheduler() [for the rest of state 1 stuff] |
| 2391 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2392 | VG_(debugLog)(1, "main", "Initialise thread 1's state\n"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2393 | init_thread1state( initial_client_IP, initial_client_SP, |
| 2394 | &VG_(threads)[1].arch); |
njn | ea4b28c | 2004-11-30 16:04:58 +0000 | [diff] [blame] | 2395 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 2396 | //-------------------------------------------------------------- |
sewardj | 39b88fb | 2005-03-12 15:16:07 +0000 | [diff] [blame] | 2397 | // Initialise the pthread model |
| 2398 | // p: ? |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2399 | //-------------------------------------------------------------- |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2400 | //if (VG_(clo_model_pthreads)) |
| 2401 | // VG_(pthread_init)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2402 | |
| 2403 | //-------------------------------------------------------------- |
| 2404 | // Initialise the signal handling subsystem |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2405 | // p: n/a |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2406 | //-------------------------------------------------------------- |
| 2407 | // Nb: temporarily parks the saved blocking-mask in saved_sigmask. |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2408 | VG_(debugLog)(1, "main", "Initialise signal management\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2409 | VG_(sigstartup_actions)(); |
| 2410 | |
| 2411 | //-------------------------------------------------------------- |
| 2412 | // Perhaps we're profiling Valgrind? |
| 2413 | // p: process_cmd_line_options() [for VG_(clo_profile)] |
| 2414 | // p: others? |
| 2415 | // |
| 2416 | // XXX: this seems to be broken? It always says the tool wasn't built |
| 2417 | // for profiling; vg_profile.c's functions don't seem to be overriding |
| 2418 | // vg_dummy_profile.c's? |
| 2419 | // |
| 2420 | // XXX: want this as early as possible. Looking for --profile |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2421 | // in get_helprequest_and_toolname() could get it earlier. |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2422 | //-------------------------------------------------------------- |
| 2423 | if (VG_(clo_profile)) |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 2424 | VG_(init_profiling)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2425 | |
| 2426 | VGP_PUSHCC(VgpStartup); |
| 2427 | |
| 2428 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2429 | // Read suppression file |
| 2430 | // p: process_cmd_line_options() [for VG_(clo_suppressions)] |
| 2431 | //-------------------------------------------------------------- |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2432 | if (VG_(needs).core_errors || VG_(needs).tool_errors) { |
| 2433 | VG_(debugLog)(1, "main", "Load suppressions\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2434 | VG_(load_suppressions)(); |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2435 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2436 | |
| 2437 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2438 | // Setup pointercheck |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 2439 | // p: layout_remaining_space() [for VG_(client_{base,end})] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2440 | // p: process_cmd_line_options() [for VG_(clo_pointercheck)] |
| 2441 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2442 | //if (VG_(clo_pointercheck)) |
| 2443 | // VG_(clo_pointercheck) = |
| 2444 | // VG_(setup_pointercheck)( VG_(client_base), VG_(client_end)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2445 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2446 | //-------------------------------------------------------------- |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 2447 | // register client stack |
| 2448 | //-------------------------------------------------------------- |
njn | 945ed2e | 2005-06-24 03:28:30 +0000 | [diff] [blame] | 2449 | VG_(clstk_id) = VG_(register_stack)(VG_(clstk_base), VG_(clstk_end)); |
rjwalsh | 0140af5 | 2005-06-04 20:42:33 +0000 | [diff] [blame] | 2450 | |
| 2451 | //-------------------------------------------------------------- |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2452 | // Show the address space state so far |
| 2453 | //-------------------------------------------------------------- |
| 2454 | VG_(debugLog)(1, "main", "\n"); |
| 2455 | VG_(debugLog)(1, "main", "\n"); |
| 2456 | VG_(am_show_nsegments)(1,"Memory layout at client startup"); |
| 2457 | VG_(debugLog)(1, "main", "\n"); |
| 2458 | VG_(debugLog)(1, "main", "\n"); |
| 2459 | |
| 2460 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2461 | // Run! |
| 2462 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2463 | VGP_POPCC(VgpStartup); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2464 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2465 | if (VG_(clo_xml)) { |
sewardj | 68cde6f | 2005-07-19 12:17:51 +0000 | [diff] [blame] | 2466 | HChar buf[50]; |
| 2467 | VG_(ctime)(buf); |
sewardj | 753673f | 2005-08-09 22:03:08 +0000 | [diff] [blame] | 2468 | VG_(message)(Vg_UserMsg, "<status>\n" |
| 2469 | " <state>RUNNING</state>\n" |
| 2470 | " <time>%t</time>\n" |
| 2471 | "</status>", |
| 2472 | buf); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2473 | VG_(message)(Vg_UserMsg, ""); |
| 2474 | } |
| 2475 | |
sewardj | 1fbc1a5 | 2005-04-25 02:05:54 +0000 | [diff] [blame] | 2476 | VG_(debugLog)(1, "main", "Running thread 1\n"); |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2477 | /* As a result of the following call, the last thread standing |
| 2478 | eventually winds up running VG_(shutdown_actions_NORETURN) just |
| 2479 | below. */ |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 2480 | VG_(main_thread_wrapper_NORETURN)(1); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2481 | |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2482 | /*NOTREACHED*/ |
| 2483 | vg_assert(0); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | |
njn | 9cb54ac | 2005-06-12 04:19:17 +0000 | [diff] [blame] | 2487 | /* Final clean-up before terminating the process. |
| 2488 | Clean up the client by calling __libc_freeres() (if requested) |
| 2489 | This is Linux-specific? |
| 2490 | */ |
| 2491 | static void final_tidyup(ThreadId tid) |
| 2492 | { |
njn | bc6d84d | 2005-06-19 18:58:03 +0000 | [diff] [blame] | 2493 | Addr __libc_freeres_wrapper; |
| 2494 | |
njn | 9cb54ac | 2005-06-12 04:19:17 +0000 | [diff] [blame] | 2495 | vg_assert(VG_(is_running_thread)(tid)); |
| 2496 | |
njn | bc6d84d | 2005-06-19 18:58:03 +0000 | [diff] [blame] | 2497 | if ( !VG_(needs).libc_freeres || |
| 2498 | !VG_(clo_run_libc_freeres) || |
| 2499 | 0 == (__libc_freeres_wrapper = VG_(get_libc_freeres_wrapper)()) ) |
njn | 9cb54ac | 2005-06-12 04:19:17 +0000 | [diff] [blame] | 2500 | return; /* can't/won't do it */ |
| 2501 | |
| 2502 | if (VG_(clo_verbosity) > 2 || |
| 2503 | VG_(clo_trace_syscalls) || |
| 2504 | VG_(clo_trace_sched)) |
| 2505 | VG_(message)(Vg_DebugMsg, |
| 2506 | "Caught __NR_exit; running __libc_freeres()"); |
| 2507 | |
| 2508 | /* point thread context to point to libc_freeres_wrapper */ |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 2509 | VG_(set_IP)(tid, __libc_freeres_wrapper); |
njn | 9cb54ac | 2005-06-12 04:19:17 +0000 | [diff] [blame] | 2510 | // XXX should we use a special stack? |
| 2511 | |
| 2512 | /* Block all blockable signals by copying the real block state into |
| 2513 | the thread's block state*/ |
| 2514 | VG_(sigprocmask)(VKI_SIG_BLOCK, NULL, &VG_(threads)[tid].sig_mask); |
| 2515 | VG_(threads)[tid].tmp_sig_mask = VG_(threads)[tid].sig_mask; |
| 2516 | |
| 2517 | /* and restore handlers to default */ |
| 2518 | VG_(set_default_handler)(VKI_SIGSEGV); |
| 2519 | VG_(set_default_handler)(VKI_SIGBUS); |
| 2520 | VG_(set_default_handler)(VKI_SIGILL); |
| 2521 | VG_(set_default_handler)(VKI_SIGFPE); |
| 2522 | |
| 2523 | // We were exiting, so assert that... |
| 2524 | vg_assert(VG_(is_exiting)(tid)); |
| 2525 | // ...but now we're not again |
| 2526 | VG_(threads)[tid].exitreason = VgSrc_None; |
| 2527 | |
| 2528 | // run until client thread exits - ideally with LIBC_FREERES_DONE, |
| 2529 | // but exit/exitgroup/signal will do |
| 2530 | VG_(scheduler)(tid); |
| 2531 | |
| 2532 | vg_assert(VG_(is_exiting)(tid)); |
| 2533 | } |
| 2534 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2535 | /* Do everything which needs doing when the last thread exits */ |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2536 | void VG_(shutdown_actions_NORETURN) ( ThreadId tid, |
| 2537 | VgSchedReturnCode tids_schedretcode ) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2538 | { |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2539 | VG_(debugLog)(1, "main", "entering VG_(shutdown_actions_NORETURN)\n"); |
| 2540 | |
| 2541 | vg_assert( VG_(count_living_threads)() == 1 ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2542 | vg_assert(VG_(is_running_thread)(tid)); |
| 2543 | |
| 2544 | // Wait for all other threads to exit. |
njn | 7b85dd5 | 2005-06-12 17:26:29 +0000 | [diff] [blame] | 2545 | VG_(reap_threads)(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2546 | |
| 2547 | VG_(clo_model_pthreads) = False; |
| 2548 | |
| 2549 | // Clean the client up before the final report |
njn | 9cb54ac | 2005-06-12 04:19:17 +0000 | [diff] [blame] | 2550 | final_tidyup(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 2551 | |
| 2552 | // OK, done |
| 2553 | VG_(exit_thread)(tid); |
| 2554 | |
| 2555 | /* should be no threads left */ |
| 2556 | vg_assert(VG_(count_living_threads)() == 0); |
| 2557 | |
| 2558 | VG_(threads)[tid].status = VgTs_Empty; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2559 | //-------------------------------------------------------------- |
| 2560 | // Finalisation: cleanup, messages, etc. Order no so important, only |
| 2561 | // affects what order the messages come. |
| 2562 | //-------------------------------------------------------------- |
| 2563 | if (VG_(clo_verbosity) > 0) |
| 2564 | VG_(message)(Vg_UserMsg, ""); |
| 2565 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2566 | if (VG_(clo_xml)) { |
sewardj | 68cde6f | 2005-07-19 12:17:51 +0000 | [diff] [blame] | 2567 | HChar buf[50]; |
sewardj | 9f297ca | 2005-05-20 02:29:52 +0000 | [diff] [blame] | 2568 | if (VG_(needs).core_errors || VG_(needs).tool_errors) { |
| 2569 | VG_(show_error_counts_as_XML)(); |
| 2570 | VG_(message)(Vg_UserMsg, ""); |
| 2571 | } |
sewardj | 68cde6f | 2005-07-19 12:17:51 +0000 | [diff] [blame] | 2572 | VG_(ctime)(buf); |
sewardj | 753673f | 2005-08-09 22:03:08 +0000 | [diff] [blame] | 2573 | VG_(message)(Vg_UserMsg, "<status>\n" |
| 2574 | " <state>FINISHED</state>\n" |
| 2575 | " <time>%t</time>\n" |
| 2576 | "</status>", |
| 2577 | buf); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2578 | VG_(message)(Vg_UserMsg, ""); |
| 2579 | } |
| 2580 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2581 | /* Print out file descriptor summary and stats. */ |
| 2582 | if (VG_(clo_track_fds)) |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 2583 | VG_(show_open_fds)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2584 | |
njn | 95ec870 | 2004-11-22 16:46:13 +0000 | [diff] [blame] | 2585 | if (VG_(needs).core_errors || VG_(needs).tool_errors) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2586 | VG_(show_all_errors)(); |
| 2587 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2588 | VG_TDICT_CALL(tool_fini, 0/*exitcode*/); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2589 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 2590 | if (VG_(clo_xml)) { |
| 2591 | VG_(message)(Vg_UserMsg, ""); |
| 2592 | VG_(message)(Vg_UserMsg, "</valgrindoutput>"); |
| 2593 | VG_(message)(Vg_UserMsg, ""); |
| 2594 | } |
| 2595 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 2596 | VG_(sanity_check_general)( True /*include expensive checks*/ ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2597 | |
| 2598 | if (VG_(clo_verbosity) > 1) |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 2599 | print_all_stats(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2600 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2601 | if (VG_(clo_profile)) |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 2602 | VG_(done_profiling)(); |
njn | 2025cf9 | 2005-06-26 20:44:48 +0000 | [diff] [blame] | 2603 | |
| 2604 | if (VG_(clo_profile_flags) > 0) { |
| 2605 | #define N_MAX 100 |
| 2606 | BBProfEntry tops[N_MAX]; |
| 2607 | ULong score_total = VG_(get_BB_profile) (tops, N_MAX); |
| 2608 | show_BB_profile(tops, N_MAX, score_total); |
| 2609 | } |
sewardj | fa8ec11 | 2005-01-19 11:55:34 +0000 | [diff] [blame] | 2610 | |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 2611 | /* Print Vex storage stats */ |
sewardj | bf42651 | 2005-01-17 18:35:30 +0000 | [diff] [blame] | 2612 | if (0) |
| 2613 | LibVEX_ShowAllocStats(); |
sewardj | 1d88711 | 2005-05-30 21:44:08 +0000 | [diff] [blame] | 2614 | |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2615 | /* Ok, finally exit in the os-specific way, according to the scheduler's |
| 2616 | return code. In short, if the (last) thread exited by calling |
| 2617 | sys_exit, do likewise; if the (last) thread stopped due to a fatal |
| 2618 | signal, terminate the entire system with that same fatal signal. */ |
| 2619 | VG_(debugLog)(1, "core_os", |
njn | 7b85dd5 | 2005-06-12 17:26:29 +0000 | [diff] [blame] | 2620 | "VG_(terminate_NORETURN)(tid=%lld)\n", (ULong)tid); |
njn | 8aa3585 | 2005-06-10 22:59:56 +0000 | [diff] [blame] | 2621 | |
| 2622 | vg_assert(VG_(count_living_threads)() == 0); |
| 2623 | |
| 2624 | switch (tids_schedretcode) { |
| 2625 | case VgSrc_ExitSyscall: /* the normal way out */ |
| 2626 | VG_(exit)( VG_(threads)[tid].os_state.exitcode ); |
| 2627 | /* NOT ALIVE HERE! */ |
| 2628 | VG_(core_panic)("entered the afterlife in main() -- ExitSyscall"); |
| 2629 | break; /* what the hell :) */ |
| 2630 | |
| 2631 | case VgSrc_FatalSig: |
| 2632 | /* We were killed by a fatal signal, so replicate the effect */ |
| 2633 | vg_assert(VG_(threads)[tid].os_state.fatalsig != 0); |
| 2634 | VG_(kill_self)(VG_(threads)[tid].os_state.fatalsig); |
| 2635 | VG_(core_panic)("main(): signal was supposed to be fatal"); |
| 2636 | break; |
| 2637 | |
| 2638 | default: |
| 2639 | VG_(core_panic)("main(): unexpected scheduler return code"); |
| 2640 | } |
njn | e96be67 | 2005-05-08 19:08:54 +0000 | [diff] [blame] | 2641 | } |
sewardj | 8b635a4 | 2004-11-22 19:01:47 +0000 | [diff] [blame] | 2642 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2643 | |
| 2644 | /*====================================================================*/ |
| 2645 | /*=== Getting to main() alive ===*/ |
| 2646 | /*====================================================================*/ |
| 2647 | |
| 2648 | /* If linking of the final executables is done with glibc present, |
| 2649 | then Valgrind starts at main() above as usual, and all of the |
| 2650 | following code is irrelevant. |
| 2651 | |
| 2652 | However, this is not the intended mode of use. The plan is to |
| 2653 | avoid linking against glibc, by giving gcc the flags |
| 2654 | -nodefaultlibs -lgcc -nostartfiles at startup. |
| 2655 | |
| 2656 | From this derive two requirements: |
| 2657 | |
| 2658 | 1. gcc may emit calls to memcpy and memset to deal with structure |
| 2659 | assignments etc. Since we have chosen to ignore all the |
| 2660 | "normal" supporting libraries, we have to provide our own |
| 2661 | implementations of them. No problem. |
| 2662 | |
| 2663 | 2. We have to provide a symbol "_start", to which the kernel |
| 2664 | hands control at startup. Hence the code below. |
| 2665 | */ |
| 2666 | |
| 2667 | /* ---------------- Requirement 1 ---------------- */ |
| 2668 | |
| 2669 | void* memcpy(void *dest, const void *src, size_t n); |
| 2670 | void* memcpy(void *dest, const void *src, size_t n) { |
| 2671 | return VG_(memcpy)(dest,src,n); |
| 2672 | } |
| 2673 | void* memset(void *s, int c, size_t n); |
| 2674 | void* memset(void *s, int c, size_t n) { |
| 2675 | return VG_(memset)(s,c,n); |
| 2676 | } |
| 2677 | |
| 2678 | /* ---------------- Requirement 2 ---------------- */ |
| 2679 | |
| 2680 | /* Glibc's sysdeps/i386/elf/start.S has the following gem of a |
| 2681 | comment, which explains how the stack looks right at process start |
| 2682 | (when _start is jumped to). Hence _start passes %esp to |
| 2683 | _start_in_C, which extracts argc/argv/envp and starts up |
| 2684 | correctly. */ |
| 2685 | |
| 2686 | /* This is the canonical entry point, usually the first thing in the text |
| 2687 | segment. The SVR4/i386 ABI (pages 3-31, 3-32) says that when the entry |
| 2688 | point runs, most registers' values are unspecified, except for: |
| 2689 | |
| 2690 | %edx Contains a function pointer to be registered with `atexit'. |
| 2691 | This is how the dynamic linker arranges to have DT_FINI |
| 2692 | functions called for shared libraries that have been loaded |
| 2693 | before this code runs. |
| 2694 | |
| 2695 | %esp The stack contains the arguments and environment: |
| 2696 | 0(%esp) argc |
| 2697 | 4(%esp) argv[0] |
| 2698 | ... |
| 2699 | (4*argc)(%esp) NULL |
| 2700 | (4*(argc+1))(%esp) envp[0] |
| 2701 | ... |
| 2702 | NULL |
| 2703 | */ |
| 2704 | |
| 2705 | /* The kernel hands control to _start, which extracts the initial |
| 2706 | stack pointer and calls onwards to _start_in_C. This also switches the new stack. */ |
| 2707 | #if defined(VGP_x86_linux) |
| 2708 | asm("\n" |
| 2709 | "\t.globl _start\n" |
| 2710 | "\t.type _start,@function\n" |
| 2711 | "_start:\n" |
| 2712 | /* set up the new stack in %eax */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 2713 | "\tmovl $vgPlain_interim_stack, %eax\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2714 | "\taddl $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %eax\n" |
| 2715 | "\taddl $"VG_STRINGIFY(VG_STACK_ACTIVE_SZB)", %eax\n" |
| 2716 | "\tsubl $16, %eax\n" |
| 2717 | "\tandl $~15, %eax\n" |
| 2718 | /* install it, and collect the original one */ |
| 2719 | "\txchgl %eax, %esp\n" |
| 2720 | /* call _start_in_C, passing it the startup %esp */ |
| 2721 | "\tpushl %eax\n" |
| 2722 | "\tcall _start_in_C\n" |
| 2723 | "\thlt\n" |
| 2724 | ); |
| 2725 | #elif defined(VGP_amd64_linux) |
| 2726 | asm("\n" |
| 2727 | "\t.globl _start\n" |
| 2728 | "\t.type _start,@function\n" |
| 2729 | "_start:\n" |
| 2730 | /* set up the new stack in %rdi */ |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 2731 | "\tmovq $vgPlain_interim_stack, %rdi\n" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2732 | "\taddq $"VG_STRINGIFY(VG_STACK_GUARD_SZB)", %rdi\n" |
| 2733 | "\taddq $"VG_STRINGIFY(VG_STACK_ACTIVE_SZB)", %rdi\n" |
| 2734 | "\tandq $~15, %rdi\n" |
| 2735 | /* install it, and collect the original one */ |
| 2736 | "\txchgq %rdi, %rsp\n" |
| 2737 | /* call _start_in_C, passing it the startup %rsp */ |
| 2738 | "\tcall _start_in_C\n" |
| 2739 | "\thlt\n" |
| 2740 | ); |
| 2741 | #else |
| 2742 | #error "_start: needs implementation on this platform" |
| 2743 | #endif |
| 2744 | |
| 2745 | /* Avoid compiler warnings: this fn _is_ used, but labelling it |
| 2746 | 'static' causes gcc to complain it isn't. */ |
| 2747 | void _start_in_C ( UWord* pArgc ); |
| 2748 | void _start_in_C ( UWord* pArgc ) |
| 2749 | { |
| 2750 | Int r; |
| 2751 | Word argc = pArgc[0]; |
| 2752 | HChar** argv = (HChar**)&pArgc[1]; |
| 2753 | HChar** envp = (HChar**)&pArgc[1+argc+1]; |
sewardj | fdf91b4 | 2005-09-28 00:53:09 +0000 | [diff] [blame] | 2754 | sp_at_startup = (Addr)pArgc; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2755 | r = main( (Int)argc, argv, envp ); |
| 2756 | VG_(exit)(r); |
| 2757 | } |
| 2758 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2759 | /*--------------------------------------------------------------------*/ |
njn | 04e1698 | 2005-05-31 00:23:43 +0000 | [diff] [blame] | 2760 | /*--- end ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2761 | /*--------------------------------------------------------------------*/ |