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