您的位置:首页 > 移动开发 > Android开发

Android使用ndk-gdb调试jni程序

2012-06-02 14:22 477 查看
之前在研究gdb调试的时候遇到些莫名其妙的问题,由于搞不懂真正的原因所在,隧归咎于‘人品’问题

,其实任何表象都是有原因的。

青铜鞋们仔细查看我在ubuntu 12.04 和 ubuntu 10.04 上打印的log有什么不同?你们对比一下adb的版本,在12.04上是6.6,而在10.04上是7.3.1!

gdb调试是一定要gdbclient和gdbserver的版本匹配才行,否则很容易出现问题。

请阅读我另一篇关于
gdb调试 的文章

******************************************************旧内容*******************************************************

使用 gdb 调试程序很考人品,同样的方法换个环境可能会出现莫名奇妙的错误,我曾经尝试调试hello-jni例子,环境配置如下:
操作系统: ubuntu 12.04
NDK version: ndkr8
ANDROID version:android 4.1 emulator
始终出现找不到符号的错误,如下:

[2012/08/26]
使用ndk-gdb调试hello-jni,ndk版本为ndkr8,android版本为4.1 JellyBean,始终找不到符号!

$ndk-gdb --start --verbose

Android NDK installation path: /home/wangxin/AndroidLibs/android-ndk-r8

Using default adb command: /home/wangxin/AndroidLibs/android-sdk-linux/platform-tools/adb

ADB version found: Android Debug Bridge version 1.0.29

Using ADB flags:

Using auto-detected project path: .

Found package name: com.example.hellojni

ABIs targetted by application: armeabi

Device API Level: 16

Device CPU ABIs: armeabi-v7a armeabi

Compatible device ABI: armeabi

Using gdb setup init: ./libs/armeabi/gdb.setup

Using toolchain prefix: /home/wangxin/AndroidLibs/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-

Using app out directory: ./obj/local/armeabi

Found debuggable flag: true

Found device gdbserver: /data/data/com.example.hellojni/lib/gdbserver

Found data directory: '/data/data/com.example.hellojni'

Found first launchable activity: .HelloJni

Launching activity: com.example.hellojni/.HelloJni

## COMMAND: /home/wangxin/AndroidLibs/android-sdk-linux/platform-tools/adb shell am start -n com.example.hellojni/.HelloJni

Starting: Intent { cmp=com.example.hellojni/.HelloJni }

## COMMAND: /home/wangxin/AndroidLibs/android-sdk-linux/platform-tools/adb shell sleep 2

Found running PID: 3886

Launched gdbserver succesfully.

Setup network redirection

## COMMAND: /home/wangxin/AndroidLibs/android-sdk-linux/platform-tools/adb forward tcp:5039 localfilesystem:/data/data/com.example.hellojni/debug-socket

## COMMAND: /home/wangxin/AndroidLibs/android-sdk-linux/platform-tools/adb shell run-as com.example.hellojni lib/gdbserver +debug-socket --attach 3886

## COMMAND: /home/wangxin/AndroidLibs/android-sdk-linux/platform-tools/adb pull /system/bin/app_process ./obj/local/armeabi/app_process

Attached; pid = 3886

Listening on sockaddr socket debug-socket

110 KB/s (9572 bytes in 0.084s)

Pulled app_process from device/emulator.

## COMMAND: /home/wangxin/AndroidLibs/android-sdk-linux/platform-tools/adb pull /system/lib/libc.so ./obj/local/armeabi/libc.so

2005 KB/s (286496 bytes in 0.139s)

Pulled libc.so from device/emulator.

GNU gdb 6.6

Copyright (C) 2006 Free Software Foundation, Inc.

GDB is free software, covered by the GNU General Public License, and you are

welcome to change it and/or distribute copies of it under certain conditions.

Type "show copying" to see the conditions.

There is absolutely no warranty for GDB. Type "show warranty" for details.

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

(no debugging symbols found)

warning: Unable to find dynamic linker breakpoint function.

GDB will be unable to debug shared library initializers

and track explicitly loaded dynamic code.

warning: shared library handler failed to enable breakpoint

0x40033a40 in ?? ()

gdb: Unable to get location for thread creation breakpoint: requested event is not supported

(gdb) b 100

No symbol table is loaded. Use the "file" command.

(gdb)

调试环境变为:
ubuntu 10.04(vmware station), ndkr8 ,android 4.1
hello-jni可以正常调试:
[2012/08/27] 调试hello-jni成功
ckt@ckt-desktop:~/AndroidLibs/android-ndk-r8b/samples/hello-jni$ ndk-gdb --verbose

Android NDK installation path: /home/ckt/AndroidLibs/android-ndk-r8b

Using default adb command: /home/ckt/work/android-sdk-linux/platform-tools/adb

ADB version found: Android Debug Bridge version 1.0.29

Using ADB flags:

Using auto-detected project path: .

Found package name: com.example.hellojni

ABIs targetted by application: armeabi

Device API Level: 16

Device CPU ABIs: armeabi-v7a armeabi

Compatible device ABI: armeabi

Using gdb setup init: ./libs/armeabi/gdb.setup

Using toolchain prefix: /home/ckt/AndroidLibs/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-

Using app out directory: ./obj/local/armeabi

Found debuggable flag: true

Found device gdbserver: /data/data/com.example.hellojni/lib/gdbserver

Found data directory: '/data/data/com.example.hellojni'

Found running PID: 620

Launched gdbserver succesfully.

Setup network redirection

## COMMAND: adb_cmd shell run-as com.example.hellojni lib/gdbserver +debug-socket --attach 620

## COMMAND: adb_cmd forward tcp:5039 localfilesystem:/data/data/com.example.hellojni/debug-socket

## COMMAND: adb_cmd pull /system/bin/app_process ./obj/local/armeabi/app_process

Attached; pid = 620

Listening on Unix socket debug-socket

119 KB/s (9572 bytes in 0.078s)

Pulled app_process from device/emulator.

## COMMAND: adb_cmd pull /system/bin/linker ./obj/local/armeabi/linker

975 KB/s (79972 bytes in 0.080s)

Pulled linker from device/emulator.

## COMMAND: adb_cmd pull /system/lib/libc.so ./obj/local/armeabi/libc.so

1704 KB/s (286496 bytes in 0.164s)

Pulled libc.so from device/emulator.

GNU gdb (GDB) 7.3.1-gg2

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-linux-android".

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>.

Remote debugging from host 0.0.0.0

warning: while parsing target library list (at line 2): No segment defined for /system/bin/linker

warning: .dynamic section for "/home/ckt/AndroidLibs/android-ndk-r8b/samples/hello-jni/obj/local/armeabi/linker" is not at the expected address (wrong library or version mismatch?)

warning: Could not load shared library symbols for 70 libraries, e.g. libstdc++.so.

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

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

libthread_db:td_ta_new: Probing system for platform bug.

libthread_db:_get_task_permitted_caps: Found CapPerm of 0 in /proc/662/task/662/status

libthread_db:_get_task_permitted_caps: Found CapPerm of 0 in /proc/620/task/620/status

libthread_db:_get_task_permitted_caps: Found CapPerm of 0 in /proc/620/task/622/status

libthread_db:_get_task_permitted_caps: Found CapPerm of 0 in /proc/620/task/626/status

libthread_db:_get_task_permitted_caps: Found CapPerm of 0 in /proc/620/task/627/status

libthread_db:_get_task_permitted_caps: Found CapPerm of 0 in /proc/620/task/628/status

libthread_db:_get_task_permitted_caps: Found CapPerm of 0 in /proc/620/task/629/status

libthread_db:_get_task_permitted_caps: Found CapPerm of 0 in /proc/620/task/630/status

libthread_db:_get_task_permitted_caps: Found CapPerm of 0 in /proc/620/task/631/status

libthread_db:_get_task_permitted_caps: Found CapPerm of 0 in /proc/620/task/632/status

libthread_db:_get_task_permitted_caps: Found CapPerm of 0 in /proc/620/task/633/status

libthread_db:td_ta_new: Victory: We can debug theads!

warning: Unable to find dynamic linker breakpoint function.

GDB will be unable to debug shared library initializers

and track explicitly loaded dynamic code.

0x40033a40 in epoll_wait () from /home/ckt/AndroidLibs/android-ndk-r8b/samples/hello-jni/obj/local/armeabi/libc.so

(gdb) l

1 /*

2 * Copyright (C) 2009 The Android Open Source Project

3 *

4 * Licensed under the Apache License, Version 2.0 (the "License");

5 * you may not use this file except in compliance with the License.

6 * You may obtain a copy of the License at

7 *

8 * http://www.apache.org/licenses/LICENSE-2.0
9 *

10 * Unless required by applicable law or agreed to in writing, software

(gdb)

11 * distributed under the License is distributed on an "AS IS" BASIS,

12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

13 * See the License for the specific language governing permissions and

14 * limitations under the License.

15 *

16 */

17 #include <string.h>

18 #include <jni.h>

19

20 /* This is a trivial JNI example where we use a native method

(gdb)

21 * to return a new VM String. See the corresponding Java source

22 * file located at:

23 *

24 * apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java

25 */

26 jstring

27 Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,

28 jobject thiz )

29 {

30 return (*env)->NewStringUTF(env, "Hello from JNI !");

(gdb)

31 }

(gdb)

Line number 32 out of range; jni/hello-jni.c has 31 lines.

(gdb)

Line number 32 out of range; jni/hello-jni.c has 31 lines.

(gdb) lib 30

Undefined command: "lib". Try "help".

(gdb) b 30

Breakpoint 1 at 0x4a274c2c: file jni/hello-jni.c, line 30.

(gdb) c

Continuing.

Breakpoint 1, Java_com_example_hellojni_HelloJni_stringFromJNI (env=0x2a008470, thiz=0x411fabd8) at jni/hello-jni.c:30

30 return (*env)->NewStringUTF(env, "Hello from JNI !");

(gdb)

【在cygwin环境下】win7 + cygwin

首先,在cygwin命令行下进入到你工程的根目录下
如果你要调试的程序已经在运行了,直接运行ndk-gdb
如果你要调试的程序没有运行,可以使用ndk-gdb --start


键入ndk-gdb命令,会出现一系列库文件加载的信息,这些库可能会加载失败,不过没有关系
出现<gdb>提示符,输入help命令可以查看一系列命令

使用l命令列出c文件的内容,但是遇到 No symbol table is loaded. Use the "file" command.

使用以下命令加载需要调试的so文件:

file obj/local/armeabi/libsearch-algorithm.so

使用list或者l可以查看加载的c文件。

使用b 100,在c文件的第100行设置断点。
使用c或者continue命令继续运行程序。
出现错误:
Continuing.
Warning:
Cannot insert breakpoint 1.
Error accessing memory address 0x11a0: Input/output error.
错误原因:使用ndk-gdb --start 启动程序第一个activity,但是此时so文件并没有被加载。解决方法:首先打开程序并使用,在保证so文件已经被使用的情况下,调用ndk-gdb命令调试程序进程。正确流程:1.创建一个模拟器或者使用真机运行想要调试的程序,确保so文件已经被加载。2.在此程序的根目录下调用ndk-gdb,出现gdb提示符。3.使用l命令,应该可以打印出c文件。4.使用b
10,可以在c文件的第十行打印断点。5.使用c命令恢复程序运行。6.更多操作请参考gdb使用手册。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: