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.

315 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. namespace juce
  18. {
  19. String SystemStats::getJUCEVersion()
  20. {
  21. // Some basic tests, to keep an eye on things and make sure these types work ok
  22. // on all platforms. Let me know if any of these assertions fail on your system!
  23. static_assert (sizeof (pointer_sized_int) == sizeof (void*), "Basic sanity test failed: please report!");
  24. static_assert (sizeof (int8) == 1, "Basic sanity test failed: please report!");
  25. static_assert (sizeof (uint8) == 1, "Basic sanity test failed: please report!");
  26. static_assert (sizeof (int16) == 2, "Basic sanity test failed: please report!");
  27. static_assert (sizeof (uint16) == 2, "Basic sanity test failed: please report!");
  28. static_assert (sizeof (int32) == 4, "Basic sanity test failed: please report!");
  29. static_assert (sizeof (uint32) == 4, "Basic sanity test failed: please report!");
  30. static_assert (sizeof (int64) == 8, "Basic sanity test failed: please report!");
  31. static_assert (sizeof (uint64) == 8, "Basic sanity test failed: please report!");
  32. return "JUCE v" JUCE_STRINGIFY (JUCE_MAJOR_VERSION)
  33. "." JUCE_STRINGIFY (JUCE_MINOR_VERSION)
  34. "." JUCE_STRINGIFY (JUCE_BUILDNUMBER);
  35. }
  36. #if JUCE_ANDROID && ! defined (JUCE_DISABLE_JUCE_VERSION_PRINTING)
  37. #define JUCE_DISABLE_JUCE_VERSION_PRINTING 1
  38. #endif
  39. #if JUCE_DEBUG && ! JUCE_DISABLE_JUCE_VERSION_PRINTING
  40. struct JuceVersionPrinter
  41. {
  42. JuceVersionPrinter()
  43. {
  44. DBG (SystemStats::getJUCEVersion());
  45. }
  46. };
  47. static JuceVersionPrinter juceVersionPrinter;
  48. #endif
  49. StringArray SystemStats::getDeviceIdentifiers()
  50. {
  51. for (const auto flag : { MachineIdFlags::fileSystemId, MachineIdFlags::macAddresses })
  52. if (auto ids = getMachineIdentifiers (flag); ! ids.isEmpty())
  53. return ids;
  54. jassertfalse; // Failed to create any IDs!
  55. return {};
  56. }
  57. String getLegacyUniqueDeviceID();
  58. StringArray SystemStats::getMachineIdentifiers (MachineIdFlags flags)
  59. {
  60. auto macAddressProvider = [] (StringArray& arr)
  61. {
  62. for (const auto& mac : MACAddress::getAllAddresses())
  63. arr.add (mac.toString());
  64. };
  65. auto fileSystemProvider = [] (StringArray& arr)
  66. {
  67. #if JUCE_WINDOWS
  68. File f (File::getSpecialLocation (File::windowsSystemDirectory));
  69. #else
  70. File f ("~");
  71. #endif
  72. if (auto num = f.getFileIdentifier())
  73. arr.add (String::toHexString ((int64) num));
  74. };
  75. auto legacyIdProvider = [] ([[maybe_unused]] StringArray& arr)
  76. {
  77. #if JUCE_WINDOWS
  78. arr.add (getLegacyUniqueDeviceID());
  79. #endif
  80. };
  81. auto uniqueIdProvider = [] (StringArray& arr)
  82. {
  83. arr.add (getUniqueDeviceID());
  84. };
  85. struct Provider { MachineIdFlags flag; void (*func) (StringArray&); };
  86. static const Provider providers[] =
  87. {
  88. { MachineIdFlags::macAddresses, macAddressProvider },
  89. { MachineIdFlags::fileSystemId, fileSystemProvider },
  90. { MachineIdFlags::legacyUniqueId, legacyIdProvider },
  91. { MachineIdFlags::uniqueId, uniqueIdProvider }
  92. };
  93. StringArray ids;
  94. for (const auto& provider : providers)
  95. {
  96. if (hasBitValueSet (flags, provider.flag))
  97. provider.func (ids);
  98. }
  99. return ids;
  100. }
  101. //==============================================================================
  102. struct CPUInformation
  103. {
  104. CPUInformation() noexcept { initialise(); }
  105. void initialise() noexcept;
  106. int numLogicalCPUs = 0, numPhysicalCPUs = 0;
  107. bool hasMMX = false, hasSSE = false, hasSSE2 = false, hasSSE3 = false,
  108. has3DNow = false, hasFMA3 = false, hasFMA4 = false, hasSSSE3 = false,
  109. hasSSE41 = false, hasSSE42 = false, hasAVX = false, hasAVX2 = false,
  110. hasAVX512F = false, hasAVX512BW = false, hasAVX512CD = false,
  111. hasAVX512DQ = false, hasAVX512ER = false, hasAVX512IFMA = false,
  112. hasAVX512PF = false, hasAVX512VBMI = false, hasAVX512VL = false,
  113. hasAVX512VPOPCNTDQ = false,
  114. hasNeon = false;
  115. };
  116. static const CPUInformation& getCPUInformation() noexcept
  117. {
  118. static CPUInformation info;
  119. return info;
  120. }
  121. int SystemStats::getNumCpus() noexcept { return getCPUInformation().numLogicalCPUs; }
  122. int SystemStats::getNumPhysicalCpus() noexcept { return getCPUInformation().numPhysicalCPUs; }
  123. bool SystemStats::hasMMX() noexcept { return getCPUInformation().hasMMX; }
  124. bool SystemStats::has3DNow() noexcept { return getCPUInformation().has3DNow; }
  125. bool SystemStats::hasFMA3() noexcept { return getCPUInformation().hasFMA3; }
  126. bool SystemStats::hasFMA4() noexcept { return getCPUInformation().hasFMA4; }
  127. bool SystemStats::hasSSE() noexcept { return getCPUInformation().hasSSE; }
  128. bool SystemStats::hasSSE2() noexcept { return getCPUInformation().hasSSE2; }
  129. bool SystemStats::hasSSE3() noexcept { return getCPUInformation().hasSSE3; }
  130. bool SystemStats::hasSSSE3() noexcept { return getCPUInformation().hasSSSE3; }
  131. bool SystemStats::hasSSE41() noexcept { return getCPUInformation().hasSSE41; }
  132. bool SystemStats::hasSSE42() noexcept { return getCPUInformation().hasSSE42; }
  133. bool SystemStats::hasAVX() noexcept { return getCPUInformation().hasAVX; }
  134. bool SystemStats::hasAVX2() noexcept { return getCPUInformation().hasAVX2; }
  135. bool SystemStats::hasAVX512F() noexcept { return getCPUInformation().hasAVX512F; }
  136. bool SystemStats::hasAVX512BW() noexcept { return getCPUInformation().hasAVX512BW; }
  137. bool SystemStats::hasAVX512CD() noexcept { return getCPUInformation().hasAVX512CD; }
  138. bool SystemStats::hasAVX512DQ() noexcept { return getCPUInformation().hasAVX512DQ; }
  139. bool SystemStats::hasAVX512ER() noexcept { return getCPUInformation().hasAVX512ER; }
  140. bool SystemStats::hasAVX512IFMA() noexcept { return getCPUInformation().hasAVX512IFMA; }
  141. bool SystemStats::hasAVX512PF() noexcept { return getCPUInformation().hasAVX512PF; }
  142. bool SystemStats::hasAVX512VBMI() noexcept { return getCPUInformation().hasAVX512VBMI; }
  143. bool SystemStats::hasAVX512VL() noexcept { return getCPUInformation().hasAVX512VL; }
  144. bool SystemStats::hasAVX512VPOPCNTDQ() noexcept { return getCPUInformation().hasAVX512VPOPCNTDQ; }
  145. bool SystemStats::hasNeon() noexcept { return getCPUInformation().hasNeon; }
  146. //==============================================================================
  147. String SystemStats::getStackBacktrace()
  148. {
  149. String result;
  150. #if JUCE_ANDROID || JUCE_MINGW || JUCE_WASM
  151. jassertfalse; // sorry, not implemented yet!
  152. #elif JUCE_WINDOWS
  153. HANDLE process = GetCurrentProcess();
  154. SymInitialize (process, nullptr, TRUE);
  155. void* stack[128];
  156. int frames = (int) CaptureStackBackTrace (0, numElementsInArray (stack), stack, nullptr);
  157. HeapBlock<SYMBOL_INFO> symbol;
  158. symbol.calloc (sizeof (SYMBOL_INFO) + 256, 1);
  159. symbol->MaxNameLen = 255;
  160. symbol->SizeOfStruct = sizeof (SYMBOL_INFO);
  161. for (int i = 0; i < frames; ++i)
  162. {
  163. DWORD64 displacement = 0;
  164. if (SymFromAddr (process, (DWORD64) stack[i], &displacement, symbol))
  165. {
  166. result << i << ": ";
  167. IMAGEHLP_MODULE64 moduleInfo;
  168. zerostruct (moduleInfo);
  169. moduleInfo.SizeOfStruct = sizeof (moduleInfo);
  170. if (::SymGetModuleInfo64 (process, symbol->ModBase, &moduleInfo))
  171. result << moduleInfo.ModuleName << ": ";
  172. result << symbol->Name << " + 0x" << String::toHexString ((int64) displacement) << newLine;
  173. }
  174. }
  175. #else
  176. void* stack[128];
  177. auto frames = backtrace (stack, numElementsInArray (stack));
  178. char** frameStrings = backtrace_symbols (stack, frames);
  179. for (auto i = (decltype (frames)) 0; i < frames; ++i)
  180. result << frameStrings[i] << newLine;
  181. ::free (frameStrings);
  182. #endif
  183. return result;
  184. }
  185. //==============================================================================
  186. #if ! JUCE_WASM
  187. static SystemStats::CrashHandlerFunction globalCrashHandler = nullptr;
  188. #if JUCE_WINDOWS
  189. static LONG WINAPI handleCrash (LPEXCEPTION_POINTERS ep)
  190. {
  191. globalCrashHandler (ep);
  192. return EXCEPTION_EXECUTE_HANDLER;
  193. }
  194. #else
  195. static void handleCrash (int signum)
  196. {
  197. globalCrashHandler ((void*) (pointer_sized_int) signum);
  198. ::kill (getpid(), SIGKILL);
  199. }
  200. int juce_siginterrupt (int sig, int flag);
  201. #endif
  202. void SystemStats::setApplicationCrashHandler (CrashHandlerFunction handler)
  203. {
  204. jassert (handler != nullptr); // This must be a valid function.
  205. globalCrashHandler = handler;
  206. #if JUCE_WINDOWS
  207. SetUnhandledExceptionFilter (handleCrash);
  208. #else
  209. const int signals[] = { SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGABRT, SIGSYS };
  210. for (int i = 0; i < numElementsInArray (signals); ++i)
  211. {
  212. ::signal (signals[i], handleCrash);
  213. juce_siginterrupt (signals[i], 1);
  214. }
  215. #endif
  216. }
  217. #endif
  218. bool SystemStats::isRunningInAppExtensionSandbox() noexcept
  219. {
  220. #if JUCE_MAC || JUCE_IOS
  221. static bool isRunningInAppSandbox = [&]
  222. {
  223. File bundle = File::getSpecialLocation (File::invokedExecutableFile).getParentDirectory();
  224. #if JUCE_MAC
  225. bundle = bundle.getParentDirectory().getParentDirectory();
  226. #endif
  227. if (bundle.isDirectory())
  228. return bundle.getFileExtension() == ".appex";
  229. return false;
  230. }();
  231. return isRunningInAppSandbox;
  232. #else
  233. return false;
  234. #endif
  235. }
  236. #if JUCE_UNIT_TESTS
  237. class UniqueHardwareIDTest final : public UnitTest
  238. {
  239. public:
  240. //==============================================================================
  241. UniqueHardwareIDTest() : UnitTest ("UniqueHardwareID", UnitTestCategories::analytics) {}
  242. void runTest() override
  243. {
  244. beginTest ("getUniqueDeviceID returns usable data.");
  245. {
  246. expect (SystemStats::getUniqueDeviceID().isNotEmpty());
  247. }
  248. }
  249. };
  250. static UniqueHardwareIDTest uniqueHardwareIDTest;
  251. #endif
  252. } // namespace juce