gdb学习笔记

阅读量: searchstar 2023-10-27 16:30:12
Categories: Tags:

运行带参数的程序:

gdb --args executablename arg1 arg2 arg3

来源:https://stackoverflow.com/questions/6121094/how-do-i-run-a-program-with-commandline-arguments-using-gdb-within-a-bash-script

attach到正在运行的进程:

gdb --pid xxx

add-symbol-file: https://stackoverflow.com/questions/30281766/need-to-load-debugging-symbols-for-shared-library-in-gdb

list / l

打印当前正在运行的位置附近的代码

打印变量的值

backtrace / bt

打印调用栈。

例子:

(gdb) backtrace
#0  __pthread_clockjoin_ex (threadid=140512052778752, thread_return=0x0, clockid=<optimized out>, 
    abstime=<optimized out>, block=<optimized out>) at pthread_join_common.c:145
#1  0x00007fcb96c5c0e3 in std::thread::join() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x000055d5daf78905 in Tester::Test (this=this@entry=0x7ffe1974a070) at /home/searchstar/kvexe/src/test.hpp:314
#3  0x000055d5daf5e78a in main (argc=<optimized out>, argv=<optimized out>)
    at /home/searchstar/kvexe/src/main.cpp:598

frame / f

选择backtrace里的某一项。例如要选择上面的例子中的:

#2  0x000055d5daf78905 in Tester::Test (this=this@entry=0x7ffe1974a070) at /home/searchstar/kvexe/src/test.hpp:314

f 2

info threads

打印每个线程执行到了什么地方

thread apply

在指定线程中执行命令。

例子:thread apply all bt,在所有线程中执行bt,即打印所有线程的调用栈。

thread

切换到info threads里的某一个thread。例如要选择这个thread:

  10   Thread 0x7f199dffb700 (LWP 1530942) "checker"       __lll_lock_wait (futex=futex@entry=0x55aea2384ea8, 
    private=0) at lowlevellock.c:52

就输入thread 10

参考文献

https://sourceware.org/gdb/download/onlinedocs/gdb/Threads.html