From 20c136a15100cd54e4fbd924360babf67fb79f91 Mon Sep 17 00:00:00 2001
From: Chris Craik <ccraik@google.com>
Date: Fri, 9 Nov 2012 18:02:19 -0800
Subject: [PATCH] Improve gdbclient pid handling

Improves attaching to multi-process applications, connecting to the root process
if child process not specified instead of passing bad data to gdbserver

Also logs error instead of passing bad data to gdbserver if pid undetermined

Change-Id: I68ad62645c4f0a7a24aef02c84e3b5b84e14461e
---
 envsetup.sh | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/envsetup.sh b/envsetup.sh
index c758fccb5..47dab37f8 100644
--- a/envsetup.sh
+++ b/envsetup.sh
@@ -776,13 +776,24 @@ function gdbclient()
            PORT=":5039"
        fi
 
-       local PID
-       local PROG="$3"
-       if [ "$PROG" ] ; then
-           if [[ "$PROG" =~ ^[0-9]+$ ]] ; then
-               PID="$3"
-           else
+       local PID="$3"
+       if [ "$PID" ] ; then
+           if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
                PID=`pid $3`
+               if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
+                   # that likely didn't work because of returning multiple processes
+                   # try again, filtering by root processes (don't contain colon)
+                   PID=`adb shell ps | grep $3 | grep -v ":" | awk '{print $2}'`
+                   if [[ ! "$PID" =~ ^[0-9]+$ ]]
+                   then
+                       echo "Couldn't resolve '$3' to single PID"
+                       return 1
+                   else
+                       echo ""
+                       echo "WARNING: multiple processes matching '$3' observed, using root process"
+                       echo ""
+                   fi
+               fi
            fi
            adb forward "tcp$PORT" "tcp$PORT"
            adb shell gdbserver $PORT --attach $PID &
@@ -792,7 +803,7 @@ function gdbclient()
                echo "If you haven't done so already, do this first on the device:"
                echo "    gdbserver $PORT /system/bin/$EXE"
                    echo " or"
-               echo "    gdbserver $PORT --attach $PID"
+               echo "    gdbserver $PORT --attach <PID>"
                echo ""
        fi
 
-- 
GitLab