You may be happy (or sad) to learn that NT already implements a good
deal of this for you. The user-mode heaps have excellent debug support,
although you must use a user-mode debugger that understands how to
interact with the heaps in debug mode. Combined, they can report on the
number and length of each heap block allocated, the thread stack capture
when the block was allocated (if enabled), the block tag (if enabled),
and a few other features. Tools like DH.EXE, although crude, can be
used to detect heap leaks. (You dump the heap at process start up, then
at process exit. There are tools for finding the difference (i.e.
leaks).)
A lot of the tools for debugging the heap are unfortunately not
documented very well, and many have very terse interfaces. However, all
of the technology is fairly mature – it’s been used for a very long
time internally at Microsoft, and has been a real boon. The heaps are
automatically put in debug mode if you start the process under a
debugger. Querying the heaps requires using Microsoft’s user-mode
debuggers (NTSD, CDB). Find and download the most recent version of the
WinDbg debugger (3.x – not to be confused with that horrid excuse for a
debugger, WinDbg 1.x). WinDbg is (these days) a set of fairly powerful,
mature user- and kernel-mode debuggers. And, they’re free.
For #3, you can do what you describe, or you can use data breakpoints in
WinDbg or VS .Net. Data breakpoints do exactly what you describe,
except I believe they are used to monitor access to a specific location
in memory (usually with machine-word granularity (i.e. 32-bit or
64-bit)). This obviously won’t cover the entire block you want to
cover, so you may still want to do it your way. And yes, doing what you
are describing (beating up on the VM protections) will be very, very,
very slow. The exception handling system is built for robustness and
widely-scoped reporting, not speed.
The debugger can be downloaded at
http://www.microsoft.com/whdc/ddk/debugging/ . Don’t let the “ddk” in
the URL mislead you – it’s a full user-mode debugger as well. And it
actually has documentation these days – look for the !heap command,
which will give you an obscene amount of information about the process
heaps.
– arlie
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of gameplugin
Sent: Friday, December 19, 2003 10:13 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] some problems about monitor memory.
hello, everyone.
I meet some problems about monitor the memory owned by a specific
process.
1.when the specific process runing, it asks for how many memorys?
2.when the specific process exits , it generates memory leaks?
3.i want to know all operations(read/write, allocate/free) on a block
of memory owned by the specific process.
for problem 1, i hook all the memory allocate/free
functions(heapalloc,virtualalloc,…).
for problem 2, when i hook all memory allocate/free functions, i use
some variables store how many times the functions called, when the
process exits, i check whether the allocate counts is matching the free
counts, if not maybe has memory leak.
for problem 3, i use the VirtualProtect function modify the protection
of the block of memory that i want to monitor to NO ACCESS. then I catch
the EXCEPTION_ACCESS_VIOLATION excption, if the adress occurs in the
range of this block of memory, i do some records,and restore the old
protection of this block of memory,then do the normal operations and set
single step flag, when i handle the EXCEPTION_SINGLE_STEP, I modify the
protection of the block of memory to NO ACCESS again. but this method
has very low performance,when it have some operatsons on the block of
memory, it conitues EXCEPTION_ACCESS_VIOLATION and excutes by single
step. the usage of CPU is 100%. but i don’t know the other methed that
can know the read/write status of memory.
it’s all my problems , Can somebody help me?
If you know where i was wrong , pls points out.if you have any other
good methods, pls tell me also,thanks! thanks! thanks!!!
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as: xxxxx@sublinear.org To
unsubscribe send a blank email to xxxxx@lists.osr.com