Transcript LMem Output

Memory Leak Detection
Douglas Boling
President
Boling Consulting Inc.
www.bolingconsulting.com
Speaker
Douglas Boling
dbolingmedc @ bolingconsulting.com
Author – “Programming Microsoft
Windows CE 3rd Edition”
Trainer – Classes on
Windows CE App Development
Windows CE OAL Development
Consultant – Work with companies to
help their application and
platform development
efforts
Hardware/Drivers
OEM/IHV Supplied
Programming
Model
Data
Device Building
Tools
BSP
(ARM, SH4, MIPS)
EDB
SQL Server 2005 Mobile Edition
Relational
Native
Server Side
Standard PC
Hardware and Drivers
Windows XP DDK
Windows Embedded
Studio
Platform Builder
Lightweight
Managed
OEM Hardware and
Standard Drivers
SQL Server 2005 Express Edition
SQL Server 2005
Win32
MFC 8.0, ATL 8.0
.NET Compact Framework
ASP.NET Mobile Controls
.NET Framework
ASP.NET
Windows Media
DirectX
Multimedia
Location Services
MapPoint
Development Tools
Visual Studio 2005
Internet Security and Acceleration Server
Exchange Server
Live Communications Server
Speech Server
Communications
& Messaging
Device Update Agent
Management
Tools
Image Update
Software Update Services
Systems Management Server
Microsoft Operations Manager
Agenda
Intro
LMemDebug
Application Verifier
CeLog Data
Memory Leaks…
…are deadly
Embedded/Mobile devices run
constantly
They aren’t treated like a PC
System’s are designed with just
enough memory to do the job
More cost too much
Agenda
LMemDebug
Application Verifier
CeLog Data
LMemDebug
Monitors heap APIs
Provided in Platform Builder
Access through ‘Shell’ tool
Programmatic LMem
Applications can control LMemDebug
Append extra information to allocs
Set flags per allocations
Set flags globally
Applications can tag memory allocs
HeapAllocTrace
Easily tag allocs with file and line number
Using LMemDebug
Use Shell to query memory status
LMem <proc>|* [recent] [hist] [chk] [delta] [info]
[<heap>|*] [breaksize <NN>] [breakcnt <NN>]
proc
: Proc index, name, PID or * (for all processes)
recent : Show list of recent allocations
hist
: Show histogram of alloc size
chk
: Checkpoint all current allocations
delta : Report allocations since checkpoint
info
: Report any additional info stored with
the allocations (stack etc.)
heap
: Heap index, handle or * for all heaps
breaksize <NN> : Break when size is NN
breakcnt <NN> : Break when count is NN
LMem Output
Output of one mem alloc
Show alloc call file and line number
Stack at time of alloc call
Ptr=0x000302C0 Size=32768 Count=15 LineNum=724
File=C:\.. \41MemTest.C
Stack=0x03F7A3C2 coredll.dll+0x3A3C2
Stack=0x03F78BB6 coredll.dll+0x38BB6
Stack=0x1001205C 41MemTest.exe+0x1205C
Stack=0x10011704 41MemTest.exe+0x11704
Stack=0x1001133A 41MemTest.exe+0x1133A
Stack=0x03F6199C coredll.dll+0x2199C
Stack=0x03F61F66 coredll.dll+0x21F66
Stack=0x10011092 41MemTest.exe+0x11092
Stack=0x10013DC4 41MemTest.exe+0x13DC4
Stack=0x03F6B6A0 coredll.dll+0x2B6A0
LMem Output
Ptr=0x00030320 Size=212 Count=8 LineNum=3217
File=d:\jameson\private\winceos\osshell\commctrl\toolbar.cpp
Stack=0x03F7A3C2
Stack=0x03F78BB6
Stack=0x03E52130
Stack=0x03F647C0
Stack=0x0A06D378
Stack=0x0A035BB8
Stack=0x0A03ECEA
Stack=0x03F5F5D8
Stack=0x03E49DD4
Stack=0x100114C6
coredll.dll+0x3A3C2
coredll.dll+0x38BB6
commctrl.dll+0x32130
coredll.dll+0x247C0
gwes.exe+0x6D378
gwes.exe+0x35BB8
gwes.exe+0x3ECEA
coredll.dll+0x1F5D8
commctrl.dll+0x29DD4
41MemTest.exe+0x114C6
_HeapAllocTrace
_LocalAllocTrace
?ToolbarWndProc@@...
_xxx_PerformCallBack4
?CallWindowProcWInternal@..
?SendMessageWithOptions@...
?RootToNull@CWindowManager@
_xxx_CreateWindowExW
_CommandBar_Create
_DoCreateMain
How LMem Works
Application
LocalAlloc
HeapSize
HeapAlloc
CoreDLL
Int_HeapAlloc
Kernel, FileSys,Device,GWES
LMEMDebug.DLL
How LMem Works
Application
CoreDLL
HeapCreate
HeapAlloc
HeapAllocTrace
HeapReAlloc
HeapFree
HeapSize
HeapDestroy
Kernel, FileSys,Device,GWES
Writing your Own LMem
CoreDLL heap API has built in hooks
At App init, coredll looks for
LMemDebug.DLL
Loads the following entry points
HeapCreate
HeapAlloc
HeapReAlloc
HeapSize
HeapDestroy
HeapAllocTrace
HeapFree
Writing Your Own LMem
Helper functions will do the work
Int_HeapCreate
Int_HeapAlloc
Int_HeapReAlloc
Int_HeapSize
Int_HeapDestroy
Int_HeapAllocTrace
Int_HeapFree
Only need to export functions you want
You get the source to LMemDebug!
LMemDebug Improvements
Track fragmentation over time
Track Page usage
Track buffer overruns
Improved UI
Real time reporting to Dev PC
Graphical histograms
LMem Summary
Advantages
Fairly quick
Provides call stack info for allocations
Heap histogram
Disadvantages
Uses debug serial port as output
Monitors all applications
Command line interface
Agenda
LMemDebug
Application Verifier
CeLog Data
Application Verifier
Part of the Windows CE Test Kit
Monitors
Heap allocations
Handle allocations
GDI Resources
More…
If you knew how to write a shim
Using the App Verifier
Can be used many ways
Locally on the device
Using the Platform Builder Shell tool
From a development PC via Ethernet
Output is a log file that is placed on the
device
App Verifier from the PC
App Verifier from the PC
Advantages
Easier to control from PC
Automatically brings data to PC
Disadvantages
Needs ActiveSync or Ethernet connection
App Verifier on the Device
App Verifier on the Device
Advantages
No need for a connection with a PC
Disadvantages
Requires a device U/I
Dialog needs a 800x600 screen
App Verifier with Shell
Application verifier can be run as a
‘Shell’ extension
At the shell prompt
Load the extensions
Windows CE>loadext AppVerifSh.dll
Windows CE>loadext shim_heap.dll
To ready the shim for the application
Windows CE>appverif -m pword.exe -s shim_heap.dll
Run the application
App Verifier with Shell
Advantages
No device U/I required
Can run from
Device
PC using Platform Builder
PC using a serial port
Disadvantages
Not a polished as the other methods
How It Works
Inserts a ‘shim’ between the
application and specific APIs exported
by the operating system
The shim intercepts the heap APIs
Captures the stack on every alloc
Matches every free with an alloc
Reports allocs that don’t have a free
The Shim Engine
Application
Shim 1
CoreDLL
Kernel
Registry
Shim 2
Other Dlls
The Shim Engine
Application Verifier uses the Shim
Engine to monitor calls from the
application to the operating system
The Shim Engine has hooks in the
kernel loader
When dynamic linking occurs, the shim
engine looks in the registry to see if a
shim should be linked instead
App Verifier Summary
Advantages
Monitors on a per process level
Uses .map file info for stack trace
Flexible user interface
Disadvantages
Sometime flaky connectivity
Not open to developer improvement
Agenda
LMemDebug
Application Verifier
CeLog Data
CELog Tool
Generates data for the Kernel Tracker
Implemented as a kernel library
The raw data is in a known format
Other tools use the same data
ReadLog
Memalyzer
CELog Zones
CELog records information in many areas
CELZONE_INTERRUPT
CELZONE_RESCHEDULE
CELZONE_MIGRATE
CELZONE_TLB
CELZONE_DEMANDPAGE
CELZONE_THREAD
CELZONE_PROCESS
CELZONE_PRIORITYINV
CELZONE_CRITSECT
CELZONE_SYNCH
CELZONE_HEAP
CELZONE_VIRTMEM
CELZONE_LOADER
CELZONE_MEMTRACKING
CELZONE_BOOT_TIME
CELZONE_KCALL
Events related to interrupts.
Events related to the scheduler.
Events related to migration of threads between processes.
Events related to the translation look-aside buffer (TLB).
Events related to paging.
Events related to threads, except for thread switches.
Events related to processes.
Events related to priority inversion.
Events related to critical sections.
Events related to synchronization.
Events related to heaps.
Events related to virtual memory.
Events related to the loader.
Events related to memory tracking.
Events in the boot process
Events related to KCALLs.
Zones For Memory Tracking
By default, almost all zones are enabled
All but CELZONE_KCALL
This generates a huge amount of data
For memory tracking
Turn off all zones but
CELZONE_HEAP, CELZONE_VIRTMEM,
CELZONE_MEMTRACKING
This dramatically reduces the information being
saved and flushed
This is still a fair amount of information
CELog Zone Control
Zones are set in the registry
On the device
On the PC (if attached via KITL)
Using Shell
memtrack command
Programmatically
void CeLogSetZones (DWORD dwZoneUser, DWORD dwZoneCE,
DWORD dwZoneProcess);
dwZoneCE set the zones discussed
Using CELog
Copy the following files to the device
CELog.DLL
CELogFlush.EXE
Start CELogFlush –z 8000 –n <file>
Enable memtracking events to <file>
Filename shouldn’t have spaces
Run app
Then copy file.clg to PC
Memalyzer
This tool is provided in Platform
Builder but not documented
Takes a CELog file as input
Parses out the memory log info
Outputs the results to .txt or .htm
Running Memalyzer
USAGE:
memalyzer.exe [-pid <process handle>, -name <process name>, -fullscan]
[-map <path>] [output_flags] <file name>
Choose one way to specify which process(es) to output:
-pid <handle> - specify process by handle
-name <name>
- specify process by name
-fullscan
- output data for all processes (DEFAULT)
-map <path>
- specify location to read .map files from
Output flags are one or more of the following:
-v
- verbose: print full stack frames (cooked output only)
-nounload
- do not unload modules
-out:raw
- print packets as logged
-out:cooked
- print packets after processing
-out:noerror - suppress errors (such as duplicate loads/allocs)
-out:nomods
- suppress output of module load/unload data
-out:html
- output HTML files instead of plaintext
CELog Summary
Advantages
Kernel level monitoring
Open data collection model
Memalyzer understands .map files
Disadvantages
Logging is globally enabled
The log buffer can be quite large
Logging is a performance hit
Memalyzer isn’t formally supported
Summary
LMemDebug
Great for heap only analysis
Source available
App Verifier
Memory, handle, GDI leak monitor
CELog / Memalyzer
Handy for detailed dumps
Source available
Questions
dbolingmedc @ bolingconsulting.com
© 2005 Microsoft Corporation. All rights reserved.
This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
While at MEDC 2005…
Fill out an evaluation for this session
Randomly selected instant WIN prizes!
Visit the Microsoft Product Pavilion
in the Exhibit Hall
Shorelines B
Use real technology in a lab
Instructor led
Reef E/F & Breakers L
Self-paced
Reef B/C
After The Conference…
Build
Install
Full-featured trial versions of Windows CE
and/or Windows XP Embedded
Build
Cool stuff & tell us about it:
msdn.microsoft.com/embedded/community
Join
Windows Embedded Partner Program:
www.mswep.com
Develop
Install
Windows Mobile 5.0 Eval Kit including
Visual Studio 2005 Beta 2
Enter
Mobile2Market Contest and win up to $25000:
mobile2marketcontest.com
Join
Microsoft Solutions Partner Program:
partner.microsoft.com
Tools & Resources
Build
Develop
msdn.microsoft.com/
embedded
msdn.microsoft.com/
mobility
microsoft.public.
windowsxp.embedded
windowsce.platbuilder
windowsce.embedded.vc
microsoft.public.
pocketpc.developer
smartphone.developer
dotnet.framework.compactframework
Blogs
blogs.msdn.com/
mikehall
blogs.msdn.com/
windowsmobile
vsdteam
netcfteam
Tools
Windows CE 5.0 Eval Kit
Windows XP Embedded Eval Kit
Windows Mobile 5.0 Eval Kit
Websites
Newsgroups