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.

146 lines
3.4 KiB

  1. /*-----------------------------------------------------------------------------
  2. debug.cpp
  3. This file implements the debuggin features
  4. Copyright (c) 1996-1998 Microsoft Corporation
  5. All rights reserved
  6. Authors:
  7. ChrisK Chris Kauffman
  8. Histroy:
  9. 7/22/96 ChrisK Cleaned and formatted
  10. 7/31/96 ValdonB Changes for Win16
  11. -----------------------------------------------------------------------------*/
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdarg.h>
  16. #if defined(WIN16)
  17. extern HINSTANCE g_hInst;
  18. extern LPSTR g_lpszCommandLine;
  19. extern LPSTR GetCommandLine(void);
  20. #endif
  21. BOOL fInAssert=FALSE;
  22. // ############################################################################
  23. // DebugSz
  24. //
  25. // This function outputs debug string
  26. //
  27. // Created 1/28/96, Chris Kauffman
  28. // ############################################################################
  29. void DebugSz(LPCSTR psz)
  30. {
  31. #if defined(_DEBUG)
  32. OutputDebugString(psz);
  33. #endif
  34. } // DebugSz
  35. // ############################################################################
  36. // Debug Printf to debug output screen
  37. void Dprintf(LPCSTR pcsz, ...)
  38. {
  39. #ifdef _DEBUG
  40. va_list argp;
  41. char szBuf[1024];
  42. va_start(argp, pcsz);
  43. #if 0
  44. vsprintf(szBuf, pcsz, argp);
  45. #else
  46. wvsprintf(szBuf, pcsz, argp);
  47. #endif
  48. DebugSz(szBuf);
  49. va_end(argp);
  50. #endif
  51. } // Dprintf()
  52. // ############################################################################
  53. // Handle asserts
  54. BOOL FAssertProc(LPCSTR szFile, DWORD dwLine, LPCSTR szMsg, DWORD dwFlags)
  55. {
  56. BOOL fAssertIntoDebugger = FALSE;
  57. char szMsgEx[1024], szTitle[255], szFileName[MAX_PATH];
  58. int id;
  59. UINT fuStyle;
  60. LPTSTR pszCommandLine = GetCommandLine();
  61. //BYTE szTime[80];
  62. #if !defined(WIN16)
  63. CHAR szTime[80];
  64. HANDLE hAssertTxt;
  65. SYSTEMTIME st;
  66. DWORD cbWritten;
  67. #endif
  68. // no recursive asserts
  69. if (fInAssert)
  70. {
  71. DebugSz("***Recursive Assert***\r\n");
  72. return(FALSE);
  73. }
  74. fInAssert = TRUE;
  75. #if defined(WIN16)
  76. GetModuleFileName(g_hInst, szFileName, MAX_PATH);
  77. wsprintf(szMsgEx,"%s:#%ld\r\n%s,\r\n%s", szFile, dwLine, szFileName, szMsg);
  78. #else
  79. GetModuleFileName(NULL, szFileName, MAX_PATH);
  80. wsprintf(szMsgEx,"%s:#%d\r\nProcess ID: %d %s, Thread ID: %d\r\n%s",
  81. szFile,dwLine,GetCurrentProcessId(),szFileName,GetCurrentThreadId(),szMsg);
  82. #endif
  83. wsprintf(szTitle,"Assertion Failed");
  84. fuStyle = MB_APPLMODAL | MB_ABORTRETRYIGNORE;
  85. fuStyle |= MB_ICONSTOP;
  86. DebugSz(szTitle);
  87. DebugSz(szMsgEx);
  88. // dump the assert into ASSERT.TXT
  89. #if !defined(WIN16)
  90. hAssertTxt = CreateFile("assert.txt", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
  91. if (INVALID_HANDLE_VALUE != hAssertTxt)
  92. {
  93. SetFilePointer(hAssertTxt, 0, NULL, FILE_END);
  94. GetLocalTime(&st);
  95. wsprintf(szTime, "\r\n\r\n%02d/%02d/%02d %d:%02d:%02d\r\n", st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond);
  96. WriteFile(hAssertTxt, szTime, lstrlen(szTime), &cbWritten, NULL);
  97. WriteFile(hAssertTxt, szMsgEx, lstrlen(szMsgEx), &cbWritten, NULL);
  98. CloseHandle(hAssertTxt);
  99. }
  100. #endif
  101. id = MessageBox(NULL, szMsgEx, szTitle, fuStyle);
  102. switch (id)
  103. {
  104. case IDABORT:
  105. #if defined(WIN16)
  106. exit(0);
  107. #else
  108. ExitProcess(0);
  109. #endif
  110. break;
  111. case IDCANCEL:
  112. case IDIGNORE:
  113. break;
  114. case IDRETRY:
  115. fAssertIntoDebugger = TRUE;
  116. break;
  117. }
  118. fInAssert = FALSE;
  119. return(fAssertIntoDebugger);
  120. } // AssertProc()