ndk-gdb: Fix debugging of private services

This patch fixes ndk-gdb so it can properly debug an application
that has private services. These will appear in the 'ps' output
as <package-name>:<service-name>, and the way the extract-pid.awk
script worked, were matched against <package-name> incorrectly.

+ Make extrac-pid.awk usable when custom ROM use a Busybox 'ps'
  instead of the standard Android toolbox version

+ Add unit test for extract-pid.awk

Change-Id: Ifb38d5bdfd5b648ff468cb3642db3bb64297707d
diff --git a/ndk-gdb b/ndk-gdb
index ec22f80..e6cf7af 100755
--- a/ndk-gdb
+++ b/ndk-gdb
@@ -340,6 +340,14 @@
     _adb_var_shell 1 $@
 }
 
+# Return the PID of a given package or program, or 0 if it doesn't run
+# $1: Package name ("com.example.hellojni") or program name ("/lib/gdbserver")
+# Out: PID number, or 0 if not running
+get_pid_of ()
+{
+    $ADB_CMD shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE="$1"
+}
+
 # Check the awk tool
 AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk
 AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk`
@@ -571,7 +579,7 @@
 fi
 
 # Find the PID of the application being run
-PID=`$ADB_CMD shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE=$PACKAGE_NAME`
+PID=$(get_pid_of "$PACKAGE_NAME")
 log "Found running PID: $PID"
 if [ $? != 0 -o "$PID" = "0" ] ; then
     echo "ERROR: Could not extract PID of application on device/emulator."
@@ -590,17 +598,14 @@
 fi
 
 # Check that there is no other instance of gdbserver running
-GDBSERVER_PS=`$ADB_CMD shell ps | grep lib/gdbserver`
-if [ -n "$GDBSERVER_PS" ] ; then
+GDBSERVER_PID=$(get_pid_of lib/gdbserver)
+if [ "$GDBSERVER_PID" != "0" ]; then
     if [ "$OPTION_FORCE" = "no" ] ; then
         echo "ERROR: Another debug session running, Use --force to kill it."
         exit 1
     fi
     log "Killing existing debugging session"
-    GDBSERVER_PID=`echo $GDBSERVER_PS | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE=lib/gdbserver`
-    if [ $GDBSERVER_PID != 0 ] ; then
-        run $ADB_CMD shell kill -9 $GDBSERVER_PID
-    fi
+    run $ADB_CMD shell kill -9 $GDBSERVER_PID
 fi
 
 # Launch gdbserver now