bjacob | 32067b8 | 2020-11-02 13:20:03 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 3 | # Copyright 2020 The IREE Authors |
Hanhan Wang | 1874d14 | 2020-11-03 10:03:50 -0800 | [diff] [blame] | 4 | # |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 5 | # Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 6 | # See https://llvm.org/LICENSE.txt for license information. |
| 7 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Hanhan Wang | 1874d14 | 2020-11-03 10:03:50 -0800 | [diff] [blame] | 8 | |
bjacob | 32067b8 | 2020-11-02 13:20:03 -0500 | [diff] [blame] | 9 | # Reads text from stdin containing stack frames in the format |
| 10 | # `#3 0x5b09835a14 (/data/local/tmp/iree-benchmark-module+0x18aa14)` |
| 11 | # and symbolizes such lines and prints them to stdout. |
| 12 | # |
| 13 | # The approach is to pull the files out of the device and use the host |
| 14 | # build of llvm-symbolizer provided in the NDK. |
| 15 | # |
| 16 | # Other lines are just echo'd to stdout. |
| 17 | # This is meant to be used like this: |
bjacob | 3fd9467 | 2020-12-04 14:02:07 -0500 | [diff] [blame] | 18 | # ANDROID_NDK=~/android-ndk-r21d ./scripts/android_symbolize.sh < /tmp/asan.txt |
| 19 | # Where one has previously stored e.g. some ASan report to a file, here /tmp/asan.txt. |
| 20 | # |
| 21 | # See docs/developing_iree/sanitizers.md. |
bjacob | 32067b8 | 2020-11-02 13:20:03 -0500 | [diff] [blame] | 22 | # |
| 23 | # Discussion of alternatives: https://github.com/android/ndk/issues/753 |
| 24 | |
| 25 | # Provide the location of your Android NDK in the ANDROID_NDK env var. |
| 26 | : ${ANDROID_NDK:=""} |
| 27 | |
| 28 | if [ -z "${ANDROID_NDK}" ] |
| 29 | then |
| 30 | echo "Please define ANDROID_NDK to point to your Android NDK." |
| 31 | exit 1 |
| 32 | fi |
| 33 | |
| 34 | tmpdir="$(mktemp -d)" |
| 35 | |
| 36 | while read line |
| 37 | do |
| 38 | header="$(echo "$line" | grep -o '^\s*#[0-9]\+')" |
| 39 | if [ -z "$header" ] |
| 40 | then |
| 41 | echo "$line" |
| 42 | continue |
| 43 | fi |
| 44 | |
| 45 | location_with_parentheses="$(echo "$line" | grep -o '(/[^)]*)$')" |
| 46 | location="$(echo "$location_with_parentheses" | tr -d '()' )" |
| 47 | file="$(echo "$location" | cut -d '+' -f 1)" |
| 48 | address="$(echo "$location" | cut -d '+' -f 2)" |
| 49 | if [[ -z "$file" || -z "$address" ]] |
| 50 | then |
| 51 | echo "$line" |
| 52 | continue |
| 53 | fi |
| 54 | |
| 55 | file_basename="$(basename "$file")" |
| 56 | pulled_file="$tmpdir/$file_basename" |
| 57 | if [ ! -f "$pulled_file" ] |
| 58 | then |
| 59 | adb pull "$file" "$pulled_file" 1>/dev/null 2>/dev/null |
| 60 | fi |
| 61 | llvm_symbolizer_output="$($ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-symbolizer -obj "$pulled_file" "$address")" |
| 62 | if [ -z "$llvm_symbolizer_output" ] |
| 63 | then |
| 64 | echo "$line" |
| 65 | continue |
| 66 | fi |
| 67 | |
| 68 | function="$(echo "$llvm_symbolizer_output" | head -n1)" |
| 69 | source_location="$(echo "$llvm_symbolizer_output" | tail -n1)" |
| 70 | echo "$header $source_location $function" |
| 71 | done |
| 72 | |
| 73 | rm -rf "$tmpdir" |