1#!/bin/bash
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# Get backtraces for a process.
17if [[ $1 =~ ^[0-9]+$ ]] ; then
18    PID="$1"
19elif [ "$1" ] ; then
20    # Try --exact first, and fallback to nonexact if we find nothing.
21    PIDLIST="$(pid --exact $1 || pid $1)"
22    if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
23        PID="$PIDLIST"
24    elif [ "$PIDLIST" ] ; then
25        echo "more than one process: $1"
26    else
27        echo "no such process: $1"
28    fi
29else
30    echo "usage: stacks [pid|process name]"
31fi
32
33if [ "$PID" ] ; then
34    # Use `debuggerd -j` on java processes.
35    if adb shell readlink /proc/$PID/exe | egrep -q '^/system/bin/app_process' ; then
36        # But not the zygote.
37        if ! adb shell cat /proc/$PID/cmdline | egrep -q '^zygote'; then
38            adb shell debuggerd -j $PID
39            exit $?
40        fi
41    fi
42
43    adb shell debuggerd -b $PID
44fi
45