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