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