Leaked source code of windows server 2003
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.

241 lines
6.7 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1996-1997 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // Barf.h
  7. //
  8. // Abstract:
  9. // Definition of the Basic Artifical Resource Failure classes.
  10. // BARF allows API call failures to be simulated automatically to
  11. // ensure full code test coverage.
  12. //
  13. // Implementation File:
  14. // Barf.cpp
  15. //
  16. // Author:
  17. // David Potter (davidp) April 11, 1997
  18. //
  19. // Revision History:
  20. //
  21. // Notes:
  22. // This file compiles only in _DEBUG mode.
  23. //
  24. // To implement a new BARF type, declare a global instance of CBarf:
  25. // CBarf g_barfMyApi(_T("My API"));
  26. //
  27. // To bring up the BARF dialog:
  28. // DoBarfDialog();
  29. // This brings up a modeless dialog with the BARF settings.
  30. //
  31. // A few functions are provided for special circumstances.
  32. // Usage of these should be fairly limited:
  33. // BarfAll(void); Top Secret -> NYI.
  34. // EnableBarf(BOOL); Allows you to disable/reenable BARF.
  35. // FailOnNextBarf; Force the next failable call to fail.
  36. //
  37. // NOTE: Your code calls the standard APIs (e.g. LoadIcon) and the
  38. // BARF files do the rest.
  39. //
  40. /////////////////////////////////////////////////////////////////////////////
  41. #ifndef _BARF_H_
  42. #define _BARF_H_
  43. // Only process the rest of this file if BARF is to be implemented in the
  44. // including module.
  45. //#ifndef _NO_BARF_DEFINITIONS_
  46. //#define _USING_BARF_
  47. /////////////////////////////////////////////////////////////////////////////
  48. // Forward Class Declarations
  49. /////////////////////////////////////////////////////////////////////////////
  50. class CBarf;
  51. class CBarfSuspend;
  52. /////////////////////////////////////////////////////////////////////////////
  53. // External Class Declarations
  54. /////////////////////////////////////////////////////////////////////////////
  55. class CTraceTag;
  56. /////////////////////////////////////////////////////////////////////////////
  57. // Type Definitions
  58. /////////////////////////////////////////////////////////////////////////////
  59. #define BARF_REG_SECTION _T("Debug\\BARF")
  60. #define BARF_REG_SECTION_FMT BARF_REG_SECTION _T("\\%s")
  61. /////////////////////////////////////////////////////////////////////////////
  62. // Include Files
  63. /////////////////////////////////////////////////////////////////////////////
  64. /////////////////////////////////////////////////////////////////////////////
  65. //
  66. // CBarf
  67. //
  68. // Purpose:
  69. // Basic Artificial Resource Failure class. Contains the BARF
  70. // information for a class of calls
  71. //
  72. // The constructor initializes a bunch of parameters. CBarfDialog
  73. // (a friend class) adjusts the various flags. The only public API
  74. // is FFail(). This method determines if the next call should generate
  75. // an artificial failure or not.
  76. //
  77. /////////////////////////////////////////////////////////////////////////////
  78. #ifdef _DEBUG
  79. typedef void (*PFNBARFPOSTUPDATE)(void);
  80. class CBarf : public CObject
  81. {
  82. friend class CBarfSuspend;
  83. friend class CBarfDialog;
  84. friend void InitBarf(void);
  85. friend void CleanupBarf(void);
  86. friend void EnableBarf(BOOL);
  87. friend void BarfAll(void);
  88. friend void DoBarfDialog(void);
  89. public:
  90. CBarf(IN LPCTSTR pszName);
  91. protected:
  92. void Init(void);
  93. // Attributes
  94. protected:
  95. LPCTSTR m_pszName;
  96. BOOL m_bDisabled;
  97. BOOL m_bContinuous;
  98. DWORD m_nFail;
  99. DWORD m_nCurrent;
  100. DWORD m_nCurrentSave;
  101. DWORD m_nBarfAll;
  102. public:
  103. LPCTSTR PszName(void) const { return m_pszName; }
  104. BOOL BDisabled(void) const { return m_bDisabled; }
  105. BOOL BContinuous(void) const { return m_bContinuous; }
  106. DWORD NFail(void) const { return m_nFail; }
  107. DWORD NCurrent(void) const { return m_nCurrent; }
  108. DWORD NCurrentSave(void) const { return m_nCurrentSave; }
  109. DWORD NBarfAll(void) const { return m_nBarfAll; }
  110. // Operations
  111. public:
  112. BOOL BFail(void);
  113. // Implementation
  114. public:
  115. static PVOID PvSpecialMem(void) { return s_pvSpecialMem; }
  116. protected:
  117. static CBarf * s_pbarfFirst;
  118. CBarf * m_pbarfNext;
  119. static LONG s_nSuspend;
  120. static BOOL s_bGlobalEnable;
  121. // Routine for use by the BARF dialog so that it can be
  122. // automatically updated with results of the BARF run.
  123. static PFNBARFPOSTUPDATE s_pfnPostUpdate;
  124. static void SetPostUpdateFn(IN PFNBARFPOSTUPDATE pfn) { ASSERT(pfn != NULL); s_pfnPostUpdate = pfn; }
  125. static void ClearPostUpdateFn(void) { ASSERT(s_pfnPostUpdate != NULL); s_pfnPostUpdate = NULL; }
  126. static PFNBARFPOSTUPDATE PfnPostUpdate(void) { return s_pfnPostUpdate; }
  127. // Pointer for use by the memory subsystem so that the BARF
  128. // dialog can be ignored.
  129. static PVOID s_pvSpecialMem;
  130. static void SetSpecialMem(IN PVOID pv) { ASSERT(pv != NULL); s_pvSpecialMem = pv; }
  131. }; //*** class CBarf
  132. #endif // _DEBUG
  133. /////////////////////////////////////////////////////////////////////////////
  134. //
  135. // class CBarfSuspend
  136. //
  137. // Purpose:
  138. // Temporarily suspends BARF counters. This is especially useful
  139. // from within BARF code.
  140. //
  141. // Usage:
  142. // Create an object on the stack. Counting will be
  143. // suspended while the object exist.
  144. //
  145. // For example:
  146. //
  147. // void Foo(void)
  148. // {
  149. // DoFuncA(); // BARF counters are enabled
  150. //
  151. // {
  152. // CBarfSuspend bs;
  153. //
  154. // DoFuncB(); // BARF counters are suspended
  155. // }
  156. //
  157. // DoFuncC(); // BARF counters are enabled again
  158. // }
  159. //
  160. // NOTE: This is mostly for use within the DEBUG subsystem
  161. // to avoid testing the DEBUG code against BARF.
  162. //
  163. /////////////////////////////////////////////////////////////////////////////
  164. #ifdef _DEBUG
  165. class CBarfSuspend
  166. {
  167. private:
  168. static CRITICAL_SECTION s_critsec;
  169. static BOOL s_bCritSecValid;
  170. protected:
  171. static PCRITICAL_SECTION Pcritsec(void) { return &s_critsec; }
  172. static BOOL BCritSecValid(void) { return s_bCritSecValid; }
  173. public:
  174. CBarfSuspend(void);
  175. ~CBarfSuspend(void);
  176. // for initialization only.
  177. static void Init(void);
  178. static void Cleanup(void);
  179. }; //*** class CBarfSuspend
  180. #endif // _DEBUG
  181. /////////////////////////////////////////////////////////////////////////////
  182. // Global Functions and Data
  183. /////////////////////////////////////////////////////////////////////////////
  184. #ifdef _DEBUG
  185. extern BOOL g_bFailOnNextBarf;
  186. void EnableBarf(IN BOOL bEnable);
  187. inline void FailOnNextBarf(void) { g_bFailOnNextBarf = TRUE; }
  188. void InitBarf(void);
  189. void CleanupBarf(void);
  190. extern CTraceTag g_tagBarf;
  191. #else
  192. inline void EnableBarf(IN BOOL bEnable) { }
  193. inline void FailOnNextBarf(void) { }
  194. inline void InitBarf(void) { }
  195. inline void CleanupBarf(void) { }
  196. #endif // _DEBUG
  197. /////////////////////////////////////////////////////////////////////////////
  198. //#endif // _NO_BARF_DEFINITIONS_
  199. #endif // _BARF_H_