Linux下使用gdb调试程序
Linux下使用gdb调试程序
gdb命令
- 堆栈层的切换
(gdb) bt
#0 COnTimeThread::Run (this=0x1013cdd0) at ontimethread.cpp:60
#1 0x00003fffa6e1cae0 in CIIThreadPrivate::InternalRun (that=0x1013cdd0) at ies_thread_unix.cpp:75
#2 0x00003fffa6e1c4c4 in start_thread (t=0x1013cdd0) at ies_thread_unix.cpp:84
#3 0x00003fffa590ad94 in start_thread (arg=0x3fff817ff040) at pthread_create.c:336
#4 0x00003fffa54e23a8 in .__clone () at ../sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S:96
(gdb) frame 0
#0 COnTimeThread::Run (this=0x1013cdd0) at ontimethread.cpp:60
60 if(NowHour != LastProcedureCallHour &&
- 变量的打印
buffer的打印需要使用*解引用,并指定长度
(gdb) p *chaBuffer@10
$9 = {0x68, 0x4, 0x43, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
0结尾的字符串直接打印指针变量即可,若截断胜率则可调整最大显示字符个数
(gdb) show print elements
Limit on string chars or array elements to print is 200.
(gdb) set print elements 1024
(gdb) show print elements
Limit on string chars or array elements to print is 1024.
(gdb) p strSaveMSGProcedure.local8Bit().data()
$5 = 0x3fff54002850 "call save_msg()"
调试启动
- 输入
gdb启动并进入gdb命令行 - 输入
run启动程序,run后可附带传给程序的参数,如run -h
[root@localhost ~]# gdb datacenter
GNU gdb (GDB) Fedora 7.10.1-31.fc23
Copyright (C) 2015 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 "ppc64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from datacenter...done.
(gdb) r -d
Starting program: /usr/appsoft/iesbase/bin/datacenter -d
---加载调试符号---
- 程序运行后,可以输入
Ctrl+C暂停程序运行,从而增加断点、查看堆栈和打印变量等
操作完成后,输入continue恢复程序运行
当天第一次告警[对时异常],加入缓存队列
^C
Program received signal SIGINT, Interrupt.
0x00003fffb65ab35c in .__GI___read () at ../sysdeps/unix/syscall-template.S:84
84 T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)
(gdb) b main
Breakpoint 1 at 0x10018e38: file main.cpp, line 52.
(gdb) c
Continuing.
调试运行中程序
- gdb挂载
[root@localhost datacenter]# ps -ef|grep datacenter
ies 24182 22561 5 16:28 pts/1 00:00:04 datacenter -d
root 26849 28639 0 16:30 pts/0 00:00:00 grep --color=auto datacenter
[root@localhost datacenter]# gdb attach 24182
GNU gdb (GDB) Fedora 7.10.1-31.fc23
Copyright (C) 2015 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 "ppc64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
attach: No such file or directory.
Attaching to process 24182
---加载调试符号---
Reading symbols from /usr/appsoft/iesbase/lib/libmysql.so.1...done.
Reading symbols from /usr/local/thirdlib/library/libmariadb.so.3...done.
Reading symbols from /usr/lib64/libnss_files.so.2...Reading symbols from /usr/lib/debug/usr/lib64/libnss_files-2.22.so.debug...done.
done.
0x00003fffa54cc35c in .__GI___read () at ../sysdeps/unix/syscall-template.S:84
84 T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)
(gdb)
- 挂载后程序运行中断,在适当的函数(如出错处理)或确定的代码行处增加断点,然后输入`continue`恢复程序运行
如果断点是一个多处调用的错误处理函数,不能确定其调用堆栈,可在错误处理函数处增加函数断点,中断后查看堆栈确定调用路径
```cpp
(gdb)
(gdb) b main
Breakpoint 1 at 0x10018e38: file main.cpp, line 52.
(gdb) b main.cpp:170
Breakpoint 2 at 0x1001985c: file main.cpp, line 170.
(gdb) c
Continuing.
Breakpoint 2, main (argc=2, argv=0x3ffff4675028) at main.cpp:170
170 cin >> cmdBuf;
(gdb) bt
#0 main (argc=2, argv=0x3ffff4675028) at main.cpp:170
(gdb)
- 使用gstack采样堆栈
[root@localhost datacenter]# ps -ef|grep datacenter
ies 23175 22561 7 16:47 pts/1 00:00:01 datacenter -d
root 23691 28639 0 16:47 pts/0 00:00:00 grep --color=auto datacenter
[root@localhost datacenter]# gstack 23175
Thread 21 (Thread 0x3fff8e004040 (LWP 23199)):
#0 0x00003fff968a3670 in __pthread_cond_wait (cond=0x3fff88002bb8, mutex=0x3fff88002b90) at pthread_cond_wait.c:186
#1 0x00003fff96198d3c in .Poco::EventImpl::waitImpl() () from /usr/local/poco/lib/libPocoFoundation.so.31
可以将输出重定向到文件,通过比较多次采样获得线程运行情况
[root@localhost datacenter]# gstack 23175 > 1
[root@localhost datacenter]# gstack 23175 > 2
[root@localhost datacenter]# diff 1 2

浙公网安备 33010602011771号