SuperDex Physics C++ API
Loading...
Searching...
No Matches
debug_inl.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18#include "debug.h" // Reverse include for Intellisense
19
20#include <mutex>
21#include <type_traits>
22
23namespace superdex {
24
25#if MOCHI_ARCH_CPU
26// Assert reporting in CPU code
27#if MOCHI_HAS_VA_OPT
28#define MOCHI_ASSERT_ON_FAILURE(file, line, conditionStr, ...) \
29 if (::superdex::OnAssertionFailure(file, line, conditionStr __VA_OPT__(, ) __VA_ARGS__)) { \
30 MOCHI_DEBUG_BREAK(); \
31 }
32#else
33#define MOCHI_ASSERT_ON_FAILURE(file, line, conditionStr, ...) \
34 if (::superdex::OnAssertionFailure(file, line, conditionStr, __VA_ARGS__)) { \
35 MOCHI_DEBUG_BREAK(); \
36 }
37#endif
38#elif MOCHI_ARCH_GPU
39// GPU paths cannot use the full logger functionality. Instead we have to rely on simpler
40// mechanisms to get the point across. CUDA for example, supports printf.
41//
42// @TODO[Nate,Hector] Is ther an alternative for MOCHI_DEBUG_BREAK in CUDA code? The compiler
43// currently reports errors any time it is used inside a constexpr function (of which we have many).
44#define MOCHI_ASSERT_ON_FAILURE(file, line, conditionStr, ...) \
45 printf("%s(%d): Assertion Failure! Expected (%s)\n", file, line, conditionStr); \
46 printf("" __VA_ARGS__); \
47 printf("\n"); \
48 /*MOCHI_DEBUG_BREAK()*/
49#else
50#error Expected MOCHI_ARCH_CPU or MOCHI_ARCH_GPU
51#define MOCHI_ASSERT_ON_FAILURE
52#endif
53
54// Assert macro implementation (does not compile out if used directly)
55#if MOCHI_HAS_VA_OPT
56#define MOCHI_ASSERT_IMPL(condition, ...) \
57 if (condition) { \
58 } else MOCHI_UNLIKELY { \
59 MOCHI_ASSERT_ON_FAILURE(__FILE__, __LINE__, #condition __VA_OPT__(, ) __VA_ARGS__); \
60 }
61#else
62#define MOCHI_ASSERT_IMPL(condition, ...) \
63 if (condition) { \
64 } else MOCHI_UNLIKELY { \
65 MOCHI_ASSERT_ON_FAILURE(__FILE__, __LINE__, #condition, __VA_ARGS__); \
66 }
67#endif
68
69// These implementation functions are inlined so that MOCHI_ASSERT can be used
70// via header-only dependency. This does not require linking with MochiCore.
71namespace assert_impl {
72
73#if MOCHI_ARCH_CPU
74inline std::recursive_mutex& GetAssertMutexRef() {
75 static std::recursive_mutex s_assertMutex;
76 return s_assertMutex;
77}
78inline OnAssertFn& GetOnAssertFnRef() {
79 static OnAssertFn s_onAssertFn;
80 return s_onAssertFn;
81}
82inline int& GetAssertCallDepthRef() {
83 static int s_assertCallDepth = 0;
84 return s_assertCallDepth;
85}
86#endif // MOCHI_ARCH_CPU
87
88inline static bool
89DefaultAssertHandler(char const* condition, char const* message, char const* file, int line) {
91 "\n"
92 "*****************************************************************************\n"
93 "MOCHI ASSERTION FAILURE:\n"
94 " Message: %s\n"
95 " Expected: (%s)\n"
96 " Location: %s(%d)\n"
97 "*****************************************************************************\n",
98 message,
99 condition,
100 file,
101 line);
102
103 // Make sure the message is flushed before the breakpoint
104 FlushLog();
105
106 // TODO:
107 // Show a dialog box with options like: "Abort", "Debug", and "Continue at your own risk".
108 // For now, we will always stop at a breakpoint, or crash with a non-zero exit code if no debugger
109 // is connected.
110 bool const triggerDebugBreak = true;
111
112 return triggerDebugBreak;
113}
114
115} // namespace assert_impl
116
117template <class DstPtrT, class SrcT>
118MOCHI_FORCE_INLINE DstPtrT assert_cast(SrcT* ptr) {
119 static_assert(std::is_pointer_v<DstPtrT>, "Invalid cast. Expected a pointer type.");
120 MOCHI_ASSERT(dynamic_cast<DstPtrT>(ptr) == static_cast<DstPtrT>(ptr), "Invalid cast");
121 return static_cast<DstPtrT>(ptr);
122}
123template <class DstRefT, class SrcT>
124MOCHI_FORCE_INLINE DstRefT assert_cast(SrcT&& ref) {
125 static_assert(std::is_reference_v<DstRefT>, "Invalid cast. Expected a reference type.");
127 dynamic_cast<std::remove_reference_t<DstRefT>*>(&ref) ==
128 static_cast<std::remove_reference_t<DstRefT>*>(&ref),
129 "Invalid cast");
130 return static_cast<DstRefT>(ref);
131}
132
133/** @cond */
134
135inline OnAssertFn GetAssertionFailureCallback() {
136#if MOCHI_ARCH_CPU
137 std::scoped_lock lock(assert_impl::GetAssertMutexRef());
138 auto& fn = assert_impl::GetOnAssertFnRef();
139 return fn ? fn : OnAssertFn{assert_impl::DefaultAssertHandler};
140#else
141 return OnAssertFn{assert_impl::DefaultAssertHandler};
142#endif
143}
144
145inline void SetAssertionFailureCallback([[maybe_unused]] OnAssertFn fn) {
146#if MOCHI_ARCH_CPU
147 std::scoped_lock lock(assert_impl::GetAssertMutexRef());
148 assert_impl::GetOnAssertFnRef() = fn ? fn : OnAssertFn{assert_impl::DefaultAssertHandler};
149#endif
150}
151
152// Overload for asserts with WITHOUT formatted args. Never inline this function!
153MOCHI_NO_INLINE inline bool
154OnAssertionFailure(char const* file, int line, char const* condition, char const* msg) {
155#if MOCHI_ARCH_CPU
156 std::scoped_lock lock(assert_impl::GetAssertMutexRef());
157 int& callDepth = assert_impl::GetAssertCallDepthRef();
158 if (callDepth != 0) {
159 // HELP! Assert failure within an assert handler function!
161 }
162 ++callDepth;
163 bool shouldBreak = GetAssertionFailureCallback()(condition, msg, file, line);
164 --callDepth;
165 return shouldBreak;
166#else
167 return GetAssertionFailureCallback()(condition, msg, file, line);
168#endif
169}
170
171// Overload for asserts with a formatted message. Never inline this function!
172template <typename... Args>
173MOCHI_NO_INLINE bool OnAssertionFailure(
174 char const* file,
175 int line,
176 char const* condition,
177 char const* fmt,
178 Args... args) {
179 return OnAssertionFailure(file, line, condition, Format(fmt, args...).c_str());
180}
181
182// Overload for asserts with no message at all. Never inline this function!
183MOCHI_NO_INLINE inline bool OnAssertionFailure(char const* file, int line, char const* condition) {
184 return OnAssertionFailure(file, line, condition, "");
185}
186
187/** @endcond */
188
189} // namespace superdex
#define MOCHI_ASSERT(condition_without_side_effects,...)
Definition debug.h:81
#define MOCHI_LOG_ERROR(...)
Definition log.h:44
#define MOCHI_DEBUG_BREAK()
#define MOCHI_FORCE_INLINE
std::function< bool(char const *condition, char const *message, char const *file, int line)> OnAssertFn
Callback invoked when an assertion fails.
Definition debug.h:140
void FlushLog()
Definition log.h:156
std::string Format()
Definition log.h:96