系统相关
首页 > 系统相关> > ubuntu程序debug工具

ubuntu程序debug工具

作者:互联网

1,valgrind

Valgrind通常用来成分析程序性能及程序中的内存泄露错误

常被安装在ubuntu上,通过使用命令的方式调用

安装

sudo apt-get install valgrind
#也可以直接make /make install编译

valgrind的7大模块:
1、memcheck:检查程序中的内存问题,如泄漏、越界、非法指针等。

2、callgrind:检测程序代码的运行时间和调用过程,以及分析程序性能。

3、cachegrind:分析CPU的cache命中率、丢失率,用于进行代码优化。

4、helgrind:用于检查多线程程序的竞态条件。

5、massif:堆栈分析器,指示程序中使用了多少堆内存等信息。

6、lackey:

7、nulgrind:
使用 --tool=xxx来命名,默认使用mencheck,

使用,我有一个test.cpp,我想看一下我的代码是不是有什么内存方面的错误于是我编译程序,当然我也可以查看多线程等,这取决于valgrind的--tool后面我给了什么参数

 g++ test.cpp -o test `pkg-config --cflags --libs opencv4` -g  --std=c++11

#加-g的作用是显示行号

然后我让valgrind来查找我是不是有啥内存错误,注意,如果程序非正常退出,你将会看不到完整输出,所以我们有责任保证程序执行完退出

valgrind  --tool=memcheck --leak-check=yes ./test 

然后我得到了类似以下输出,这是我copy的,我自己没有模拟这些错误

==4832== Memcheck, a memory error detector  
==4832== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.  
==4832== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info  
==4832== Command: ./tmp  
==4832==   
==4832== Invalid write of size 4      // 内存越界  
==4832==    at 0x804843F: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==  Address 0x41a6050 is 0 bytes after a block of size 40 alloc'd  
==4832==    at 0x4026864: malloc (vg_replace_malloc.c:236)  
==4832==    by 0x8048435: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==   
==4832== Source and destination overlap in memcpy(0x41a602c, 0x41a6028, 5) // 踩内存  
==4832==    at 0x4027BD6: memcpy (mc_replace_strmem.c:635)  
==4832==    by 0x8048461: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==   
==4832== Invalid free() / delete / delete[] // 重复释放  
==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)  
==4832==    by 0x8048477: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==  Address 0x41a6028 is 0 bytes inside a block of size 40 free'd  
==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)  
==4832==    by 0x804846C: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==   
==4832== Use of uninitialised value of size 4 // 非法指针  
==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==   
==4832==   
==4832== Process terminating with default action of signal 11 (SIGSEGV) //由于非法指针赋值导致的程序崩溃  
==4832==  Bad permissions for mapped region at address 0x419FFF4  
==4832==    at 0x804847Bdi: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
==4832==   
==4832== HEAP SUMMARY:  
==4832==     in use at exit: 0 bytes in 0 blocks  
==4832==   total heap usage: 1 allocs, 2 frees, 40 bytes allocated  
==4832==   
==4832== All heap blocks were freed -- no leaks are possible  
==4832==   
==4832== For counts of detected and suppressed errors, rerun with: -v  
==4832== Use --track-origins=yes to see where uninitialised values come from  
==4832== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 11 from 6)  
Segmentation fault  

2,特殊介绍一下valgrind的Callgrind工具

Callgrind是用于分析代码运行时间和调用过程的一个工具,它能给你生成用于追踪和查看的运行信息,命令如下

valgrind --tool=callgrind  ./test

然后你能得到一个或者n个callgrind.out.xxxx(xxxx表示进程号或者线程号)的文件,里面全都是看不懂的数据,那么如何转换呢?你需要一个gprof2dot.py脚本和dot命令,很遗憾,脚本我一直下载不到,所以没办法转换成功,所以对于valgrind这个工具,目前的debug办法就是直接查看memcheck打印出来的文字信息。

3,g++自带的调试工具

通过在编译指令上添加-gp参数,能得到一个可输出gmon.out的运行文件

 g++ test.cpp -o test `pkg-config --cflags --libs opencv4` -pg  --std=c++11
./test
#运行以后你将得到一个gmon.out

如何查看gmon.out呢?

你需要三个工具,gprof2dot,graphviz,inkscape

如何安装:

sudo pip3 install gprof2dot
#安装gprof2dot

sudo apt-get install graphviz
#安装grapgviz:将性能结果绘制成图的工具,就是dot那个命令的工具

sudo apt-get install inkscape
#安装inkspace:查看svg图片的工具

然后依次执行如下命令,你将得到svg图片:

gprof ./test -p 
gprof ./test -q 
#查看能否顺利生成报告

gprof ./test > report.txt
#生成报告

gprof2dot report.txt > report.dot 
#txt转dot

dot -Tsvg report.dot -o report.svg
#dot转svg,svg可以直接使用ai或者浏览器打开

然后我生成了一张这样的图

 还没详细研究

标签:tmp,4832,程序,Desktop,test,testC,ubuntu,debug,yanghao
来源: https://www.cnblogs.com/0-lingdu/p/15687447.html