Saturday, March 14, 2009

3. Debug native process via ssh tunnel on Windows

By leveraging SSH tunnel, we can setup a gdb session from remote linux workstation to the connected device on Windows following below steps:

1. Config a Putty session with SSH tunnel enabled, adding a forwarded port from remote <port> -> <device ip>:<port> (e.g. R5039->192.168.16.2:5139)















2. Connect to the device on Windows via adb shell, then start gdb server to attach the specified native process. 
    $gdbserver <port> --attach <pid> #(e.g. $gdbserver :5139 --attach 65)

3. Connect to remote linux workstation with the session defined in step 1
    $cd ~/repo/android_root
    $./mygdb.sh <executable> <port>

Below is the source code of mygdb.sh:


#!/bin/sh
#This script is used to setup gdb to debug native processes on the device

if [ ! -f .repo/manifest.xml ]; then
echo "Please run gdb under android repo root directory!"
exit 1;
fi

rootdir=`pwd`
product="generic"
scriptdir=`dirname $0`
if [ $1 = "-p" ]; then
shift
product=$1
shift
elif [ -f $rootdir/product.cfg ]; then
product=`cat $rootdir/product.cfg`
elif [ -f $scriptdir/product.cfg ]; then
product=`cat $scriptdir/product.cfg`
fi

gdbtool="./codesourcery/toolchain/bin/arm-none-linux-gnueabi-gdb"
#gdbtool="./prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gdb"
exename="$1"
if [ -z "$exename" ] ; then
exename="mediaserver"
fi
portnum="$2"
if [ -z "$portnum" ] ; then
portnum=":5039"
fi

OUT_UNSTIPPED_DIR=$rootdir/out/target/product/$product/symbols/
if [ ! -z "$OUT_DIR" ]; then
OUT_UNSTIPPED_DIR=$OUT_DIR/target/product/$product/symbols/
fi
OUT_SO_SYMBOLS=$OUT_UNSTIPPED_DIR/system/lib/
OUT_BIN_SYMBOLS=$OUT_UNSTIPPED_DIR/system/bin/

echo >"$rootdir/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
echo >>"$rootdir/gdbclient.cmds" "target remote $portnum"
echo >>"$rootdir/gdbclient.cmds" "set auto-solib-add on"
echo >>"$rootdir/gdbclient.cmds" "set sysroot $OUT_UNSTIPPED_DIR"
#echo >>"$rootdir/gdbclient.cmds" "set solib-absolute-prefix $OUT_UNSTIPPED_DIR"
echo >>"$rootdir/gdbclient.cmds" ""

$gdbtool -x "$rootdir/gdbclient.cmds" "$OUT_BIN_SYMBOLS/$exename"

[1]SSH tunnel explained
[2]Tunneling protocol
[3]gdbclient in envsetup.sh

No comments: