Add --exact flag to `pid` tool; new `qpid` tool.
The new --exact flag for pid does an exact match on the
process name, rather than grepping anywhere in the ps
output, which helps target a specific process if its name is
a substring of another process name. (Nothing else about
pid's output, including inclusion of the ps header if it
matches, is affected.)
qpid ("quick pid" or "quiet pid") lists all processes in the
following simplified format:
<pid> <procname>
It also helpfully strips off the header line from ps.
With an argument, qpid will search for processes or pids
matching the argument. With --exact it matches process names
exactly (as does pid, above).
Change-Id: I28a201df40281a66cb1a2918b7ee3a0b2d7b6ffd
diff --git a/envsetup.sh b/envsetup.sh
index 6ed3863..c2f13b3 100644
--- a/envsetup.sh
+++ b/envsetup.sh
@@ -774,15 +774,50 @@
echo "can't find Android.mk"
}
+# simplified version of ps; output in the form
+# <pid> <procname>
+function qpid() {
+ local prepend=''
+ local append=''
+ if [ "$1" = "--exact" ]; then
+ prepend=' '
+ append='$'
+ shift
+ elif [ "$1" = "--help" -o "$1" = "-h" ]; then
+ echo "usage: qpid [[--exact] <process name|pid>"
+ return 255
+ fi
+
+ local EXE="$1"
+ if [ "$EXE" ] ; then
+ qpid | grep "$prepend$EXE$append"
+ else
+ adb shell ps \
+ | tr -d '\r' \
+ | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
+ fi
+}
+
function pid()
{
- local EXE="$1"
- if [ "$EXE" ] ; then
- local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
- echo "$PID"
- else
- echo "usage: pid name"
- fi
+ local prepend=''
+ local append=''
+ if [ "$1" = "--exact" ]; then
+ prepend=' '
+ append='$'
+ shift
+ fi
+ local EXE="$1"
+ if [ "$EXE" ] ; then
+ local PID=`adb shell ps \
+ | tr -d '\r' \
+ | grep "$prepend$EXE$append" \
+ | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
+ echo "$PID"
+ else
+ echo "usage: pid [--exact] <process name>"
+ return 255
+ fi
}
# systemstack - dump the current stack trace of all threads in the system process