| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/win/scoped_handle.h" | 5 #include "base/win/scoped_handle.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 13 #include "base/win/windows_version.h" | 13 #include "base/win/windows_version.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 struct Info { | 17 struct Info { |
| 18 const void* owner; | 18 const void* owner; |
| 19 const void* pc1; | 19 const void* pc; |
| 20 const void* pc2; | |
| 21 DWORD thread_id; | 20 DWORD thread_id; |
| 22 }; | 21 }; |
| 23 typedef std::map<HANDLE, Info> HandleMap; | 22 typedef std::map<HANDLE, Info> HandleMap; |
| 24 | 23 |
| 25 base::LazyInstance<HandleMap>::Leaky g_handle_map = LAZY_INSTANCE_INITIALIZER; | 24 base::LazyInstance<HandleMap>::Leaky g_handle_map = LAZY_INSTANCE_INITIALIZER; |
| 26 base::LazyInstance<std::set<const void*> >::Leaky g_owner_set = | 25 base::LazyInstance<std::set<const void*> >::Leaky g_owner_set = |
| 27 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
| 28 base::LazyInstance<base::Lock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER; | 27 base::LazyInstance<base::Lock>::Leaky g_lock = LAZY_INSTANCE_INITIALIZER; |
| 29 | 28 |
| 30 } // namespace | 29 } // namespace |
| 31 | 30 |
| 32 namespace base { | 31 namespace base { |
| 33 namespace win { | 32 namespace win { |
| 34 | 33 |
| 35 // Static. | 34 // Static. |
| 36 void VerifierTraits::StartTracking(HANDLE handle, const void* owner, | 35 void VerifierTraits::StartTracking(HANDLE handle, const void* owner, |
| 37 const void* pc1, const void* pc2) { | 36 const void* pc) { |
| 38 if (OSInfo::GetInstance()->version() > VERSION_XP) | 37 if (OSInfo::GetInstance()->version() > VERSION_XP) |
| 39 return; | 38 return; |
| 40 | 39 |
| 41 // Grab the thread id before the lock. | 40 // Grab the thread id before the lock. |
| 42 DWORD thread_id = GetCurrentThreadId(); | 41 DWORD thread_id = GetCurrentThreadId(); |
| 43 | 42 |
| 44 AutoLock lock(g_lock.Get()); | 43 AutoLock lock(g_lock.Get()); |
| 45 | 44 |
| 46 if (handle == INVALID_HANDLE_VALUE) { | 45 if (handle == INVALID_HANDLE_VALUE) { |
| 47 // Cannot track this handle. | 46 // Cannot track this handle. |
| 48 g_owner_set.Get().insert(owner); | 47 g_owner_set.Get().insert(owner); |
| 49 return; | 48 return; |
| 50 } | 49 } |
| 51 | 50 |
| 52 Info handle_info = { owner, pc1, pc2, thread_id }; | 51 Info handle_info = { owner, pc, thread_id }; |
| 53 std::pair<HANDLE, Info> item(handle, handle_info); | 52 std::pair<HANDLE, Info> item(handle, handle_info); |
| 54 std::pair<HandleMap::iterator, bool> result = g_handle_map.Get().insert(item); | 53 std::pair<HandleMap::iterator, bool> result = g_handle_map.Get().insert(item); |
| 55 if (!result.second) { | 54 if (!result.second) { |
| 56 Info other = result.first->second; | 55 Info other = result.first->second; |
| 57 debug::Alias(&other); | 56 debug::Alias(&other); |
| 58 CHECK(false); | 57 CHECK(false); |
| 59 } | 58 } |
| 60 } | 59 } |
| 61 | 60 |
| 62 // Static. | 61 // Static. |
| 63 void VerifierTraits::StopTracking(HANDLE handle, const void* owner, | 62 void VerifierTraits::StopTracking(HANDLE handle, const void* owner, |
| 64 const void* pc1, const void* pc2) { | 63 const void* pc) { |
| 65 if (OSInfo::GetInstance()->version() > VERSION_XP) | 64 if (OSInfo::GetInstance()->version() > VERSION_XP) |
| 66 return; | 65 return; |
| 67 | 66 |
| 68 AutoLock lock(g_lock.Get()); | 67 AutoLock lock(g_lock.Get()); |
| 69 HandleMap::iterator i = g_handle_map.Get().find(handle); | 68 HandleMap::iterator i = g_handle_map.Get().find(handle); |
| 70 if (i == g_handle_map.Get().end()) { | 69 if (i == g_handle_map.Get().end()) { |
| 71 std::set<const void*>::iterator j = g_owner_set.Get().find(owner); | 70 std::set<const void*>::iterator j = g_owner_set.Get().find(owner); |
| 72 if (j != g_owner_set.Get().end()) { | 71 if (j != g_owner_set.Get().end()) { |
| 73 g_owner_set.Get().erase(j); | 72 g_owner_set.Get().erase(j); |
| 74 return; | 73 return; |
| 75 } | 74 } |
| 76 CHECK(false); | 75 CHECK(false); |
| 77 } | 76 } |
| 78 | 77 |
| 79 Info other = i->second; | 78 Info other = i->second; |
| 80 if (other.owner != owner) { | 79 if (other.owner != owner) { |
| 81 debug::Alias(&other); | 80 debug::Alias(&other); |
| 82 CHECK(false); | 81 CHECK(false); |
| 83 } | 82 } |
| 84 | 83 |
| 85 g_handle_map.Get().erase(i); | 84 g_handle_map.Get().erase(i); |
| 86 } | 85 } |
| 87 | 86 |
| 88 } // namespace win | 87 } // namespace win |
| 89 } // namespace base | 88 } // namespace base |
| OLD | NEW |