The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

219 lines
7.3KB

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