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.

180 lines
5.8 KiB

  1. //-----------------------------------------------------------------------------
  2. //
  3. //
  4. // File: basetrac.h
  5. //
  6. // Description: Defines class designed for tracking call stacks. Designed
  7. // for use in tracking Addref/Release... but can also be used to track
  8. // any user
  9. //
  10. // Author: Mike Swafford (MikeSwa)
  11. //
  12. // History:
  13. // 10/28/98 - MikeSwa Created
  14. //
  15. // Copyright (C) 1998 Microsoft Corporation
  16. //
  17. //-----------------------------------------------------------------------------
  18. #ifndef __BASETRAC_H__
  19. #define __BASETRAC_H__
  20. #include "baseobj.h"
  21. #define DEFAULT_CALL_STACKS 10
  22. #define DEFAULT_CALL_STACK_DEPTH 20
  23. #define TRACKING_OBJ_SIG 'jbOT'
  24. #ifndef DEBUG //Retail
  25. //In retail... just map to straight COM AddRef/Release functionality
  26. typedef CBaseObject CBaseTrackingObject;
  27. #else //DEBUG - add support for tracking Addref/Release by default
  28. class CDebugTrackingObject;
  29. typedef CDebugTrackingObject CBaseTrackingObject;
  30. #endif //DEBUG
  31. //---[ eObjectTrackingReasons ]------------------------------------------------
  32. //
  33. //
  34. // Description: An enum that describes some basic tracking reasons. If
  35. // an implementor of a subclass need more reasons, then simply create
  36. // a subclass specific enum that start with the value of
  37. // TRACKING_OBJECT_START_USER_DEFINED.
  38. //
  39. //-----------------------------------------------------------------------------
  40. enum eObjectTrackingReasons
  41. {
  42. TRACKING_OBJECT_UNUSED = 0,
  43. TRACKING_OBJECT_CONSTRUCTOR,
  44. TRACKING_OBJECT_DESTRUCTOR,
  45. TRACKING_OBJECT_ADDREF,
  46. TRACKING_OBJECT_RELEASE,
  47. TRACKING_OBJECT_START_USER_DEFINED,
  48. };
  49. //---[ CCallStackEntry_Base ]--------------------------------------------------
  50. //
  51. //
  52. // Description:
  53. // The base class for store call stack entry information. This class
  54. // is not inteded to be instantiated directly... and exists primarily
  55. // for use by a debugger extension.
  56. // Hungarian: (think CDB and windbg command to get call stacks)
  57. // kbeb, pkbeb
  58. //
  59. //-----------------------------------------------------------------------------
  60. class CCallStackEntry_Base
  61. {
  62. public:
  63. DWORD m_dwCallStackType;
  64. DWORD m_dwCallStackDepth;
  65. DWORD_PTR *m_pdwptrCallers;
  66. CCallStackEntry_Base();
  67. void GetCallers();
  68. };
  69. //---[ CCallStackEntry ]-------------------------------------------------------
  70. //
  71. //
  72. // Description:
  73. // A default subclas of CCallStackEntry_Base. Provides storage for
  74. // a call stack of depth DEFAULT_CALL_STACK_DEPTH.
  75. // Hungarian:
  76. // kbe, pkbe
  77. //
  78. //-----------------------------------------------------------------------------
  79. class CCallStackEntry : public CCallStackEntry_Base
  80. {
  81. protected:
  82. //Storage for call stack data
  83. DWORD_PTR m_rgdwptrCallers[DEFAULT_CALL_STACK_DEPTH];
  84. public:
  85. CCallStackEntry()
  86. {
  87. m_pdwptrCallers = m_rgdwptrCallers;
  88. m_dwCallStackDepth = DEFAULT_CALL_STACK_DEPTH;
  89. };
  90. };
  91. //---[ CDebugTrackingObject_Base ]----------------------------------------------
  92. //
  93. //
  94. // Description:
  95. // A class that provides the neccessary primitives for tracking call
  96. // stacks. Like CCallStackEntry_Base it is designed to be used through
  97. // a subclass that provides storage for the required number call stack
  98. // entries.
  99. //
  100. // To effectively create a subclass... you will need to create a subclass
  101. // That contains (or allocates) the memory required to hold the
  102. // tracking data, and set the following 3 protected member variables:
  103. // m_cCallStackEntries
  104. // m_cbCallStackEntries
  105. // m_pkbebCallStackEntries
  106. // Hungarian:
  107. // tracb, ptracb
  108. //
  109. //-----------------------------------------------------------------------------
  110. class CDebugTrackingObject_Base : public CBaseObject
  111. {
  112. private:
  113. DWORD m_dwSignature;
  114. //A running total of the number of stack entires recorded
  115. //The index of the next call stack entry is defined as:
  116. // m_cCurrentStackEntries % m_cCallStackEntries
  117. DWORD m_cCurrentStackEntries;
  118. protected:
  119. //The following 2 values are stored in memory as a way to have size
  120. //independent implementations... child classes may wish to implement
  121. //a subclass with more (or less) entries... or store more debug
  122. //information. By having an explicit self-decriptive in-memory format,
  123. //we can use a single implementation to handle getting the call stack
  124. //and a single debugger extension can be used to dump all sizes of entries.
  125. DWORD m_cCallStackEntries; //Number of callstacks kept
  126. DWORD m_cbCallStackEntries; //Size of each callstack entry
  127. CCallStackEntry_Base *m_pkbebCallStackEntries;
  128. //Used to internally log TRACKING events
  129. void LogTrackingEvent(DWORD dwTrackingReason);
  130. public:
  131. CDebugTrackingObject_Base();
  132. ~CDebugTrackingObject_Base();
  133. };
  134. //---[ CDebugTrackingObject ]---------------------------------------------------
  135. //
  136. //
  137. // Description:
  138. // The default subclass for CDebugTrackingObject_Base. It provides storage
  139. // for DEFAULT_CALL_STACKS CCallStackEntry objects
  140. // Hungarian:
  141. // trac, ptrac
  142. //
  143. //-----------------------------------------------------------------------------
  144. class CDebugTrackingObject :
  145. public CDebugTrackingObject_Base
  146. {
  147. protected:
  148. CCallStackEntry m_rgkbeCallStackEntriesDefault[DEFAULT_CALL_STACKS];
  149. public:
  150. CDebugTrackingObject::CDebugTrackingObject()
  151. {
  152. m_cbCallStackEntries = sizeof(CCallStackEntry);
  153. m_cCallStackEntries = DEFAULT_CALL_STACKS;
  154. m_pkbebCallStackEntries = m_rgkbeCallStackEntriesDefault;
  155. LogTrackingEvent(TRACKING_OBJECT_CONSTRUCTOR);
  156. };
  157. CDebugTrackingObject::~CDebugTrackingObject()
  158. {
  159. LogTrackingEvent(TRACKING_OBJECT_DESTRUCTOR);
  160. };
  161. ULONG AddRef();
  162. ULONG Release();
  163. };
  164. #endif //__BASETRAC_H__