android下使用gdb 调试方法

来源:互联网 发布:好用的乳液推荐知乎 编辑:程序博客网 时间:2024/05/17 05:51

1)首先,生成debug版本的执行程序与被调试库文件。

设置 Application.mk中调试选项,添加:

APP_OPTIM  := debug

表示生成debug版本的so文件以及可执行测试程序。

2)执行命令:ndk-build -B NDK_DEBUG=1

其中 -B 为强制重新编译, NDK_DEBUG=1 为编译为调试版本。

具体ndk-build相关选项可以参考ndk提供的文档。

下面举例说明:

测试程序:main.cpp

#include <fstream>

#include <iostream>

#include <map>

#include <string>

using namespace std;

 

int main(int argc, char* argv[])

{

       printf("main enter \n");

 

  for(int i = 0;i<3;i++)

    {

   printf("i = %d\n",i);

   char * p=NULL;

   char src[]="1232222222";

   strcpy(p, src);

     }

  printf("main leave \n");

  return 0;

}

Android.mk文件:

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

#test

LOCAL_MODULE :=temptest

LOCAL_SRC_FILES := \

  main.cpp

 

LOCAL_C_INCLUDES += \

  $(LOCAL_PATH) \

  $(NDK)/sources/cxx-stl/gnu-libstdc++/include/4.4.3/backward

 

LOCAL_CFLAGS := \

  -DANDROID \

  -fexceptions \

  -frtti \

  -DUSE_ARES \

  -D__ACE_INLINE__ \

  -Wno-deprecated \

  -fno-strict-aliasing \

  -Wpointer-arith \

  -Wno-psabi \

  -W \

  -Wpointer-arith \

  -pipe \

  -D_REENTRANT \

  -D_GNU_SOURCE \

  -DACE_HAS_CUSTOM_EXPORT_MACROS=0

 

LOCAL_LDLIBS += -llog -ldl -lstdc++ 

include $(BUILD_EXECUTABLE)

Application.mk文件:

APP_PLATFORM     := android-14

APP_STL          := gnustl_shared

APP_ABI          := armeabi-v7a

APP_OPTIM        := debug

执行编译命令:

 

[root@bogon testCodes]# ndk-build -B NDK_DEBUG=1

Gdbserver      : [arm-linux-androideabi-4.4.3] libs/armeabi-v7a/gdbserver

Gdbsetup       : libs/armeabi-v7a/gdb.setup

Compile++ thumb  : temptest <= main.cpp

Prebuilt       : libcrystax.so <= <NDK>/sources/crystax/libs/armeabi-v7a/4.4.3/

Prebuilt       : libgnustl_shared.so <= <NDK>/sources/cxx-stl/gnu-libstdc++/libs/armeabi-v7a/4.4.3/

Executable     : temptest

Install        : temptest => libs/armeabi-v7a/temptest

Install        : libcrystax.so => libs/armeabi-v7a/libcrystax.so

Install        : libgnustl_shared.so => libs/armeabi-v7a/libgnustl_shared.so

[root@bogon testCodes]#

 

 

3)生成debug版可调式程序,注意

4)使用adb push命令复制初gdb.setup之外的其它程序到仿真机/data/data/xtest目录

其中xtest目录可以随便命名,为了区别其它测试,这里命名为xtest.为了简单这里直接使用了adb push命令,把整个文件夹内所有文件都push到了仿真机中。

[root@bogon libs]# ls

armeabi-v7a

[root@bogon libs]# adb push armeabi-v7a/ /data/data/xtest

push: armeabi-v7a/gdbserver -> /data/data/xtest/gdbserver

push: armeabi-v7a/gdb.setup -> /data/data/xtest/gdb.setup

push: armeabi-v7a/temptest -> /data/data/xtest/temptest

push: armeabi-v7a/libcrystax.so -> /data/data/xtest/libcrystax.so

push: armeabi-v7a/libgnustl_shared.so -> /data/data/xtest/libgnustl_shared.so

5 files pushed. 0 files skipped.

616 KB/s (1110818 bytes in 1.760s)

[root@bogon libs]#

 

5)使用adb 登录该目录。

adb shell /data/data/xtest

然后启动gdbserver:服务器端对被调试程序进行调试。

则在仿真机中进入调试监听状态。同时启动了服务器调试,等待接收客户端gdb程序命令状态。

6)在本地主机Linux环境下打开另一个终端,执行端口重定向,

# adb forward tcp:6011 tcp:7011

 

7)在本地主机Linux环境下启动gdb客户端,该工具位置在ndk目录:

#cd  /opt/android-ndk-r7-crystax-1/toolchains/arm-linux-androideabi-4.6.3/prebuilt/linux-x86/bin

启动命令:

[root@bogon bin]# arm-linux-androideabi-gdb /home/testCodes/obj/local/armeabi-v7a/temptest

arm-linux-androideabi-gdb: /usr/lib/libz.so.1: no version information available (required by arm-linux-androideabi-gdb)

GNU gdb (Linaro GDB) 7.3-2011.12

Copyright (C) 2011 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.  Type "show copying"

and "show warranty" for details.

This GDB was configured as "--host=x86_64-linux-gnu --target=arm-elf-linux".

For bug reporting instructions, please see:

<http://bugs.launchpad.net/gdb-linaro/>...

Reading symbols from /home/testCodes/obj/local/armeabi-v7a/temptest...done.

(gdb)

注意:这里通过gdb客户端程序arm-linux-androideabi-gdb 启动相应的带调试符号的程序,必须是obj/local/目录下的相应程序,而不能是libs/下的相应程序。

8)进入gdb后,对重定向后的端口进行监听:

target remote :6011

此时gdb会进入调试状态:

(gdb) target remote :6011

Remote debugging using :6011

warning: Unable to find dynamic linker breakpoint function.

GDB will be unable to debug shared library initializers

and track explicitly loaded dynamic code.

0xb0001000 in ?? ()

(gdb)

同时远程仿真机内的gdbserver也会收到重定向监听联调信息进入调试。

9)后续就可以像在linux下一样使用gdb命令进行调试了。如输入l,则加载文件。

输入b 15 设置断点。

Command name abbreviations are allowed if unambiguous.

(gdb) l

Cannot access memory at address 0x0

12        for(int i = 0;i<3;i++)

13          {

14         printf("i = %d\n",i);

15         char * p=NULL;

16         char src[]="1232222222";

17         strcpy(p, src);

18           }

19        printf("main leave \n");

20        return 0;

21      }

(gdb)  b 15

Breakpoint 1 at 0x8e92: file jni/../test/main.cpp, line 15.

(gdb) c

Continuing.

warning: Could not load shared library symbols for 7 libraries, e.g. /system/bin/linker.

Use the "info sharedlibrary" command to see the complete listing.

Do you need "set solib-search-path" or "set sysroot"?

 

Breakpoint 1, main (argc=1, argv=0xbeae9c64) at jni/../test/main.cpp:15

15         char * p=NULL;

(gdb)

 

10)如果调试中遇到错误,则通过c命令继续执行,会提示是否退出调试如下:

Continuing.

Cannot access memory at address 0x0

 

Program received signal SIGCONT, Continued.

0xb00010a8 in ?? ()

(gdb) c

Continuing.

Cannot access memory at address 0x0

 

Program received signal SIGSEGV, Segmentation fault.

0x40033038 in ?? ()

(gdb) q

A debugging session is active.

 

        Inferior 1 [Remote target] will be killed.

 

Quit anyway? (y or n) y

[root@bogon bin]#

仿真机shellgdbserver也会退出,如下图所示,如果需要继续调试,则需要重新启动监听。

 

11)如果带调试程序需要依赖动态库,则需要进入gdb后设置库的搜索路径,命令如下:

(gdb) set solib-search-path /home/testCodes/obj/local/armeabi-v7a/

这里,同样是带调试符号的库,即obj目录下的so所在目录。而不应该是libs目录

查看当前搜索路径命令:

(gdb) show solib-search-path

The search path for loading non-absolute shared library symbol files is /home/testCodes/obj/local/armeabi-v7a/.

(gdb)

从上面可以看出,搜索路径已经设置完成。

0 0
原创粉丝点击