blob: 6f31bad37d346df40d867ce5346f23ee1897a553 [file] [log] [blame]
bjacob32067b82020-11-02 13:20:03 -05001#!/bin/bash
2
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07003# Copyright 2020 The IREE Authors
Hanhan Wang1874d142020-11-03 10:03:50 -08004#
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07005# 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 Wang1874d142020-11-03 10:03:50 -08008
bjacob32067b82020-11-02 13:20:03 -05009# 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:
bjacob3fd94672020-12-04 14:02:07 -050018# 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.
bjacob32067b82020-11-02 13:20:03 -050022#
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
28if [ -z "${ANDROID_NDK}" ]
29then
30 echo "Please define ANDROID_NDK to point to your Android NDK."
31 exit 1
32fi
33
34tmpdir="$(mktemp -d)"
35
36while read line
37do
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"
71done
72
73rm -rf "$tmpdir"