juce_SystemStats.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. String SystemStats::getJUCEVersion()
  22. {
  23. // Some basic tests, to keep an eye on things and make sure these types work ok
  24. // on all platforms. Let me know if any of these assertions fail on your system!
  25. static_jassert (sizeof (pointer_sized_int) == sizeof (void*));
  26. static_jassert (sizeof (int8) == 1);
  27. static_jassert (sizeof (uint8) == 1);
  28. static_jassert (sizeof (int16) == 2);
  29. static_jassert (sizeof (uint16) == 2);
  30. static_jassert (sizeof (int32) == 4);
  31. static_jassert (sizeof (uint32) == 4);
  32. static_jassert (sizeof (int64) == 8);
  33. static_jassert (sizeof (uint64) == 8);
  34. return "JUCE v" JUCE_STRINGIFY(JUCE_MAJOR_VERSION)
  35. "." JUCE_STRINGIFY(JUCE_MINOR_VERSION)
  36. "." JUCE_STRINGIFY(JUCE_BUILDNUMBER);
  37. }
  38. #if JUCE_ANDROID && ! defined (JUCE_DISABLE_JUCE_VERSION_PRINTING)
  39. #define JUCE_DISABLE_JUCE_VERSION_PRINTING 1
  40. #endif
  41. #if JUCE_DEBUG && ! JUCE_DISABLE_JUCE_VERSION_PRINTING
  42. struct JuceVersionPrinter
  43. {
  44. JuceVersionPrinter()
  45. {
  46. DBG (SystemStats::getJUCEVersion());
  47. }
  48. };
  49. static JuceVersionPrinter juceVersionPrinter;
  50. #endif
  51. //==============================================================================
  52. struct CPUInformation
  53. {
  54. CPUInformation() noexcept
  55. : numCpus (0), hasMMX (false), hasSSE (false),
  56. hasSSE2 (false), hasSSE3 (false), has3DNow (false)
  57. {
  58. initialise();
  59. }
  60. void initialise() noexcept;
  61. int numCpus;
  62. bool hasMMX, hasSSE, hasSSE2, hasSSE3, has3DNow;
  63. };
  64. static const CPUInformation& getCPUInformation() noexcept
  65. {
  66. static CPUInformation info;
  67. return info;
  68. }
  69. int SystemStats::getNumCpus() noexcept { return getCPUInformation().numCpus; }
  70. bool SystemStats::hasMMX() noexcept { return getCPUInformation().hasMMX; }
  71. bool SystemStats::hasSSE() noexcept { return getCPUInformation().hasSSE; }
  72. bool SystemStats::hasSSE2() noexcept { return getCPUInformation().hasSSE2; }
  73. bool SystemStats::hasSSE3() noexcept { return getCPUInformation().hasSSE3; }
  74. bool SystemStats::has3DNow() noexcept { return getCPUInformation().has3DNow; }
  75. //==============================================================================
  76. String SystemStats::getStackBacktrace()
  77. {
  78. String result;
  79. #if JUCE_ANDROID || JUCE_MINGW
  80. jassertfalse; // sorry, not implemented yet!
  81. #elif JUCE_WINDOWS
  82. HANDLE process = GetCurrentProcess();
  83. SymInitialize (process, nullptr, TRUE);
  84. void* stack[128];
  85. int frames = (int) CaptureStackBackTrace (0, numElementsInArray (stack), stack, nullptr);
  86. HeapBlock<SYMBOL_INFO> symbol;
  87. symbol.calloc (sizeof (SYMBOL_INFO) + 256, 1);
  88. symbol->MaxNameLen = 255;
  89. symbol->SizeOfStruct = sizeof (SYMBOL_INFO);
  90. for (int i = 0; i < frames; ++i)
  91. {
  92. DWORD64 displacement = 0;
  93. if (SymFromAddr (process, (DWORD64) stack[i], &displacement, symbol))
  94. {
  95. result << i << ": ";
  96. IMAGEHLP_MODULE64 moduleInfo;
  97. zerostruct (moduleInfo);
  98. moduleInfo.SizeOfStruct = sizeof (moduleInfo);
  99. if (::SymGetModuleInfo64 (process, symbol->ModBase, &moduleInfo))
  100. result << moduleInfo.ModuleName << ": ";
  101. result << symbol->Name << " + 0x" << String::toHexString ((int64) displacement) << newLine;
  102. }
  103. }
  104. #else
  105. void* stack[128];
  106. int frames = backtrace (stack, numElementsInArray (stack));
  107. char** frameStrings = backtrace_symbols (stack, frames);
  108. for (int i = 0; i < frames; ++i)
  109. result << frameStrings[i] << newLine;
  110. ::free (frameStrings);
  111. #endif
  112. return result;
  113. }
  114. //==============================================================================
  115. static SystemStats::CrashHandlerFunction globalCrashHandler = nullptr;
  116. #if JUCE_WINDOWS
  117. static LONG WINAPI handleCrash (LPEXCEPTION_POINTERS)
  118. {
  119. globalCrashHandler();
  120. return EXCEPTION_EXECUTE_HANDLER;
  121. }
  122. #else
  123. static void handleCrash (int)
  124. {
  125. globalCrashHandler();
  126. kill (getpid(), SIGKILL);
  127. }
  128. int juce_siginterrupt (int sig, int flag);
  129. #endif
  130. void SystemStats::setApplicationCrashHandler (CrashHandlerFunction handler)
  131. {
  132. jassert (handler != nullptr); // This must be a valid function.
  133. globalCrashHandler = handler;
  134. #if JUCE_WINDOWS
  135. SetUnhandledExceptionFilter (handleCrash);
  136. #else
  137. const int signals[] = { SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGABRT, SIGSYS };
  138. for (int i = 0; i < numElementsInArray (signals); ++i)
  139. {
  140. ::signal (signals[i], handleCrash);
  141. juce_siginterrupt (signals[i], 1);
  142. }
  143. #endif
  144. }