Debugger.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // Debugger.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Debugger.cpp#3 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: Debugger
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Debugger.h"
  16. #include <sstream>
  17. #include <cstdlib>
  18. #include <cstdio>
  19. #if defined(POCO_OS_FAMILY_WINDOWS)
  20. #include "Poco/UnWindows.h"
  21. #elif defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_VXWORKS)
  22. #include <unistd.h>
  23. #include <signal.h>
  24. #elif defined(POCO_OS_FAMILY_VMS)
  25. #include <lib$routines.h>
  26. #include <ssdef.h>
  27. #endif
  28. #if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
  29. #include "Poco/UnicodeConverter.h"
  30. #endif
  31. // NOTE: In this module, we use the C library functions (fputs) for,
  32. // output since, at the time we're called, the C++ iostream objects std::cout, etc.
  33. // might not have been initialized yet.
  34. namespace Poco {
  35. bool Debugger::isAvailable()
  36. {
  37. #if defined(_DEBUG)
  38. #if defined(POCO_OS_FAMILY_WINDOWS)
  39. #if defined(_WIN32_WCE)
  40. #if (_WIN32_WCE >= 0x600)
  41. BOOL isDebuggerPresent;
  42. if (CheckRemoteDebuggerPresent(GetCurrentProcess(), &isDebuggerPresent))
  43. {
  44. return isDebuggerPresent ? true : false;
  45. }
  46. return false;
  47. #else
  48. return false;
  49. #endif
  50. #else
  51. return IsDebuggerPresent() ? true : false;
  52. #endif
  53. #elif defined(POCO_VXWORKS)
  54. return false;
  55. #elif defined(POCO_OS_FAMILY_UNIX)
  56. return std::getenv("POCO_ENABLE_DEBUGGER") ? true : false;
  57. #elif defined(POCO_OS_FAMILY_VMS)
  58. return true;
  59. #endif
  60. #else
  61. return false;
  62. #endif
  63. }
  64. void Debugger::message(const std::string& msg)
  65. {
  66. #if defined(_DEBUG)
  67. std::fputs("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n", stderr);
  68. std::fputs(msg.c_str(), stderr);
  69. std::fputs("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n", stderr);
  70. #if defined(POCO_OS_FAMILY_WINDOWS)
  71. if (isAvailable())
  72. {
  73. #if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
  74. std::wstring umsg;
  75. UnicodeConverter::toUTF16(msg, umsg);
  76. umsg += '\n';
  77. OutputDebugStringW(umsg.c_str());
  78. #else
  79. OutputDebugStringA(msg.c_str());
  80. OutputDebugStringA("\n");
  81. #endif
  82. }
  83. #elif defined(POCO_OS_FAMILY_UNIX)
  84. #elif defined(POCO_OS_FAMILY_VMS)
  85. #endif
  86. #endif
  87. }
  88. void Debugger::message(const std::string& msg, const char* file, int line)
  89. {
  90. #if defined(_DEBUG)
  91. std::ostringstream str;
  92. str << msg << " [in file \"" << file << "\", line " << line << "]";
  93. message(str.str());
  94. #endif
  95. }
  96. void Debugger::enter()
  97. {
  98. #if defined(_DEBUG)
  99. #if defined(POCO_OS_FAMILY_WINDOWS)
  100. if (isAvailable())
  101. {
  102. DebugBreak();
  103. }
  104. #elif defined(POCO_VXWORKS)
  105. {
  106. // not supported
  107. }
  108. #elif defined(POCO_OS_FAMILY_UNIX)
  109. if (isAvailable())
  110. {
  111. kill(getpid(), SIGINT);
  112. }
  113. #elif defined(POCO_OS_FAMILY_VMS)
  114. {
  115. const char* cmd = "\012SHOW CALLS";
  116. lib$signal(SS$_DEBUG, 1, cmd);
  117. }
  118. #endif
  119. #endif
  120. }
  121. void Debugger::enter(const std::string& msg)
  122. {
  123. #if defined(_DEBUG)
  124. message(msg);
  125. enter();
  126. #endif
  127. }
  128. void Debugger::enter(const std::string& msg, const char* file, int line)
  129. {
  130. #if defined(_DEBUG)
  131. message(msg, file, line);
  132. enter();
  133. #endif
  134. }
  135. void Debugger::enter(const char* file, int line)
  136. {
  137. #if defined(_DEBUG)
  138. message("BREAK", file, line);
  139. enter();
  140. #endif
  141. }
  142. } // namespace Poco