How do I use and benefit from the GFlags setting Enable heap tagging by DLL?
I know how to activate the setting for a process, but I did not find useful information in the output of !heap -t
in WinDbg. I was expecting some output like this:
0:000> !heap -t
Index Address Allocated by
1: 005c0000 MyDll.dll
2: 006b0000 AnotherDll.dll
so that I can identify which heap was created by which DLL and then e.g. identify the source of a memory leak.
Is this a misunderstanding of the term "heap tagging by DLL" or do I need some more commands to get to the desired result?
My research so far:
- I googled for a tutorial on this topic, but I couldn't find a detailed description
- I read WinDbg's
.hh !heap
but it's not explained there in detail as well. Tag is only used in!heap -b
again a very late answer
to benefit from HeapTagging
you need to create a tag
first in your code.as far as i know (that is upto xp-sp3) there were
no Documented APIS to Create a tag
(I havent mucked with heap since then so i am not aware of latest apis in os > vista Rewrites were done to heap manager so probably many of the
^^^features^^^
that i post below might have been corrected or bettered or bugs removed )in xp-sp3 you can use undocumented
RtlCreateTagHeap
to create a new tag to eitherProcess Heap
orPrivate Heap
and after you create tha tag you need to set the global flag 8000 | 800
and
theoratically all allocs and frees must get tagged
.but
practically only allocations > 512 kB gets tagged
in xp-sp3 with these basic stepsit either is a bug or a feature that limits tagging to allocations and frees > 512 kB
HeapAlloc goes through ZwAllocateVirtualMemory
in case of Allocations > 512 kB in 32 bit processrefer HeapCreate / HeapAlloc Documentation in msdn
and as a
debuging aid
you canpatch ntdll.dll
onthe fly to enable tagging
forall Allocations and frees
.below is a sample code that demonstrates the tagging and how to view it all in windbg
compile using
cl /Zi /analyze /W4 <src> /link /RELEASE
use windbg to execute the app and watch tagging with
!heap * -t
commandthe compiled exe to be run with windbg as below
DEFAULT execution and inspection
**only 50 tags will be visible all of them are > 512 kB Allocations
cdb -c "g;!heap * -t;q" newheaptag.exe | grep Tag**
patching ntdll on system breakpoint should make all tags visible
eb = write byte patch and run the exe on exit inspect heaps with tags cdb -c "eb 7c94b8a1+3 00;g;!heap * -t;q" newheaptag.exe | grep Tag