linux 內存檢測工具之memleak

概述

MemLeak 是一個C語言內存泄漏檢測工具。 原理是利用 C 語言的宏調用來替代原有的函數調用, memleak 維護了一個鏈表,在這個鏈表中保存着程序中對內存函數

調用的記錄,這些函數包括:malloc、calloc、realloc、free。


接口聲明(對外)

memleak.h代碼如下:
[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. /*************************************************************************** 
  2.    memleak.h: Redirects calls to the functions defined in memleak.c 
  3.    Copyright (c) 2005, 2008 Stanislav Maslovski. 
  4.  
  5.    This program is free software; you can redistribute it and/or modify 
  6.    it under the terms of the GNU General Public License as published by 
  7.    the Free Software Foundation; either version 2 of the License, or 
  8.    (at your option) any later version. 
  9.  
  10.    This program is distributed in the hope that it will be useful, 
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  13.    GNU General Public License for more details. 
  14.  
  15.    You should have received a copy of the GNU General Public License 
  16.    along with this program; if not, write to the Free Software 
  17.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
  18.    ************************************************************************* 
  19.     
  20.    Include this file into the module you want to debug for memory leaks. 
  21.     
  22.    IMPORTANT: 
  23.    ========== 
  24.    If you do not want to catch ramdom segfaults make sure that this file gets 
  25.    included in _every_ module which uses malloc(), free() and friends. 
  26.    Only if you know that the blocks allocated in a particular module will never 
  27.    be freed or reallocated in any other module you may include this file only in 
  28.    that module. For additional information see memleak.c 
  29. */  
  30.   
  31. #ifndef H_MEMLEAK_H  
  32. #define H_MEMLEAK_H  
  33.   
  34. /* this makes sure that stdlib.h gets included before our macros */  
  35. #include <stdlib.h>  
  36.   
  37. extern char *dbg_file_name;  
  38. extern unsigned long dbg_line_number;  
  39.   
  40. #define CHK_FREED 1  
  41. #define CHK_ALLOC 2  
  42. #define CHK_ALL (CHK_FREED | CHK_ALLOC)  
  43.   
  44. extern void *dbg_malloc(size_t size);  
  45. extern void *dbg_realloc(void *ptr, size_t size);  
  46. extern void *dbg_calloc(size_t num, size_t size);  
  47. extern void dbg_free(void *ptr);  
  48.   
  49. extern void dbg_init(int history_length);  
  50. extern int dbg_check_addr(char *msg, void *ptr, int opt);  
  51. extern void dbg_mem_stat(void);  
  52. extern void dbg_zero_stat(void);  
  53. extern void dbg_abort(char *msg);  
  54. extern void dbg_heap_dump(char *keyword);  
  55. extern void dbg_history_dump(char *keyword);  
  56. extern void dbg_catch_sigsegv(void);  
  57.   
  58. #define FILE_LINE dbg_file_name = __FILE__, dbg_line_number = __LINE__  
  59. #define malloc(s) (FILE_LINE, dbg_malloc(s))  
  60. #define realloc(p, s) (FILE_LINE, dbg_realloc(p, s))  
  61. #define calloc(n, s) (FILE_LINE, dbg_calloc(n, s))  
  62. #define free(p) (FILE_LINE, dbg_free(p))  
  63.   
  64. #define dbg_init(n) (FILE_LINE, dbg_init(n))  
  65. #define dbg_check_addr(m, p, o) (FILE_LINE, dbg_check_addr(m, p, o))  
  66. #define dbg_mem_stat() (FILE_LINE, dbg_mem_stat())  
  67. #define dbg_zero_stat() (FILE_LINE, dbg_zero_stat())  
  68. #define dbg_abort(m) (FILE_LINE, dbg_abort(m))  
  69. #define dbg_heap_dump(k) (FILE_LINE, dbg_heap_dump(k))  
  70. #define dbg_history_dump(k) (FILE_LINE, dbg_history_dump(k))  
  71. #define dbg_catch_sigsegv() (FILE_LINE, dbg_catch_sigsegv())  
  72.   
  73. #ifdef WITH_DBG_STRDUP  
  74. /* this makes sure that string.h gets included before our macros */  
  75. #include <string.h>  
  76.   
  77. extern char *dbg_strdup(const char *s);  
  78. extern char *dbg_strndup(const char *s, size_t n);  
  79.   
  80. #define strdup(s) (FILE_LINE, dbg_strdup(s))  
  81. #define strndup(s, n) (FILE_LINE, dbg_strndup(s, n))  
  82.   
  83. #endif  
  84.   
  85. #endif  

  通過定義的struct head 鏈表接口體來記錄所有內存申請來源(文件,行數,大小),dbg_heap_dump()有可以打印出所有內存的分配情況.

memleak源碼

sourceforge下載源碼地址:http://sourceforge.net/projects/memleak/

測試代碼

example.c代碼如下:
[cpp] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. #include <unistd.h>  
  4. #include "memleak.h"  
  5.   
  6. int main()  
  7. {  
  8.   void *a,*b,*c;  
  9.   char *s;  
  10.   dbg_init(10);  
  11.   dbg_catch_sigsegv();  
  12.   a = malloc(1000);  
  13.   b = malloc(30);  
  14.   a = realloc(a, 150);  
  15.   c = calloc(90, 3);  
  16.   b = realloc(b, 0);  
  17.   
  18.   malloc(0);  
  19.   calloc(0, 10);  
  20.   realloc(0, 10);  
  21.   
  22.   realloc(a, 0);  
  23.   
  24.   free(0);  
  25.   
  26.   s = strdup("A string.");  
  27.   s = strndup(s, 5);  
  28.   
  29.   puts(s);  
  30.   
  31.   dbg_mem_stat();  
  32.   dbg_heap_dump("");  
  33.   return 0;  
  34. }  

編譯運行

   直接make生成example,運行即可查看所有內存的分配使用情況,也可修example.c文件測試代碼,對自己的程序進行驗證內存問題.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章