From dd7066901f67233c09f3b0409a57db7686c7ea5b Mon Sep 17 00:00:00 2001 From: Po Lu Date: Wed, 15 Feb 2023 13:39:47 +0800 Subject: [PATCH] Make debug.sh detect adb running as root * java/debug.sh: Run gdbserver directly if possible. --- java/debug.sh | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/java/debug.sh b/java/debug.sh index 2e95f9738c7..cbef7518984 100755 --- a/java/debug.sh +++ b/java/debug.sh @@ -263,6 +263,8 @@ if [ -n "$jdb_command" ]; then fi # See if gdbserver has to be uploaded +gdbserver_cmd= +is_root= if [ -z "$gdbserver" ]; then gdbserver_bin=/system/bin/gdbserver else @@ -272,9 +274,26 @@ else # Upload the specified gdbserver binary to the device. adb -s $device push "$gdbserver" "$gdbserver_bin" - # Copy it to the user directory. - adb -s $device shell "$gdbserver_cat" - adb -s $device shell "run-as $package chmod +x gdbserver" + + if (adb -s $device pull /system/bin/tee /dev/null &> /dev/null); then + # Copy it to the user directory. + adb -s $device shell "$gdbserver_cat" + adb -s $device shell "run-as $package chmod +x gdbserver" + gdbserver_cmd="./gdbserver" + else + # Hopefully this is an old version of Android which allows + # execution from /data/local/tmp. Its `chmod' doesn't support + # `+x' either. + adb -s $device shell "chmod 777 $gdbserver_bin" + gdbserver_cmd="$gdbserver_bin" + + # If the user is root, then there is no need to open any kind + # of TCP socket. + if (adb -s $device shell id | grep -G root); then + gdbserver= + is_root=yes + fi + fi fi # Now start gdbserver on the device asynchronously. @@ -284,13 +303,19 @@ exec 5<> /tmp/file-descriptor-stamp rm -f /tmp/file-descriptor-stamp if [ -z "$gdbserver" ]; then - adb -s $device shell run-as $package $gdbserver_bin --once \ - "+debug.$package.socket" --attach $pid >&5 & - gdb_socket="localfilesystem:$app_data_dir/debug.$package.socket" + if [ "$is_root" = "yes" ]; then + adb -s $device shell $gdbserver_bin --once \ + "+/data/local/tmp/debug.$package.socket" --attach $pid >&5 & + gdb_socket="localfilesystem:/data/local/tmp/debug.$package.socket" + else + adb -s $device shell run-as $package $gdbserver_bin --once \ + "+debug.$package.socket" --attach $pid >&5 & + gdb_socket="localfilesystem:$app_data_dir/debug.$package.socket" + fi else # Normally the program cannot access $gdbserver_bin when it is # placed in /data/local/tmp. - adb -s $device shell run-as $package "./gdbserver" --once \ + adb -s $device shell run-as $package $gdbserver_cmd --once \ "0.0.0.0:7654" --attach $pid >&5 & gdb_socket="tcp:7654" fi -- 2.39.5