ndk-gdb使用简介

文档摘自android-ndk-r9c-linux-x86_64.tar.bz2中的docs/ndk-gdb.html

下面是渣渣版翻译,禁止围观,禁止吐槽


'ndk-gdb' and 'ndk-gdb-py' Overview

IMPORTANT: IF YOU ARE DEBUGGING THREADED PROGRAMS, PLEASE READ THE SECTION BELOW TITLED 'Thread Support'.

 注意:如果你需要调试多线程程序,请阅读下面的 “Thread Support”小节

I. Usage:

The Android NDK r4 introduced a helper shell script named 'ndk-gdb' to easily launch a native debugging session for your NDK-generated machine code.

The script is located at the top-level directory of the NDK, and shall be invoked from the command-line when in your application project directory, or any of its sub-directories. For example:

安卓NDK r4引入了一个ndk-gdb的协助脚本,用来简单地运行NDK产生的机器代码的本地调试会话。脚本位于NDK目录的顶层,你需要在你的应用的工程目录,或者任何它的子目录里,通过命令行来调用。举个栗子

    cd $PROJECT
    $NDK/ndk-gdb

Where $NDK points to your NDK installation path. You can also create an alias or add $NDK to your PATH to avoid typing it every time.

其中$NDK指向你的NDK安装路径。你同样可以创建一个alias或者增加$NDK到PATH环境变量来避免每次输入前面那一长串...

IMPORTANT: Native debugging can only work if all these conditions are met:

注意:只有当下列条件全部满足的时候,本地调试才可以正常工作

  1. Your application is built with the 'ndk-build' script:

    Building with the legacy "make APP=<name>" method is not supported by ndk-gdb.

  2. Your application is debuggable:

    In other words, your AndroidManifest.xml has an element that sets the android:debuggable attribute to "true"

  3. You are running your application on Android 2.2 (or higher):

    ndk-gdb will not work if you try to run your application on previous versions of the system. That does not mean that your application should target the Android 2.2. API level, just that the debugging session should happen on a 2.2+ device or emulator system image.

    IMPORTANT IMPORTANT IMPORTANT !!

    If you are using the ADT Eclipse plug-in to build your application, make sure you're using version 0.9.7 or later.

    If you are using the 'ant' build tool, make sure that you have the latest revision of the SDK Platform components. The following minimal revisions are required:

          Android 1.5      r4
          Android 1.6      r3
          Android 2.1      r2
          Android 2.2      r1

    These should be available through the SDK updater.

    If these conditions are not met, the generated .apk will not contain required support files and native debugging will not be possible.


1.你的应用是用ndk-build脚本建立的。

        用早期的"make APP=<name>"方法建立的应用不被ndk-gdb支持。

2.你的应用是可调试的。

        换句话说,你的AndroidManifest.xml里面有一个<application>对象,并且设置了android:debuggable 属性为"ture".

3.应用运行在安卓2.2(或者更高)的版本上。

        ndk-gdb不能在这之前的系统版本上工作。这并不意味着你的应用需要使用target为Android2.2 的API,仅仅是调试会话需要在一个2.2+的设备或者仿真器上而已。

        注意!!!注意!!!注意!!!如果你用安卓开发工具ADT Eclipse来建立应用,请确保版本为 0.9.7或更高.如果你用ant,确保你已经有最新版本的的SDK 组件。需要的最小的版本为:

                Android 1.5     r4 

                Android 1.6     r3 

                Android 2.1     r2 

                Android 2.2     r1。 

        这些都可以通过SDK updater 工具来下载。

        如果这些条件没有达成,产生的.apk文件将不包含用于支持本地调试的一些文件,导致不能调试。

'ndk-gdb' handles many error conditions and will dump an informative error message if it finds a problem. For example, it:

  • checks that adb is in your path.

  • checks that your application is declared debuggable in its manifest.

  • checks that, on the device, the installed application with the same package name is also debuggable.


‘ndk-gdb’处理很多的错误条件,并且会dump一些它找到的有用的错误消息。举个栗子:

    -检查adb是否在PATH环境变量中,

    -检查应用是否在manifest里声明了debuggable

    -检查设备上是否安装了同名的并且可调试的包。


By default, ndk-gdb will search for an already-running application process, and will dump an error if it doesn't find one. You can however use the --start or --launch= option to automatically start your activity before the debugging session.

默认情况下,ndk-gdb会搜索已经在运行的应用进程,如果没找到,会dump一条错误信息。你也可以用 --start 或者 --launch=<name>选项,它将在调试会话之前自动启动activity。

When it successfully attaches to your application process, ndk-gdb will give you a normal GDB prompt, after setting up the session to properly look for your source files and symbol/debug versions of your generated native libraries.

当它成功地attach 到你的应用进程,然后搜索你产生本地库的的源文件和符号/调试版本以适当地设置调试会话后,ndk-gdb将会给出一个正常的GDB提示符。

You can set breakpoints with 'b ' and resume execution with 'c' (for 'continue'). See the GDB manual for a list of commands.

你可以用b <location>来设置断点,然后用c来恢复执行,具体请参见GDB手册的命令列表。

IMPORTANT: When quitting the GDB prompt, your debugged application process will be stopped! This is a gdb limitation.

注意:当退出GDB提示符的时候,你的调试的应用进程会被停止!这是来自gdb的限制。

IMPORTANT: The GDB prompt will be preceded by a long list of error messages, where gdb complains that it cannot find various system libraries (e.g. libc.so, libstdc++.so, liblog.so, libcutils.so, etc...)

       This is normal, because there are no symbol/debug versions of
       these libraries corresponding to your target device on your
       development machine. You can safely ignore these messages.

注意:GDB提示符开始会产生一长串的错误消息,抱怨它找不到各种各样的系统库(libc.so,libstdc++.so,libcutils.so,等等...)

这是正常的,因为你的开发机器上没有与你目标机上一致的 带符号表/调试信息 的版本的这些库。

你可以安全的忽略这些信息。

II. Options:

To see a list of options, type 'ndk-gdb --help'. Notable ones are:

要查看选项列表,键入 ndk-gdb --help. 值得注意的有:

--verbose:

Print verbose information about the native debugging session setup. Only needed to debug problems when you can't connect and that the error messages printed by ndk-gdb are not enough.

打印调试会话的详细的信息。只有当你无法连接,并且ndk-gdb打印出来的错误消息不够的时候,你才需要使用它来解决不能连接的问题。

--force:

By default, ndk-gdb aborts if it finds that another native debugging session is running on the same device. Using --force will kill the session, and replace it with a new one. Note that the debugged program is not killed and will be stopped again.

如果ndk-gdb发现同一台设备上正运行着另一个调试会话,ndk-gdb默认会终止。用--force 将会杀掉先前的会话,然后用新的替代之。注意 

--start:

By default, ndk-gdb will try to attach to an existing running instance of your application on the target device. You can use --start to explicitly launch your application before the debugging session.

NOTE: This launches the first launchable activity listed from your application manifest. Use --launch=<name> to start another one. See --launch-list to dump the list of such activities.

默认情况下,ndk-gdb会尝试attach到目标设备上一个已经在运行的应用实例。你可以用 --start直接地在调试会话启动前运行应用。

注意:这样其实是运行在你的manifest中列出的第一个可运行的activity。用--lauch=<name>来启动其他的。用--lauch-list来列出所有这些activity

--launch=<name>:

This is similar to --start, except that it allows you to start a specific activity from your application. This is only useful if your manifest defines several launchable activities.

与 --start类似,除了它允许你启动应用中指定的activity之外。这只在你的应用中定义了多个可运行的activity时候才有用。

--launch-list:

Convenience option that prints the list of all launchable activity names found in your application manifest. The first one will be used by --start

快速地打印所有在你的应用manifest中找到的可运行的activity名字。其中第一个会被--start使用


--project=<path>:

Specify application project directory. Useful if you want to launch the script without cd-ing to the directory before that.

指定应用的工作目录,如果你想不cd到对应的目录下就运行对应的脚本,这个选项会非常有用

--port=<port>:

By default, ndk-gdb will use local TCP port 5039 to communicate with the debugged application. By using a different port, it is possible to natively debug programs running on different devices/emulators connected to the same development machine.

默认情况下,ndk-gdb会使用本地的TCP 5039端口来与被调试的应用通信。通过改变端口号,可以让不同设备/仿真器上的调试程序同时连接到同一台开发机器。

--adb=<file>:

Specify the adb tool executable, in case it is not in your path.

指定adb工具的路径,假如adb没有在你的环境变量PATH中的时候。

-d-e-s <serial>:

These flags are similar to the ADB ones and allow you to handle the case where you have several devices/emulators connected to your development machine.

    -d:          Connect to a single physical device
    -e:          Connect to a single emulator device
    -s <serial>: Connect to a specific device or emulator
                 where <serial> is the device's name as listed
                 by the "adb devices" command.

Alternatively, you can define the ADB_SERIAL environment variable to list a specific device, without the need for a specific option.

这些标志位跟ADB的一样,它允许你处理以下情况,当你有多个设备/仿真器连接到你的开发机器的时候。


    -d:          连接到单个物理设备。
    -e:          连接到单个仿真器设备。
    -s <serial>: 连接到指定串号的设备,可以用adb devices先查看设备串号列表

 或者,你可以定义ADB_SERIAL环境变量为指定的设备,从而不需要使用上面这些选项。


--exec=<file>-x <file>:

After connecting to the debugged process, run the GDB initialization commands found in . This is useful if you want to do something repeatedly, e.g. setting up a list of breakpoints then resuming execution automatically.

连接到调试进程之后,马上运行该文件中的GDB命令。当你总是要做一些重复的事情的时候,这是非常有用的。举个栗子:设置一系列的断点,然后自动恢复执行

--nowait:

Disable pausing the Java code until GDB connects. Passing this option may cause early breakpoints to be missed.

GDB连接上前不暂停Java代码,启用该选项将导致前面设置的一些断点被忽略。

--tui-t:

Enable Text User Interface if GDB was built with it. [ndk-gdb-py only]

启用文本用户接口,如果GDB在build的时候带了该功能。[只对ndk-gdb-py有效]

--gnumake-flag=<flag>:

Extra flag(s) to pass to the ndk-build system when querying it for project information. Multiple instances can be used. [ndk-gdb-py only]


--stdcxx-py-pr={auto|none|gnustdcxx[-GCCVER]|stlport}:

Use specified Python pretty-printers for displaying types in the Standard C++ Library. 'auto' mode works by looking at the .so files for a libstdc++ library, and as such only works in the shared scenario. When linking statically to a libstdc++ library, the required printers must be specified. The default is 'none'. [ndk-gdb-py only]

III. Requirements:

'ndk-gdb' requires a Unix shell to run. This means that Cygwin is required to run it on Windows. An experimental Python re-implementation called 'ndk-gdb-py' is also provided, removing this restriction and providing some new features.

The other NDK requirements apply: e.g. GNU Make 3.81 or higher.

ndk-gdb 需要unix shell来运行,这意味着window下需要借助Cygwin.一个实验性的重新实现的版本ndk-gdb-py,去除了该限制,并且提供了一些新的特性。

其他NDK 需要的应用:GNU Make 3.81或者更高。

IV. Thread Support:

If your application runs on a platform older than Android 2.3, ndk-gdb will not be able to debug native threads properly. Instead, the debugger will only be able to put breakpoints on the main thread, completely ignoring the execution of other ones.

The root of the problem is complex, but is essentially due to a very unfortunate bug in the platform, which was only discovered lately.

The gdbserver binary that comes with this NDK has special code to detect this condition at runtime and adapt its behaviour automatically (in other words, you don't have anything special to do when building your code).

What this means in practical terms are:

  • If you are on Android 2.3, or a prior platform release which has had the platform bug-fix back-ported to it, you will be able to debug native threads automatically.

  • If you are not, you will only be able to debug the main thread (as in previous NDK releases). You will also see the following message when launching ndk-gdb (just before the gdb prompt):

        Thread debugging is unsupported on this Android platform!

If you place a breakpoint on a function executed on a non-main thread, the program will exit with the following message in GDB:

        Program terminated with signal SIGTRAP, Trace/breakpoint trap.
        The program no longer exists.

如果在比Android 2.3更早的平台上运行,ndk-gdb将不能很好地调试本地线程.此时,调试器只能在主线程中放置断点,其他位置的将完全被忽略。

这个问题的根本原因很复杂,不过它本质上归结于android平台一个非常不幸的BUG,一个不久前才被发现的BUG

gdbserver文件是NDK指定的代码,用来检测运行时条件并且自适应其行为。(换句话说,你不需要做任何指定的事情,当你build你的代码的时候)

实际上这意味着:

  • 如果你在android 2.3,或者先前已经修复这个bug的平台,你自动就可以多线程调试。

  • 如果不是,你将只能调试主线程(就像以前发行的NDK版本一样). 并且,当你运行ndk-gdb的时候你会看见下面的消息:(在gdb提示符之前):

        Thread debugging is unsupported on this Android platform!

        如果你放置一个断点在非主线程调用的函数中,程序将会退出并且GDB会发出如下信息

        Program terminated with signal SIGTRAP, Trace/breakpoint trap.
        The program no longer exists.

    



















转载于:https://my.oschina.net/u/1183791/blog/194133

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值