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