GDB + gdbserver 远程调试android native code
以调试模拟器中的native library code为例。
Host: ubuntu
Target: Android ICS
1.将gdbserver放入设备。
确保系统有arm-*-gdb,及存在设备端将要运行的gdbserver,gdbserver可以由google ndk中获取,在ndk的如下目录可以找到这个文件:
点击(此处)折叠或打开
- android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt
然后,通过adb shell进入设备,在/data下创建bin 目录:
点击(此处)折叠或打开
- hanpfei@hanpfei-F6Ve:/mnt/android_proj/android_src$ adb shell
- #mkdir /data/bin/
再通过adb push命令将gdbserver 放入设备:
点击(此处)折叠或打开
- hanpfei@hanpfei-F6Ve:/mnt/android_proj/softwares/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt$ ls
- gdbserver linux-x86
- hanpfei@hanpfei-F6Ve:/mnt/android_proj/softwares/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt$ adb remount
- remount succeeded
- hanpfei@hanpfei-F6Ve:/mnt/android_proj/softwares/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt$ adb push gdbserver /data/bin/
- 543 KB/s (125208 bytes in 0.225s)
2、在设备端启动gdbserver并attach到所要debug的进程
点击(此处)折叠或打开
- system 1168 37 156620 31924 ffffffff 40011384 S com.android.keychain
- app_34 2758 37 158360 34572 ffffffff 40011384 S com.hanpfei.text
- root 2774 617 900 348 00000000 40010438 R ps
- # gdbserver :1234 --attach 2758
- Attached; pid = 2758
- Listening on port 1234
gdbserver后面跟的第一个参数为端口号,之后的--atach 为gdbserver的选项,之后的数字为所要调试的进程的PID。
3、在Host端启动arm-*-gdb
网 上有高手build的各种交叉编译工具链来方便大家的嵌入式开发,单这个工具链中的gdb命令实际文件名称的前缀和后缀都会是相同的,中间的那个*可以见 到各种各样的形式,比如linux,linux-androideabi,none-linux-gnueabi等等。
点击(此处)折叠或打开
- hanpfei@hanpfei-F6Ve:/mnt/android_proj/softwares/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin$ ./arm-linux-androideabi-gdb
- GNU gdb 6.6
- Copyright (C) 2006 Free Software Foundation, Inc.
- GDB is free software, covered by the GNU General Public License, and you are
- welcome 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=i386-linux-gnu --target=arm-elf-linux".
- (gdb) shell adb forward tcp:1234 tcp:1234
- (gdb) target remote localhost:1234
- Remote debugging using localhost:1234
- [New Thread 2758]
- Malformed packet(b) (missing colon): re:0;
- Packet: 'T050b:7476b6be;0d:b874b6be;0f:84130140;thread:ac6;core:0;'
- (gdb)
使用ndk中所带的交叉编译工具链,会出现报错信息,意为畸形数据包:
点击(此处)折叠或打开
- [New Thread 2758]
- Malformed packet(b) (missing colon): re:0;
- Packet: 'T050b:7476b6be;0d:b874b6be;0f:84130140;thread:ac6;core:0;'
同时,在设备端也可以看到有如下的输出,来显示着这server端与client端难以协调的步调:
点击(此处)折叠或打开
- # gdbserver :1234 --attach 2758
- Attached; pid = 2758
- Listening on port 1234
- Remote debugging from host 127.0.0.1
- readchar: Got EOF
- Remote side has terminated connection. GDBserver will reopen the connection.
- Listening on port 1234
出现这样的状况,是由于gdbserver和arm-*-gdb版本不对应所致。由上面我们启动arm-linux-androideabi-gdb时的输出可以看到,其版本为GNU gdb 6.6。在设备上,我们可以查看gdbserver的版本信息:
点击(此处)折叠或打开
- # gdbserver --version
- GNU gdbserver (GDB) 7.1-android-gg2
- Copyright (C) 2010 Free Software Foundation, Inc.
- gdbserver is free software, covered by the GNU General Public License.
- This gdbserver was configured as "arm-elf-linux"
- #
我们可以看到gdbserver版本为GNU gdbserver (GDB) 7.1-android-gg2。这也就是症结所在。
我们可以换用和gdbserver版本较为接近的arm-*-gdb。此时在host端所需输入的命令及相应的输出如下:
点击(此处)折叠或打开
- hanpfei@hanpfei-F6Ve:~/develop_tool_chain/arm-2011.03/bin$ ./arm-none-linux-gnueabi-gdb
- GNU gdb (Sourcery G++ Lite 2011.03-41) 7.2.50.20100908-cvs
- Copyright (C) 2010 Free Software Foundation, Inc.
- License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
- This is free software: you are free to change and redistribute it.
- There is NO WARRANTY, to the extent permitted by law. Type "show copying"
- and "show warranty" for details.
- This GDB was configured as "--host=i686-pc-linux-gnu --target=arm-none-linux-gnueabi".
- For bug reporting instructions, please see:
- <https://support.codesourcery.com/GNUToolchain/>.
- (gdb) shell adb forward tcp:1234 tcp:1234
- (gdb) target remote localhost:1234
- Remote debugging using localhost:1234
- 0x40011384 in ?? ()
- (gdb)
点击(此处)折叠或打开
- Listening on port 1234
- Remote debugging from host 127.0.0.1
这即是表明,gdb client端和server端有成功的链接。
接下来通过file命令来加载将要调试的可执行文件,对于android application来说,均为 out/target/product/generic/symbols/system/bin/app_process 这个文件,及设置搜索solib的搜索路径。
点击(此处)折叠或打开
- (gdb) target remote localhost:1234
- Remote debugging using localhost:1234
- 0x40011384 in ?? ()
- (gdb) /mnt/android_proj/android_src/out/target/product/generic/symbols/system/bin/app_process
- Undefined command: "". Try "help".
- (gdb) file /mnt/android_proj/android_src/out/target/product/generic/symbols/system/bin/app_process
- A program is being debugged already.
- Are you sure you want to change the file? (y or n) y
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/bin/app_process...done.
- (gdb) set solib-search-path /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib
- warning: Could not load shared library symbols for 3 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"?
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libc.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libc.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstdc++.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstdc++.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libm.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libm.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/liblog.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/liblog.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libcutils.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libcutils.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libz.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libz.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libutils.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libutils.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libbinder.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libbinder.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libexpat.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libexpat.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libcrypto.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libcrypto.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libgabi++.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libgabi++.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libicuuc.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libicuuc.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libicui18n.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libicui18n.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libssl.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libssl.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstlport.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstlport.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libnativehelper.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libnativehelper.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libnetutils.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libnetutils.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libGLESv2_dbg.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libGLESv2_dbg.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libEGL.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libEGL.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libwpa_client.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libwpa_client.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libhardware_legacy.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libhardware_legacy.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libpixelflinger.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libpixelflinger.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libhardware.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libhardware.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libemoji.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libemoji.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libjpeg.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libjpeg.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libskia.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libskia.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libui.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libui.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libGLESv2.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libGLESv2.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libgui.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libgui.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libcamera_client.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libcamera_client.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libsqlite.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libsqlite.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libdvm.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libdvm.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libGLESv1_CM.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libGLESv1_CM.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libETC1.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libETC1.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libsonivox.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libsonivox.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright_foundation.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright_foundation.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libmedia.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libmedia.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libnfc_ndef.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libnfc_ndef.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libusbhost.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libusbhost.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libharfbuzz.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libharfbuzz.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libandroid_runtime.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libandroid_runtime.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libvorbisidec.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libvorbisidec.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright_yuv.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright_yuv.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libdrmframework.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libdrmframework.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libchromium_net.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libchromium_net.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright_amrnb_common.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright_amrnb_common.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright_enc_common.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright_enc_common.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright_avc_common.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright_avc_common.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libstagefright.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libmtp.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libmtp.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libexif.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libexif.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libmedia_jni.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libmedia_jni.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libbcc.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libbcc.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libbcinfo.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libbcinfo.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libRS.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libRS.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/librs_jni.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/librs_jni.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libandroid.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libandroid.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libwebcore.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libwebcore.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/lib_renderControl_enc.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/lib_renderControl_enc.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libGLESv2_enc.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libGLESv2_enc.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libGLESv1_enc.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libGLESv1_enc.so
- Reading symbols from /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libOpenglSystemCommon.so...done.
- Loaded symbols for /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib/libOpenglSystemCommon.so
- warning: Unable to find dynamic linker.
- GDB will be unable to debug shared library initializers
- and track explicitly loaded dynamic code.
- (gdb) set solib-absolute-prefix /mnt/android_proj/android_src/out/target/product/generic/symbols/system/lib
- warning: Unable to find dynamic linker.
- GDB will be unable to debug shared library initializers
- and track explicitly loaded dynamic code.
- (gdb)
之后,即可如调试PC端的C/C++ code一样,下断点,执行,查看内存内容,查看back trace等,来进行对library的debug工作:
点击(此处)折叠或打开
- (gdb) break HB_ShapeItem
- Note: breakpoint 1 also set at pc 0x4094bf2c.
- Breakpoint 2 at 0x4094bf2c: file external/harfbuzz/src/harfbuzz-shaper.cpp, line 1340.
- (gdb) info break
- Num Type Disp Enb Address What
- 2 breakpoint keep y 0x4094bf2c in HB_ShapeItem(HB_ShaperItem*) at external/harfbuzz/src/harfbuzz-shaper.cpp:1340
- (gdb) continue
- Continuing.
- Breakpoint 2, HB_ShapeItem (shaper_item=0xbeb6729c) at external/harfbuzz/src/harfbuzz-shaper.cpp:1340
- 1340 if (shaper_item->num_glyphs < shaper_item->item.length) {
- (gdb) n
- 1338 {
- (gdb) s
- 1340 if (shaper_item->num_glyphs < shaper_item->item.length) {
- (gdb) list
- 1335 }
- 1336
- 1337 HB_Bool HB_ShapeItem(HB_ShaperItem *shaper_item)
- 1338 {
- 1339 HB_Bool result = false;
- 1340 if (shaper_item->num_glyphs < shaper_item->item.length) {
- 1341 shaper_item->num_glyphs = shaper_item->item.length;
- 1342 return false;
- 1343 }
- 1344 assert(shaper_item->item.script < HB_ScriptCount);
- (gdb)
gdb常用调试命令
0、查看程序源代码
directory(dir) DIR
Add directory DIR to beginning of search path for source files.Forget cached info on
to $cdir:$cwd, the default.
1、程序运行时参数。
set args 可指定运行时参数。(如:set args 10 20 30 40 50)
show args 命令可以查看设置好的运行参数。
2、运行环境。
path <dir> Add directory DIR(s) to beginning of search path for object files.
show paths Current search path for finding object files.
set environment VAR [=value] 设置环境变量。如:set env HOME=/home
unset environment VAR Cancel environment variable VAR for the program.
show environment [varname] 查看环境变量。
3、工作目录。
cd <dir> 相当于shell的cd命令。
pwd 显示当前的所在目录。
4、程序的输入输出
info terminal 显示你程序用到的终端的模式。
使用重定向控制程序输出。如:run > outfile
tty命令可以指写输入输出的终端设备。如:tty /dev/ttyb
5、设置断点(BreakPoint)
我们用break命令来设置断点。正面有几点设置断点的方法:
break <function>
在进入指定函数时停住。C++中可以使用class::function或function(type,type)格式来指定函数名。
break <linenum> 在指定行号设置断点。
break +offset
break -offset
在当前行号的前面或后面的offset行停住。offiset为自然数。
break filename:linenum 在特定源文件filename的特定行linenum处设置断点。
break filename:function 在源文件filename的function函数的入口处停住。
break *address 在程序运行的内存地址处停住。
break break命令没有参数时,表示在下一条指令处停住。
break ... if <condition>
...可以是上述的参数,condition表示条件,在条件成立时停住。比如在循环体中,可以设置break if i=100,表示当i为100时停住程序。
查看断点,可使用info命令,如下所示:(注:n表示断点号)
info breakpoints [n]
info break [n]
delete Delete some breakpoints or auto-display expressions.
disable Disable some breakpoints.
enable Enable some breakpoints.
6、设置观察点(WatchPoint)
观察点一般用来观察某个表达式(变量也是一种表达式)的值是否有变化,如果有变化,马上停住程序。我们有下面的几种方法来设置观察点:
watch <expr> 为表达式(变量)expr设置一个观察点。一旦表达式值有变化,马上停住程序。
rwatch <expr> 当表达式(变量)expr被读时,停住程序。
awatch <expr> 当表达式(变量)的值被读或被写时,停住程序。
info watchpoints 列出当前所设置了的所有观察点。
7、设置捕捉点(CatchPoint)
可设置捕捉点来补捉程序运行时的一些事件。如:载入共享库(动态链接库)或是C++的异常。设置捕捉点的格式为:
catch <event> 当event发生时,停住程序。event可以是下面的内容:
1、throw 一个C++抛出的异常。(throw为关键字)
2、catch 一个C++捕捉到的异常。(catch为关键字)
3、exec 调用系统调用exec时。(exec为关键字,目前此功能只在HP-UX下有用)
4、fork 调用系统调用fork时。(fork为关键字,目前此功能只在HP-UX下有用)
5、vfork 调用系统调用vfork时。(vfork为关键字,目前此功能只在HP-UX下有用)
6、load 或 load <libname> 载入共享库(动态链接库)时。(load为关键字,目前此功能只在HP-UX下有用)
7、unload 或 unload <libname> 卸载共享库(动态链接库)时。(unload为关键字,目前此功能只在HP-UX下有用)
tcatch <event> 设置一次捕捉点,当程序停住以后,相应捕捉点被自动删除。
8、运行程序
run(r) Start debugged program.。
continue(c) [ignore-count]
fg [ignore-count]
恢复程序运行,直到程序结束,或是下一个断点到来。ignore-count表示其后忽略断点的次数。continue,c,fg三个命令都是一样的意思。
step N Step program until it reaches a different source line.
Argument N means do this N times (or till program stops for another reason).单步跟踪,如果有函数调用,他会进入该函数。进入函数的前提是,此函数被编译有debug信息。
next N
同样单步跟踪,如果有函数调用,他不会进入该函数。后面的count参数可选,不加表示一条条地执行,加表示执行后面的count行code,然后再停住。
kill Kill execution of program being debugged.
set step-mode
set step-mode on
打开step-mode模式,在进行单步跟踪时,程序不会因为没有debug信息而不停住。这个参数有很利于查看机器码。
set step-mod off 关闭step-mode模式。
finish 运行程序,直到当前函数完成返回。并打印函数返回时的堆栈地址和返回值及参数值等信息。
until(u) 这个命令可以运行程序直到退出循环体。
stepi(si)
nexti(ni)
单步跟踪一条机器指令!一条程序代码有可能由数条机器指令完成,stepi和nexti可以单步执行机器指令。与之一样有相同功能的命令是“display/i $pc” ,当运行完这个命令后,单步跟踪会在打出程序代码的同时打出机器指令(也就是汇编代码).
9、查看表达式的值
print EXP Print value of expression EXP.
display EXP Print value of expression EXP each time the program stops.
undisplay Cancel some expressions to be displayed when program stops.
info display Expressions to display when program stops, with code numbers.
whatis EXP Print data type of expression EXP.
ptype TYPE Print definition of type TYPE.
info locals Local variables of current stack frame.
10、查看Call Stack
backtrace(bt)/where COUNT Print backtrace of all stack frames, or innermost COUNT frames.
frame Select and print a stack frame.
up Select and print stack frame that called this one.
down Select and print stack frame called by this one.
info args Argument variables of current stack frame.
11、Files
file FILE Use FILE as program to be debugged.
add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR> ...]
Load symbols from FILE, assuming FILE has been dynamically loaded. ADDR is the starting address of the file's text.
12、共享库
sharedlibrary Load shared object library symbols for files matching REGEXP.
info sharedlibrary Status of loaded shared object libraries.
set solib-search-path Set the search path for loading non-absolute shared library symbol files.This takes precedence over the environment variables PATH and LD_LIBRARY_PATH.
set solib-absolute-prefix Set an alternate system root. The system root is used to load absolute shared library symbol files.
13、Support
shell Execute the rest of the line as a shell command.
source [-s] [-v] FILE Read commands from a file named FILE.用于执行gdb调试脚本
dump memory FILE START STOP Write contents of memory to a raw binary file. Writes the contents of memory within the range [START .. STOP) to the specifed FILE in raw target ordered bytes.
info threads IDs of currently known threads.
thread thread_ID Use this command to switch between threads. The new thread ID must be currently known.
target remote ip:port Use a remote computer via a serial line, using a gdb-specific protocol. Specify the serial device it is connected to