Skip to content

Instantly share code, notes, and snippets.

@s1ntoneli
Last active October 15, 2018 09:45
Show Gist options
  • Save s1ntoneli/f8072da85eab24f070b04e319886a01f to your computer and use it in GitHub Desktop.
Save s1ntoneli/f8072da85eab24f070b04e319886a01f to your computer and use it in GitHub Desktop.
检查安卓指定进程的 fd 变化情况脚本
#!/bin/bash
help() {
cat <<- HELP
Usage: `basename $0` -p <packageName> [-v]
List fd info for package indicated.
-p, --pkg package name
-v verbose
Examples:
`basename $0` -p com.android.browser -v
`basename $0` -p com.android.browser
HELP
exit 1
}
SHORT_PARAM="vp:h"
LONG_PARAM="pkg,help"
GETOPT="getopt"
FILE_FDS="/tmp/fds"
FILE_LAST_FDS="/tmp/last_fds"
if [ "$SHORT_PARAM" ] ; then GETOPT="$GETOPT -o $SHORT_PARAM" ; fi
if [ "$LONG_PARAM" ] ; then GETOPT="$GETOPT --long $LONG_PARAM" ; fi
NEW_PARAMS=`$GETOPT -n $0 -- "$@"`
eval set -- "$NEW_PARAMS"
verbose=false
while true ; do
case $1 in
-v) verbose=true ; shift ;;
-p|--pkg) packageName="$2"; shift 2;;
-h|--help) help; shift ;;
--) shift ; break ;;
esac
done
if [ ! "$packageName" ] ; then echo "Usage: $0 -p <packageName>"; exit 1; fi
pid=`adb shell ps -ef|grep com.android.browser$|sed 's/[[:space:]]\+/ /g'|cut -d' ' -f2`
if [ ! "$pid" ] ; then echo "pid of $packageName is not exists"; exit 1; fi
if [ -f "${FILE_FDS}_$pid" ] ; then mv "${FILE_FDS}_$pid" "${FILE_LAST_FDS}_$pid"; fi
adb shell ls -l /proc/$pid/fd |cut -d' ' -f1,2,3,4,5,8,9,10 | sort > "${FILE_FDS}_$pid"
if [ ! -f "${FILE_LAST_FDS}_$pid" ] ; then echo "Fds file init finished."; exit 0; fi
newFds=`comm -13 "${FILE_LAST_FDS}_$pid" "${FILE_FDS}_$pid"`
freedFds=`comm -23 "${FILE_LAST_FDS}_$pid" "${FILE_FDS}_$pid"`
if [ "$newFds" ] ; then newCount=`echo "$newFds" |wc -l`; else newCount="0" ; fi
if [ "$freedFds" ] ; then freedCount=`echo "$freedFds"|wc -l`; else freedCount="0" ; fi
totalCount=`wc -l ${FILE_FDS}_$pid |cut -d' ' -f1`
if $verbose ; then
if [ "$newFds" ] ; then
echo "New fds: $newCount"
echo "$newFds"
echo
fi
if [ "$freedFds" ] ; then
echo "Freed fds: $freedCount"
echo "$freedFds"
echo
fi
fi
echo "total: $totalCount"
echo "New fds: $newCount"
echo "Freed fds: $freedCount"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment