Using NDK-GDB to debug the native program under as

Source: Internet
Author: User

1.Create Android Studio Project

(1) Create a new empty activity project: TESTNDK

(2) Add a reference to the. So in the main activity of the project. This step is critical, putting the system LoadLibrary into the main activity is to get the program to load immediately after it is started.

static {

Load Native Library to invoke Chreographercallback ().

System.loadlibrary ("Debugndk");

}

Public native String Stringfromjni ();

(3) Use Javah to generate the corresponding. h file

In the terminal window of Android studio, enter:

CD App\src\main\java #进入工程的app \src\main\java Directory

Javah stable.testndk.MainActivity #生成. h file, STABLE.TESTNDK is the package name, Mainactivity is the class name where the native function resides

The NDK can then be compiled, and the NDK will come back after compiling.

(4) Copy the files under the Libs directory compiled by the NDK into the App\libs directory, as follows:

App\libs\armeabi\

Gdb.setup

Gdbserver

Libdebugndk.so

App\libs\armeabi-v7a\

Gdb.setup

Gdbserver

Libdebugndk.so

App\libs\mips\

Gdb.setup

Gdbserver

Libdebugndk.so

App\libs\x86\

Gdb.setup

Gdbserver

Libdebugndk.so

(5) Modify the App\build.gradle, add the following code to the Android, to inform the Jnilibs directory

sourcesets {

Main () {

Jnilibs.srcdirs = [' Libs ']

}

}

(6) Rebuild project, you can create apk by Build->build apk, then use 7ZIP to look inside is to pack. So in.

Next go to the third step to debug the NDK

Compilation of 2.NDK Programs

(1) Create your own NDK program (DEBUGNDK) with Android-ndk-r10b\samples\hello-jni as a template.

Your own program needs to modify the Android.mk file, add engineering and code files, here to change all the Hello-jni to DEBUGNDK.

Modify the Jni external interface function name in DEBUGNDK.C, and modify it to use the corresponding function (JAVA_STABLE_TESTNDK_MAINACTIVITY_STRINGFROMJNI) in the. h Javah everywhere.

(2) Compile. So

Under Windows command line, enter the project directory (TESTNDK directory) and enter the following command:

Ndk-build ndk_debug=1 #注意事先要将ndk-build directory is set to the system environment variable

After that, the compilation will eventually generate Libs and obj two directories under the project directory, where the Libs directory is published to the hardware. So, obj is used for debugging, and later.

Okay, so you can go back to the 4th step of Create Android Studio Project.

3.NDK Commissioning

Server-side configuration

(1) Run the TESTNDK program on the phone

(2) Gdbserver pull generated when the NDK program is compiled to the phone (I put it in/DATA/DATA/STABLE.TESTNDK.

Gdbserver under App\libs\x86\, you can put it under the phone/data/data/stable.testndk/lib

Under Windows command line, execute:

ADB push. \app\libs\x86\gdbserver/data/data/stable.testndk/lib/

(3) Start gdbserver on the phone and attach the process you want to debug and specify the port on which to listen for debug commands (this port is the port on the phone)

Under Windows command line, execute:

ADB Shell #进入手机

Cd/data/data/stable.testndk/lib

PS | grep stable.testndk #查看要调试进程的PID

gdbserver:1818--attach 19906 #:1818 is the port number, 19906 is the process ID

At this point, the terminal terminal will display:

Attached; PID = 19906

Listening on port 1818

Desktop-side configuration

(1) using ADB to do port mapping, the port on the PC is directed to the Gdbserver listening port on the phone

ADB forward tcp:1818 tcp:1818 #端口映射, maps the 1818 port of the PC to the 1818 port of the phone

(2) Pull the app_process32 on the device to the desktop for GDB debugging.

ADB pull/system/bin/app_process32./

(3) using the GDB client under Android-ndk-r10b\toolchains to connect the type of gdbserver,gdb to select for the mobile platform, since I use the phone is a x86 PC simulator, so choose:

Android-ndk-r10b\toolchains\x86-4.8\prebuilt\windows-x86_64\bin\i686-linux-android-gdb.exe

And the GDB version should be consistent with gdbserver. (Can be confirmed by--version)

Under Windows command line, execute the following command, App_process32_path represents the directory where App_process32 is located.

Android-ndk-r10b\toolchains\x86-4.8\prebuilt\windows-x86_64\bin\i686-linux-android-gdb.exe {App_process32_path} \app_process32

Once the above command is executed, it enters the GDB command mode (the command line has (GDB)) before executing:

Target remote:1818 #连接本地的: 1234 Port, this port is mapped to the 1234 port of the phone.

Set Solib-search-path {Solibpath} #加载所有的动态连接库, the path location of Solibpath can be consulted: The directory of "set Solib-search-path XXX" in the NDK compiled output file Gdb.setup , but note that you want to use an absolute directory.

#我的是set Solib-search-path d:/android/android-ndk-r10b/samples/testndk/obj/local/x86

Then use the info Shared command to see if the libdebugndk.so symbol library has been loaded, and what I'm showing is:

0xaabd92e0 0xaabd93c8 Yes D:\Android\android-ndk-r10b\samples\TestNDK\obj\local\x86\libDebugNDK.so # If the symbol library is not loaded then there is a (*) after yes.

(4) Set breakpoints, start debugging

In GDB mode, execute:

b java_stable_testndk_mainactivity_stringfromjni #设置断点

C #运行

Then, when the function is called, the breakpoint can stop! over!


Using NDK-GDB to debug the native program under as

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.