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.

119 lines
3.6 KiB

  1. /***
  2. *unhandld.cxx - Wrapper to call terminate() when an exception goes unhandled.
  3. *
  4. * Copyright (c) 1993-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Wrapper to call terminate() when an exception goes unhandled.
  8. *
  9. *Description:
  10. * This module makes use of the Win32 API SetUnhandledExceptionFilter.
  11. * This assumes the call to main() is wrapped with
  12. * __try { ... }
  13. * __except(UnhandledExceptionFilter(_exception_info())) { ... }
  14. *
  15. *Revision History:
  16. * 10-04-93 BS Module created
  17. * 10-17-94 BWT Disable code for PPC.
  18. * 02-09-95 JWM Mac merge.
  19. * 02-16-95 JWM Added __CxxRestoreUnhandledExceptionFilter().
  20. * 11-19-96 GJF Install handler during C initializers, remove it
  21. * during C termination. Also, reformatted the source a
  22. * bit for readability.
  23. * 04-28-99 PML Wrap __declspec(allocate()) in _CRTALLOC macro.
  24. * 05-17-99 PML Remove all Macintosh support.
  25. * 03-27-01 PML .CRT$XI routines must now return 0 or _RT_* fatal
  26. * error code (vs7#231220)
  27. *
  28. ****/
  29. #include <windows.h>
  30. #include <ehdata.h>
  31. #include <eh.h>
  32. #include <ehhooks.h>
  33. #include <ehassert.h>
  34. #include <internal.h>
  35. #include <stdlib.h>
  36. #pragma hdrstop
  37. #include <sect_attribs.h>
  38. int __cdecl __CxxSetUnhandledExceptionFilter(void);
  39. void __cdecl __CxxRestoreUnhandledExceptionFilter(void);
  40. #pragma data_seg(".CRT$XIY")
  41. _CRTALLOC(".CRT$XIY") static _PIFV pinit = &__CxxSetUnhandledExceptionFilter;
  42. #pragma data_seg(".CRT$XTB")
  43. _CRTALLOC(".CRT$XTB") static _PVFV pterm = &__CxxRestoreUnhandledExceptionFilter;
  44. #pragma data_seg()
  45. static LPTOP_LEVEL_EXCEPTION_FILTER pOldExceptFilter;
  46. /////////////////////////////////////////////////////////////////////////////
  47. //
  48. // __CxxUnhandledExceptionFilter - if the exception is ours, call terminate();
  49. //
  50. // Returns:
  51. // If the exception was MSVC C++ EH, does not return.
  52. // If the previous filter was NULL, returns EXCEPTION_CONTINUE_SEARCH.
  53. // Otherwise returns value returned by previous filter.
  54. //
  55. LONG WINAPI __CxxUnhandledExceptionFilter(
  56. LPEXCEPTION_POINTERS pPtrs
  57. )
  58. {
  59. if (PER_IS_MSVC_EH((EHExceptionRecord*)(pPtrs->ExceptionRecord))) {
  60. terminate(); // Does not return
  61. return EXCEPTION_EXECUTE_HANDLER;
  62. }
  63. else {
  64. #pragma warning(disable:4191)
  65. if ( pOldExceptFilter != NULL &&
  66. _ValidateExecute((FARPROC)pOldExceptFilter) )
  67. #pragma warning(default:4191)
  68. {
  69. return pOldExceptFilter(pPtrs);
  70. }
  71. else {
  72. return EXCEPTION_CONTINUE_SEARCH;
  73. }
  74. }
  75. }
  76. /////////////////////////////////////////////////////////////////////////////
  77. //
  78. // __CxxSetUnhandledExceptionFilter - sets unhandled exception filter to be
  79. // __CxxUnhandledExceptionFilter.
  80. //
  81. // Returns:
  82. // Returns 0 to indicate no error.
  83. //
  84. int __cdecl __CxxSetUnhandledExceptionFilter(void)
  85. {
  86. pOldExceptFilter = SetUnhandledExceptionFilter(&__CxxUnhandledExceptionFilter);
  87. return 0;
  88. }
  89. /////////////////////////////////////////////////////////////////////////////
  90. //
  91. // __CxxRestoreUnhandledExceptionFilter - on exit, restores OldExceptFilter
  92. //
  93. // Returns:
  94. // Nothing.
  95. //
  96. void __cdecl __CxxRestoreUnhandledExceptionFilter(void)
  97. {
  98. SetUnhandledExceptionFilter(pOldExceptFilter);
  99. }