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