gdb,神器也,必须好好深入学习之
编写一段会造成死锁的代码,如下:
#include <unistd.h>
#include <stdio.h>
#include <boost/thread.hpp>
boost::mutex mu1;
boost::mutex mu2;
void func1() {
mu1.lock();
sleep(1);
mu2.lock();
printf("func1\n");
mu2.unlock();
mu1.unlock();
}
void func2() {
mu2.lock();
sleep(1);
mu1.lock();
printf("func2\n");
mu1.unlock();
mu2.unlock();
}
int main() {
boost::thread t1(func1);
boost::thread t2(func2);
t1.join();
t2.join();
printf("Hello, world\n");
return 0;
}
构建程序,生成可执行文件test
g++ -g -o test test.cpp -lboost_thread
这里,使用 -g 选项,在生成的可执行文件中加入调试信息,程序中试用了boost的线程库,所以要链接boost_thread
执行程序,./test,无任何输出,死锁了
另开一个终端,执行 ps -ef | grep test,查找进程test的pid,输出如下:
</pre><pre>
simic@ubuntu:~$ ps -ef | grep test
simic 2566 2480 0 19:59 pts/1 00:00:00 ./test
simic 2653 2326 0 20:02 pts/0 00:00:00 grep --color=auto test
simic@ubuntu:~$
可知test进程的pid为2566
打开gdb
simic@ubuntu:~$ gdb
GNU gdb (GDB) 7.6
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb)
附着到test进程上</