Warning, /include/absl/debugging/internal/stacktrace_win32-inl.inc is written in an unsupported language. File is not indexed.
0001 // Copyright 2017 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 // https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // Produces a stack trace for Windows. Normally, one could use
0016 // stacktrace_x86-inl.h or stacktrace_x86_64-inl.h -- and indeed, that
0017 // should work for binaries compiled using MSVC in "debug" mode.
0018 // However, in "release" mode, Windows uses frame-pointer
0019 // optimization, which makes getting a stack trace very difficult.
0020 //
0021 // There are several approaches one can take. One is to use Windows
0022 // intrinsics like StackWalk64. These can work, but have restrictions
0023 // on how successful they can be. Another attempt is to write a
0024 // version of stacktrace_x86-inl.h that has heuristic support for
0025 // dealing with FPO, similar to what WinDbg does (see
0026 // http://www.nynaeve.net/?p=97). There are (non-working) examples of
0027 // these approaches, complete with TODOs, in stacktrace_win32-inl.h#1
0028 //
0029 // The solution we've ended up doing is to call the undocumented
0030 // windows function RtlCaptureStackBackTrace, which probably doesn't
0031 // work with FPO but at least is fast, and doesn't require a symbol
0032 // server.
0033 //
0034 // This code is inspired by a patch from David Vitek:
0035 // https://code.google.com/p/google-perftools/issues/detail?id=83
0036
0037 #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_WIN32_INL_H_
0038 #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_WIN32_INL_H_
0039
0040 #include <windows.h> // for GetProcAddress and GetModuleHandle
0041 #include <cassert>
0042
0043 typedef USHORT NTAPI RtlCaptureStackBackTrace_Function(
0044 IN ULONG frames_to_skip,
0045 IN ULONG frames_to_capture,
0046 OUT PVOID *backtrace,
0047 OUT PULONG backtrace_hash);
0048
0049 // It is not possible to load RtlCaptureStackBackTrace at static init time in
0050 // UWP. CaptureStackBackTrace is the public version of RtlCaptureStackBackTrace
0051 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \
0052 !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
0053 static RtlCaptureStackBackTrace_Function* const RtlCaptureStackBackTrace_fn =
0054 &::CaptureStackBackTrace;
0055 #else
0056 // Load the function we need at static init time, where we don't have
0057 // to worry about someone else holding the loader's lock.
0058 static RtlCaptureStackBackTrace_Function* const RtlCaptureStackBackTrace_fn =
0059 (RtlCaptureStackBackTrace_Function*)GetProcAddress(
0060 GetModuleHandleA("ntdll.dll"), "RtlCaptureStackBackTrace");
0061 #endif // WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP
0062
0063 template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
0064 static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
0065 const void*, int* min_dropped_frames) {
0066 USHORT n = 0;
0067 if (!RtlCaptureStackBackTrace_fn || skip_count < 0 || max_depth < 0) {
0068 // can't get a stacktrace with no function/invalid args
0069 } else {
0070 n = RtlCaptureStackBackTrace_fn(static_cast<ULONG>(skip_count) + 2,
0071 static_cast<ULONG>(max_depth), result, 0);
0072 }
0073 if (IS_STACK_FRAMES) {
0074 // No implementation for finding out the stack frame sizes yet.
0075 memset(sizes, 0, sizeof(*sizes) * n);
0076 }
0077 if (min_dropped_frames != nullptr) {
0078 // Not implemented.
0079 *min_dropped_frames = 0;
0080 }
0081 return n;
0082 }
0083
0084 namespace absl {
0085 ABSL_NAMESPACE_BEGIN
0086 namespace debugging_internal {
0087 bool StackTraceWorksForTest() {
0088 return false;
0089 }
0090 } // namespace debugging_internal
0091 ABSL_NAMESPACE_END
0092 } // namespace absl
0093
0094 #endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_WIN32_INL_H_