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.

182 lines
4.7 KiB

  1. /*** asrt.c - Assertion Manager
  2. *
  3. * Microsoft Confidential
  4. * Copyright (C) Microsoft Corporation 1993-1994
  5. * All Rights Reserved.
  6. *
  7. * Author:
  8. * Benjamin W. Slivka
  9. *
  10. * History:
  11. * 10-Aug-1993 bens Initial version
  12. * 11-Aug-1993 bens Lift code from 1988 PSCHAR.EXE
  13. * 12-Aug-1993 bens Improve documentation, move messages to asrt.msg
  14. * 14-Aug-1993 bens Add assertion flags, query calls
  15. *
  16. * Functions available in ASSERT build:
  17. * AssertRegisterFunc - Register assertion failure call back function
  18. * AsrtCheck - Check that parameter is TRUE
  19. * AsrtStruct - Check that pointer points to specified structure
  20. * AssertForce - Force an assertion failure
  21. */
  22. #include "types.h"
  23. #include "asrt.h"
  24. #ifdef ASSERT // Must be after asrt.h!
  25. #include "asrt.msg"
  26. void doFailure(char *pszMsg, char *pszFile, int iLine);
  27. STATIC PFNASSERTFAILURE pfnafClient=NULL; // Assertion call back function
  28. STATIC ASSERTFLAGS asfClient=asfNONE; // Assertion flags
  29. /*** AssertRegisterFunc - Register assertion failure call back function
  30. *
  31. * NOTE: See asrt.h for entry/exit conditions.
  32. */
  33. void AssertRegisterFunc(PFNASSERTFAILURE pfnaf)
  34. {
  35. pfnafClient = pfnaf; // Store for future use
  36. }
  37. /*** AssertGetFunc - Get current assertion failure call back function
  38. *
  39. * NOTE: See asrt.h for entry/exit conditions.
  40. */
  41. PFNASSERTFAILURE AssertGetFunc(void)
  42. {
  43. return pfnafClient;
  44. }
  45. /*** AssertSetFlags - Set special assertion control flags
  46. *
  47. * NOTE: See asrt.h for entry/exit conditions.
  48. */
  49. void AssertSetFlags(ASSERTFLAGS asf)
  50. {
  51. asfClient = asf;
  52. }
  53. /*** AssertGetFlags - Get special assertion control flags
  54. *
  55. * NOTE: See asrt.h for entry/exit conditions.
  56. */
  57. ASSERTFLAGS AssertGetFlags(void)
  58. {
  59. return asfClient;
  60. }
  61. /*** AsrtCheck - Check assertion that argument is TRUE
  62. *
  63. * Entry:
  64. * f - Boolean value to check
  65. * pszFile - name of source file
  66. * iLine - source line number
  67. *
  68. * Exit-Success:
  69. * Returns; f was TRUE
  70. *
  71. * Exit-Failure:
  72. * Calls assertion failure callback function; f was false.
  73. */
  74. void AsrtCheck(BOOL f, char *pszFile, int iLine)
  75. {
  76. if (!f) {
  77. doFailure(pszASRTERR_FALSE,pszFile,iLine); // Inform client
  78. // Client returned, ignore error!
  79. }
  80. }
  81. /*** AsrtStruct - Check assertion that pointer is of correct type
  82. *
  83. * Entry:
  84. * pv - Pointer to structure
  85. * sig - Expected signature
  86. * pszFile - name of source file
  87. * iLine - source line number
  88. *
  89. * Exit-Success:
  90. * Returns; pv != NULL, and pv->sig == sig.
  91. *
  92. * Exit-Failure:
  93. * Calls assertion failure callback function; pv was bad.
  94. */
  95. void AsrtStruct(void *pv, SIGNATURE sig, char *pszFile, int iLine)
  96. {
  97. if (pv == NULL) {
  98. doFailure(pszASRTERR_NULL_POINTER,pszFile,iLine); // Inform client
  99. // Client returned, ignore error!
  100. }
  101. else if (*((PSIGNATURE)pv) != sig) {
  102. (*pfnafClient)(pszASRTERR_SIGNATURE_BAD,pszFile,iLine);// Inform client
  103. // Client returned, ignore error!
  104. }
  105. }
  106. /*** AssertForce - Force an assertion failure
  107. *
  108. * NOTE: See asrt.h for entry/exit conditions.
  109. */
  110. void AssertForce(char *pszMsg, char *pszFile, int iLine)
  111. {
  112. doFailure(pszMsg,pszFile,iLine); // Inform client
  113. // Client returned, ignore error!
  114. }
  115. /*** AssertErrPath - Report an internal error path
  116. *
  117. * NOTE: See asrt.h for entry/exit conditions.
  118. */
  119. void AssertErrPath(char *pszMsg, char *pszFile, int iLine)
  120. {
  121. //** Only assert if we are not skipping error path assertions
  122. if (!(asfClient & asfSKIP_ERROR_PATH_ASSERTS)) {
  123. doFailure(pszMsg,pszFile,iLine); // Inform client
  124. }
  125. // Client returned, ignore error!
  126. }
  127. /*** doFailure - Call registered call back function
  128. *
  129. * Entry:
  130. * pszMsg - Message to display
  131. * pszFile - Name of source file
  132. * iLine - Source line number
  133. *
  134. * Exit-Success:
  135. * Returns; client wanted to ignore assertion.
  136. *
  137. * Exit-Failure:
  138. * Does not return.
  139. */
  140. void doFailure(char *pszMsg, char *pszFile, int iLine)
  141. {
  142. if (pfnafClient == NULL) {
  143. //** Call back not registered!
  144. //
  145. // We don't have any output mechanism of our own, since we
  146. // are platform-independent. So, just spin in a loop and
  147. // hope the developer can break in with a debugger to see
  148. // what is wrong!
  149. for (;;)
  150. ;
  151. }
  152. else { //** Call back registered
  153. (*pfnafClient)(pszMsg,pszFile,iLine); // Inform client
  154. }
  155. }
  156. #endif // !ASSERT