`
yanlijun250
  • 浏览: 751046 次
文章分类
社区版块
存档分类
最新评论

自己编译Android gdbserver(解决运行 gdbserver时 Segmentation fault 问题)

 
阅读更多

一、引言

使用Android 预装 gdbserver 调试 动态库时出现,Segmentation fault错误。不得不使用现有编译链重新编译一份gdbserver。多谢Android - How-to Rebuild gdbserver这篇文章,参考这篇文章我的解决过程如下。

这里要强调一点:下面编译sysroot时sysroot的目标路径,和编译gdbserver时指定的sysroot路径,一定要使用[绝对路径]。我编译过程中,曾因未使用绝对路径浪费不少精力。


二、开发环境

Ubuntu 11.04

编译通过的Android 2.3 源码


三、解决过程

1、因已经有了Android 2.3源码,省去下载源码并编译的漫长过程。

#设置Android源码目录为环境变量:$ANDROID_SRC

simba@simba-Vostro-3400:~$ export ANDROID_SRC=/home/simba/neptune/android_2.3


2、下载gdb源码,并打补丁

simba@simba-Vostro-3400:~$ mkdir gdb_build

simba@simba-Vostro-3400:~/neptune/gdb_src$ git clone https://android.googlesource.com/toolchain/gdb.git

#坐等下载完毕,然后...

#将以下保存成.patch文件


#打补丁

simba@simba-Vostro-3400:~/gdb_build$ cd gdb/

simba@simba-Vostro-3400:~/gdb_build/gdb$ patch -p1 <./gdb_patch.patch


3、下载build源码

simba@simba-Vostro-3400:~/gdb_build$ git clone https://android.googlesource.com/toolchain/build.git

#坐等下载完毕,然后...

#编译 sysroot
simba@simba-Vostro-3400:~/neptune/build_src$ cd build/
simba@simba-Vostro-3400:~/gdb_build/build$ ./build-sysroot.sh $ANDROID_SRC/out/target/product/imx51_bbg/$HOME/gdb_build/sysroot

#注意此处sysroot的目标目录一定要使用绝对路径,否则sysroot不能正常生成,之后编译gdbserver时会报错。


4.修改编译脚本

#备份并修改build-gdbserver.sh

simba@simba-Vostro-3400:~/gdb_build/build$ cd ..
simba@simba-Vostro-3400:~/gdb_build$cp $ANDROID_SRC/ndk/build/tools/build-gdbserver.sh $ANDROID_SRC/ndk/build/tools/build-gdbserver.sh_back

simba@simba-Vostro-3400:~/gdb_build$gedit$ANDROID_SRC/ndk/build/tools/build-gdbserver.sh


#以下红字部分为添加内容,目的是注释掉部分代码

<<NOT_NEEDED
# Remove libthread_db to ensure we use exactly the one we want.
rm -f $BUILD_SYSROOT/usr/lib/libthread_db*
rm -f $BUILD_SYSROOT/usr/include/thread_db.h

if [ "$NOTHREADS" != "yes" ] ; then
# We're going to rebuild libthread_db.o from its source
# that is under sources/android/libthread_db and place its header
# and object file into the build sysroot.
LIBTHREAD_DB_DIR=$ANDROID_NDK_ROOT/sources/android/libthread_db/gdb-$GDB_VERSION
if [ ! -d "$LIBTHREAD_DB_DIR" ] ; then
dump "ERROR: Missing directory: $LIBTHREAD_DB_DIR"
exit 1
fi
# Small trick, to avoid calling ar, we store the single object file
# with an .a suffix. The linker will handle that seamlessly.
run cp $LIBTHREAD_DB_DIR/thread_db.h $BUILD_SYSROOT/usr/include/
run $TOOLCHAIN_PREFIX-gcc --sysroot=$BUILD_SYSROOT -o $BUILD_SYSROOT/usr/lib/libthread_db.a -c $LIBTHREAD_DB_DIR/libthread_db.c
if [ $? != 0 ] ; then
dump "ERROR: Could not compile libthread_db.c!"
exit 1
fi
fi
NOT_NEEDED


#备份并修改prebuilt-common.sh
simba@simba-Vostro-3400:~/gdb_build$ cp $ANDROID_SRC/ndk/build/tools/prebuilt-common.sh $ANDROID_SRC/ndk/build/tools/prebuilt-common.sh_back
simba@simba-Vostro-3400:~/gdb_build$ gedit $ANDROID_SRC/ndk/build/tools/prebuilt-common.sh


get_toolchain_install ()
{
echo "$1/toolchains/$TOOLCHAIN/prebuilt/$HOST_TAG"
}

修改为:

get_toolchain_install ()
{
echo "$1/prebuilt/$HOST_TAG/toolchain/$TOOLCHAIN"
}

5.编译gdbserver
simba@simba-Vostro-3400:~/gdb_build$ $ANDROID_SRC/ndk/build/tools/build-gdbserver.sh $HOME/gdb_build $ANDROID_SRC arm-eabi-4.4.3 --verbose --build-out=$HOME/gdb_build/install--gdb-version=7.1.x --sysroot=$HOME/gdb_build/sysroot
#注意此处一定要是用绝对路径指定sysroot的目录

6.目标文件位于~/gdb_build/install目录下

7.参考文章
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics