tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

stacktrace_win32-inl.inc (4054B)


      1 // Copyright 2017 The Abseil Authors.
      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 //      https://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 // Produces a stack trace for Windows.  Normally, one could use
     16 // stacktrace_x86-inl.h or stacktrace_x86_64-inl.h -- and indeed, that
     17 // should work for binaries compiled using MSVC in "debug" mode.
     18 // However, in "release" mode, Windows uses frame-pointer
     19 // optimization, which makes getting a stack trace very difficult.
     20 //
     21 // There are several approaches one can take.  One is to use Windows
     22 // intrinsics like StackWalk64.  These can work, but have restrictions
     23 // on how successful they can be.  Another attempt is to write a
     24 // version of stacktrace_x86-inl.h that has heuristic support for
     25 // dealing with FPO, similar to what WinDbg does (see
     26 // http://www.nynaeve.net/?p=97).  There are (non-working) examples of
     27 // these approaches, complete with TODOs, in stacktrace_win32-inl.h#1
     28 //
     29 // The solution we've ended up doing is to call the undocumented
     30 // windows function RtlCaptureStackBackTrace, which probably doesn't
     31 // work with FPO but at least is fast, and doesn't require a symbol
     32 // server.
     33 //
     34 // This code is inspired by a patch from David Vitek:
     35 //   https://code.google.com/p/google-perftools/issues/detail?id=83
     36 
     37 #ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_WIN32_INL_H_
     38 #define ABSL_DEBUGGING_INTERNAL_STACKTRACE_WIN32_INL_H_
     39 
     40 #include <windows.h>    // for GetProcAddress and GetModuleHandle
     41 #include <cassert>
     42 
     43 typedef USHORT NTAPI RtlCaptureStackBackTrace_Function(
     44    IN ULONG frames_to_skip,
     45    IN ULONG frames_to_capture,
     46    OUT PVOID *backtrace,
     47    OUT PULONG backtrace_hash);
     48 
     49 // It is not possible to load RtlCaptureStackBackTrace at static init time in
     50 // UWP. CaptureStackBackTrace is the public version of RtlCaptureStackBackTrace
     51 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \
     52    !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
     53 static RtlCaptureStackBackTrace_Function* const RtlCaptureStackBackTrace_fn =
     54    &::CaptureStackBackTrace;
     55 #else
     56 // Load the function we need at static init time, where we don't have
     57 // to worry about someone else holding the loader's lock.
     58 static RtlCaptureStackBackTrace_Function* const RtlCaptureStackBackTrace_fn =
     59    (RtlCaptureStackBackTrace_Function*)GetProcAddress(
     60        GetModuleHandleA("ntdll.dll"), "RtlCaptureStackBackTrace");
     61 #endif  // WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP
     62 
     63 template <bool IS_STACK_FRAMES, bool IS_WITH_CONTEXT>
     64 static int UnwindImpl(void** result, uintptr_t* frames, int* sizes,
     65                      int max_depth, int skip_count, const void*,
     66                      int* min_dropped_frames) {
     67  USHORT n = 0;
     68  if (!RtlCaptureStackBackTrace_fn || skip_count < 0 || max_depth < 0) {
     69    // can't get a stacktrace with no function/invalid args
     70  } else {
     71    n = RtlCaptureStackBackTrace_fn(static_cast<ULONG>(skip_count) + 2,
     72                                    static_cast<ULONG>(max_depth), result, 0);
     73  }
     74  if (IS_STACK_FRAMES) {
     75    // No implementation for finding out the stack frames yet.
     76    if (frames != nullptr) {
     77      memset(frames, 0, sizeof(*frames) * n);
     78    }
     79    if (sizes != nullptr) {
     80      memset(sizes, 0, sizeof(*sizes) * n);
     81    }
     82  }
     83  if (min_dropped_frames != nullptr) {
     84    // Not implemented.
     85    *min_dropped_frames = 0;
     86  }
     87  return n;
     88 }
     89 
     90 namespace absl {
     91 ABSL_NAMESPACE_BEGIN
     92 namespace debugging_internal {
     93 bool StackTraceWorksForTest() {
     94  return false;
     95 }
     96 }  // namespace debugging_internal
     97 ABSL_NAMESPACE_END
     98 }  // namespace absl
     99 
    100 #endif  // ABSL_DEBUGGING_INTERNAL_STACKTRACE_WIN32_INL_H_