Home | History | Annotate | Download | only in valgrind
      1 #!/bin/bash
      2 
      3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 # This is a small script for manually launching valgrind, along with passing
      8 # it the suppression file, and some helpful arguments (automatically attaching
      9 # the debugger on failures, etc).  Run it from your repo root, something like:
     10 #  $ sh ./tools/valgrind/valgrind.sh ./out/Debug/chrome
     11 #
     12 # This is mostly intended for running the chrome browser interactively.
     13 # To run unit tests, you probably want to run chrome_tests.sh instead.
     14 # That's the script used by the valgrind buildbot.
     15 
     16 export THISDIR=`dirname $0`
     17 
     18 setup_memcheck() {
     19   RUN_COMMAND="valgrind"
     20   GDB=gdb
     21   EXE_INFO=$(file $1)
     22   if [[ $? -eq 0 ]]; then
     23     # Prefer a gdb that matches the executable if it's available.
     24     if [[ "$EXE_INFO" == *32-bit* && -x /usr/bin/gdb32 ]]; then
     25       GDB="/usr/bin/gdb32";
     26     elif [[ "$EXE_INFO" == *64-bit* && -x /usr/bin/gdb64 ]]; then
     27       GDB="/usr/bin/gdb64";
     28     fi
     29   fi
     30 
     31   # Prompt to attach gdb when there was an error detected.
     32   DEFAULT_TOOL_FLAGS=("--db-command=$GDB -nw %f %p" "--db-attach=yes" \
     33                       # Keep the registers in gdb in sync with the code.
     34                       "--vex-iropt-register-updates=allregs-at-mem-access" \
     35                       # Overwrite newly allocated or freed objects
     36                       # with 0x41 to catch inproper use.
     37                       "--malloc-fill=41" "--free-fill=41" \
     38                       # Increase the size of stacks being tracked.
     39                       "--num-callers=30")
     40 }
     41 
     42 setup_tsan() {
     43   RUN_COMMAND="valgrind-tsan.sh"
     44   IGNORE_FILE="$THISDIR/tsan/ignores.txt"
     45   DEFAULT_TOOL_FLAGS=("--announce-threads" "--pure-happens-before=yes" \
     46                       "--ignore=$IGNORE_FILE")
     47 }
     48 
     49 setup_unknown() {
     50   echo "Unknown tool \"$TOOL_NAME\" specified, the result is not guaranteed"
     51   DEFAULT_TOOL_FLAGS=()
     52 }
     53 
     54 set -e
     55 
     56 if [ $# -eq 0 ]; then
     57   echo "usage: <command to run> <arguments ...>"
     58   exit 1
     59 fi
     60 
     61 TOOL_NAME="memcheck"
     62 declare -a DEFAULT_TOOL_FLAGS[0]
     63 
     64 # Select a tool different from memcheck with --tool=TOOL as a first argument
     65 TMP_STR=`echo $1 | sed 's/^\-\-tool=//'`
     66 if [ "$TMP_STR" != "$1" ]; then
     67   TOOL_NAME="$TMP_STR"
     68   shift
     69 fi
     70 
     71 if echo "$@" | grep "\-\-tool" ; then
     72   echo "--tool=TOOL must be the first argument" >&2
     73   exit 1
     74 fi
     75 
     76 case $TOOL_NAME in
     77   memcheck*)  setup_memcheck "$1";;
     78   tsan*)      setup_tsan;;
     79   *)          setup_unknown;;
     80 esac
     81 
     82 
     83 SUPPRESSIONS="$THISDIR/$TOOL_NAME/suppressions.txt"
     84 
     85 CHROME_VALGRIND=`sh $THISDIR/locate_valgrind.sh`
     86 if [ "$CHROME_VALGRIND" = "" ]
     87 then
     88   # locate_valgrind.sh failed
     89   exit 1
     90 fi
     91 echo "Using valgrind binaries from ${CHROME_VALGRIND}"
     92 
     93 set -x
     94 PATH="${CHROME_VALGRIND}/bin:$PATH"
     95 # We need to set these variables to override default lib paths hard-coded into
     96 # Valgrind binary.
     97 export VALGRIND_LIB="$CHROME_VALGRIND/lib/valgrind"
     98 export VALGRIND_LIB_INNER="$CHROME_VALGRIND/lib/valgrind"
     99 
    100 # G_SLICE=always-malloc: make glib use system malloc
    101 # NSS_DISABLE_UNLOAD=1: make nss skip dlclosing dynamically loaded modules,
    102 # which would result in "obj:*" in backtraces.
    103 # NSS_DISABLE_ARENA_FREE_LIST=1: make nss use system malloc
    104 # G_DEBUG=fatal_warnings: make  GTK abort on any critical or warning assertions.
    105 # If it crashes on you in the Options menu, you hit bug 19751,
    106 # comment out the G_DEBUG=fatal_warnings line.
    107 #
    108 # GTEST_DEATH_TEST_USE_FORK=1: make gtest death tests valgrind-friendly
    109 #
    110 # When everyone has the latest valgrind, we might want to add
    111 #  --show-possibly-lost=no
    112 # to ignore possible but not definite leaks.
    113 
    114 G_SLICE=always-malloc \
    115 NSS_DISABLE_UNLOAD=1 \
    116 NSS_DISABLE_ARENA_FREE_LIST=1 \
    117 G_DEBUG=fatal_warnings \
    118 GTEST_DEATH_TEST_USE_FORK=1 \
    119 $RUN_COMMAND \
    120   --trace-children=yes \
    121   --leak-check=yes \
    122   --suppressions="$SUPPRESSIONS" \
    123   "${DEFAULT_TOOL_FLAGS[@]}" \
    124   "$@"
    125