| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 #include "chrome/chrome_watcher/wait_chain_util_win.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Helper deleter to hold a HWCT into a unique_ptr. |
| 15 struct WaitChainSessionDeleter { |
| 16 using pointer = HWCT; |
| 17 void operator()(HWCT session_handle) { |
| 18 ::CloseThreadWaitChainSession(session_handle); |
| 19 } |
| 20 }; |
| 21 using ScopedWaitChainSessionHandle = |
| 22 std::unique_ptr<HWCT, WaitChainSessionDeleter>; |
| 23 |
| 24 const wchar_t* WctObjectTypeToString(WCT_OBJECT_TYPE type) { |
| 25 switch (type) { |
| 26 case WctCriticalSectionType: |
| 27 return L"CriticalSection"; |
| 28 case WctSendMessageType: |
| 29 return L"SendMessage"; |
| 30 case WctMutexType: |
| 31 return L"Mutex"; |
| 32 case WctAlpcType: |
| 33 return L"Alpc"; |
| 34 case WctComType: |
| 35 return L"Com"; |
| 36 case WctThreadWaitType: |
| 37 return L"ThreadWait"; |
| 38 case WctProcessWaitType: |
| 39 return L"ProcessWait"; |
| 40 case WctThreadType: |
| 41 return L"Thread"; |
| 42 case WctComActivationType: |
| 43 return L"ComActivation"; |
| 44 case WctUnknownType: |
| 45 return L"Unknown"; |
| 46 default: |
| 47 NOTREACHED(); |
| 48 return L""; |
| 49 } |
| 50 } |
| 51 |
| 52 const wchar_t* WctObjectStatusToString(WCT_OBJECT_STATUS status) { |
| 53 switch (status) { |
| 54 case WctStatusNoAccess: |
| 55 return L"NoAccess"; |
| 56 case WctStatusRunning: |
| 57 return L"Running"; |
| 58 case WctStatusBlocked: |
| 59 return L"Blocked"; |
| 60 case WctStatusPidOnly: |
| 61 return L"PidOnly"; |
| 62 case WctStatusPidOnlyRpcss: |
| 63 return L"PidOnlyRpcss"; |
| 64 case WctStatusOwned: |
| 65 return L"Owned"; |
| 66 case WctStatusNotOwned: |
| 67 return L"NotOwned"; |
| 68 case WctStatusAbandoned: |
| 69 return L"Abandoned"; |
| 70 case WctStatusUnknown: |
| 71 return L"Unknown"; |
| 72 case WctStatusError: |
| 73 return L"Error"; |
| 74 default: |
| 75 NOTREACHED(); |
| 76 return L""; |
| 77 } |
| 78 } |
| 79 |
| 80 } // namespace |
| 81 |
| 82 bool GetThreadWaitChain(DWORD thread_id, |
| 83 std::vector<WAITCHAIN_NODE_INFO>* wait_chain, |
| 84 bool* is_deadlock) { |
| 85 DCHECK(wait_chain); |
| 86 DCHECK(is_deadlock); |
| 87 |
| 88 // Open a synchronous session. |
| 89 ScopedWaitChainSessionHandle session_handle( |
| 90 ::OpenThreadWaitChainSession(0, nullptr)); |
| 91 if (!session_handle) { |
| 92 PLOG(ERROR) << "Failed to create the Wait Chain session."; |
| 93 return false; |
| 94 } |
| 95 |
| 96 DWORD nb_nodes = WCT_MAX_NODE_COUNT; |
| 97 wait_chain->resize(nb_nodes); |
| 98 BOOL is_cycle; |
| 99 if (!::GetThreadWaitChain(session_handle.get(), NULL, WCTP_GETINFO_ALL_FLAGS, |
| 100 thread_id, &nb_nodes, wait_chain->data(), |
| 101 &is_cycle)) { |
| 102 PLOG(ERROR) << "Failed to get the thread wait chain."; |
| 103 return false; |
| 104 } |
| 105 |
| 106 *is_deadlock = is_cycle ? true : false; |
| 107 wait_chain->resize(nb_nodes); |
| 108 |
| 109 return true; |
| 110 } |
| 111 |
| 112 std::wstring WaitChainNodeToString(const WAITCHAIN_NODE_INFO& node) { |
| 113 if (node.ObjectType == WctThreadType) { |
| 114 return base::StringPrintf(L"Thread #%d with status %ls", |
| 115 node.ThreadObject.ThreadId, |
| 116 WctObjectStatusToString(node.ObjectStatus)); |
| 117 } else { |
| 118 return base::StringPrintf(L"Lock of type %ls with status %ls", |
| 119 WctObjectTypeToString(node.ObjectType), |
| 120 WctObjectStatusToString(node.ObjectStatus)); |
| 121 } |
| 122 } |
| OLD | NEW |