如何使用 GDBSERVER 在 Android 上调试应用程序? [英] How to debug an App on Android with GDBSERVER?

查看:21
本文介绍了如何使用 GDBSERVER 在 Android 上调试应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试我的应用程序通过 JNI 使用的本机共享库.我可以使用gdbserver --attach pid"附加到正在运行的应用程序,但我需要在启动 gdbserver 命令时实际启动我的应用程序.

I'm trying to debug a native shared library that my App uses through JNI. I can attach to a running app just fine with "gdbserver --attach pid" but i need to actually launch my app when i launch the gdbserver command.

关于此主题的博客点击量达到一百万,但似乎没有一个人清楚地说明您如何启动应用程序.他们都说只输入gdbserver 10.0.2.2:1234 ./MyProgram",但究竟什么是MyProgram".那是 MyProgram.apk 吗?是 MyProgram.so 吗?它是在安装应用程序时创建的其他文件吗?如果有,它的路径是什么?

There's a million blog hits on this topic but none of them seem to be clear as to how you launch your app. They all say to just type "gdbserver 10.0.2.2:1234 ./MyProgram" but what exactly is "MyProgram". Is that MyProgram.apk? Is it MyProgram.so? Is it some other file that gets created when the app is installed? If so, what's its path?

推荐答案

虽然可以像其他人描述的那样开发可以直接从 shell 启动的独立应用程序,但听起来您的代码运行在 Android 应用程序框架中.因此,您没有可执行文件,而是有一个 APK,其中包含您的 Dalvik 类文件以及其他资源,包括您的本机共享对象.

While it is possible to develop free standing applications that can be launched directly from the shell as others are describing, it sounds like your code runs within the Android application framework. Therefore, you don't have an executable and instead have an APK that contains your Dalvik class files along with other resources including your native shared object.

在 APK 中启动应用程序涉及几个步骤

Launching an application in an APK involves several steps

  1. system_server 进程接收到请求您的应用程序的意图.
  2. zygote 进程被告知派生出一个新进程并运行您的类的方法.
  3. 您的应用程序在新进程中运行.

虽然您不能通过将可执行文件传递给 gdbserver 来直接启动 APK,但使用 am 命令从 shell 触发启动它相当容易.

While you can't launch an APK directly by passing an executable to gdbserver, its fairly easy to trigger a launch it from the shell using the am command.

$ adb -d shell
# am
usage: am [subcommand] [options]

    start an Activity: am start [-D] <INTENT>
        -D: enable debugging

    send a broadcast Intent: am broadcast <INTENT>

    start an Instrumentation: am instrument [flags] <COMPONENT>
        -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
        -e <NAME> <VALUE>: set argument <NAME> to <VALUE>
        -p <FILE>: write profiling data to <FILE>
        -w: wait for instrumentation to finish before returning

    start profiling: am profile <PROCESS> start <FILE>
    stop profiling: am profile <PROCESS> stop

    <INTENT> specifications include these flags:
        [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
        [-c <CATEGORY> [-c <CATEGORY>] ...]
        [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
        [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
        [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
        [-n <COMPONENT>] [-f <FLAGS>] [<URI>]


# am start -n com.android.browser/.BrowserActivity
Starting: Intent { cmp=com.android.browser/.BrowserActivity }
#

一旦您的应用程序运行,请像以前一样使用 gdbserver --attach <pid>.如果幸运的话,您的应用程序会在调用您的本机代码之前等待一些用户交互,以便您有机会在 GDB 中附加和设置断点.

Once your application is running, use gdbserver --attach <pid> like you have before. If you are lucky your application waits for some user interaction before calling into your native code to give you a chance to attach and set your breakpoints in GDB.

这篇关于如何使用 GDBSERVER 在 Android 上调试应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆