ARADebug.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //------------------------------------------------------------------------------
  2. //! \file ARADebug.h
  3. //! debug helpers for the ARA SDK Library
  4. //! \project ARA SDK Library
  5. //! \copyright Copyright (c) 2012-2021, Celemony Software GmbH, All Rights Reserved.
  6. //! \license Licensed under the Apache License, Version 2.0 (the "License");
  7. //! you may not use this file except in compliance with the License.
  8. //! You may obtain a copy of the License at
  9. //!
  10. //! http://www.apache.org/licenses/LICENSE-2.0
  11. //!
  12. //! Unless required by applicable law or agreed to in writing, software
  13. //! distributed under the License is distributed on an "AS IS" BASIS,
  14. //! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. //! See the License for the specific language governing permissions and
  16. //! limitations under the License.
  17. //------------------------------------------------------------------------------
  18. #ifndef ARADebug_h
  19. #define ARADebug_h
  20. #include "ARA_API/ARAInterface.h"
  21. #if defined(__cplusplus)
  22. namespace ARA
  23. {
  24. extern "C"
  25. {
  26. // helper define to properly insert ARA namespace into C compatible macros
  27. #define ARA_NAMESPACE ARA::
  28. #else
  29. #define ARA_NAMESPACE
  30. #endif
  31. /*******************************************************************************/
  32. // printf-like debug output facility
  33. // can be explicitly toggled by defining ARA_ENABLE_DEBUG_OUTPUT, is otherwise
  34. // enabled if NDEBUG is not defined
  35. // can be redirected to a custom implementation by defining the ARA_DEBUG_MESSAGE
  36. // macro accordingly
  37. /*******************************************************************************/
  38. // enable or disable debug output
  39. #if !defined(ARA_ENABLE_DEBUG_OUTPUT)
  40. #if defined(NDEBUG)
  41. #define ARA_ENABLE_DEBUG_OUTPUT 0
  42. #else
  43. #define ARA_ENABLE_DEBUG_OUTPUT 1
  44. #endif
  45. #endif
  46. #if ARA_ENABLE_DEBUG_OUTPUT
  47. // debug levels
  48. typedef enum ARADebugLevel
  49. {
  50. kARADebugLevelLog = 0, // output is expected and part of the normal program flow
  51. kARADebugLevelWarning = 1, // output indicates unusual runtime condition that can be recovered from
  52. kARADebugLevelAssert = 2 // output indicates programming error
  53. } ARADebugLevel;
  54. // primitive macro for all debug output
  55. // can be overridden by SDK clients to use their own custom logging system
  56. // typically not called directly - instead ARA_LOG, ARA_WARN or asserts are used
  57. #if !defined(ARA_DEBUG_MESSAGE)
  58. #define ARA_DEBUG_MESSAGE(level, text, ...) ARA_NAMESPACE ARADebugMessage(ARA_NAMESPACE level, text, ##__VA_ARGS__)
  59. // default implementation of ARA_DEBUG_MESSAGE
  60. // logs to the debugger console and/or to the error output (e.g. stderr)
  61. void ARADebugMessage(ARADebugLevel level, const char * file, int line, const char * text, ...);
  62. // this logging can be prefixed by a custom static string if desired, which is useful e.g.
  63. // if multiple plug-ins are build using the same compiled library, or in the IPC Demo example
  64. // typically, this setup call is made once when loading the final binary and passes its name
  65. void ARASetupDebugMessagePrefix(const char * prefix);
  66. #if defined(__cplusplus)
  67. // simple static singleton to set logging prefix at binary load time
  68. // add in one of your .cpp files: ARA_SETUP_DEBUG_MESSAGE_PREFIX("some prefix");
  69. #define ARA_SETUP_DEBUG_MESSAGE_PREFIX(prefix) \
  70. static struct LoggingConfigurator \
  71. { \
  72. explicit LoggingConfigurator(const char* txt) { ARA::ARASetupDebugMessagePrefix(txt); } \
  73. } loggingConfiguratorSingleton { prefix }
  74. #endif
  75. #endif
  76. // macros to conveniently use ARA_DEBUG_MESSAGE with predefined levels
  77. #define ARA_LOG(text, ...) ARA_DEBUG_MESSAGE(kARADebugLevelLog, __FILE__, __LINE__, text, ##__VA_ARGS__)
  78. #define ARA_WARN(text, ...) ARA_DEBUG_MESSAGE(kARADebugLevelWarning, __FILE__, __LINE__, text, ##__VA_ARGS__)
  79. #else
  80. #define ARA_LOG(text, ...) ((void)0)
  81. #define ARA_WARN(text, ...) ((void)0)
  82. #endif
  83. #if defined(__cplusplus) && !defined(ARA_SETUP_DEBUG_MESSAGE_PREFIX)
  84. #define ARA_SETUP_DEBUG_MESSAGE_PREFIX(prefix) struct EmptyUnusedDummy {}
  85. #endif
  86. /*******************************************************************************/
  87. // prevent unused variable warnings
  88. /*******************************************************************************/
  89. #if defined(__cplusplus) && (__cplusplus >= 201703L)
  90. #define ARA_MAYBE_UNUSED_VAR(var) var [[maybe_unused]]
  91. #elif defined(__GNUC__)
  92. #define ARA_MAYBE_UNUSED_VAR(var) var __attribute__((unused))
  93. #elif defined(_MSC_VER)
  94. #define ARA_MAYBE_UNUSED_VAR(var) var(0); (false ? (void)var : (void)false)
  95. #else
  96. #define ARA_MAYBE_UNUSED_VAR(var) var; (void)var
  97. #endif
  98. #if defined(__cplusplus) && (__cplusplus >= 201703L)
  99. #define ARA_MAYBE_UNUSED_ARG(var) var [[maybe_unused]]
  100. #elif defined(__GNUC__)
  101. #define ARA_MAYBE_UNUSED_ARG(var) var __attribute__((unused))
  102. #else
  103. #define ARA_MAYBE_UNUSED_ARG(var) var
  104. #endif
  105. /*******************************************************************************/
  106. // internal assertions, similar to the C standard assert() macro
  107. // internal assertions are not reported through the ARA API assertion facility -
  108. // if the assertion is related to the ARA API partner behavior, the validation
  109. // macros below should be used instead
  110. // can be explicitly toggled by defining ARA_ENABLE_INTERNAL_ASSERTS, is otherwise
  111. // enabled if NDEBUG is not defined
  112. // can be redirected to a custom implementation by defining the ARA_HANDLE_ASSERT
  113. // macro accordingly
  114. /*******************************************************************************/
  115. // enable or disable asserts for internal conditions
  116. #if !defined(ARA_ENABLE_INTERNAL_ASSERTS)
  117. #if defined(NDEBUG)
  118. #define ARA_ENABLE_INTERNAL_ASSERTS 0
  119. #else
  120. #define ARA_ENABLE_INTERNAL_ASSERTS 1
  121. #endif
  122. #endif
  123. #if ARA_ENABLE_INTERNAL_ASSERTS
  124. // internal assert, used to track non-ARA-API related problems.
  125. // behaves like C standard assert, but can be customized if desired
  126. // typically, some assertion technique will already be implemented in the code bases
  127. // using this library, any such implementation can be injected here.
  128. #if !defined(ARA_INTERNAL_ASSERT)
  129. #define ARA_INTERNAL_ASSERT(condition) do { if (!(condition)) { ARA_HANDLE_ASSERT(__FILE__, __LINE__, #condition); } } while (0)
  130. #endif
  131. // primitive macro for handling failed assertions
  132. // can be overridden by the SDKs client to use their own assertion report system.
  133. #if !defined(ARA_HANDLE_ASSERT)
  134. #define ARA_HANDLE_ASSERT(file, line, diagnosis) ARA_NAMESPACE ARAHandleAssert(file, line, diagnosis)
  135. // default implementation of ARA_DEBUG_MESSAGE
  136. // prints an error message if ARA_ENABLE_DEBUG_OUTPUT is enabled, then calls abort()
  137. #if defined(_MSC_VER)
  138. __declspec (noreturn)
  139. #elif defined(__GNUC__)
  140. __attribute__ ((noreturn))
  141. #endif
  142. void ARAHandleAssert(const char * file, int line, const char * diagnosis);
  143. #endif
  144. #else
  145. #define ARA_INTERNAL_ASSERT(condition) ((void)0)
  146. #endif
  147. /*******************************************************************************/
  148. // ARA API validation: wrapper for the ARA API debug assertion facility
  149. // the ARA API allows for communicating API contract violations to the partner
  150. // binary, i.e. when building a host, this validates plug-in behavior and reports
  151. // errors back, and vice versa
  152. // can be explicitly toggled by defining ARA_VALIDATE_API_CALLS, is otherwise
  153. // enabled if NDEBUG is not defined
  154. // if enabled, ARASetExternalAssertReference () must be called to setup proper
  155. // forwarding of the asserts to the partner binary if desired
  156. /*******************************************************************************/
  157. // enable or disable built-in validation of the API partner
  158. #if !defined(ARA_VALIDATE_API_CALLS)
  159. #if defined(NDEBUG)
  160. #define ARA_VALIDATE_API_CALLS 0
  161. #else
  162. #define ARA_VALIDATE_API_CALLS 1
  163. #endif
  164. #endif
  165. #if ARA_VALIDATE_API_CALLS
  166. // helper define to properly silence Xcode static analyzer warnings triggered by repeated
  167. // uses of ARA_VALIDATE_... when any one of these test relies on a previous one to succeed
  168. #if !defined(ARA_ATTRIBUTE_ANALYZER_NORETURN)
  169. #define ARA_ATTRIBUTE_ANALYZER_NORETURN
  170. #if defined(__clang__) && defined(__has_feature)
  171. #if __has_feature(attribute_analyzer_noreturn)
  172. #undef ARA_ATTRIBUTE_ANALYZER_NORETURN
  173. #define ARA_ATTRIBUTE_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
  174. #endif
  175. #endif
  176. #endif
  177. // setup for the ARA assert function, see ARAInterfaceConfiguration::assertFunctionAddress
  178. // typically, only called when debugging to inject the proper shared assertion handling,
  179. // see sample code for host and plug-in.
  180. void ARASetExternalAssertReference(ARAAssertFunction * address);
  181. // asserts related to the ARA API
  182. // the various asserts basically only differ by the assert category that they communicate across the API.
  183. // they are written so that their success can be used as a bool to gracefully handle asserts,
  184. // e.g. if (!ARA_VALIDATE_API_STATE(...)) return;
  185. #if defined(__cplusplus)
  186. #define ARA_VALIDATE_API_STATE(condition) ((condition) ? true : (ARA::ARAValidationFailure(ARA::kARAAssertInvalidState, nullptr, __FILE__, __LINE__, #condition), false))
  187. #define ARA_VALIDATE_API_ARGUMENT(arg, condition) ((condition) ? true : (ARA::ARAValidationFailure(ARA::kARAAssertInvalidArgument, arg, __FILE__, __LINE__, #condition), false))
  188. #define ARA_VALIDATE_API_CONDITION(condition) ((condition) ? true : (ARA::ARAValidationFailure(ARA::kARAAssertUnspecified, nullptr, __FILE__, __LINE__, #condition), false))
  189. #define ARA_VALIDATE_API_STRUCT_PTR(structInstance, structType) (ARA_VALIDATE_API_ARGUMENT(structInstance, (structInstance != nullptr) && (structInstance->structSize >= ARA::k##structType##MinSize)))
  190. //! \todo eventually we should switch to this definition which checks alignment as well - either for all structs,
  191. //! or at least inside ARACheckFunctionPointersAreValid since it may have actual performance impact there.
  192. // #define ARA_VALIDATE_API_STRUCT_PTR(structInstance, structType) (ARA_VALIDATE_API_ARGUMENT(structInstance, (structInstance != nullptr) && ((((intptr_t)(const ARA::structType *)structInstance) & (_Alignof(size_t) - 1)) == 0) && (structInstance->structSize >= ARA::k##structType##MinSize)))
  193. #define ARA_VALIDATE_API_INTERFACE(interfacePointer, InterfaceType) ((ARA_VALIDATE_API_STRUCT_PTR(interfacePointer, InterfaceType)) ? ARA_VALIDATE_API_ARGUMENT(interfacePointer, ARA::ARACheckFunctionPointersAreValid(interfacePointer)) : false)
  194. #else
  195. #define ARA_VALIDATE_API_STATE(condition) ((condition) ? kARATrue : (ARAValidationFailure(kARAAssertInvalidState, NULL, __FILE__, __LINE__, #condition), kARAFalse))
  196. #define ARA_VALIDATE_API_ARGUMENT(arg, condition) ((condition) ? kARATrue : (ARAValidationFailure(kARAAssertInvalidArgument, arg, __FILE__, __LINE__, #condition), kARAFalse))
  197. #define ARA_VALIDATE_API_CONDITION(condition) ((condition) ? kARATrue : (ARAValidationFailure(kARAAssertUnspecified, NULL, __FILE__, __LINE__, #condition), kARAFalse))
  198. #define ARA_VALIDATE_API_STRUCT_PTR(structInstance, structType) (ARA_VALIDATE_API_ARGUMENT(structInstance, (structInstance != NULL) && (structInstance->structSize >= k##structType##MinSize)))
  199. #define ARA_VALIDATE_API_INTERFACE(interfacePointer, InterfaceType) ((ARA_VALIDATE_API_STRUCT_PTR(interfacePointer, InterfaceType)) ? ARA_VALIDATE_API_ARGUMENT(interfacePointer, ARACheckFunctionPointersAreValid(interfacePointer)) : kARAFalse)
  200. #endif
  201. // internal helper functions for the macros above
  202. void ARAInterfaceAssert(ARAAssertCategory category, const void * problematicArgument, const char * diagnosis);
  203. void ARAValidationFailure(ARAAssertCategory category, const void * problematicArgument, const char * file, int line, const char * diagnosis) ARA_ATTRIBUTE_ANALYZER_NORETURN;
  204. ARABool ARACheckFunctionPointersAreValid(const void * interfacePointer);
  205. #else
  206. #if defined(__cplusplus)
  207. #define ARA_VALIDATE_API_STATE(condition) true
  208. #define ARA_VALIDATE_API_ARGUMENT(arg, condition) true
  209. #define ARA_VALIDATE_API_CONDITION(condition) true
  210. #define ARA_VALIDATE_API_STRUCT_PTR(structInstance, structType) true
  211. #define ARA_VALIDATE_API_INTERFACE(interfacePointer, InterfaceType) true
  212. #else
  213. #define ARA_VALIDATE_API_STATE(condition) kARATrue
  214. #define ARA_VALIDATE_API_ARGUMENT(arg, condition) kARATrue
  215. #define ARA_VALIDATE_API_CONDITION(condition) kARATrue
  216. #define ARA_VALIDATE_API_STRUCT_PTR(structInstance, structType) kARATrue
  217. #define ARA_VALIDATE_API_INTERFACE(interfacePointer, InterfaceType) kARATrue
  218. #endif
  219. #endif
  220. #if defined(__cplusplus)
  221. } // extern "C"
  222. } // namespace ARA
  223. #endif
  224. #endif // ARADebug_h