gpt4 book ai didi

Android NDK 应用程序无法达到任何断点

转载 作者:行者123 更新时间:2023-12-02 09:14:16 30 4
gpt4 key购买 nike

我正在使用 https://www.youtube.com/watch?v=kjsC-lKUgM8尝试调试简单 NDK 应用程序的教程。 我已经完成了视频中的所有操作,除了:

  • 我使用的是 OS X 10.9.3 而不是 Windows。
  • 我不使用 android:debuggable=true (因为 eclipse 认为它是错误)在 AndroidManifest.xml 中,我已经从 Preferences->Android->NDK 设置了 NDK 路径并在 Project Properties -> C/C++ Build未选中 Use default build command并设置在那里 ndk-build NDK_DEBUG=1 APP_OPTIM=debug .
  • 我不使用 x86 emulator但是 Samsung Duos S设备与 Android 4.0.4

  • 但是视频中使用的 breakpoiin 在我的情况下没有被击中。我已经在第 4 天尝试调试一个简单的 NDK 测试项目。查了很多资料:
  • Android 原生开发工具包说明书
  • 一堆论坛和教程
  • 视频

  • 但是不能打一个该死的断点。如果你能做到这一点,请帮忙。

    最佳答案

    以下是我为我们的内部 Android 开发团队编写的教程的摘录。其中大部分来自此博客:http://mhandroid.wordpress.com/
    重要笔记:

  • 我的 Android 工作使用 Linux (Ubuntu 12.04) 环境。
  • 我使用了适用于 Linux 的 ADT bundle ,版本:v22.2.1-833290(Eclipse IDE + Extras)
  • 这些步骤用于从 Java Activity 调试到 JNI 共享对象。
  • 我为本教程创建了测试项目,不会在此处发布,但随后的说明中提供了对这些项目的引用。您将需要一个现有的 Android 应用程序项目以及一个由您的 Java 代码调用的 JNI 共享对象。

  • 项目设置
  • 启用调试。

  • Open AndroidManifest.xml, select the Application tab, and set Debuggable=true. This will make the application debuggable even when it's running on a device that's running in user mode.4. Build the Native Sources. From the Terminal, enter the project directory and enter:

        ndk-build -B
    您应该看到以下输出:

    Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
    Gdbsetup : libs/armeabi/gdb.setup
    Compile++ thumb : DebuggingTestJNI <= com_sample_test_DebuggingTestActivity.cppStaticLibrary : libstdc++.a
    SharedLibrary : libDebuggingTestJNI.so
    Install : libDebuggingTestJNI.so => libs/armeabi/libDebuggingTestJNI.so


  • 清理项目。 在 Eclipse 菜单条中,选择 Project→Clean。每当您更改/构建您的 native 源时,最好执行此步骤。此步骤可确保 Eclipse 重建您的 .apk。

  • native 调试设置
  • 创建 Java 调试配置。 我们需要创建一个调试配置以进入 Java 源代码。

    • In the Eclipse toolbar, you'll see a green bug. Click the little arrow next to the bug and select "Debug Configurations…".
    • Double-click "Android Application" in the tree structure on the left. This will create a template for a new Android Application Debug Configuration.
    • In the "Name:" field, name it "DebuggingTest Java Debug" to make sure you know this Configuration applies specifically to the DebuggingTest project, and targets your Java source.
    • Under "Project:", click the "Browse…" button and select DebuggingTest.
    • Click "Apply" to save your changes.
    • Click "Close".
    • In the Eclipse toolbar, click the little arrow next to the bug and select "Organize Favorites…".
    • Click "Add…"
    • Select "DebuggingTest Java Debug" and click "OK".

    您的新调试配置现已创建并添加到您的收藏夹中。您可以通过单击工具栏中错误旁边的小箭头来访问您的收藏夹。 “DebuggingTest Java Debug”应该在列表的顶部。
  • 运行 ndk-gdb。

    • In the Eclipse toolbar, click the little arrow next to the bug and select "DebuggingTest Java Debug". This will deploy and install the DebuggingTest.apk to your connected Android device and start the debugger.

    • Navigate to the DebuggingTest project directory in your Terminal and type the following:

      ndk-gdb


    如果命令成功,您应该看到以下内容:

    GNU gdb 6.6Copyright (C) 2006 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome 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=x86_64-linux-gnu --target=arm-elf-linux".(no debugging symbols found)…


    如果您在此步骤之前忘记开始调试应用程序,您会得到以下信息:

    ERROR: Could not extract PID of application on device/emulator.Are you sure the application is already started?Consider using --start or --launch= if not.If your Android.mk file is malformed or contains $(info) blocks, you get the following:cp: target ./obj/local/armeabi/gdb.setup' is not a directory /home/Dev/NDK/ndk-gdb: 639: cannot create start DebuggingTest/jni/Android.mk end DebuggingTest/jni/Android.mk ./obj/local/armeabi/gdb.setup: Directory nonexistent /home/Dev/NDK/ndk-gdb: 640: cannot create start DebuggingTest/jni/Android.mk end DebuggingTest/jni/Android.mk ./obj/local/armeabi/gdb.setup: Directory nonexistent start: invalid option: -x Try start --help' for more information.


    如果 ndk-gdb 已经在运行,你会得到以下信息:

    ERROR: Another debug session running, Use --force to kill it.


    在继续之前解决您的错误。 ndk-gdb 必须成功运行。
    运行 ndk-gdb 不仅确保我们到目前为止所做的一切都是正确的,而且还创建了 app_process , gdb.setup libc.so 我们项目的 obj/local/armeabi/子目录中的文件。在后面的步骤中将需要这些文件。
  • 停止调试。

    • In your Terminal, type CTRL+Z to stop ndk-gdb.
    • In Eclipse, select Run → Terminate.4. Create C/C++ Debug Configuration. We need to create a debug configuration for stepping into C/C++ source code.
    • In Eclipse, click the little arrow next to the bug and select "Debug Configurations…".
    • Double-click "C/C++ Application" in the tree structure on the left. This will create a template for a new C/C++ Application Debug Configuration.
    • In the "Name:" field, name it "DebuggingTest C and CPP Debug" to make sure you know this Configuration applies specifically to the DebuggingTest project, and targets your C/C++ source.
    • In the "Main" tab:
    • Click "Browse…" on the "C/C++ Application:" field.Navigate to "/home/Test/testing/DebuggingTest/obj/local/armeabi/app_process" and click "OK".
    • Click "Browse…" on the "Project:" field.
    • Select "DebuggingTest" and click "OK".
    • Check the "Disable auto build" box.
    • At the bottom of the form, you'll see "Using GDB (DSF) … - Select other…". Press the "Select other…" button.
    • In the pop-up, check the "Use configuration specific settings" box.
    • Select "Standard Create Process Launcher" in the list and press "OK".
    • In the "Debugger" tab:
    • Click the combo-box on the "Debugger:" field and select "gdbserver".
    • Uncheck the "Stop on startup at:" box.
    • In the "Main" sub-tab:
    • Click "Browse…" on the "GDB debugger:" field.
    • Navigate to "/home/Dev/ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gdb" and click "OK". This debugger is distributed with the Android NDK.
    • In the "GDB command file:" field, type "/home/Test/testing/DebuggingTest/obj/local/armeabi/gdb2.setup". The gdb2.setup file does not exist yet, but we'll create it shortly.
    • Check the "Use full file path to set breakpoints" box.
    • In the "Connection" sub-tab:
    • Set "Type:" to TCP
    • Set "Port number:" to 5039
    • Click "Apply" to save your changes.
    • Click "Close"
    • In the Eclipse toolbar, click the little arrow next to the bug and select "Organize Favorites…".
    • Click "Add…"
    • Select "DebuggingTest C and CPP Debug" and click "OK".

    您的新调试配置现已创建并添加到您的收藏夹中。您可以通过单击工具栏中错误旁边的小箭头来访问您的收藏夹。 “DebuggingTest C and CPP Debug”应该在列表的顶部。
  • 创建 gdb2.setup。 Eclipse 不喜欢 gdb 设置文件中的“target remote :5039”行,因为它想在内部输入此命令(这就是您在上一步中配置端口 5039 的原因)。因为 gdb.setup 文件是由 NDK 脚本重新创建的,所以您必须将其复制到 gdb2.setup 并将 Eclipse 指向 gdb2.setup 文件(我们在上一步中已完成)。

    • In your File Explorer, navigate to "/home/Test/testing/DebuggingTest/obj/local/armeabi/".
    • Copy the "gdb.setup" file and then paste it into the same folder. The result should be a file named "gdb (copy).setup".
    • Rename "gdb (copy).setup" to "gdb2.setup".
    • Open gdb2.setup by double-clicking the file.
    • Replace "set solib-search-path ./obj/local/armeabi" with "set solib-search-path /home/Test/testing/DebuggingTest/obj/local/armeabi".
    • Replace "file ./obj/local/armeabi/app_process" with "file /home/Test/testing/DebuggingTest/obj/local/armeabi/app_process".
    • Remove the line that reads "target remote :5039".
    • Save and Close the file.5. Create ndk-gdb-eclipse. One last Eclipse housekeeping item. Eclipse will run the gdb binary itself, so we have to remove the execution of gdb from ndk-gdb. We'll save the original content by doing another Copy-Paste-Rename.
    • In your File Explorer, navigate to "/home/Dev/NDK".
    • Copy the "ndk-gdb" file and then paste it into the same folder. The result should be a file named "ndk-gdb (copy)".
    • Rename "ndk-gdb (copy)" to "ndk-gdb-eclipse".
    • Open ndk-gdb-eclipse by doing a right-click → Open With Other Application …
    • Select Text Editor from the list of applications
    • In the file, locate the line that reads "$GDBCLIENT -x native_path $GDBSETUP" (probably at the very bottom) and comment it out by prefixing it with a "#" character.
    • Save and Close the file.

    在 Eclipse IDE 中调试 native 源时 ,我们将使用 ndk-gdb-eclipse 而不是 ndk-gdb .
    调试/步入代码
  • 步入 Java 代码!

    • Put a breakpoint in the DebuggingTestActivity.java file at line 20 (System.out.println("hello world!")).
    • Insert a breakpoint in your main Activity BEFORE any calls into native code are made. onCreate() is generally the best place for this.
    • Start the DebuggingTest application in Debug mode by clicking on the little arrow next to the bug and selecting "DebuggingTest Java Debug".
    • You'll see a pop-up on the screen labeled "Confirm Perspective Switch". Press "Yes" if you'd like it to switch to your Debug Perspective. I would recommend doing so.
    • At this point, you should have hit the breakpoint you set.

    警告:我们刚刚击中的断点位于 onCreate 内功能。此函数将在所有静态之后被调用 loadLibrary已拨打电话。请注意,在 onCreate 函数之外有一个 System.loadLibrary("DebuggingTestJNI")静态块内部。此 loadLibrary 调用将在我们输入 onCreate 之前执行函数,确保我们的原生符号在我们到达初始断点时加载。在继续之前,我们必须在断点处停止!
  • 步入 C/C++ 代码!

    • In your Terminal, navigate to the DebuggingTest directory and type the following command:ndk-gdb-eclipseRemember that we created this file back in step 6
    • If successful, the command should complete without any response.
    • Go back to Eclipse and run your C/C++ debugger by clicking the little arrow next to the bug and selecting "DebuggingTest C and CPP Debug"
    • Note: When I do this, I see dozens of errors in my Eclipse console, but things still seem to be working…
    • Switch back to your Java Perspective.
    • Click the double arrows at the top right of right of your Eclipse window and select Java.
    • Open DebuggingTest/jni/com_sample_test_DebuggingTestActivity.cpp
    • Set a breakpoint in the first JNI function that will be called by your main Activity.
    • Click Run → Resume (F8)
    • You'll get the Perspective Switch message again, so feel free to switch back to the Debug Perspective.

    您应该刚刚在 native 代码中设置的断点!
    恭喜!!!
    在这一点上,您应该能够将所学的知识应用到您可能拥有的现有项目中。

    关于Android NDK 应用程序无法达到任何断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23987279/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com