Debugging with GDB

转载 2010年06月05日 00:50:00

Debugging with GDB

In this document

Debugging

The current version of envsetup.sh has a gdbclient command that handles much of the setup. For example, to attach the already-running globaltime application, execute the following, making sure that: 1) you do this from the same window used to build the software on the device you are debugging and 2) verify that the symbols in the object files in the build tree match up with what is installed on the device or emulator.

gdbclient app_process :5039 globaltime

Debugging

Short Instructions

Android runs gdbserver on the device and an ARM aware gdb, named arm-eabi-gdb, on the desktop machine.

  1. First you need to run gdbserver on the device:
    	gdbserver :5039 /system/bin/executable       
      

    The :5039 tells gdbserver to listen on port 5039 on the localhost, which adb bridges from the host to the device. executable represents the command to debug, a common one being runtime -s which starts the entire system all running in a single process.
  2. Launch gdb on the desktop. This can be done easily with the following command in the shell from which you built:
    gdbclient executable
    

At this point gdb will connect with your device and you should be able to enter c to have the device start executing inside of the desktop gdb session.

Detailed Instructions

If the short instructions don't work, these detailed instructions should:

  1. On the device, launch a new command:
    gdbserver :5039 /system/bin/executable
    or attach to an existing process:
    gdbserver :5039 --attach pid
  2. On your workstation, forward port 5039 to the device with adb:
    adb forward tcp:5039 tcp:5039
  3. Start a special version of gdb that lives in the "prebuilt" area of the source tree:
    prebuilt/Linux/toolchain-eabi-4.2.1/bin/arm-eabi-gdb (for Linux)
    prebuilt/darwin-x86/toolchain-eabi-4.2.1/bin/arm-eabi-gdb (for Darwin)
  4. If you can't find either special version of gdb, run find prebuilt -name arm-eabi-gdb in your source tree to find and run the latest version:
    prebuilt/Linux/toolchain-eabi-4.2.1/bin/arm-eabi-gdb  out/target/product/product-name/symbols/system/bin/executable
    

    Where product-name is the name of the device product that you're building (for example, sooner), and executable is the program to debug (usually app_process for an application).

    Make sure to use the copy of the executable in the symbols directory, not the primary android directory, because the one in the primary directory has been stripped of its debugging information.
  5. In gdb, Tell gdb where to find the shared libraries that will get loaded:
    set solib-absolute-prefix /absolute-source-path/out/target/product/product-name/symbols
    set solib-search-path /absolute-source-path/out/target/product/product-name/symbols/system/lib
    

    absolute-source-path is the path to your source tree; for example, /work/device or /Users/hoser/android/device.
    product-name is the same as above; for example, sooner.

    Make sure you specify the correct directories—gdb may not tell you if you make a mistake.
  6. Connect to the device by issuing the gdb command:
    target remote :5039
    


    The :5039 tells gdb to connect to the localhost port 5039, which is bridged to the device by adb.

    You may need to inspire gdb to load some symbols by typing:
    shared

You should be connected and able to debug as you normally would. You can ignore the error about not finding the location for the thread creation breakpoint. It will be found when the linker loads libc into your process before hitting main(). Also note that the gdb remote protocol doesn't have a way for the device to tell the host about newly created threads so you will not always see notifications about newly created threads. Info about other threads will be queried from the device when a breakpoint is hit or you ask for it by running info thread.

Just-In-Time Debug Feature

If you see the red LED flashing it means a process is in that new state (crashed and waiting for GDB connection). If this happens to the system process, most likely your device will be frozen at this point. Do not press the home key. Bring the device to someone who can debug native crashes and ask for advice. If you're in the field and just want your device to continue as it would have without this feature (like cylonning), press home (a tombstone will be recorded as usual). To enable a process to be debugged this way, you need to set a property:
adb shell setprop debug.db.uid 10000
and all processes with a uid <= 10000 will be trapped in this manner. When one of them crashes, the tombstone is processed as usual, an explicit message is printed into the log, and the red LED starts flashing waiting for the Home key to be depressed (in which case it continues execution as usual).
I/DEBUG   (   27): ********************************************************
I/DEBUG   (   27): * process 82 crashed. debuggerd waiting for gdbserver
I/DEBUG   (   27): *
I/DEBUG   (   27): *     adb shell gdbserver :port --attach 82 &
I/DEBUG   (   27): *
I/DEBUG   (   27): * and press the HOME key.
I/DEBUG   (   27): ********************************************************

When you see the entry above, make sure adb is forwarding port 5039 (you only need to do this once, unless the ADB server dies) and execute:

% adb forward tcp:5039 tcp:5039
Execute the line shown in the debug output, substituting 5039 for the proper port:
% adb shell gdbserver :5039 --attach 82 &

If the crashing process is based off zygote (that is, system_server and all applications), the default values for the gdbclient command, app_process binary and port 5039, are correct, so you can execute:

% cd <top of device source tree>
% gdbclient

Otherwise you need to determine the path of the crashing binary and follow the steps as mentioned above (for example, gdbclient hoser :5039 if the hoser command has failed).

↑ Go to top

零基础学会在Linux上编译调试C++项目

本课程主要针对没有或者很少写过linux上C++程序的同学, 本课程会教你如何从0基础开始,安装配置ubuntu虚拟机、使用GCC编译普通程序、动态库、静态库,编写复杂项目配置文件makefile,使用GDB工具调试C++程序。
  • 2017年01月11日 14:04

gdb加了-g仍然(no debugging symbols found)

想用gdb对程序进行调试,之前的时候就想用gdb的,结果发现加了-g也还是不行,显示如下: GNU gdb 6.8 Copyright (C) 2008 Free Software Foundat...
  • gzxx2007sddx
  • gzxx2007sddx
  • 2014-10-23 10:55:53
  • 2590

Art of Debugging with GDB, DDD, and Eclipse 软件调试的艺术

  • 2017年08月25日 14:04
  • 11.08MB
  • 下载

gdb调试出现“no debugging symbols found”,不能打断点调试的原因.

1.代码编译时,是否添加-g选项。 2.程序链接安装时,是否添加-s,用于生成striped文件,导致把调试信息删除。 所以,去除“no debugging symbols found”提示,要添...
  • GandM
  • GandM
  • 2013-07-23 15:16:38
  • 1237

Debugging with gdb

  • 2018年01月19日 11:10
  • 2.52MB
  • 下载

gdb Debugging Full Example (Tutorial): ncurses

I'm a little frustrated with finding "gdb examples" online that show the commands but not their outp...
  • wallwind
  • wallwind
  • 2016-08-15 11:46:48
  • 5114

Debugging with gdb GDB官方手册

  • 2010年04月25日 22:05
  • 1.79MB
  • 下载

gdb调试(no debugging symbols found)

  gdb  调试程序时出现下面信息:  GNU   gdb   Red   Hat   Linux   (5.3post-0.20021129.18rh)     Copyright   2003 ...
  • ixidof
  • ixidof
  • 2009-11-09 23:04:00
  • 12092

Debugging with GDB学习记录(一)

最近使用GDB调试程序,所以阅读了《Debugging with GDB》一书,略作记录,如需仔细研究使用,建议仍阅读英文版书籍或MAN手册 调试器,比如GDB,的目的就是用于查看运行中的程...
  • kafeiflynn
  • kafeiflynn
  • 2011-08-22 13:27:04
  • 4061

Debugging with GDB 9th.pdf

  • 2010年07月01日 14:15
  • 1.91MB
  • 下载
收藏助手
不良信息举报
您举报文章:Debugging with GDB
举报原因:
原因补充:

(最多只允许输入30个字)