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