Windows和Linux記憶體檢測工具:Valgrind,Visual Leak Detector,CppCheck, Cpplint
1 Linux記憶體洩漏檢測工具Valgrind
Valgrind簡介
Valgrind是一套Linux下,開放原始碼(GPL V2)的模擬除錯工具的集合。Valgrind由核心(core)以及基於核心的其他除錯工具組成。核心類似於一個框架(framework),它模擬了一個CPU環境,並提供服務給其他工具;而其他工具則類似於外掛 (plug-in),利用核心提供的服務完成各種特定的記憶體除錯任務。
Valgrind是一套Linux下,開放原始碼(GPL V2)的模擬除錯工具的集合。Valgrind由核心(core)以及基於核心的其他除錯工具組成。核心類似於一個框架(framework),它模擬了一個CPU環境,並提供服務給其他工具;而其他工具則類似於外掛 (plug-in),利用核心提供的服務完成各種特定的記憶體除錯任務。
Valgrind用法
valgrind [options] prog-and-args [options]: 常用選項,適用於所有Valgrind工具。
- tool=<name> 最常用的選項。執行 valgrind中名為toolname的工具。預設memcheck。h –help 顯示幫助資訊。
-version 顯示valgrind核心的版本,每個工具都有各自的版本。
q –quiet 安靜地執行,只打印錯誤資訊。
v –verbose 更詳細的資訊, 增加錯誤數統計。
-trace-children=no|yes 跟蹤子執行緒? [no]
-track-fds=no|yes 跟蹤開啟的檔案描述?[no]
-time-stamp=no|yes 增加時間戳到LOG資訊? [no]
-log-fd=<number> 輸出LOG到描述符檔案 [2=stderr]
-log-file=<file> 將輸出的資訊寫入到filename.PID的檔案裡,PID是執行程式的進行ID
-log-file-exactly=<file> 輸出LOG資訊到 file
-log-file-qualifier=<VAR> 取得環境變數的值來做為輸出資訊的檔名。 [none]
-log-socket=ipaddr:port 輸出LOG到socket ,ipaddr:port
LOG資訊輸出
-xml=yes 將資訊以xml格式輸出,只有memcheck可用
-num-callers=<number> show <number> callers in stack traces [12]
-error-limit=no|yes 如果太多錯誤,則停止顯示新錯誤? [yes]
-error-exitcode=<number> 如果發現錯誤則返回錯誤程式碼 [0=disable]
-db-attach=no|yes 當出現錯誤,valgrind會自動啟動偵錯程式gdb。[no]
-db-command=<command> 啟動偵錯程式的命令列選項[gdb -nw %f %p]
適用於Memcheck工具的相關選項:
-leak-check=no|summary|full 要求對leak給出詳細資訊? [summary]
-leak-resolution=low|med|high how much bt merging in leak check [low]
-show-reachable=no|yes show reachable blocks in leak check? [no]
安裝valgrind
[email protected]:~/test$ sudo apt-get install valgrind
測試程式碼
[email protected]:~/test$ cat testcode.c#include <stdlib.h>
int *fun()
{
int *p = malloc(10 * sizeof(int));
p[10] =0 ; //error 1
return p;
}
int main()
{
int *ptr = NULL;
ptr = fun();
//free(c); //error 2
ptr = NULL;
return 0;
}
編譯:
[email protected]
[email protected]:~/test$ ls
main testcode.c
valgrind使用
[email protected]:~/test$ valgrind --tool=memcheck --leak-check=full ./main報告
==77673== Memcheck, a memory error detector==77673== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==77673== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==77673== Command: ./main
==77673==
==77673== Invalid write of size 4
==77673== at 0x400544: fun (in /home/azheng/test/main)
==77673== by 0x400566: main (in /home/azheng/test/main)
==77673== Address 0x5204068 is 0 bytes after a block of size 40 alloc'd
==77673== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==77673== by 0x400537: fun (in /home/azheng/test/main)
==77673== by 0x400566: main (in /home/azheng/test/main)
==77673==
==77673==
==77673== HEAP SUMMARY:
==77673== in use at exit: 40 bytes in 1 blocks
==77673== total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==77673==
==77673== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==77673== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==77673== by 0x400537: fun (in /home/azheng/test/main)
==77673== by 0x400566: main (in /home/azheng/test/main)
==77673==
==77673== LEAK SUMMARY:
==77673== definitely lost: 40 bytes in 1 blocks
==77673== indirectly lost: 0 bytes in 0 blocks
==77673== possibly lost: 0 bytes in 0 blocks
==77673== still reachable: 0 bytes in 0 blocks
==77673== suppressed: 0 bytes in 0 blocks
==77673==
==77673== For counts of detected and suppressed errors, rerun with: -v
==77673== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
重新修改程式碼後,進行記憶體檢查
[email protected]:~/test$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./main==77813== Memcheck, a memory error detector
==77813== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==77813== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==77813== Command: ./main
==77813==
==77813==
==77813== HEAP SUMMARY:
==77813== in use at exit: 0 bytes in 0 blocks
==77813== total heap usage: 1 allocs, 1 frees, 40 bytes allocated
==77813==
==77813== All heap blocks were freed -- no leaks are possible
==77813==
==77813== For counts of detected and suppressed errors, rerun with: -v
==77813== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
說明
Invalid write of size 4:表示陣列越界寫了4位元組40 bytes in 1 blocks:表示因程式退出而發生記憶體洩露40位元組
2 Windows記憶體洩漏檢測工具VLD
Visual Leak Detector
下載連結:在Visual C++ IDE的"工具"→"選項"→"專案和解決方案"→"VC++ 目錄","包含檔案"增加VLD的"\include"路徑,"庫檔案"增加VLD的"\lib\Win32"路徑,另外動態庫"\bin\Win32"路徑在安裝時已經新增到環境變數裡面了,若是未新增,則需要手動拷貝"\bin\Win32"下的檔案到工程Debug目錄。
新增#include "vld.h"
3 CppCheck
Windows版本不好用,Linux還可以。4 Cpplint
程式碼規範檢查
[email protected]:~$ cat test/testcode.c#include <stdlib.h>
int *fun()
{
int *x = malloc(10 * sizeof(int));
x[10] =0 ; //error
}
int main()
{
int *c = NULL;
c = fun();
return 0;
}
[email protected]:~$ cppcheck test/testcode.c
Checking test/testcode.c...[test/testcode.c:5]: (error) Array 'x[10]' accessed at index 10, which is out of bounds.
[test/testcode.c:6]: (error) Memory leak: x
[email protected]:~$ cpplint-1.3.0/cpplint.py test/testcode.ctest/testcode.c:0: No copyright message found. You should have a line: "Copyright [year] <Copyright Owner>" [legal/copyright] [5]
test/testcode.c:3: { should almost always be at the end of the previous line [whitespace/braces] [4]
test/testcode.c:4: Tab found; better to use spaces [whitespace/tab] [1]
test/testcode.c:5: Tab found; better to use spaces [whitespace/tab] [1]
test/testcode.c:5: At least two spaces is best between code and comments [whitespace/comments] [2]
test/testcode.c:5: Should have a space between // and comment [whitespace/comments] [4]
test/testcode.c:5: Missing spaces around = [whitespace/operators] [4]
test/testcode.c:5: Extra space before last semicolon. If this should be an empty statement, use {} instead. [whitespace/semicolon] [5]
test/testcode.c:9: { should almost always be at the end of the previous line [whitespace/braces] [4]
test/testcode.c:10: Tab found; better to use spaces [whitespace/tab] [1]
test/testcode.c:11: Tab found; better to use spaces [whitespace/tab] [1]
test/testcode.c:12: Tab found; better to use spaces [whitespace/tab] [1]
Done processing test/testcode.c
Total errors found: 12
相關推薦
Windows和Linux記憶體檢測工具:Valgrind,Visual Leak Detector,CppCheck, Cpplint
1 Linux記憶體洩漏檢測工具ValgrindValgrind簡介 Valgrind是一套Linux下,開放原始碼(GPL V2)的模擬除錯工具的集合。Valgrind由核心(core)以及基於核心的其他除錯工具組成。核心類似於一個框架(framework),
Linux下記憶體檢測工具:asan
Linux下記憶體檢測工具:asan ASAN(Address-Sanitizier)早先是LLVM中的特性,後被加入GCC 4.8,在GCC 4.9後加入對ARM平臺的支援。因此GCC
linux硬碟檢測工具:Smartmontools使用指南(轉)
轉載注:在ubuntu 12.04.2LTS server中使用sudo apt-get install smartmontools來安裝 smartmontools是一款開源的磁碟控制,監視工具,可以執行在 Linux,Unix,BSD,Solaris,Mac OS,OS
Linux下記憶體問題檢測神器:Valgrind
在寫大型C/C++工程時難免會發生記憶體洩漏現象,系統程式設計中一個重要的方面就是有效地處理與記憶體相關的問題。你的工作越接近系統
Setting up a EDK II build environment on Windows and Linux:搭建Windows和Linux開發環境[2.2]
set clu cto 無法安裝 urn ems water 了解 源代碼管理 Setting up a EDK II build environment on Windows and Linux:搭建Windows和Linux開發環境[2.2] 2015-07 北
SQL Server 2017 正式發布:同時支持 Windows 和 Linux(現在看下來,當年那德拉的“雲優先,移動優先”是有遠見的,而且是有一系列的措施和產品相配合的,只是需要一點時間而已。真是佩服!!)
suse 中新 新的 ada 開發 tap 安全性 adding 互連 微軟在去年 3 月首次對外宣布了 Linux 版的 SQL Server,並於今年 7 月發布了首個公開 RC 版。前幾日在美國奧蘭多召開的微軟 Ignite 2017 大會上,微軟宣布 SQL Ser
C語言編程的兩個工具:valgrind和core
mit 編程 崩潰 文件 gdb 程序 程序崩潰 檢查內存泄漏 ted 檢查內存泄漏: valgrind --leak-check=full ./ecox_rws_helper 來檢查內存泄漏 程序崩潰看錯誤: ulimit -c unlimited 然後執行程序,會在
windows和linux下的抓包工具
gpo tcpdump linu window clas dst post blog windows Linux 抓包工具 tcpdump 示例 tcpdump -i bond0 host 10.70.11.182 -w ./sms.cap windows抓包
nginx日誌分割:windows和linux
nginx日誌分割 linux windows 一、為什麽對日誌進行分割。1.nginx日誌默認情況下統統寫入到一個文件中,文件會變的越來越大.2.單個的日誌文件非常不方便查看分析。二、簡析日誌分割。不論是windows還是linux,對日誌的分割都是一條思路。即:1.對現有日誌文件進行重命名。2
Linux學習命令匯總十——linux網絡管理及配置和網絡檢測工具
網絡配置 網絡管理 網絡檢測 ifconfig ip 本章Blog相關Linux知識點CSMA/CD(Carrier Sense Multiple Access with Collision Detection)基帶沖突檢測的載波監聽多路訪問技術(載波監聽多點接入/碰撞檢測),是一種爭用型
Linux磁碟檢測工具smartctl的使用和分析
http://blog.chinaunix.net/uid-29518139-id-4156635.html 1 編寫目的 在如今大資料的環境中,磁碟的效能和穩定性是非常重要
cmake工具使用簡明教程(基於命令列和gui,編譯到windows和linux雙平臺)
cmake可以用來構建跨平臺的專案,本文簡要講解針對多目錄原始碼專案使用cmake構建和編譯的方法。 專案結構 整個工程多目錄多檔案組織而成,其中build目錄用於生成各平臺解決方案檔案的,程式碼如下 bird.h class bird
記憶體檢測工具使用(Linux、VS)
一、Linux中記憶體檢測工具(valgrind) 記憶體檢查工具: valgrind linux上線上安裝:yum install valgrind 使用方法: valgrind 你的
淺談windows和linux下記憶體分配規律
首先先說明下,本文中程式碼來自牛刀教程。寫的很不錯。給我不少的啟發。謝謝了 我們都知道,在使用C語言時,比如定義一個數組,一個變數。那麼系統都會隨機的分配記憶體。那麼你知道記憶體分配的規律嗎? 讓我們用兩個實驗來說明windows和linux下,記憶體分配方式的不同。 同一
hadoop安裝步驟之:windows和linux下的hosts檔案的使用
1,linux下hosts檔案在:/etc/hosts,切換到root使用者下新增節點。 如配置hadoop叢集時,將主機ip和主機名的對應關係寫到hosts檔案末尾後,就可以在配置檔案中方便的用主機名來代替繁瑣的ip地址了。 內容形式如下: 192.168.188.16
用Mac和Linux命令列工具使用Windows Azure
除了Windows Azure Web Sites和令人興奮的新的虛擬機器功能的引入,我們最近釋出了一系列開放原始碼的命令列工具使您可以在任何作業系統上用命令列管理和部署這些新的服務,包括第一次支援蘋果 OSX 與 Linux 作業系統。 這篇文章裡我將引導您完成安裝和基
linux記憶體管理2:記憶體對映和需求分頁(英文名字:demand Paging,又叫:缺頁中斷)
圖 10-5 vm_area_struct 資料結構示意圖當可執行映象對映到程序的虛擬地址空間時,將產生一組 vm_area_struct 結構來描述虛擬記憶體區域的起始點和終止點,每個 vm_struct 結構代表可執行映象的一部分,可能是可執行程式碼,也可能是初始化的變數或未初始化的資料。
linux記憶體管理演算法 :夥伴演算法和slab
良好的作業系統效能部分依賴於作業系統有效管理資源的能力。在過去,堆記憶體管理器是實際的規範,但是其效能會受到記憶體碎片和記憶體回收需求的影響。現在,Linux® 核心使用了源自於 Solaris 的一種方法,但是這種方法在嵌入式系統中已經使用了很長時間了,它是將記憶體作為物件按照大小進行分配。本文將探索
C/C++獲取作業系統、CPU、記憶體資訊、硬碟、IP和MAC(windows和linux)
有時候需要在工程裡面獲取一些系統或者硬體的資訊,比如系統版本,cpu,記憶體,顯示卡,硬碟,網路等,作為後續軟體功能判斷的依據,甚至參與效能演算法自適應建模 Windows 作業系統和記憶體資訊在windows下通過系統的API來獲取,CPU資訊則需要需要通過底層CPUI
Lua學習筆記1:開發環境搭建(windows和linux)
一 windows 1 下載安裝“Lua For Windows” 2 安裝 3 編碼 開啟SciTE,輸入: print("hello,lua!"); 然後儲存,字尾名一定要是".lua",不然無法執行 4 執行 Tools-》Go,或者快捷鍵F5 二