Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame^] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | // Copyright (c) 2008, Google Inc. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | // |
| 31 | // --- |
| 32 | // Produces a stack trace for Windows. Normally, one could use |
| 33 | // stacktrace_x86-inl.h or stacktrace_x86_64-inl.h -- and indeed, that |
| 34 | // should work for binaries compiled using MSVC in "debug" mode. |
| 35 | // However, in "release" mode, Windows uses frame-pointer |
| 36 | // optimization, which makes getting a stack trace very difficult. |
| 37 | // |
| 38 | // There are several approaches one can take. One is to use Windows |
| 39 | // intrinsics like StackWalk64. These can work, but have restrictions |
| 40 | // on how successful they can be. Another attempt is to write a |
| 41 | // version of stacktrace_x86-inl.h that has heuristic support for |
| 42 | // dealing with FPO, similar to what WinDbg does (see |
| 43 | // http://www.nynaeve.net/?p=97). |
| 44 | // |
| 45 | // The solution we've ended up doing is to call the undocumented |
| 46 | // windows function RtlCaptureStackBackTrace, which probably doesn't |
| 47 | // work with FPO but at least is fast, and doesn't require a symbol |
| 48 | // server. |
| 49 | // |
| 50 | // This code is inspired by a patch from David Vitek: |
| 51 | // http://code.google.com/p/gperftools/issues/detail?id=83 |
| 52 | |
| 53 | #ifndef BASE_STACKTRACE_WIN32_INL_H_ |
| 54 | #define BASE_STACKTRACE_WIN32_INL_H_ |
| 55 | // Note: this file is included into stacktrace.cc more than once. |
| 56 | // Anything that should only be defined once should be here: |
| 57 | |
| 58 | #include "config.h" |
| 59 | #include <windows.h> // for GetProcAddress and GetModuleHandle |
| 60 | #include <assert.h> |
| 61 | |
| 62 | typedef USHORT NTAPI RtlCaptureStackBackTrace_Function( |
| 63 | IN ULONG frames_to_skip, |
| 64 | IN ULONG frames_to_capture, |
| 65 | OUT PVOID *backtrace, |
| 66 | OUT PULONG backtrace_hash); |
| 67 | |
| 68 | // Load the function we need at static init time, where we don't have |
| 69 | // to worry about someone else holding the loader's lock. |
| 70 | static RtlCaptureStackBackTrace_Function* const RtlCaptureStackBackTrace_fn = |
| 71 | (RtlCaptureStackBackTrace_Function*) |
| 72 | GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlCaptureStackBackTrace"); |
| 73 | |
| 74 | static int GetStackTrace_win32(void** result, int max_depth, |
| 75 | int skip_count) { |
| 76 | if (!RtlCaptureStackBackTrace_fn) { |
| 77 | // TODO(csilvers): should we log an error here? |
| 78 | return 0; // can't find a stacktrace with no function to call |
| 79 | } |
| 80 | return (int)RtlCaptureStackBackTrace_fn(skip_count + 3, max_depth, |
| 81 | result, 0); |
| 82 | } |
| 83 | |
| 84 | static int not_implemented(void) { |
| 85 | assert(0 == "Not yet implemented"); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int GetStackFrames_win32(void** /* pcs */, |
| 90 | int* /* sizes */, |
| 91 | int /* max_depth */, |
| 92 | int /* skip_count */) { |
| 93 | return not_implemented(); |
| 94 | } |
| 95 | |
| 96 | static int GetStackFramesWithContext_win32(void** result, int* sizes, int max_depth, |
| 97 | int skip_count, const void *uc) { |
| 98 | return not_implemented(); |
| 99 | } |
| 100 | |
| 101 | static int GetStackTraceWithContext_win32(void** result, int max_depth, |
| 102 | int skip_count, const void *uc) { |
| 103 | return not_implemented(); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | #endif // BASE_STACKTRACE_WIN32_INL_H_ |