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