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.

233 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. String SystemStats::getJUCEVersion()
  18. {
  19. // Some basic tests, to keep an eye on things and make sure these types work ok
  20. // on all platforms. Let me know if any of these assertions fail on your system!
  21. static_assert (sizeof (pointer_sized_int) == sizeof (void*), "Basic sanity test failed: please report!");
  22. static_assert (sizeof (int8) == 1, "Basic sanity test failed: please report!");
  23. static_assert (sizeof (uint8) == 1, "Basic sanity test failed: please report!");
  24. static_assert (sizeof (int16) == 2, "Basic sanity test failed: please report!");
  25. static_assert (sizeof (uint16) == 2, "Basic sanity test failed: please report!");
  26. static_assert (sizeof (int32) == 4, "Basic sanity test failed: please report!");
  27. static_assert (sizeof (uint32) == 4, "Basic sanity test failed: please report!");
  28. static_assert (sizeof (int64) == 8, "Basic sanity test failed: please report!");
  29. static_assert (sizeof (uint64) == 8, "Basic sanity test failed: please report!");
  30. return "JUCE v" JUCE_STRINGIFY(JUCE_MAJOR_VERSION)
  31. "." JUCE_STRINGIFY(JUCE_MINOR_VERSION)
  32. "." JUCE_STRINGIFY(JUCE_BUILDNUMBER);
  33. }
  34. #if JUCE_ANDROID && ! defined (JUCE_DISABLE_JUCE_VERSION_PRINTING)
  35. #define JUCE_DISABLE_JUCE_VERSION_PRINTING 1
  36. #endif
  37. #if JUCE_DEBUG && ! JUCE_DISABLE_JUCE_VERSION_PRINTING
  38. struct JuceVersionPrinter
  39. {
  40. JuceVersionPrinter()
  41. {
  42. DBG (SystemStats::getJUCEVersion());
  43. }
  44. };
  45. static JuceVersionPrinter juceVersionPrinter;
  46. #endif
  47. StringArray SystemStats::getDeviceIdentifiers()
  48. {
  49. StringArray ids;
  50. #if JUCE_WINDOWS
  51. File f (File::getSpecialLocation (File::windowsSystemDirectory));
  52. #else
  53. File f ("~");
  54. #endif
  55. if (auto num = f.getFileIdentifier())
  56. {
  57. ids.add (String::toHexString ((int64) num));
  58. }
  59. else
  60. {
  61. Array<MACAddress> addresses;
  62. MACAddress::findAllAddresses (addresses);
  63. for (auto& address : addresses)
  64. ids.add (address.toString());
  65. }
  66. jassert (ids.size() > 0); // Failed to create any IDs!
  67. return ids;
  68. }
  69. //==============================================================================
  70. struct CPUInformation
  71. {
  72. CPUInformation() noexcept { initialise(); }
  73. void initialise() noexcept;
  74. int numLogicalCPUs = 0, numPhysicalCPUs = 0;
  75. bool hasMMX = false, hasSSE = false, hasSSE2 = false, hasSSE3 = false,
  76. has3DNow = false, hasSSSE3 = false, hasSSE41 = false,
  77. hasSSE42 = false, hasAVX = false, hasAVX2 = false, hasNeon = false;
  78. };
  79. static const CPUInformation& getCPUInformation() noexcept
  80. {
  81. static CPUInformation info;
  82. return info;
  83. }
  84. int SystemStats::getNumCpus() noexcept { return getCPUInformation().numLogicalCPUs; }
  85. int SystemStats::getNumPhysicalCpus() noexcept { return getCPUInformation().numPhysicalCPUs; }
  86. bool SystemStats::hasMMX() noexcept { return getCPUInformation().hasMMX; }
  87. bool SystemStats::has3DNow() noexcept { return getCPUInformation().has3DNow; }
  88. bool SystemStats::hasSSE() noexcept { return getCPUInformation().hasSSE; }
  89. bool SystemStats::hasSSE2() noexcept { return getCPUInformation().hasSSE2; }
  90. bool SystemStats::hasSSE3() noexcept { return getCPUInformation().hasSSE3; }
  91. bool SystemStats::hasSSSE3() noexcept { return getCPUInformation().hasSSSE3; }
  92. bool SystemStats::hasSSE41() noexcept { return getCPUInformation().hasSSE41; }
  93. bool SystemStats::hasSSE42() noexcept { return getCPUInformation().hasSSE42; }
  94. bool SystemStats::hasAVX() noexcept { return getCPUInformation().hasAVX; }
  95. bool SystemStats::hasAVX2() noexcept { return getCPUInformation().hasAVX2; }
  96. bool SystemStats::hasNeon() noexcept { return getCPUInformation().hasNeon; }
  97. //==============================================================================
  98. String SystemStats::getStackBacktrace()
  99. {
  100. String result;
  101. #if JUCE_ANDROID || JUCE_MINGW
  102. jassertfalse; // sorry, not implemented yet!
  103. #elif JUCE_WINDOWS
  104. HANDLE process = GetCurrentProcess();
  105. SymInitialize (process, nullptr, TRUE);
  106. void* stack[128];
  107. int frames = (int) CaptureStackBackTrace (0, numElementsInArray (stack), stack, nullptr);
  108. HeapBlock<SYMBOL_INFO> symbol;
  109. symbol.calloc (sizeof (SYMBOL_INFO) + 256, 1);
  110. symbol->MaxNameLen = 255;
  111. symbol->SizeOfStruct = sizeof (SYMBOL_INFO);
  112. for (int i = 0; i < frames; ++i)
  113. {
  114. DWORD64 displacement = 0;
  115. if (SymFromAddr (process, (DWORD64) stack[i], &displacement, symbol))
  116. {
  117. result << i << ": ";
  118. IMAGEHLP_MODULE64 moduleInfo;
  119. zerostruct (moduleInfo);
  120. moduleInfo.SizeOfStruct = sizeof (moduleInfo);
  121. if (::SymGetModuleInfo64 (process, symbol->ModBase, &moduleInfo))
  122. result << moduleInfo.ModuleName << ": ";
  123. result << symbol->Name << " + 0x" << String::toHexString ((int64) displacement) << newLine;
  124. }
  125. }
  126. #else
  127. void* stack[128];
  128. int frames = backtrace (stack, numElementsInArray (stack));
  129. char** frameStrings = backtrace_symbols (stack, frames);
  130. for (int i = 0; i < frames; ++i)
  131. result << frameStrings[i] << newLine;
  132. ::free (frameStrings);
  133. #endif
  134. return result;
  135. }
  136. //==============================================================================
  137. static SystemStats::CrashHandlerFunction globalCrashHandler = nullptr;
  138. #if JUCE_WINDOWS
  139. static LONG WINAPI handleCrash (LPEXCEPTION_POINTERS ep)
  140. {
  141. globalCrashHandler (ep);
  142. return EXCEPTION_EXECUTE_HANDLER;
  143. }
  144. #else
  145. static void handleCrash (int signum)
  146. {
  147. globalCrashHandler ((void*) (pointer_sized_int) signum);
  148. kill (getpid(), SIGKILL);
  149. }
  150. int juce_siginterrupt (int sig, int flag);
  151. #endif
  152. void SystemStats::setApplicationCrashHandler (CrashHandlerFunction handler)
  153. {
  154. jassert (handler != nullptr); // This must be a valid function.
  155. globalCrashHandler = handler;
  156. #if JUCE_WINDOWS
  157. SetUnhandledExceptionFilter (handleCrash);
  158. #else
  159. const int signals[] = { SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGABRT, SIGSYS };
  160. for (int i = 0; i < numElementsInArray (signals); ++i)
  161. {
  162. ::signal (signals[i], handleCrash);
  163. juce_siginterrupt (signals[i], 1);
  164. }
  165. #endif
  166. }
  167. bool SystemStats::isRunningInAppExtensionSandbox() noexcept
  168. {
  169. #if JUCE_MAC || JUCE_IOS
  170. static bool firstQuery = true;
  171. static bool isRunningInAppSandbox = false;
  172. if (firstQuery)
  173. {
  174. firstQuery = false;
  175. File bundle = File::getSpecialLocation (File::invokedExecutableFile).getParentDirectory();
  176. #if JUCE_MAC
  177. bundle = bundle.getParentDirectory().getParentDirectory();
  178. #endif
  179. if (bundle.isDirectory())
  180. isRunningInAppSandbox = (bundle.getFileExtension() == ".appex");
  181. }
  182. return isRunningInAppSandbox;
  183. #else
  184. return false;
  185. #endif
  186. }