OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/win/critical_section_with_debug_info.h" |
| 16 |
| 17 namespace crashpad { |
| 18 |
| 19 namespace { |
| 20 |
| 21 BOOL CrashpadInitializeCriticalSectionEx( |
| 22 CRITICAL_SECTION* critical_section, |
| 23 DWORD spin_count, |
| 24 DWORD flags) { |
| 25 static decltype(InitializeCriticalSectionEx)* initialize_critical_section_ex = |
| 26 reinterpret_cast<decltype(InitializeCriticalSectionEx)*>(GetProcAddress( |
| 27 LoadLibrary(L"kernel32.dll"), "InitializeCriticalSectionEx")); |
| 28 if (!initialize_critical_section_ex) { |
| 29 PLOG(ERROR) << "GetProcAddress"; |
| 30 return false; |
| 31 } |
| 32 bool ret = |
| 33 initialize_critical_section_ex(critical_section, spin_count, flags); |
| 34 if (!ret) { |
| 35 PLOG(ERROR) << "InitializeCriticalSectionEx"; |
| 36 return false; |
| 37 } |
| 38 return true; |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 bool InitializeCriticalSectionWithDebugInfoIfPossible( |
| 44 CRITICAL_SECTION* critical_section) { |
| 45 // On XP and Vista, a plain initialization causes the CRITICAL_SECTION to be |
| 46 // allocated with .DebugInfo. On 8 and above, we can pass an additional flag |
| 47 // to InitializeCriticalSectionEx() to force the .DebugInfo on. Before Win 8, |
| 48 // that flag causes InitializeCriticalSectionEx() to fail. So, for XP, Vista, |
| 49 // and 7 we use InitializeCriticalSection(), and for 8 and above, |
| 50 // InitializeCriticalSectionEx() with the additional flag. |
| 51 // |
| 52 // TODO(scottmg): Try to find a solution for Win 7. It's unclear how to force |
| 53 // it on for Win 7, however the Loader Lock does have .DebugInfo so there may |
| 54 // be a way to do it. The comments in winnt.h imply that perhaps it's passed |
| 55 // to InitializeCriticalSectionAndSpinCount() as the top bits of the spin |
| 56 // count, but that doesn't appear to work. For now, we initialize a valid |
| 57 // CRITICAL_SECTION, but without .DebugInfo. |
| 58 |
| 59 const DWORD version = GetVersion(); |
| 60 const DWORD major_version = LOBYTE(LOWORD(version)); |
| 61 const DWORD minor_version = HIBYTE(LOWORD(version)); |
| 62 const bool win7_or_lower = |
| 63 major_version < 6 || (major_version == 6 && minor_version <= 1); |
| 64 |
| 65 if (win7_or_lower) { |
| 66 InitializeCriticalSection(critical_section); |
| 67 return true; |
| 68 } |
| 69 |
| 70 return CrashpadInitializeCriticalSectionEx( |
| 71 critical_section, 0, RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO); |
| 72 } |
| 73 |
| 74 } // namespace crashpad |
OLD | NEW |