# "x"命令会把最后检查的内存地址值存在“$_”这个“convenience variable”中,并且会把这个地址中的内容放在“$__”这个“convenience variable” # https://sourceware.org/gdb/onlinedocs/gdb/Convenience-Vars.html p $_ p $__
# 改变字符串的值 # https://stackoverflow.com/questions/19503057/in-gdb-how-can-i-write-a-string-to-memory set main::p1="Bill" p &p1 set {char[4]} 0x80477a4 = "Ace"
# 设置变量的值,set var variable=expr set var i = 8 # set {type}address=expr,通过地址给变量赋值 p &i set {int}0x8047a54 = 8
# 修改PC寄存器的值 disassemble main info line 6 info line 7 p $pc set var $pc=0x08050949
# 使用断点命令改变程序的执行 # https://sourceware.org/gdb/onlinedocs/gdb/Break-Commands.html#Break-Commands b fun command 1 >silent >set variable n = 0 >continue >end r
# 修改被调试程序的二进制文件 # https://sourceware.org/gdb/onlinedocs/gdb/Patching.html#Patching gcc-write ./a.out # 命令后选项 set write on file ./a.out disassemble /mr fun set variable *(short*)0x400651=0x0ceb disassemble /mr fun
# 调试已经运行的进程 gdb program -p=10210 gdb program processID gdb program --pid=10210
# attach已经运行的进程 attach 10210 detash
# 调试多进程程序时,gdb默认会追踪父进程,以下命令可是其追踪子进程 set follow-fork-mode child
# 调试多进程程序,同时调试父进程和子进程 set detach-on-fork off i inferior inferior 2
# 调试多进程程序,让父进程和子进程同时运行 set schedule-multiple on
# 查看线程信息 i threads
# 打印所有线程堆栈信息 # https://sourceware.org/gdb/onlinedocs/gdb/Threads.html thread apply all bt
# 不显示线程启动和退出信息 setprint thread-events off
# 用gdb调试多线程程序时,一旦程序断住,所有的线程都处于暂停状态。此时当你调试其中一个线程时(比如执行“step”,“next”命令),所有的线程都会同时执行 # 如果想在调试一个线程时,让其它线程暂停执行,可以使用“set scheduler-locking on”命令 set scheduler-locking on
# `$_thread`变量表示线程号 # https://sourceware.org/gdb/onlinedocs/gdb/Threads.html wa a command 2 printf"thread id=%d\n", $_thread end c