15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2008, Google Inc.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modification, are permitted provided that the following conditions are
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// met:
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions of source code must retain the above copyright
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notice, this list of conditions and the following disclaimer.
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions in binary form must reproduce the above
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the documentation and/or other materials provided with the
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// distribution.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contributors may be used to endorse or promote products derived from
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this software without specific prior written permission.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ---
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Produces a stack trace for Windows.  Normally, one could use
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// stacktrace_x86-inl.h or stacktrace_x86_64-inl.h -- and indeed, that
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// should work for binaries compiled using MSVC in "debug" mode.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// However, in "release" mode, Windows uses frame-pointer
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// optimization, which makes getting a stack trace very difficult.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// There are several approaches one can take.  One is to use Windows
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// intrinsics like StackWalk64.  These can work, but have restrictions
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// on how successful they can be.  Another attempt is to write a
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// version of stacktrace_x86-inl.h that has heuristic support for
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// dealing with FPO, similar to what WinDbg does (see
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// http://www.nynaeve.net/?p=97).
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The solution we've ended up doing is to call the undocumented
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// windows function RtlCaptureStackBackTrace, which probably doesn't
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// work with FPO but at least is fast, and doesn't require a symbol
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// server.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This code is inspired by a patch from David Vitek:
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   http://code.google.com/p/gperftools/issues/detail?id=83
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_STACKTRACE_WIN32_INL_H_
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_STACKTRACE_WIN32_INL_H_
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note: this file is included into stacktrace.cc more than once.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Anything that should only be defined once should be here:
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "config.h"
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h>    // for GetProcAddress and GetModuleHandle
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <assert.h>
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef USHORT NTAPI RtlCaptureStackBackTrace_Function(
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IN ULONG frames_to_skip,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IN ULONG frames_to_capture,
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OUT PVOID *backtrace,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OUT PULONG backtrace_hash);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Load the function we need at static init time, where we don't have
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to worry about someone else holding the loader's lock.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static RtlCaptureStackBackTrace_Function* const RtlCaptureStackBackTrace_fn =
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   (RtlCaptureStackBackTrace_Function*)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlCaptureStackBackTrace");
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PERFTOOLS_DLL_DECL int GetStackTrace(void** result, int max_depth,
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     int skip_count) {
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!RtlCaptureStackBackTrace_fn) {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(csilvers): should we log an error here?
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return 0;     // can't find a stacktrace with no function to call
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return (int)RtlCaptureStackBackTrace_fn(skip_count + 2, max_depth,
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          result, 0);
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PERFTOOLS_DLL_DECL int GetStackFrames(void** /* pcs */,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      int* /* sizes */,
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      int /* max_depth */,
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      int /* skip_count */) {
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  assert(0 == "Not yet implemented");
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return 0;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // BASE_STACKTRACE_WIN32_INL_H_
92