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.

238 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. 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. StringArray ids;
  52. #if JUCE_WINDOWS
  53. File f (File::getSpecialLocation (File::windowsSystemDirectory));
  54. #else
  55. File f ("~");
  56. #endif
  57. if (auto num = f.getFileIdentifier())
  58. {
  59. ids.add (String::toHexString ((int64) num));
  60. }
  61. else
  62. {
  63. Array<MACAddress> addresses;
  64. MACAddress::findAllAddresses (addresses);
  65. for (auto& address : addresses)
  66. ids.add (address.toString());
  67. }
  68. jassert (ids.size() > 0); // Failed to create any IDs!
  69. return ids;
  70. }
  71. //==============================================================================
  72. struct CPUInformation
  73. {
  74. CPUInformation() noexcept { initialise(); }
  75. void initialise() noexcept;
  76. int numLogicalCPUs = 0, numPhysicalCPUs = 0;
  77. bool hasMMX = false, hasSSE = false, hasSSE2 = false, hasSSE3 = false,
  78. has3DNow = false, hasSSSE3 = false, hasSSE41 = false,
  79. hasSSE42 = false, hasAVX = false, hasAVX2 = false, hasNeon = false;
  80. };
  81. static const CPUInformation& getCPUInformation() noexcept
  82. {
  83. static CPUInformation info;
  84. return info;
  85. }
  86. int SystemStats::getNumCpus() noexcept { return getCPUInformation().numLogicalCPUs; }
  87. int SystemStats::getNumPhysicalCpus() noexcept { return getCPUInformation().numPhysicalCPUs; }
  88. bool SystemStats::hasMMX() noexcept { return getCPUInformation().hasMMX; }
  89. bool SystemStats::has3DNow() noexcept { return getCPUInformation().has3DNow; }
  90. bool SystemStats::hasSSE() noexcept { return getCPUInformation().hasSSE; }
  91. bool SystemStats::hasSSE2() noexcept { return getCPUInformation().hasSSE2; }
  92. bool SystemStats::hasSSE3() noexcept { return getCPUInformation().hasSSE3; }
  93. bool SystemStats::hasSSSE3() noexcept { return getCPUInformation().hasSSSE3; }
  94. bool SystemStats::hasSSE41() noexcept { return getCPUInformation().hasSSE41; }
  95. bool SystemStats::hasSSE42() noexcept { return getCPUInformation().hasSSE42; }
  96. bool SystemStats::hasAVX() noexcept { return getCPUInformation().hasAVX; }
  97. bool SystemStats::hasAVX2() noexcept { return getCPUInformation().hasAVX2; }
  98. bool SystemStats::hasNeon() noexcept { return getCPUInformation().hasNeon; }
  99. //==============================================================================
  100. String SystemStats::getStackBacktrace()
  101. {
  102. String result;
  103. #if JUCE_ANDROID || JUCE_MINGW
  104. jassertfalse; // sorry, not implemented yet!
  105. #elif JUCE_WINDOWS
  106. HANDLE process = GetCurrentProcess();
  107. SymInitialize (process, nullptr, TRUE);
  108. void* stack[128];
  109. int frames = (int) CaptureStackBackTrace (0, numElementsInArray (stack), stack, nullptr);
  110. HeapBlock<SYMBOL_INFO> symbol;
  111. symbol.calloc (sizeof (SYMBOL_INFO) + 256, 1);
  112. symbol->MaxNameLen = 255;
  113. symbol->SizeOfStruct = sizeof (SYMBOL_INFO);
  114. for (int i = 0; i < frames; ++i)
  115. {
  116. DWORD64 displacement = 0;
  117. if (SymFromAddr (process, (DWORD64) stack[i], &displacement, symbol))
  118. {
  119. result << i << ": ";
  120. IMAGEHLP_MODULE64 moduleInfo;
  121. zerostruct (moduleInfo);
  122. moduleInfo.SizeOfStruct = sizeof (moduleInfo);
  123. if (::SymGetModuleInfo64 (process, symbol->ModBase, &moduleInfo))
  124. result << moduleInfo.ModuleName << ": ";
  125. result << symbol->Name << " + 0x" << String::toHexString ((int64) displacement) << newLine;
  126. }
  127. }
  128. #else
  129. void* stack[128];
  130. int frames = backtrace (stack, numElementsInArray (stack));
  131. char** frameStrings = backtrace_symbols (stack, frames);
  132. for (int i = 0; i < frames; ++i)
  133. result << frameStrings[i] << newLine;
  134. ::free (frameStrings);
  135. #endif
  136. return result;
  137. }
  138. //==============================================================================
  139. static SystemStats::CrashHandlerFunction globalCrashHandler = nullptr;
  140. #if JUCE_WINDOWS
  141. static LONG WINAPI handleCrash (LPEXCEPTION_POINTERS ep)
  142. {
  143. globalCrashHandler (ep);
  144. return EXCEPTION_EXECUTE_HANDLER;
  145. }
  146. #else
  147. static void handleCrash (int signum)
  148. {
  149. globalCrashHandler ((void*) (pointer_sized_int) signum);
  150. kill (getpid(), SIGKILL);
  151. }
  152. int juce_siginterrupt (int sig, int flag);
  153. #endif
  154. void SystemStats::setApplicationCrashHandler (CrashHandlerFunction handler)
  155. {
  156. jassert (handler != nullptr); // This must be a valid function.
  157. globalCrashHandler = handler;
  158. #if JUCE_WINDOWS
  159. SetUnhandledExceptionFilter (handleCrash);
  160. #else
  161. const int signals[] = { SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGABRT, SIGSYS };
  162. for (int i = 0; i < numElementsInArray (signals); ++i)
  163. {
  164. ::signal (signals[i], handleCrash);
  165. juce_siginterrupt (signals[i], 1);
  166. }
  167. #endif
  168. }
  169. bool SystemStats::isRunningInAppExtensionSandbox() noexcept
  170. {
  171. #if JUCE_MAC || JUCE_IOS
  172. static bool firstQuery = true;
  173. static bool isRunningInAppSandbox = false;
  174. if (firstQuery)
  175. {
  176. firstQuery = false;
  177. File bundle = File::getSpecialLocation (File::invokedExecutableFile).getParentDirectory();
  178. #if JUCE_MAC
  179. bundle = bundle.getParentDirectory().getParentDirectory();
  180. #endif
  181. if (bundle.isDirectory())
  182. isRunningInAppSandbox = (bundle.getFileExtension() == ".appex");
  183. }
  184. return isRunningInAppSandbox;
  185. #else
  186. return false;
  187. #endif
  188. }
  189. } // namespace juce