14
Android & Android & GDB GDB

Android & GDB

Embed Size (px)

DESCRIPTION

Android & GDB. What is GDB?. GDB the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed. - PowerPoint PPT Presentation

Citation preview

Page 1: Android & GDB

Android &Android & GDBGDBAndroid &Android & GDBGDB

Page 2: Android & GDB

What is GDB?What is GDB?

GDBGDB

the GNU Project debugger, allows you to see what is going on `inside' another the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment program while it executes -- or what another program was doing at the moment it crashed.it crashed.

GDB can do four main kinds of things (plus other things in support of these) to help

you catch bugs in the act: Start your program, specifying anything that might affect its behavior.

Make your program stop on specified conditions.

Examine what has happened, when your program has stopped.

Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.

The program being debugged can be written in Ada, C, C++, Objective-C, Pascal (and many other languages). Those programs might be executing on the same machine as GDB (native) or on another machine (remote). GDB can run on most popular UNIX and Microsoft Windows variants.

Page 3: Android & GDB

主要内容主要内容

GDBGDB (( gdbservergdbserver )工作原理)工作原理

Android Android 与 与 GdbGdb

Elastos3.1Elastos3.1 (架构在(架构在 androidandroid 上的) 与上的) 与 Gdb Gdb

Page 4: Android & GDB

GDBGDB 工作原理之组织架构工作原理之组织架构

调试机目标机

gdbgdbserver

ptrace系统调用 ptrace系统调用

目标程序 目标程序

mi接口 cli接口

图形化调试界面

用户

远程调试 本地调试

Page 5: Android & GDB

GDBGDB 工作原理之与工作原理之与 gdbservergdbserver 关系关系

远程调试环境由宿主机远程调试环境由宿主机 GDBGDB 和目标机调试和目标机调试 stubstub 共同构成,两者通过串口或共同构成,两者通过串口或 TCPTCP 连连

接。使用 接。使用 GDBGDB 标准程串行协议协同工作,实现对目标机上的系统内核和上层应标准程串行协议协同工作,实现对目标机上的系统内核和上层应

用的监控和调试功能。调试用的监控和调试功能。调试 stubstub 是嵌入式系统中的一段代码,作为宿主机是嵌入式系统中的一段代码,作为宿主机 GDBGDB

和目标机调试程序间的一个媒介而存在。 就目前而言,嵌入式和目标机调试程序间的一个媒介而存在。 就目前而言,嵌入式 LinuxLinux 系统中,系统中,

主要有三种远程调试方法,分别适用于不同场合的调试工作:用主要有三种远程调试方法,分别适用于不同场合的调试工作:用 ROM MonitorROM Monitor

调试目标机程序、用调试目标机程序、用 KGDBKGDB 调试系统内核和用调试系统内核和用 gdbservergdbserver 调试用户空间程序。调试用户空间程序。

这三种调试方法的区别主要在于,目标机远程调试这三种调试方法的区别主要在于,目标机远程调试 stub stub 的存在形式的不同,而的存在形式的不同,而

其设计思路和实现方法则是大致相同的。 而我们最常用的是调试应用程序。就其设计思路和实现方法则是大致相同的。 而我们最常用的是调试应用程序。就

是采用是采用 gdb+gdbservergdb+gdbserver 的方式进行调试。在很多情况下,用户需要对一个应用的方式进行调试。在很多情况下,用户需要对一个应用

程序进行反复调试,特别是复杂的程序。采用程序进行反复调试,特别是复杂的程序。采用 GDBGDB 方法调试,由于嵌入式系统方法调试,由于嵌入式系统

资源有限性,一般不能直接在目标系统上进行调试,通常采用资源有限性,一般不能直接在目标系统上进行调试,通常采用 gdb+gdbservergdb+gdbserver

的方式进行调试。的方式进行调试。

Page 6: Android & GDB

GDBGDB 工作原理之工作原理之 ptraceptrace 系统调用系统调用

ptrace 系统调用的原型 long ptrace(enum __ptrace_request request, pid_t pid, void

*addr, void *data);

request 决定 ptrace 做什么 ,

pid 是被跟踪进程的 ID,

data 存储从进程空间偏移量为 addr 的地方开始将被读取 / 写入的数据 .

ptrace 系统调用的简要说明 ptrace() 是一个系统调用 , 它允许一个进程控制另外一个进程的执行 . 不

仅如此 , 我们还可以借助于 ptrace 修改某个进程的空间 ( 内存或寄存器 ), 任何传递给一个进程 ( 即被跟踪进程 ) 的信号 ( 除了会直接杀死进程的 SIGKILL 信号 ) 都会使得这个进程进入暂停状态 , 这时系统通过wait() 通知跟踪进程 , 这样 , 跟踪进程就可以修改被跟踪进程的行为了 .

Page 7: Android & GDB

Android Android 与 与 GdbGdb 之之 ndk-buildndk-build (一)(一)

Android NDK r4Android NDK r4 引入一个新的小型引入一个新的小型 shellshell 脚本,名字是脚本,名字是 ndk-buildndk-build ,以简化机器码的构,以简化机器码的构

建建

脚本位于 NDK 的顶级目录,并且将在你的应用程序工程目录,或它的任意子目录中的

命令行调用

参数选项:

ndk-build 重新构建所需的机器代码

ndk-build clean 清除所有生成的二进制文件

ndk-build NDK_DEBUG=1/0 生产 / 不生产 可调试的本地代码

ndk-build V=1 启动构建,显示构建命令

ndk-build –B 强制完全重新构建

Page 8: Android & GDB

Android Android 与 与 GdbGdb 之之 ndk-buildndk-build (二)(二)

编译输出目录:编译输出目录:

LibsLibs :(:( stripstrip 之后的)之后的)

gdb.setup gdbserver gdb.setup gdbserver (( NDK_DEBUG=1NDK_DEBUG=1 ))

soso 或可执行文件或可执行文件

ObjObj : (未: (未 stripstrip 之前的)之前的)

soso 或可执行文件或可执行文件

Page 9: Android & GDB

Android Android 与 与 GdbGdb 之之 gdbgdb 与与 gdbservergdbserver 调试流程调试流程

Gdb 和 gdbserver 联合调试

Gdb运行在客户端, gdbserver运行在目标开发板上

Gdbserver是要按照 android的一套工具和方法来编译的,否则编译出来在模拟器

上不能执行。 (gingerbread/ndk/build/tools/build-gdbserver.sh )

手动调试一段 c 代码可执行程序了解背后运作机制:

文件目录 X:\eclipse3.7.1\android-ndk-r6b\samples\helloworld

Page 10: Android & GDB

Android Android 与 与 GdbGdb 之之 ndk-gdb ndk-gdb 命令行调试命令行调试 nativenative 代码代码

从 android 2.2开始(同样适用于未来更高版本的 android系统),开发者可以

使用“ ndk-gdb”工具很方便地对 native的模块进行调试

这个脚本中干了很多事,最后去启了 gdb,中间包括了对模拟器的很多操作,例如端

口重定向,启动 gdbserver,获得应用程序的 pid,等等。主机这边干的事就

是傻瓜式的启动 ndk-gdb。

手动编译调试一个 android app java 代码调用一个本地函数

文件目录 x/eclipse3.7.1/android-ndk-r6b/samples/ndkfoo

Page 11: Android & GDB

Android Android 与 与 GdbGdb 之图形化调试之图形化调试 nativenative 代码代码

要先配置好要先配置好 eclipseeclipse 中关于中关于 SDK SDK 和 和 NDK NDK 的配置(两个的配置(两个 worldworld 文件)文件)

其中其中 cc 代码的编译关键在为项目配置一个代码的编译关键在为项目配置一个 C_BuilderC_Builder 。其实就是指定。其实就是指定

ndk_buildndk_build 路径路径

WindowsWindows 下搭建整个下搭建整个 AndroidAndroid 开发环境开发环境 (( 一一 )--SDK.doc)--SDK.doc

WindowsWindows 下搭建整个下搭建整个 AndroidAndroid 开发环境开发环境 (( 二二 )--NDK.doc)--NDK.doc

事例:用 eclipse联合调试 java和 native代码

Page 12: Android & GDB

ElastosElastos 与 与 GdbGdb 之之 CC 代码调试代码调试 ----- ServiceManager----- ServiceManager

 命令行调试  命令行调试 ServiceManagerServiceManager

与 helloworld 的调试基本没区别,就是多设置个 solib-search-

path

图形化调试 图形化调试 ServiceManagerServiceManager (( eclipseeclipse ))

Page 13: Android & GDB

ElastosElastos 与 与 GdbGdb 之之 CC 代码调试代码调试 ----- ButtonDemo----- ButtonDemo

 命令行调试  命令行调试 ButtonDemoButtonDemo

要在想跟进某个动态库的代码是要确保这个动态库要在想跟进某个动态库的代码是要确保这个动态库

已经被已经被 gdbgdb 加载,命令加载,命令 sharedshared (( sharedlibsharedlib )。)。

图形化调试 图形化调试 ButtonDemoButtonDemo (( eclipseeclipse ))

Page 14: Android & GDB

    

谢谢观赏谢谢观赏

制作:李辉制作:李辉

2011.11.17 2011.11.17