Kernel patch for finding memory leaks

Andreas Degert (ad@papyrus.hamburg.com)
01 Apr 1997 15:32:09 +0200


Hello,

I've made some kernel patches to get a list (filename/line number) of
calls to kmalloc() / __get_free_pages() that haven't yet returned the
memory. I'd like to know if people find this useful (right now it's a
horrible hack, but it worked for me, and if there's interest, i would
work a bit on it).

how it works:

Calls of __get_free_pages() and kmalloc() are redirected via #define
to __get_free_pages_dbg() and kmalloc_dbg(). The *_dbg functions get
the additional parameters __FILE__ and __LINE__ and call functions in
page_monitor.c (new source file from this patch), which do the book
keeping.

Remarks:

- right now it's coded quite statically, needs a lot of space, and is
slow. Be careful with more than 256 MB memory (if did the
calculation right...).
- If you use modules, you should also use very recent modutils,
because i've set the sublevel in the Makefile to 29-mon, or else
delete the non-numeric tail.
- You'll find the list if calls in /proc/page-monitor. You'll see the
filename and line number where kmalloc() etc. was called (you can't
see which function was called, you have to look into the linux
sources for that), and the count of not-yet-returned kmalloc-blocks
resp. pages. References to call locations where all memory has been
returned will be listed nevertheless, but with count zero.

I'd like to know if you think it's usable, and what i should change
(apart from coding technique in page_monitor.c ;-).

ciao

Andreas

------------------------------------------------------------
begin 664 mon-diff-2.0.29.gz
M'XL(")P40#,"`VUO;BUD:69F+3(N,"XR.0"]&FM3VTCRL_TK.FQ"9"P9RS8&
M3*#B@)/UAE=AV-NZD-(*6\9:RY)+DB'<'O?;KWMF-!K)LL-=MI9*D&:ZIZ>G
MWSUBY(['8"S`,$)GN`@C]\$!S_47W[;/[*DS=CTQ-!JUNIPJ&X:1PRH-%CY\
M=.[`;(.YV]G9[S3VP=S?WRU7J]4"$@S_S`ZA68=&H]/8ZS3J'/_]>S!,O0U5
M^O7^?1E^[5T-^A?G<`B-,EQVKX]_/NW]VCO%<;UL#&X^)*/&?KF:&1JSP"]#
M&;I7QS_CA-O<:^.0Z#>:=7T7JOQ!>Y3!]8?>8N1L\V,]."B*P*]-.E!+CPVE
M]\YP$L#M3R.<\!VXN1Y85[W37G?0@]N-UYI@M5)[K:6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