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.

115 lines
4.4 KiB

  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. #pragma once
  5. #ifdef ASSERT_WITH_STACK
  6. #ifndef _WIN64
  7. #include <windows.h>
  8. #include <imagehlp.h>
  9. #include <crtdbg.h>
  10. //
  11. //--- Constants ---------------------------------------------------------------
  12. //
  13. const UINT cchMaxAssertModuleLen = 12;
  14. const UINT cchMaxAssertSymbolLen = 257;
  15. const UINT cfrMaxAssertStackLevels = 20;
  16. const UINT cchMaxAssertExprLen = 257;
  17. const UINT cchMaxAssertStackLevelStringLen =
  18. (2 * 8) + cchMaxAssertModuleLen + cchMaxAssertSymbolLen + 12;
  19. // 2 addresses of at most 8 char, module, symbol, and the extra chars:
  20. // 0x<address>: <module>! <symbol> + 0x<offset>\n
  21. //
  22. //--- Prototypes --------------------------------------------------------------
  23. //
  24. /****************************************************************************
  25. * MagicDeinit *
  26. *-------------*
  27. * Description:
  28. * Cleans up for the symbol loading code. Should be called before
  29. * exiting in order to free the dynamically loaded imagehlp.dll
  30. ****************************************************************************/
  31. void MagicDeinit(void);
  32. /****************************************************************************
  33. * GetStringFromStackLevels *
  34. *--------------------------*
  35. * Description:
  36. * Retrieves a string from the stack frame. If more than one frame, they
  37. * are separated by newlines. Each fram appears in this format:
  38. *
  39. * 0x<address>: <module>! <symbol> + 0x<offset>
  40. ****************************************************************************/
  41. void GetStringFromStackLevels(UINT ifrStart, UINT cfrTotal, CHAR *pszString);
  42. /****************************************************************************
  43. * GetAddrFromStackLevel *
  44. *-----------------------*
  45. * Description:
  46. * Retrieves the address of the next instruction to be executed on a
  47. * particular stack frame.
  48. *
  49. * Return:
  50. * The address as a DWORD.
  51. ****************************************************************************/
  52. DWORD GetAddrFromStackLevel(UINT ifrStart);
  53. /****************************************************************************
  54. * GetStringFromAddr *
  55. *-------------------*
  56. * Description:
  57. * Builds a string from an address in the format:
  58. *
  59. * 0x<address>: <module>! <symbol> + 0x<offset>
  60. ****************************************************************************/
  61. void GetStringFromAddr(DWORD dwAddr, TCHAR *szString);
  62. //
  63. //--- _ASSERTE replacement ----------------------------------------------------
  64. //
  65. /****************************************************************************
  66. * _ASSERTE *
  67. *----------*
  68. * Description:
  69. * A replacement for the CRT runtime's version of _ASSERTE that also
  70. * includes stack information in the assert.
  71. ****************************************************************************/
  72. #undef _ASSERTE
  73. #define _ASSERTE(expr) \
  74. do \
  75. { \
  76. if (!(expr)) \
  77. { \
  78. char *pszExprWithStack = \
  79. (char*)_alloca( \
  80. cchMaxAssertStackLevelStringLen * \
  81. cfrMaxAssertStackLevels + cchMaxAssertExprLen + 50 + 1); \
  82. strcpy(pszExprWithStack, #expr); \
  83. strcat(pszExprWithStack, "\n\n"); \
  84. GetStringFromStackLevels(0, 10, pszExprWithStack + strlen(pszExprWithStack)); \
  85. strcat(pszExprWithStack, "\n"); \
  86. SYSTEMTIME sysTime; \
  87. GetLocalTime(&sysTime); \
  88. CHAR pszDateTime[50]; \
  89. sprintf(pszDateTime, "\n%d.%d.%d %02d:%02d:%02d", \
  90. sysTime.wMonth,sysTime.wDay,sysTime.wYear, \
  91. sysTime.wHour,sysTime.wMinute,sysTime.wSecond); \
  92. strcat(pszExprWithStack, pszDateTime); \
  93. if (1 == _CrtDbgReport(_CRT_ASSERT, \
  94. __FILE__, \
  95. __LINE__, \
  96. NULL, pszExprWithStack)) \
  97. _CrtDbgBreak(); \
  98. } \
  99. } while (0) \
  100. #endif // _WIN64
  101. #endif // ASSERT_WITH_STACK