1#!/bin/bash
2
3TRACE_DIR=/sdcard/ANRdaemon
4
5if [ $# -eq 1 ]; then
6    DEVICE=$(echo "-s $1")
7else
8    DEVICE=""
9fi
10
11PID=$(adb $DEVICE shell "ps | grep anrd")
12
13if [ $? -ne 0 ]; then
14    echo "FAILED. ADB failed or Daemon is not running."
15    exit 1
16fi
17
18PID=$(echo "$PID" | awk '{ print $2 }')
19adb $DEVICE shell "kill -s SIGUSR1 $PID"
20
21TRACE_FILE=$(adb $DEVICE shell "ls $TRACE_DIR | tail -n1" | tr -d '\r')
22
23# Wiat the trace file generation to complete
24adb $DEVICE shell "lsof $PID" | grep $TRACE_FILE > /dev/null
25while [ $? -eq 0 ];
26do
27    sleep 1
28    adb $DEVICE shell "lsof $PID" | grep "$TRACE_FILE" > /dev/null
29done
30
31if [ -z "$TRACE_FILE" ]; then
32    echo "FAILED. Trace file not created"
33fi
34
35adb $DEVICE pull "${TRACE_DIR}/${TRACE_FILE}" ${TRACE_FILE}
36
37CURRENT_DIR=$(pwd)
38echo SUCCEED!
39echo Trace stored at ${CURRENT_DIR}/${TRACE_FILE}
40