时间:2021-07-01 10:21:17 帮助过:22人阅读
$ gdb gdbThreadTest//调试gdbThreadTest
加断点
(gdb) break 7
Breakpoint 1 at 0x400a19: file kangThread.cpp, line 7.
(gdb) break 35
Breakpoint 2 at 0x400b35: file kangThread.cpp, line 35.
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400a19 in ThreadFun(void*) at kangThread.cpp:7
2 breakpoint keep y 0x0000000000400b35 in main() at kangThread.cpp:35
开始运行
(gdb) r
Starting program: /home/kang/src/mulThread/kangThread
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Traceback (most recent call last):
File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in <module>
from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named ‘libstdcxx‘
[New Thread 0x7ffff6fd5700 (LWP 2773)]
[Switching to Thread 0x7ffff6fd5700 (LWP 2773)]
Breakpoint 1, ThreadFun (arg=0x602010) at kangThread.cpp:7
warning: Source file is more recent than executable.
7 int *value=static_cast<int*> (arg);
查看线程信息
(gdb) info thread
[New Thread 0x7ffff67d4700 (LWP 2774)]
Id Target Id Frame
3 Thread 0x7ffff67d4700 (LWP 2774) "kangThread" clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:81
* 2 Thread 0x7ffff6fd5700 (LWP 2773) "kangThread" ThreadFun (arg=0x602010) at kangThread.cpp:7
1 Thread 0x7ffff7fda780 (LWP 2769) "kangThread" clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:81
可以看到ID为2的线程执行到了断点Breakpoint 1。可以看一下value的值
(gdb) n
8 std::cout<<"This is thread"<<*value<<std::endl;
(gdb) p *value
$2 = 1
切换到线程3,看一下线程3执行到了哪里
(gdb) c
Continuing.
1
[Switching to Thread 0x7ffff67d4700 (LWP 2774)]
Breakpoint 1, ThreadFun (arg=0x602030) at kangThread.cpp:7
7 int *value=static_cast<int*> (arg);
(gdb) info thread
Id Target Id Frame
* 3 Thread 0x7ffff67d4700 (LWP 2774) "kangThread" ThreadFun (arg=0x602030) at kangThread.cpp:7
2 Thread 0x7ffff6fd5700 (LWP 2773) "kangThread" __GI__dl_debug_state () at dl-debug.c:74
1 Thread 0x7ffff7fda780 (LWP 2769) "kangThread" 0x00007ffff7bc566b in pthread_join (threadid=140737337186048,
thread_return=0x0) at pthread_join.c:92
(gdb) thread 3
[Switching to thread 3 (Thread 0x7ffff67d4700 (LWP 2774))]
#0 ThreadFun (arg=0x602030) at kangThread.cpp:7
7 int *value=static_cast<int*> (arg);
(gdb) n
8 std::cout<<"This is thread"<<*value<<std::endl;
(gdb) p *value
$3 = 2
可以看出线程3的value为2。
还有其他许多命令和方法,要在实践中慢慢熟悉。
版权声明:本文为博主原创文章,未经博主允许不得转载。
gdb调试多线程
标签:gdb linux 多线程