gdb调试总结

1、安卓机器中本地使用gdb调试linux

!android shell
$cd /data/local/tmp
$wget http://dan.drown.org/android/gdb-static.tar.gz
$tar zxf gdb-static.tar.gz
$chmod 777 gdb
$./gdb 程序名
....

2、双机(安卓上安装gdbserver,PC端运行gdbclient)android

一、编译native代码时使用NDK_DEBUG参数,编译完成后会在libs/armeabi-v7/下多生成两个文件gdb.setup和gdbserver;ios

!windows shell
$ndk-build NDK_DEBUG=1

二、将gdbserver拷贝到手机目录下shell

!windows shell
$adb push gdbserver /data/local/tmp/
$adb shell chmod 777 /data/local/tmp/gdbserver

三、运行gdbserverwindows

!android shell 1
$cd /data/local/tmp
$./gdbserver :1234 ;若是附加进程则--attach 2222
。。。

四、转发端口(另开一个shell)sass

!windows shell
$adb forward tcp:1234 tcp:1234

 

五、PC端使用gdbclient连接(使用NDK中自带的gdb)bash

!windows shell
$cd <your_path>\android-ndk-r10e\toolchains\arm-linux-androideabi-4.9\prebuilt\linux-x86_64\bin
$./arm-linux-androideabi-gdb 
    $$ target remote :1234                       ;链接远程gdbserver
    $$ set args "Hello, World" Test              ;设置参数
    $$ show args
    $$ set solib-search-path obj/local/armeabi   ;设置符号路径
    $$ bt                                        ;查看堆栈
    $$ l                                         ;有符号会显示pc附件源码
    $$ break main                                ;下断点
    $$ run                                       ;运行
    $$ n                                         ;n单步执行 s单指令执行
    $$ x                                         ;查看内存
    $$ info sharedlibrary                        ;查看加载的共享模块
    $$ show debug-file-directory                 ;调试符号目录
    $$ set debug-file-directory <directory>      ;设置调试符号目录
    $$ set symbol-file <directory>               ;设置符号文件
    $$ set arm fallback-mode                     ;设置thumb模式 
    $$ set arm force-mode                        ;设置arm模式
    $$ break 0x400c0e88 + (($cpsr>>5)&1)         ;thumb模式下断点须要+1
    $$ break context_switch if next == init_task ;break condition
    $$ command 1 > print xx1 > print xx2 >end    ;断点一中断时输出的信息
    $$ p MACROS                                  ;宏显示不了须要编译时make KCFLAGS=-ggdb3
    $$ disass                                    ;显示汇编代码
    $$ set $r0=1                                 ;设置寄存器的值
    $$ po self                                   ;po是print-object的简写 ios特有
    $$ info proc                                  ;all -- List all available /proc info
    $$ info proc                                  ;cmdline -- List command line arguments of the process
    $$ info proc                                  ;cwd -- List current working directory of the process
    $$ info proc                                  ;exe -- List absolute filename for executable of the process
    $$ info proc                                  ;mappings -- List of mapped memory regions
    $$ info proc                                  ;stat -- List process info from /proc/PID/stat
    $$ info proc                                  ;status -- List process info from /proc/PID/status

 

x /nfu 0x<addr>:查看内存地址中的值。
  n表示要显示的内存单元的个数
  f表示显示方式, 可取以下值
    x 按十六进制格式显示变量。
    d 按十进制格式显示变量。
    u 按十进制格式显示无符号整型。
    o 按八进制格式显示变量。
    t 按二进制格式显示变量。
    a 按十六进制格式显示变量。
    i 指令地址格式
    c 按字符格式显示变量。
    f 按浮点数格式显示变量。
  u表示一个地址单元的长度
    b表示单字节,
    h表示双字节,
    w表示四字节,
    g表示八字节

查看架构信息等命令 架构

$set arm disassembler
This commands selects from a list of disassembly styles. The "std" style is the standard style. 
$show arm disassembler
Show the current disassembly style. 
$set arm apcs32
This command toggles ARM operation mode between 32-bit and 26-bit. 
$show arm apcs32
Display the current usage of the ARM 32-bit mode. 
$set arm fpu fputype
This command sets the ARM floating-point unit (FPU) type. The argument fputype can be one of these:
auto
Determine the FPU type by querying the OS ABI. 
softfpa
Software FPU, with mixed-endian doubles on little-endian ARM processors. 
fpa
GCC-compiled FPA co-processor. 
softvfp
Software FPU with pure-endian doubles. 
vfp
VFP co-processor.

$show arm fpu
Show the current type of the FPU. 
$set arm abi
This command forces gdb to use the specified ABI. 
$show arm abi
Show the currently used ABI. 
$set arm fallback-mode (arm|thumb|auto)
gdb uses the symbol table, when available, to determine whether instructions are ARM or Thumb. This command controls gdbs default behavior when the symbol table is not available. The default is ‘auto’, which causes gdb to use the current execution mode (from the T bit in the CPSR register). 
$show arm fallback-mode
Show the current fallback instruction mode. 
$set arm force-mode (arm|thumb|auto)
This command overrides use of the symbol table to determine whether instructions are ARM or Thumb. The default is ‘auto’, which causes gdb to use the symbol table and then the setting of ‘set arm fallback-mode’. 
$show arm force-mode
Show the current forced instruction mode. 
$set debug arm
Toggle whether to display ARM-specific debugging messages from the ARM target support subsystem. 
$show debug arm
Show whether ARM-specific debugging messages are enabled.

切换汇编与源码调试app

$set disassemble-next-line on
$set disassemble-next-line off
$show disassemble-next-line

调试子进程tcp

set follow-fork-mode [parent|child]
parent: fork以后继续调试父进程,子进程不受影响。
child: fork以后调试子进程,父进程不受影响。

detach-on-fork参数,指示GDB在fork以后是否断开(detach)某个进程的调试,或者都交由GDB控制:
set detach-on-fork [on|off]
on: 断开调试follow-fork-mode指定的进程。
off: gdb将控制父进程和子进程。follow-fork-mode指定的进程将被调试,另外一个进程置于暂停(suspended)状态。
相关文章
相关标签/搜索