SuperDex Physics C++ API
Loading...
Searching...
No Matches
debug.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
21
22#include <functional>
23
24namespace superdex {
25
26/* Useful diagnostics for the current translation unit. Compiler settings may differ between
27 NVCC and the host compiler. Such settings will inevitably result in hard to debug crashes.
28 These could help identify the problem.
29*/
30#if false
31#define _STR(x) #x
32#define STR(x) _STR(x)
33#pragma message("[Translation Unit Diagnostics]")
34
35#pragma message("PLATFORM(s):")
36#pragma message("- Android: " STR(MOCHI_PLATFORM_ANDROID))
37#pragma message("- Linux: " STR(MOCHI_PLATFORM_LINUX))
38#pragma message("- MacOS: " STR(MOCHI_PLATFORM_MACOS))
39#pragma message("- Windows: " STR(MOCHI_PLATFORM_WINDOWS))
40
41#pragma message("COMPILER(s):")
42#pragma message("- MSVC: " STR(MOCHI_COMPILER_MSVC))
43#pragma message("- CLANG: " STR(MOCHI_COMPILER_CLANG))
44#pragma message("- GCC: " STR(MOCHI_COMPILER_GCC))
45#pragma message("- CUDA CPU: " STR(MOCHI_COMPILER_CUDA_CPU))
46#pragma message("- CUDA GPU: " STR(MOCHI_COMPILER_CUDA_GPU))
47
48#pragma message("Architecture:")
49#pragma message("- CPU: " STR(MOCHI_ARCH_CPU))
50#pragma message("- GPU: " STR(MOCHI_ARCH_GPU))
51#pragma message("- ARM: " STR(MOCHI_ARCH_ARM))
52#pragma message("- ARM NEON: " STR(MOCHI_ARCH_ARM_NEON))
53#pragma message("- ARM SVE: " STR(MOCHI_ARCH_ARM_SVE))
54#pragma message("- ARM SME: " STR(MOCHI_ARCH_ARM_SME))
55#pragma message("- X64: " STR(MOCHI_ARCH_X64))
56#pragma message("- X64 AVX2: " STR(MOCHI_ARCH_X64_AVX2))
57#pragma message("- X64 FMA: " STR(MOCHI_ARCH_X64_FMA))
58#pragma message("- X64 SVML: " STR(MOCHI_ARCH_X64_SVML))
59
60#undef STR
61#undef _STR
62#endif
63
64// MOCHI_ASSERT:
65// Used to catch mistakes made by programmers (e.g. bad parameters, bad internal state).
66// NOT used to report errors that the caller might handle (e.g. missing file, data not ready yet).
67// Enabled by default in all builds.
68//
69// WARNING:
70// MOCHI_ASSERT compiles out completely unless MOCHI_ASSERT_ENABLED.
71// Therefore, the condition should have NO SIDE EFFECTS.
72//
73// Examples:
74// MOCHI_ASSERT(ptr != nullptr, "File %s not found", filename);
75//
76// Terse Example (only legal in C++20 code, not in some public headers that are limited to C++17):
77// MOCHI_ASSERT(ptr != nullptr);
78//
79#if MOCHI_ASSERT_ENABLED
80#if MOCHI_HAS_VA_OPT
81#define MOCHI_ASSERT(condition_without_side_effects, ...) \
82 MOCHI_ASSERT_IMPL(condition_without_side_effects __VA_OPT__(, )##__VA_ARGS__)
83#else
84#define MOCHI_ASSERT(condition_without_side_effects, ...) \
85 MOCHI_ASSERT_IMPL(condition_without_side_effects, ##__VA_ARGS__)
86#endif
87#else
88#define MOCHI_ASSERT(...)
89#endif
90
91// MOCHI_ASSERT_VERBOSE:
92// Similar to MOCHI_ASSERT (see above), except that it is only enabled in Debug builds by default.
93// Used to catch programmer mistakes in performance sensitive areas of the code. Can be enabled in
94// any build configuration by defining MOCHI_ASSERT_VERBOSE_ENABLED=1.
95//
96// WARNING:
97// MOCHI_ASSERT_VERBOSE compiles out completely unless MOCHI_ASSERT_VERBOSE_ENABLED.
98// Therefore, the condition should have NO SIDE EFFECTS.
99//
100#if MOCHI_ASSERT_VERBOSE_ENABLED
101#if MOCHI_HAS_VA_OPT
102#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects, ...) \
103 MOCHI_ASSERT_IMPL(condition_without_side_effects __VA_OPT__(, )##__VA_ARGS__)
104#else
105#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects, ...) \
106 MOCHI_ASSERT_IMPL(condition_without_side_effects, ##__VA_ARGS__)
107#endif
108#else
109#define MOCHI_ASSERT_VERBOSE(...)
110#endif
111
112// Used to indicate not implemented functionalities.
113#define MOCHI_NOT_IMPLEMENTED() MOCHI_ASSERT(false, "%s not implemented", __FUNCTION__)
114
115// Used in code paths that require MOCHI_USE_EIGEN
116#if MOCHI_USE_EIGEN
117#define MOCHI_ASSERT_EIGEN()
118#else
119#define MOCHI_ASSERT_EIGEN() \
120 MOCHI_ASSERT( \
121 MOCHI_USE_EIGEN, \
122 "This feature requires Eigen. To enable, add the Eigen dependency to your build configuration and define MOCHI_USE_EIGEN=1.")
123#endif
124
125// superdex::assert_cast has the same behavior of static_cast, except that it also uses MOCHI_ASSERT to
126// report invalid casts. It works with pointers or references, but the types must be polymorphic
127// (declaring at least one virtual function). If (!MOCHI_ASSERT_ENABLED), then assert_cast will
128// compile out and only the static_cast will remain.
129template <class DstPtrT, class SrcT>
130MOCHI_FORCE_INLINE DstPtrT assert_cast(SrcT* ptr);
131template <class DstRefT, class SrcT>
132MOCHI_FORCE_INLINE DstRefT assert_cast(SrcT&& ref);
133
134/**
135 * @brief Callback invoked when an assertion fails.
136 *
137 * @details Receives the failed condition, formatted message, source file, and source line. Return
138 * `true` to break execution at the failure site.
139 */
141 std::function<bool(char const* condition, char const* message, char const* file, int line)>;
142
143/** @cond */
144
145// Get the function to call when an assert fails
146OnAssertFn GetAssertionFailureCallback();
147
148// Set the function to call when an assert fails.
149// Pass OnAssertFn{} to restore default behavior.
150void SetAssertionFailureCallback(OnAssertFn fn);
151
152// Call the current assertion failure callback.
153// Return true if execution should stop at a break point.
154bool OnAssertionFailure(char const* file, int line, char const* condition, char const* msg);
155
156// Call the current assertion failure callback with a formatted message
157// Return true if execution should stop at a break point.
158template <typename... Args>
159MOCHI_NO_INLINE bool OnAssertionFailure(
160 char const* file,
161 int line,
162 char const* condition,
163 char const* fmt,
164 Args... args);
165
166/** @endcond */
167
168/**
169 * @brief Return true if we can detect that a debugger is attached to this process. This may not be
170 * supported on all platforms.
171 *
172 * @return bool
173 */
174[[nodiscard]] bool IsDebuggerAttached();
175
176} // namespace superdex
177
178#include "debug_inl.h"
#define MOCHI_FORCE_INLINE
bool IsDebuggerAttached()
Return true if we can detect that a debugger is attached to this process.
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