Home | History | Annotate | Download | only in memory_watcher
      1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // MemoryWatcher.
      6 // The MemoryWatcher is a library that can be linked into any
      7 // win32 application.  It will override the default memory allocators
      8 // and track call stacks for any allocations that are made.  It can
      9 // then be used to see what memory is in use.
     10 
     11 #ifndef TOOLS_MEMORY_WATCHER_MEMORY_WATCHER_
     12 #define TOOLS_MEMORY_WATCHER_MEMORY_WATCHER_
     13 
     14 #include <map>
     15 #include <functional>
     16 
     17 #include "base/synchronization/lock.h"
     18 #include "tools/memory_watcher/memory_hook.h"
     19 
     20 class CallStack;
     21 class AllocationStack;
     22 
     23 // The MemoryWatcher installs allocation hooks and monitors
     24 // allocations and frees.
     25 class MemoryWatcher : MemoryObserver {
     26  public:
     27   struct StackTrack {
     28     CallStack* stack;
     29     int count;
     30     int size;
     31   };
     32 
     33   typedef std::map<int32, AllocationStack*, std::less<int32>,
     34                    PrivateHookAllocator<int32> > CallStackMap;
     35   typedef std::map<int32, StackTrack, std::less<int32>,
     36                    PrivateHookAllocator<int32> > CallStackIdMap;
     37   typedef std::basic_string<char, std::char_traits<char>,
     38                             PrivateHookAllocator<char> > PrivateAllocatorString;
     39 
     40   MemoryWatcher();
     41   virtual ~MemoryWatcher();
     42 
     43   // Dump all tracked pointers still in use.
     44   void DumpLeaks();
     45 
     46   // MemoryObserver interface.
     47   virtual void OnTrack(HANDLE heap, int32 id, int32 size);
     48   virtual void OnUntrack(HANDLE heap, int32 id, int32 size);
     49 
     50   // Sets a name that appears in the generated file name.
     51   void SetLogName(char* log_name);
     52 
     53  private:
     54   // Opens the logfile which we create.
     55   void OpenLogFile();
     56 
     57   // Close the logfile.
     58   void CloseLogFile();
     59 
     60   // Hook the memory hooks.
     61   void Hook();
     62 
     63   // Unhooks our memory hooks.
     64   void Unhook();
     65 
     66   // Check to see if this thread is already processing a block, and should not
     67   // recurse.
     68   bool LockedRecursionDetected() const;
     69 
     70   // This is for logging.
     71   FILE* file_;
     72 
     73   bool hooked_;  // True when this class has the memory_hooks hooked.
     74 
     75   // Either 0, or else the threadID for a thread that is actively working on
     76   // a stack track.  Used to avoid recursive tracking.
     77   DWORD active_thread_id_;
     78 
     79   base::Lock block_map_lock_;
     80   // The block_map provides quick lookups based on the allocation
     81   // pointer.  This is important for having fast round trips through
     82   // malloc/free.
     83   CallStackMap *block_map_;
     84 
     85   // The file name for that log.
     86   std::string file_name_;
     87 
     88   // An optional name that appears in the log file name (used to differentiate
     89   // logs).
     90   std::string log_name_;
     91 };
     92 
     93 
     94 
     95 #endif  // TOOLS_MEMORY_WATCHER_MEMORY_WATCHER_
     96