1#!/bin/sh
2# This Source Code Form is subject to the terms of the Mozilla Public
3# License, v. 2.0. If a copy of the MPL was not distributed with this
4# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
6"$REAL_DSYMUTIL" "$@"
7ret=$?
8if [ $ret -ne 139 ]; then
9  exit $ret
10fi
11
12echo "$REAL_DSYMUTIL crashed. Trying to get a reduced testcase." >&2
13tmpdir=$(mktemp -d)
14trap "rm -rf $tmpdir" EXIT
15
16# Get the library file name from the command line arguments. We assume
17# it's the last argument that doesn't start with a dash.
18for arg in "$@"; do
19  case "$arg" in
20  -*)
21    ;;
22  *)
23    lib="$arg"
24    ;;
25  esac
26done
27
28last_obj=$("$REAL_DSYMUTIL" --verbose "$@" 2> /dev/null | sed -n "/trying to open/s/trying to open '\(.*\)'/\1/p" | tail -1)
29
30case "$last_obj" in
31"")
32  echo "Could not produce a reduced testcase. Aborting." >&2
33  # Ideally, we'd produce an archive with every .o and .a involved, but so
34  # far, this case has never happened, so, meh.
35  exit 139
36  ;;
37*.a\(*.o\))
38  # The crash likely happened while reading one particular object in a library.
39  # Create a new library with just that one object.
40  archive=$(readlink -f "${last_obj%(*}")
41  obj="${last_obj#*.a(}"
42  obj="${obj%)}"
43  (cd "$tmpdir"; ar x "$archive" "$obj")
44  mkdir -p $tmpdir/crasher/$(dirname "$archive")
45  (cd "$tmpdir"; ar cr "$tmpdir/crasher/$archive" "$obj")
46  rm "$tmpdir/$obj"
47  ;;
48*)
49  # The crash likely happened while reading one particular object.
50  obj=$(readlink -f "$last_obj")
51  mkdir -p "$tmpdir/crasher/$(dirname "$obj")"
52  cp "$obj" "$tmpdir/crasher/$obj"
53  ;;
54esac
55cp "$lib" "$tmpdir/crasher"
56cat > "$tmpdir/crasher/run-me.sh" <<EOF
57#!/bin/sh
58DSYMUTIL="\${DSYMUTIL:-llvm-dsymutil}"
59dir="\$(dirname \$0)"
60\$DSYMUTIL -oso-prepend-path="\$dir" "\$dir/$(basename "$lib")"
61exit \$?
62EOF
63chmod +x "$tmpdir/crasher/run-me.sh"
64(cd "$tmpdir"/crasher; DSYMUTIL=/builds/worker/workspace/build/src/llvm-dsymutil/bin/llvm-dsymutil ./run-me.sh > /dev/null 2>&1)
65if [ $? -eq 139 ]; then
66  echo "Could reproduce with a reduced testcase. Creating an artifact." >&2
67  mkdir -p "$HOME/artifacts"
68  artifact=dsymutil-crasher.tar.xz
69  tar -Jcf "$HOME/artifacts/$artifact" -C "$tmpdir" crasher/
70  echo "Check the $artifact artifact." >&2
71else
72  echo "Could not reproduce with a reduced testcase. Sorry." >&2
73fi
74
75exit 139
76