• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# This scripts builds a self-contained executable file for Valgrind.
3# Usage:
4#   ./mk-self-contained-valgrind.sh /path/to/valgrind/installation tool_name resulting_binary [tool_flag]
5
6# Take the valgrind installation from here:
7IN_DIR="$1"
8# Tool name:
9TOOL="$2"
10# Put the result here:
11OUT="$3"
12# If not empty, use as the --tool= value:
13if [ "$4" == "" ]
14then
15  TOOLFLAG=$TOOL
16else
17  TOOLFLAG="$4"
18fi
19
20# The files/dirs to take:
21IN_FILES="bin/valgrind lib/valgrind/vgpreload_core* lib/valgrind/*$TOOL* lib/valgrind/default.supp"
22EXCLUDE_FILES="lib/valgrind/*$TOOL-debug*"
23
24rm -f $OUT && touch $OUT && chmod +x $OUT || exit 1
25
26# Create the header.
27cat << 'EOF' >> $OUT || exit 1
28#!/bin/bash
29# This is a self-extracting executable of Valgrind.
30# This file is autogenerated by mk-self-contained-valgrind.sh.
31
32# We extract the temporary files to $VALGRIND_EXTRACT_DIR/valgrind.XXXXXX
33VALGRIND_EXTRACT_DIR=${VALGRIND_EXTRACT_DIR:-/tmp}
34EXTRACT_DIR="$(mktemp -d $VALGRIND_EXTRACT_DIR/valgrind.XXXXXX)"
35
36cleanup() {
37  rm -rf $EXTRACT_DIR
38}
39# We will cleanup on exit.
40trap cleanup EXIT
41
42mkdir -p $EXTRACT_DIR
43chmod +rwx $EXTRACT_DIR
44EOF
45# end of header
46
47# Create the self-extractor
48
49# Create the runner
50cat << 'EOF' >> $OUT || exit 1
51# Extract:
52sed '1,/^__COMPRESSED_DATA_BELOW__$/d' $0 | tar xz -C $EXTRACT_DIR
53
54# Run
55# echo Extracting Valgrind to $EXTRACT_DIR
56export VALGRIND_LIB="$EXTRACT_DIR/lib/valgrind"
57export VALGRIND_LIB_INNER="$EXTRACT_DIR/lib/valgrind"
58EOF
59
60echo "\$EXTRACT_DIR/bin/valgrind --tool=$TOOLFLAG \"\$@\"" >> $OUT || exit 1
61
62cat << 'EOF' >> $OUT || exit 1
63EXIT_STATUS=$?
64cleanup # the trap above will handle the cleanup only if we are in bash 3.x
65exit $EXIT_STATUS # make sure to return the exit code from valgrind.
66
67__COMPRESSED_DATA_BELOW__
68EOF
69
70# Dump the compressed binary at the very end of the file.
71(cd $IN_DIR && tar zcvh --exclude=$EXCLUDE_FILES $IN_FILES) >> $OUT || exit 1
72
73echo "File $OUT successfully created"
74