Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8f1965ed authored by Dan Albert's avatar Dan Albert Committed by Gerrit Code Review
Browse files

Merge "Remove gdbclient from envsetup."

parents bdfec89d f9631fd9
Loading
Loading
Loading
Loading
+10 −297
Original line number Diff line number Diff line
# These unsets should stay to clear the old functions from any long running
# shells.
# TODO(danalbert): Remove this in a day or so.
unset -f gdbwrapper
unset -f get_symbols_directory
unset -f adb_get_product_device
unset -f adb_get_traced_by
unset -f gdbclient
unset -f gdbclient_old

function hmm() {
cat <<EOF
Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
@@ -947,18 +957,6 @@ function stacks()
    fi
}

function gdbwrapper()
{
    local GDB_CMD="$1"
    shift 1
    $GDB_CMD -x "$@"
}

function get_symbols_directory()
{
    echo $(get_abs_build_var TARGET_OUT_UNSTRIPPED)
}

# Read the ELF header from /proc/$PID/exe to determine if the process is
# 64-bit.
function is64bit()
@@ -975,291 +973,6 @@ function is64bit()
    fi
}

function adb_get_product_device() {
  local candidate=`adb shell getprop ro.product.device | sed s/.$//`
  if [ -z $candidate ]; then
    candidate=`adb shell getprop ro.hardware | sed s/.$//`
  fi
  echo $candidate
}

# returns 0 when process is not traced
function adb_get_traced_by() {
  echo `adb shell cat /proc/$1/status | grep -e "^TracerPid:" | sed "s/^TracerPid:\t//" | sed s/.$//`
}

function gdbclient() {
  # TODO:
  # 1. Check for ANDROID_SERIAL/multiple devices
  local PROCESS_NAME="n/a"
  local PID=$1
  local PORT=5039
  if [ -z "$PID" ]; then
    echo "Usage: gdbclient <pid|processname> [port number]"
    return -1
  fi
  local DEVICE=$(adb_get_product_device)

  if [ -z "$DEVICE" ]; then
    echo "Error: Unable to get device name. Please check if device is connected and ANDROID_SERIAL is set."
    return -2
  fi

  if [ -n "$2" ]; then
    PORT=$2
  fi

  local ROOT=$(gettop)
  if [ -z "$ROOT" ]; then
    # This is for the situation with downloaded symbols (from the build server)
    # we check if they are available.
    ROOT=`realpath .`
  fi

  local OUT_ROOT="$ROOT/out/target/product/$DEVICE"
  local SYMBOLS_DIR="$OUT_ROOT/symbols"
  local IS_TAPAS_USER="$(get_build_var TARGET_BUILD_APPS)"
  local TAPAS_SYMBOLS_DIR=

  if [ $IS_TAPAS_USER ]; then
    TAPAS_SYMBOLS_DIR=$(get_symbols_directory)
  fi

  if [ ! -d $SYMBOLS_DIR ]; then
    if [ $IS_TAPAS_USER ]; then
      mkdir -p $SYMBOLS_DIR/system/bin
    else
      echo "Error: couldn't find symbols: $SYMBOLS_DIR does not exist or is not a directory."
      return -3
    fi
  fi

  # let's figure out which executable we are about to debug

  # check if user specified a name -> resolve to pid
  if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
    PROCESS_NAME=$PID
    PID=$(pid --exact $PROCESS_NAME)
    if [ -z "$PID" ]; then
      echo "Error: couldn't resolve pid by process name: $PROCESS_NAME"
      return -4
    else
      echo "Resolved pid for $PROCESS_NAME is $PID"
    fi
  fi

  local EXE=`adb shell readlink /proc/$PID/exe | sed s/.$//`

  if [ -z "$EXE" ]; then
    echo "Error: no such pid=$PID - is process still alive?"
    return -4
  fi

  local LOCAL_EXE_PATH=$SYMBOLS_DIR$EXE

  if [ ! -f $LOCAL_EXE_PATH ]; then
    if [ $IS_TAPAS_USER ]; then
      adb pull $EXE $LOCAL_EXE_PATH
    else
      echo "Error: unable to find symbols for executable $EXE: file $LOCAL_EXE_PATH does not exist"
      return -5
    fi
  fi

  local USE64BIT=""

  if [[ "$(file $LOCAL_EXE_PATH)" =~ 64-bit ]]; then
    USE64BIT="64"
  fi

  # and now linker for tapas users...
  if [ -n "$IS_TAPAS_USER" -a ! -f "$SYMBOLS_DIR/system/bin/linker$USE64BIT" ]; then
    adb pull /system/bin/linker$USE64BIT $SYMBOLS_DIR/system/bin/linker$USE64BIT
  fi

  local GDB=
  local GDB64=
  local CPU_ABI=`adb shell getprop ro.product.cpu.abilist | sed s/.$//`
  # TODO: we assume these are available via $PATH
  if [[ $CPU_ABI =~ (^|,)arm64 ]]; then
    GDB=arm-linux-androideabi-gdb
    GDB64=aarch64-linux-android-gdb
  elif [[ $CPU_ABI =~ (^|,)arm ]]; then
    GDB=arm-linux-androideabi-gdb
  elif [[ $CPU_ABI =~ (^|,)x86_64 ]]; then
    GDB=x86_64-linux-android-gdb
  elif [[ $CPU_ABI =~ (^|,)x86 ]]; then
    GDB=x86_64-linux-android-gdb
  elif [[ $CPU_ABI =~ (^|,)mips64 ]]; then
    GDB=mipsel-linux-android-gdb
    GDB64=mips64el-linux-android-gdb
  elif [[ $CPU_ABI =~ (^|,)mips ]]; then
    GDB=mipsel-linux-android-gdb
  else
    echo "Error: unrecognized cpu.abilist: $CPU_ABI"
    return -6
  fi

  # TODO: check if tracing process is gdbserver and not some random strace...
  if [ "$(adb_get_traced_by $PID)" -eq 0 ]; then
    # start gdbserver
    echo "Starting gdbserver..."
    # TODO: check if adb is already listening $PORT
    # to avoid unnecessary calls
    echo ". adb forward for port=$PORT..."
    adb forward tcp:$PORT tcp:$PORT
    echo ". starting gdbserver to attach to pid=$PID..."
    adb shell gdbserver$USE64BIT :$PORT --attach $PID &
    echo ". give it couple of seconds to start..."
    sleep 2
    echo ". done"
  else
    echo "It looks like gdbserver is already attached to $PID (process is traced), trying to connect to it using local port=$PORT"
    adb forward tcp:$PORT tcp:$PORT
  fi

  local OUT_SO_SYMBOLS=$SYMBOLS_DIR/system/lib$USE64BIT
  local TAPAS_OUT_SO_SYMBOLS=$TAPAS_SYMBOLS_DIR/system/lib$USE64BIT
  local OUT_VENDOR_SO_SYMBOLS=$SYMBOLS_DIR/vendor/lib$USE64BIT
  local ART_CMD=""

  local SOLIB_SYSROOT=$SYMBOLS_DIR
  local SOLIB_SEARCHPATH=$OUT_SO_SYMBOLS:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx:$OUT_VENDOR_SO_SYMBOLS:$OUT_VENDOR_SO_SYMBOLS/hw:$OUT_VENDOR_SO_SYMBOLS/egl

  if [ $IS_TAPAS_USER ]; then
    SOLIB_SYSROOT=$TAPAS_SYMBOLS_DIR:$SOLIB_SYSROOT
    SOLIB_SEARCHPATH=$TAPAS_OUT_SO_SYMBOLS:$SOLIB_SEARCHPATH
  fi

  echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $SOLIB_SYSROOT"
  echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $SOLIB_SEARCHPATH"
  local DALVIK_GDB_SCRIPT=$ROOT/development/scripts/gdb/dalvik.gdb
  if [ -f $DALVIK_GDB_SCRIPT ]; then
    echo >>"$OUT_ROOT/gdbclient.cmds" "source $DALVIK_GDB_SCRIPT"
    ART_CMD="art-on"
  else
    echo "Warning: couldn't find $DALVIK_GDB_SCRIPT - ART debugging options will not be available"
  fi
  echo >>"$OUT_ROOT/gdbclient.cmds" "target remote :$PORT"
  if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
    echo >> "$OUT_ROOT/gdbclient.cmds" $ART_CMD
  fi

  echo >>"$OUT_ROOT/gdbclient.cmds" ""

  local WHICH_GDB=$GDB

  if [ -n "$USE64BIT" -a -n "$GDB64" ]; then
    WHICH_GDB=$GDB64
  fi

  gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$LOCAL_EXE_PATH"
}

# gdbclient now determines whether the user wants to debug a 32-bit or 64-bit
# executable, set up the approriate gdbserver, then invokes the proper host
# gdb.
function gdbclient_old()
{
   local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
   local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
   local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
   local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
   local OUT_EXE_SYMBOLS=$(get_symbols_directory)
   local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
   local ARCH=$(get_build_var TARGET_ARCH)
   local GDB
   case "$ARCH" in
       arm) GDB=arm-linux-androideabi-gdb;;
       arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
       mips|mips64) GDB=mips64el-linux-android-gdb;;
       x86) GDB=x86_64-linux-android-gdb;;
       x86_64) GDB=x86_64-linux-android-gdb;;
       *) echo "Unknown arch $ARCH"; return 1;;
   esac

   if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
       local EXE="$1"
       if [ "$EXE" ] ; then
           EXE=$1
           if [[ $EXE =~ ^[^/].* ]] ; then
               EXE="system/bin/"$EXE
           fi
       else
           EXE="app_process"
       fi

       local PORT="$2"
       if [ "$PORT" ] ; then
           PORT=$2
       else
           PORT=":5039"
       fi

       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"
           local USE64BIT="$(is64bit $PID)"
           adb shell gdbserver$USE64BIT $PORT --attach $PID &
           sleep 2
       else
               echo ""
               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 ""
       fi

       OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
       OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT

       echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
       echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx:$OUT_VENDOR_SO_SYMBOLS:$OUT_VENDOR_SO_SYMBOLS/hw:$OUT_VENDOR_SO_SYMBOLS/egl"
       echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
       echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
       # Enable special debugging for ART processes.
       if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
          echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
       fi
       echo >>"$OUT_ROOT/gdbclient.cmds" ""

       local WHICH_GDB=
       # 64-bit exe found
       if [ "$USE64BIT" != "" ] ; then
           WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
       # 32-bit exe / 32-bit platform
       elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
           WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
       # 32-bit exe / 64-bit platform
       else
           WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
       fi

       gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
  else
       echo "Unable to determine build system output dir."
   fi

}

case `uname -s` in
    Darwin)
        function sgrep()