Add script to symbolize android stacks, particularly for use with Asan. (#3673)

* Add script to symbolize android stacks, particularly for use with Asan.

You just paste some ASan output to a text file and feed that to stdin to
this script. It will then pull the files out of the device and symbolize
them on the host using the NDK-provided host build of llvm-symbolizer.
Because it just pulls files from the device, there's no need to point
this script to any particular obj dir. It's easy to use.

ANDROID_NDK=~/android-ndk-r21d ~/android-symbolize.sh < ~/a

Discussion of alternatives: https://github.com/android/ndk/issues/753

* rename
diff --git a/scripts/android_symbolize.sh b/scripts/android_symbolize.sh
new file mode 100755
index 0000000..8f2d3e5
--- /dev/null
+++ b/scripts/android_symbolize.sh
@@ -0,0 +1,65 @@
+#!/bin/bash
+
+# Reads text from stdin containing stack frames in the format
+# `#3 0x5b09835a14  (/data/local/tmp/iree-benchmark-module+0x18aa14)`
+# and symbolizes such lines and prints them to stdout.
+#
+# The approach is to pull the files out of the device and use the host
+# build of llvm-symbolizer provided in the NDK.
+#
+# Other lines are just echo'd to stdout.
+# This is meant to be used like this:
+# ANDROID_NDK=~/android-ndk-r21d ~/android-symbolize.sh < ~/a
+# Where one has previously stored e.g. some ASan report to a file, here ~/a.
+#
+# Discussion of alternatives: https://github.com/android/ndk/issues/753
+
+# Provide the location of your Android NDK in the ANDROID_NDK env var.
+: ${ANDROID_NDK:=""}
+
+if [ -z "${ANDROID_NDK}" ]
+then
+  echo "Please define ANDROID_NDK to point to your Android NDK."
+  exit 1
+fi
+
+tmpdir="$(mktemp -d)"
+
+while read line
+do
+  header="$(echo "$line" | grep -o '^\s*#[0-9]\+')"
+  if [ -z "$header" ]
+  then
+    echo "$line"
+    continue
+  fi
+
+  location_with_parentheses="$(echo "$line" | grep -o '(/[^)]*)$')"
+  location="$(echo "$location_with_parentheses" | tr -d '()' )"
+  file="$(echo "$location" | cut -d '+' -f 1)"
+  address="$(echo "$location" | cut -d '+' -f 2)"
+  if [[ -z "$file" || -z "$address" ]]
+  then
+    echo "$line"
+    continue
+  fi
+
+  file_basename="$(basename "$file")"
+  pulled_file="$tmpdir/$file_basename"
+  if [ ! -f "$pulled_file" ]
+  then
+    adb pull "$file" "$pulled_file" 1>/dev/null 2>/dev/null
+  fi
+  llvm_symbolizer_output="$($ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-symbolizer -obj "$pulled_file" "$address")"
+  if [ -z "$llvm_symbolizer_output" ]
+  then
+    echo "$line"
+    continue
+  fi
+
+  function="$(echo "$llvm_symbolizer_output" | head -n1)"
+  source_location="$(echo "$llvm_symbolizer_output" | tail -n1)"
+  echo "$header $source_location $function"
+done
+
+rm -rf "$tmpdir"