Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.2 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. stktrace.c
  5. Abstract:
  6. Implements IISCaptureStackBackTrace().
  7. Author:
  8. Keith Moore (keithmo) 30-Apr-1997
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include <pudebug.h>
  16. #include <stktrace.h>
  17. typedef
  18. USHORT
  19. (NTAPI * PFN_RTL_CAPTURE_STACK_BACK_TRACE)(
  20. IN ULONG FramesToSkip,
  21. IN ULONG FramesToCapture,
  22. OUT PVOID *BackTrace,
  23. OUT PULONG BackTraceHash
  24. );
  25. PFN_RTL_CAPTURE_STACK_BACK_TRACE g_pfnRtlCaptureStackBackTrace = NULL;
  26. USHORT
  27. NTAPI
  28. DummyCaptureStackBackTrace(
  29. IN ULONG FramesToSkip,
  30. IN ULONG FramesToCapture,
  31. OUT PVOID *BackTrace,
  32. OUT PULONG BackTraceHash
  33. )
  34. /*++
  35. Routine Description:
  36. Dummy implementation of RtlCaptureStackBackTrace() for Win9x.
  37. Arguments:
  38. See IISCaptureStackBackTrace() below.
  39. Return Value:
  40. USHORT - Always 0.
  41. --*/
  42. {
  43. return 0;
  44. } // DummyRtlCaptureStackBackTrace
  45. USHORT
  46. NTAPI
  47. IISCaptureStackBackTrace(
  48. IN ULONG FramesToSkip,
  49. IN ULONG FramesToCapture,
  50. OUT PVOID *BackTrace,
  51. OUT PULONG BackTraceHash
  52. )
  53. /*++
  54. Routine Description:
  55. Wrapper around RtlCaptureStackBackTrace(). Attempts to capture the
  56. stack backtrace leading up to the current instruction counter.
  57. Doesn't work very well on RISC platforms, and is often confused on
  58. X86 when FPO is enabled.
  59. Arguments:
  60. FramesToSkip - The number of stack frames to skip before capturing.
  61. FramesToCapture - The number of stack frames to capture.
  62. BackTrace - Receives the captured frames.
  63. BackTraceHash - Some kind of hash thingie.
  64. Return Value:
  65. USHORT - The number of frames captured.
  66. --*/
  67. {
  68. //
  69. // Initialize if necessary.
  70. //
  71. if( g_pfnRtlCaptureStackBackTrace == NULL ) {
  72. HMODULE mod;
  73. PFN_RTL_CAPTURE_STACK_BACK_TRACE proc = NULL;
  74. //
  75. // Note that it is perfectly safe to use GetModuleHandle() here
  76. // rather than LoadLibrary(), for the following reasons:
  77. //
  78. // 1. Under NT, NTDLL.DLL is a "well known" DLL that *never*
  79. // gets detached from the process. It's very special.
  80. //
  81. // 2. Under Win95, NTDLL.DLL doesn't export the
  82. // RtlCaptureStackBackTrace() function, so we will not be
  83. // referencing any routines within the DLL.
  84. //
  85. // Also note that we retrieve the function pointer into a local
  86. // variable, not directly into the global. This prevents a nasty
  87. // race condition that can occur when two threads try to
  88. // initialize g_pfnRtlCaptureStackBackTrace simultaneously.
  89. //
  90. mod = GetModuleHandle( "ntdll.dll" );
  91. if( mod != NULL ) {
  92. proc = (PFN_RTL_CAPTURE_STACK_BACK_TRACE)
  93. GetProcAddress( mod, "RtlCaptureStackBackTrace" );
  94. }
  95. if( proc == NULL ) {
  96. g_pfnRtlCaptureStackBackTrace = &DummyCaptureStackBackTrace;
  97. } else {
  98. g_pfnRtlCaptureStackBackTrace = proc;
  99. }
  100. }
  101. return (g_pfnRtlCaptureStackBackTrace)(
  102. FramesToSkip,
  103. FramesToCapture,
  104. BackTrace,
  105. BackTraceHash
  106. );
  107. } // IISCaptureStackBackTrace