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