gdb调试艺术之定义钩子命令:-D
自定义钩子命令可以在命令执行前/后执行
1.自定义命令执行前的钩子
(gdb) define hook-echo
Type commands for definition of "hook-echo".
End with a line saying just "end".
>echo "<<<"
>end
(gdb) echo
"<<<"(gdb)
2.自定义命令执行后的钩子
(gdb) define hookpost-echo
Type commands for definition of "hookpost-echo".
End with a line saying just "end".
>echo ">>>"
>end
(gdb) echo
"<<<"">>>"(gdb)
3.常用的自定义钩子
define hook-stop # 遇到断点或单步调试时,停顿时的钩子
....
end
define hook-run # 执行run命令时的钩子
...
end
define hook-continue # 执行continue命令时的钩子
...
end
经典用法示例:
1.程序执行时忽略SIGUSR1信号引起的运行停止
(gdb) define hook-run
> handle SIGUSR1 nostop
> end
2.汇编调试时, 跟踪显示汇编以及指定寄存器的信息
(gdb) define hook-stop
Type commands for definition of "hook-stop".
End with a line saying just "end".
>p /x $eax
>p /x $ebx
>disas $rip + 5
>end
(gdb) b main
Breakpoint 1 at 0x5fe
(gdb) r
Starting program: /tmp/a.out
$1 = 0x555545fa
$2 = 0x0
Dump of assembler code for function main:
0x00005555555545fa <+0>: push %rbp
0x00005555555545fb <+1>: mov %rsp,%rbp
=> 0x00005555555545fe <+4>: mov -0xc(%rbp),%edx
0x0000555555554601 <+7>: mov -0x8(%rbp),%eax
0x0000555555554604 <+10>: add %edx,%eax
0x0000555555554606 <+12>: mov %eax,-0x4(%rbp)
0x0000555555554609 <+15>: mov -0xc(%rbp),%eax
0x000055555555460c <+18>: sub -0x8(%rbp),%eax
0x000055555555460f <+21>: mov %eax,-0x4(%rbp)
0x0000555555554612 <+24>: mov -0xc(%rbp),%eax
0x0000555555554615 <+27>: imul -0x8(%rbp),%eax
0x0000555555554619 <+31>: mov %eax,-0x4(%rbp)
0x000055555555461c <+34>: mov $0x0,%eax
0x0000555555554621 <+39>: pop %rbp
0x0000555555554622 <+40>: retq
End of assembler dump.
Breakpoint 1, 0x00005555555545fe in main ()