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