1. 程式人生 > 其它 >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或者瀏覽器開啟

然後我生成了一張這樣的圖

還沒詳細研究