1 / 43

Memory Leak Detection

Memory Leak Detection. Matt Young Windows Mobile Joint Development Program (JDP) Microsoft. OEM/IHV Supplied. BSP (ARM, SH4, MIPS). OEM Hardware and Standard Drivers. Standard PC Hardware and Drivers. H ardware/ D rivers. Windows XP DDK. D evice B uilding T ools.

elina
Download Presentation

Memory Leak Detection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memory Leak Detection Matt Young Windows Mobile Joint Development Program (JDP) Microsoft

  2. OEM/IHV Supplied BSP(ARM, SH4, MIPS) OEM Hardware and Standard Drivers Standard PC Hardware and Drivers Hardware/Drivers Windows XP DDK Device Building Tools Windows Embedded Studio Platform Builder Data Lightweight Relational EDB SQL Server 2005 Express Edition SQL Server 2005 Mobile Edition SQL Server 2005 Win32 Native Managed Server Side Programming Model MFC 8.0, ATL 8.0 .NET Compact Framework .NET Framework ASP.NET Mobile Controls ASP.NET Windows Media Multimedia DirectX Location Services MapPoint Development Tools Visual Studio 2005 Internet Security and Acceleration Server Communications& Messaging Exchange Server Live Communications Server Speech Server Device Update Agent ManagementTools Software Update Services Image Update Systems Management Server Microsoft Operations Manager

  3. Agenda • Intro • LMemDebug • Application Verifier • CeLog Data

  4. Memory Leaks… • …are deadly • … ruin otherwise good devices • PC model does not apply! • Embedded/Mobile devices run constantly • Devices are perceived differently • Devices have as little memory as possible

  5. Agenda • LMemDebug • Application Verifier • CeLog Data

  6. LMemDebug • Monitors heap APIs • Provided in Platform Builder • Access through ‘Shell’ tool

  7. 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

  8. 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

  9. 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

  10. LMem Output Ptr=0x00030320 Size=212 Count=8 LineNum=3217 File=d:\jameson\private\winceos\osshell\commctrl\toolbar.cpp Stack=0x03F7A3C2 coredll.dll+0x3A3C2 _HeapAllocTrace Stack=0x03F78BB6 coredll.dll+0x38BB6 _LocalAllocTrace Stack=0x03E52130 commctrl.dll+0x32130 ?ToolbarWndProc@@... Stack=0x03F647C0 coredll.dll+0x247C0 _xxx_PerformCallBack4 Stack=0x0A06D378 gwes.exe+0x6D378 ?CallWindowProcWInternal@.. Stack=0x0A035BB8 gwes.exe+0x35BB8 ?SendMessageWithOptions@... Stack=0x0A03ECEA gwes.exe+0x3ECEA ?RootToNull@CWindowManager@ Stack=0x03F5F5D8 coredll.dll+0x1F5D8 _xxx_CreateWindowExW Stack=0x03E49DD4 commctrl.dll+0x29DD4 _CommandBar_Create Stack=0x100114C6 41MemTest.exe+0x114C6 _DoCreateMain

  11. How LMem Works Application LocalAlloc HeapSize HeapAlloc LMEMDebug.DLL CoreDLL Int_HeapAlloc Kernel, FileSys,Device,GWES

  12. How LMem Works Application CoreDLL HeapCreate HeapAlloc HeapAllocTrace HeapReAlloc HeapFree HeapSize HeapDestroy Kernel, FileSys,Device,GWES

  13. 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 HeapDestroy • HeapAlloc HeapAllocTrace • HeapReAlloc HeapFree • HeapSize

  14. Writing Your Own LMem • Helper functions will do the work • Int_HeapCreate Int_HeapDestroy • Int_HeapAlloc Int_HeapAllocTrace • Int_HeapReAlloc Int_HeapFree • Int_HeapSize • Only need to export functions you want • You get the source to LMemDebug! • public\common\oak\drivers\lmemdebug

  15. LMemDebug Improvements • Track fragmentation over time • Track Page usage • Track buffer overruns • Improved UI • Real time reporting to Dev PC • Graphical histograms

  16. 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

  17. Agenda • LMemDebug • Application Verifier • CeLog Data

  18. 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…(Coming soon!)

  19. 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

  20. App Verifier from the PC

  21. App Verifier from the PC • Advantages • Easier to control from PC • Automatically brings data to PC • Disadvantages • Needs ActiveSync or Ethernet connection

  22. App Verifier on the Device

  23. 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

  24. App Verifier with Shell • Application verifier can be run as a ‘Shell’ extension • At the shell prompt • Load the extensions • To ready the shim for the application • Run the application Windows CE>loadext AppVerifSh.dll Windows CE>loadext shim_heap.dll Windows CE>appverif -m pword.exe -s shim_heap.dll

  25. 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

  26. 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

  27. The Shim Engine Application Shim 1 Shim 2 CoreDLL Other Dlls Kernel Registry

  28. 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

  29. 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 (Changing soon!)

  30. Agenda • LMemDebug • Application Verifier • CeLog Data

  31. 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

  32. CELog Zones • CELog records information in many areas

  33. 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

  34. CELog Zone Control • Zones are set in the registry • On the device • On the PC (if attached via KITL) • Using Shell • memtrack command • Programmatically • dwZoneCE set the zones discussed void CeLogSetZones (DWORDdwZoneUser,DWORDdwZoneCE, DWORDdwZoneProcess);

  35. Using CELog • For retail device, 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

  36. 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

  37. 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

  38. 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

  39. Summary • LMemDebug • Great for heap only analysis • Source available • App Verifier • Memory, handle, GDI leak monitor • CELog / Memalyzer • Handy for detailed dumps • Source available

  40. Questions myoung @ microsoft.com

  41. © 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

  42. After The Conference… Build Install Build Join Full-featured trial versions of Windows CE and/or Windows XP Embedded Cool stuff & tell us about it: msdn.microsoft.com/embedded/community Windows Embedded Partner Program:www.mswep.com Develop Install Enter Join Windows Mobile 5.0 Eval Kit including Visual Studio 2005 Beta 2 Mobile2Market Contest and win up to $25000: mobile2marketcontest.com Microsoft Solutions Partner Program:partner.microsoft.com

  43. Tools & Resources Build Develop Websites msdn.microsoft.com/embedded msdn.microsoft.com/mobility Newsgroups microsoft.public.pocketpc.developer smartphone.developer dotnet.framework.compactframework microsoft.public.windowsxp.embedded windowsce.platbuilder windowsce.embedded.vc Blogs blogs.msdn.com/windowsmobilevsdteamnetcfteam blogs.msdn.com/mikehall Tools Windows CE 5.0 Eval KitWindows XP Embedded Eval Kit Windows Mobile 5.0 Eval Kit

More Related