Android uses ndk-GDB to debug the JNI Program

Source: Internet
Author: User

I encountered some inexplicable problems when I was studying GDB debugging. Because I don't understand the real reasons, the question of "character" is the root cause. In fact, there is a reason for any appearance.

The bronze shoes carefully check what is the difference between the log I printed on Ubuntu 12.04 and Ubuntu 10.04? Compare the version of ADB with 12.04 and 7.3.1 on 6.6!

GDB debugging requires that the versions of gdbclient and gdbserver be matched. Otherwise, problems may occur.

Please read another article about
GDB debugging article

**************************************** ************************ *******************************

Using GDB to debug a program is a good character. Changing the environment in the same way may cause an inexplicable error. I tried to debug the hello-JNI example. The environment configuration is as follows: Operating System: ubuntu 12.04ndk version: ndkr8android version: Android 4.1 emulator always fails to find the symbol, as shown below:
[]
Use ndk-GDB to debug hello-JNI. The ndk version is ndkr8, And the Android version is 4.1 Jellybean. No symbols are found!

$ 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/COM. example. hellojni/lib/gdbserver

Found data directory: '/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/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.084 S)

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.139 S)

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)


The debugging environment is changed to Ubuntu 10.04 (VMware station), ndkr8, and Android 4.1hello-JNI for normal debugging: [2012/08/27] debugging hello-JNI success 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/COM. example. hellojni/lib/gdbserver
Found data directory: '/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/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.078 S)
Pulled app_process from device/emulator.
# Command: adb_cmd pull/system/bin/linker./obj/local/armeabi/linker
975 kb/s (79972 bytes in 0.080 S)
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.164 S)
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 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: cocould 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: vicloud: 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 x 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 T in compliance with the license.
6 * You may obtain a copy of the license
7 *
Http://www.apache.org/licenses/LICENSE-2.0 8 *
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:
23 *
24 * apps/samples/Hello-JNI/project/src/COM/example/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)

[In cygwin Environment] win7 + cygwin
First, go to the root directory of your project under the cygwin command line. If the program you want to debug is already running, run ndk-GDB directly. If the program you want to debug is not running, you can use ndk-GDB -- start

 

Type the ndk-GDB command, and a series of library file loading information will appear. These libraries may fail to be loaded, but it does not matter. <GDB> prompt, enter the HELP command to view a series of commands
Use the l command to list the content of the C file, but the no symbol table is loaded. Use the "file" command.

Run the following command to load the so file to be debugged:

File OBJ/local/armeabi/libsearch-algorithm.so
Use List or L to view the loaded C file.
Use B 100 to set a breakpoint in line 3 of the C file. Run the C or continue command to continue running the program. Error:

Continuing.

Warning: cannot insert breakpoint 1. error accessing memory address 0x11a0: input/output error. cause of error: Use ndk-GDB -- start to start the first activity of the program, but the so file is not loaded at this time. Solution: first open the program and use it. When the so file is used, call the ndk-GDB command to debug the program process. Correct Process: 1. Create a simulator or use a real machine to run the program to be debugged to ensure that the so file has been loaded. 2. Call ndk-GDB in the root directory of the program. a gdb prompt is displayed. 3. Use the l command to print the c file. 4. Use B
10. You can print the breakpoint on the tenth line of the C file. 5. Run the C command to restore the program. 6. For more operations, see the gdb user manual.

I encountered some inexplicable problems when I was studying GDB debugging. Because I don't understand the real reasons, the question of "character" is the root cause. In fact, there is a reason for any appearance.

The bronze shoes carefully check what is the difference between the log I printed on Ubuntu 12.04 and Ubuntu 10.04? Compare the version of ADB with 12.04 and 7.3.1 on 6.6!

GDB debugging requires that the versions of gdbclient and gdbserver be matched. Otherwise, problems may occur.

Please read another article about
GDB debugging article

**************************************** ************************ *******************************

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.