stacktrace_win32-inl.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #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. #include "config.h"
  57. #include <windows.h> // for GetProcAddress and GetModuleHandle
  58. #include <assert.h>
  59. typedef USHORT NTAPI RtlCaptureStackBackTrace_Function(
  60. IN ULONG frames_to_skip,
  61. IN ULONG frames_to_capture,
  62. OUT PVOID *backtrace,
  63. OUT PULONG backtrace_hash);
  64. // Load the function we need at static init time, where we don't have
  65. // to worry about someone else holding the loader's lock.
  66. static RtlCaptureStackBackTrace_Function* const RtlCaptureStackBackTrace_fn =
  67. (RtlCaptureStackBackTrace_Function*)
  68. GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlCaptureStackBackTrace");
  69. static int GetStackTrace_win32(void** result, int max_depth,
  70. int skip_count) {
  71. if (!RtlCaptureStackBackTrace_fn) {
  72. // TODO(csilvers): should we log an error here?
  73. return 0; // can't find a stacktrace with no function to call
  74. }
  75. return (int)RtlCaptureStackBackTrace_fn(skip_count + 3, max_depth,
  76. result, 0);
  77. }
  78. static int not_implemented(void) {
  79. assert(0 == "Not yet implemented");
  80. return 0;
  81. }
  82. static int GetStackFrames_win32(void** /* pcs */,
  83. int* /* sizes */,
  84. int /* max_depth */,
  85. int /* skip_count */) {
  86. return not_implemented();
  87. }
  88. static int GetStackFramesWithContext_win32(void** result, int* sizes, int max_depth,
  89. int skip_count, const void *uc) {
  90. return not_implemented();
  91. }
  92. static int GetStackTraceWithContext_win32(void** result, int max_depth,
  93. int skip_count, const void *uc) {
  94. return not_implemented();
  95. }
  96. #endif // BASE_STACKTRACE_WIN32_INL_H_