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