简体   繁体   English

Android NDK和GDB调试

[英]Android NDK and GDB debugging

I'm having trouble debugging my native application with GDB (on Windows), please excuse the length of this post. 我在Windows上使用GDB调试本机应用程序时遇到问题,请原谅。 I'm using a standard setup (Eclipse with ADT, ndk-build). 我正在使用标准设置(带有ADT的Eclipse,ndk-build)。 My build command is: 我的构建命令是:

ndk-build NDK_DEBUG=1 V=1

and here are my Android.mk and Application.mk files: 这是我的Android.mk和Application.mk文件:

Android.mk: Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

FILE_LIST := $(wildcard $(LOCAL_PATH)/../../source/*.cpp)
FILE_LIST += $(wildcard $(LOCAL_PATH)/../../source/android/*.cpp)

LOCAL_MODULE        := RAGEAndroid
LOCAL_SRC_FILES     := $(FILE_LIST:$(LOCAL_PATH)/%=%)

LOCAL_LDLIBS        := -llog -landroid -lm -lEGL -lGLESv3
LOCAL_STATIC_LIBRARIES := android_native_app_glue

LOCAL_CPPFLAGS      := -std=c++11 -pthread -DRAGE_ANDROID #-ftemplate-backtrace-limit=0
ifeq ($(NDK_DEBUG),1)
    LOCAL_CPPFLAGS      += -DRAGE_DEBUG
endif

include $(BUILD_SHARED_LIBRARY)

$(call import-module,android/native_app_glue)

Application.mk Application.mk

APP_STL                 := gnustl_static
NDK_TOOLCHAIN_VERSION   := 4.8
APP_ABI                 := armeabi-v7a
APP_PLATFORM            := android-18

Verbose mode outputs the following during build (just showing the build of a single file for now, please note I only showed one .o file being linked and omitted the rest for easy reading with the tag #OMITTED OTHER .O FILES#): 详细模式在构建期间输出以下内容(目前仅显示单个文件的构建,请注意,我仅显示了一个.o文件被链接,其余的省略了,以便于使用标签#OMITTED OTHER .O FILES#进行阅读):

"E:\\Development\\Toolchains\\android-ndk-r9c\\ndk-build.cmd" NDK_DEBUG=1 V=1 all 
del /f/q .\libs\armeabi-v7a\libRAGEAndroid.so   >NUL 2>NUL
del /f/q .\libs\armeabi-v7a\gdbserver   >NUL 2>NUL
del /f/q .\libs\armeabi-v7a\gdb.setup   >NUL 2>NUL
[armeabi-v7a] Gdbserver      : [arm-linux-androideabi-4.8] libs/armeabi-v7a/gdbserver
copy /b/y "E:\Development\Toolchains\android-ndk-r9c\prebuilt\android-arm\gdbserver\gdbserver" ".\libs\armeabi-v7a\gdbserver" > NUL
[armeabi-v7a] Gdbsetup       : libs/armeabi-v7a/gdb.setup
E:/Development/Toolchains/android-ndk-r9c/prebuilt/windows-x86_64/bin/echo.exe "set solib-search-path ./obj/local/armeabi-v7a" > ./libs/armeabi-v7a/gdb.setup
E:/Development/Toolchains/android-ndk-r9c/prebuilt/windows-x86_64/bin/echo.exe "directory E:/Development/Toolchains/android-ndk-r9c/platforms/android-18/arch-arm/usr/include E:/Development/Toolchains/android-ndk-r9c/sources/android/native_app_glue E:/Development/Toolchains/android-ndk-r9c/sources/cxx-stl/gnu-libstdc++/4.8/include E:/Development/Toolchains/android-ndk-r9c/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include E:/Development/Toolchains/android-ndk-r9c/sources/cxx-stl/gnu-libstdc++/4.8/include/backward jni" >> ./libs/armeabi-v7a/gdb.setup
[armeabi-v7a] Compile++ thumb: RAGEAndroid <= main.cpp
E:/Development/Toolchains/android-ndk-r9c/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++ -MMD -MP -MF ./obj/local/armeabi-v7a/objs-debug/RAGEAndroid/__/__/source/android/main.o.d -fpic -ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-exceptions -fno-rtti -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -O0 -UNDEBUG -marm -fno-omit-frame-pointer -IE:/Development/Toolchains/android-ndk-r9c/sources/android/native_app_glue -IE:/Development/Toolchains/android-ndk-r9c/sources/cxx-stl/gnu-libstdc++/4.8/include -IE:/Development/Toolchains/android-ndk-r9c/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -IE:/Development/Toolchains/android-ndk-r9c/sources/cxx-stl/gnu-libstdc++/4.8/include/backward -Ijni -DANDROID  -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -pthread -DRAGE_ANDROID -DRAGE_DEBUG     -IE:/Development/Toolchains/android-ndk-r9c/platforms/android-18/arch-arm/usr/include -c  jni/../../source/android/main.cpp -o ./obj/local/armeabi-v7a/objs-debug/RAGEAndroid/__/__/source/android/main.o 
[armeabi-v7a] SharedLibrary  : libRAGEAndroid.so
E:/Development/Toolchains/android-ndk-r9c/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++ -Wl,-soname,libRAGEAndroid.so -shared --sysroot=E:/Development/Toolchains/android-ndk-r9c/platforms/android-18/arch-arm ./obj/local/armeabi-v7a/objs-debug/RAGEAndroid/__/__/source/aabb.o #OMITTED OTHER .O FILES# -Wl,--fix-cortex-a8  -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now  -LE:/Development/Toolchains/android-ndk-r9c/platforms/android-18/arch-arm/usr/lib -llog -landroid -lm -lEGL -lGLESv3 -llog -lc -lm -o ./obj/local/armeabi-v7a/libRAGEAndroid.so
[armeabi-v7a] Install        : libRAGEAndroid.so => libs/armeabi-v7a/libRAGEAndroid.so
copy /b/y ".\obj\local\armeabi-v7a\libRAGEAndroid.so" ".\libs\armeabi-v7a\libRAGEAndroid.so" > NUL
E:/Development/Toolchains/android-ndk-r9c/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/arm-linux-androideabi-strip --strip-unneeded  ./libs/armeabi-v7a/libRAGEAndroid.so

As can be seen the file gets compiled with the -g flag and later -O0 is set right before -UNDEBUG -marm -fno-omit-frame-pointer . 可以看出,该文件使用-g标志进行了编译,随后-O0 刚好-UNDEBUG -marm -fno-omit-frame-pointer之前设置。 Also notice that in the last line arm-linux-androideabi-strip --strip-unneeded is performed. 还要注意,在最后一行执行了arm-linux-androideabi-strip --strip-unneeded

In debug mode the first line of native code called in the main function sleeps to ensure that we have enough time for GDB to connect: 在调试模式下,主函数中调用的第一行本机代码处于休眠状态,以确保我们有足够的时间供GDB连接:

sleep(5);

I right-click on my project and choose "Debug As -> Native Android Application", and GDB outputs the following: 我右键单击我的项目,然后选择“调试为->本机Android应用程序”,GDB输出以下内容:

warning: Could not load shared library symbols for 98 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"?
warning: Unable to find dynamic linker breakpoint function.
GDB will retry eventurally.  Meanwhile, it is likely
that GDB is unable to debug shared library initializers
or resolve pending breakpoints after dlopen().
[New Thread 19838]
[New Thread 19840]
[New Thread 19841]
[New Thread 19842]
[New Thread 19843]
[New Thread 19844]
[New Thread 19845]
[New Thread 19846]
[New Thread 19847]
[New Thread 19850]
No symbol table is loaded.  Use the "file" command.

After a while if I pause execution and type info sharedlibrary this is the output: 过一会儿,如果我暂停执行并输入info sharedlibrary,则输出为:

info sharedlibrary
warning: Could not load shared library symbols for 5 libraries, e.g. eglsubAndroid.so.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
From        To          Syms Read   Shared Object Library
                    No          /system/bin/linker
                    No          libc.so
                    No          libstdc++.so
                    No          libm.so
                    No          liblog.so
                    No          libcutils.so
                    No          libgccdemangle.so
                    No          libcorkscrew.so
                    No          libutils.so
                    No          libbinder.so
                    No          libhardware.so
                    No          libmemtrack.so
                    No          libz.so
                    No          libandroidfw.so
                    No          libexpat.so
                    No          libstlport.so
                    No          libnativehelper.so
                    No          libnetutils.so
                    No          libsync.so
                    No          libui.so
                    No          libGLES_trace.so
                    No          libEGL.so
                    No          libGLESv2.so
                    No          libgui.so
                    No          libinput.so
                    No          libcamera_metadata.so
                    No          libcamera_client.so
                    No          libpng.so
                    No          libft2.so
                    No          libjpeg.so
                    No          libgabi++.so
                    No          libicuuc.so
                    No          libicui18n.so
                    No          libskia.so
                    No          libsqlite.so
                    No          libGLESv1_CM.so
                    No          libETC1.so
                    No          libwpa_client.so
                    No          libhardware_legacy.so
                    No          libselinux.so
                    No          libsonivox.so
                    No          libcrypto.so
                    No          libssl.so
                    No          libstagefright_foundation.so
                    No          libspeexresampler.so
                    No          libaudioutils.so
                    No          libmedia.so
                    No          libusbhost.so
                    No          libharfbuzz_ng.so
                    No          libLLVM.so
                    No          libbcinfo.so
                    No          libbcc.so
                    No          libRS.so
                    No          libRScpp.so
                    No          libhwui.so
                    No          libandroid_runtime.so
                    No          libdvm.so
                    No          libjavacore.so
                    No          memtrack.msm8974.so
                    No          libdrmframework.so
                    No          libdrmframework_jni.so
                    No          libconnectivitymanager.so
                    No          libstagefright_omx.so
                    No          libstagefright_yuv.so
                    No          libvorbisidec.so
                    No          libpowermanager.so
                    No          libstagefright_enc_common.so
                    No          libstagefright_avc_common.so
                    No          libstagefright.so
                    No          libmtp.so
                    No          libexif.so
                    No          libstagefright_amrnb_common.so
                    No          libmedia_jni.so
                    No          libexif_jni.so
                    No          libcommon_time_client.so
                    No          libnbaio.so
                    No          libeffects.so
                    No          libaudioflinger.so
                    No          libvideoeditor_osal.so
                    No          libvideoeditor_videofilters.so
                    No          libvideoeditorplayer.so
                    No          libvideoeditor_core.so
                    No          libvideoeditor_jni.so
                    No          librs_jni.so
                    No          libandroid.so
                    No          libwilhelm.so
                    No          libOpenSLES.so
                    No          libjnigraphics.so
                    No          libwebviewchromium.so
                    No          libwebviewchromium_plat_support.so
                    No          libjavacrypto.so
                    No          libgsl.so
                    No          libadreno_utils.so
                    No          libEGL_adreno.so
                    No          libGLESv1_CM_adreno.so
                    No          libGLESv2_adreno.so
                    No          libGLESv3.so
                    No          libRAGEAndroid.so
                    No          eglsubAndroid.so
                    No          libsc-a3xx.so
                    No          libqdutils.so
                    No          libmemalloc.so
                    No          gralloc.msm8974.so

If I use the file command on the libRAGEAndroid.so library (my library) this is what I get (omitted the full path): 如果我在libRAGEAndroid.so库(我的库)上使用file命令,这就是我得到的(省略完整路径):

file "#OMITTED PROJECT PATH#/libs/armeabi-v7a/libRAGEAndroid.so"

A program is being debugged already.
Are you sure you want to change the file? (y or n) [answered Y; input not from terminal]
Reading symbols from E:/Cloud/Dropbox/Development/Projects/PC/RAGE (Rajveer Aujla Game Engine)/0.0.21/RAGE/android/libs/armeabi-v7a/libRAGEAndroid.so...done.

WARNING: no debugging symbols found in E:/Cloud/Dropbox/Development/Projects/PC/RAGE (Rajveer Aujla Game Engine)/0.0.21/RAGE/android/libs/armeabi-v7a/libRAGEAndroid.so.
Either the binary was compiled without debugging information
or the debugging information was removed (e.g., with strip or strip -g).
Debugger capabilities will be very limited.
For further information: http://wiki/Main/GdbFaq#No_debugging_symbols_found

If I then replace it with the pre-stripped file from obj/local/armeabi-v7a and then perform the same file command again, I get: 如果再用obj / local / armeabi-v7a中预先剥离的文件替换它,然后再次执行相同的文件命令,则会得到:

file "E:/Cloud/Dropbox/Development/Projects/PC/RAGE (Rajveer Aujla Game Engine)/0.0.21/RAGE/android/libs/armeabi-v7a/libRAGEAndroid.so"
A program is being debugged already.
Are you sure you want to change the file? (y or n) [answered Y; input not from terminal]
Reading symbols from E:/Cloud/Dropbox/Development/Projects/PC/RAGE (Rajveer Aujla Game Engine)/0.0.21/RAGE/android/libs/armeabi-v7a/libRAGEAndroid.so...done.
Error in re-setting breakpoint 1: Cannot access memory at address 0x14ae08

I'm kind of lost as to where to go from here, so hopefully someone with more experience can help me out! 我对从这里去哪里有些迷茫,所以希望有更多经验的人可以帮助我!

What ndk plugin is doing behind scenes is generating gdb script and a batch script (on windows) to pull required binaries from device and push right version of gdbserver to the device. ndk插件在后台执行的操作是生成gdb脚本和批处理脚本(在Windows上),以从设备中提取所需的二进制文件并将正确版本的gdbserver推送到设备。

For whatever reason, if you don't get one thing right, one of those moving parts will fail and you will go on goose chase. 无论出于什么原因,如果您做对一件事不正确,那么其中一个运动部件将失败,您将继续追赶鹅。 I don't do NDK debugging as often as AOSP native debugging. 我不像AOSP本地调试那样频繁地进行NDK调试。 When I do, I go with plain vanilla eclipse debug configuration to debug remote C/C++ apps and manually configure gdb and gdb script. 完成后,我将使用普通的Eclipse Eclipse调试配置来调试远程C / C ++应用程序,并手动配置gdb和gdb脚本。 You have total control on everything. 您完全掌控一切。

Please check Debugging native code of Android Java apps article. 参阅调试Android Java应用程序的本机代码一文。 You might want to look into generated ndk-gdb script to see which gdb/gdbserver combo that the plugin is using from NDK tools folder. 您可能需要查看生成的ndk-gdb脚本,以从NDK tools文件夹中查看该插件正在使用的gdb / gdbserver组合。 You can copy them to a separate folder and use them in eclipse configuration. 您可以将它们复制到单独的文件夹中,并在Eclipse配置中使用它们。 You will have to create a dummy c/c++ project pointing to your sources and use that in the remote debug configuration. 您将必须创建一个指向您的源的虚拟c / c ++项目,并将其用于远程调试配置。 What you need to keep in mind is that this project is there for debugging, not for building the sources. 您需要记住的是,该项目用于调试,而不是用于构建源。 You should use your NDK project for building code. 您应该将NDK项目用于构建代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM