Luanti 5.18.0-dev
Loading...
Searching...
No Matches
debug.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <exception>
8#include <cassert>
9#include "log.h" // unused. for convenience.
10
11#ifdef _MSC_VER
12 #define FUNCTION_NAME __FUNCTION__
13#else
14 #define FUNCTION_NAME __PRETTY_FUNCTION__
15#endif
16
17// Abort branches should always be marked unlikely, non-inline and cold,
18// so that the compiler can reduce their performance impact.
19#define EXPR_BOOL(expr) ((expr) ? 1 : 0)
20#if defined(__GNUC__) || defined(__clang__)
21 #define COLD_FUNC __attribute__((cold, noinline))
22 #define LIKELY(expr) (__builtin_expect(EXPR_BOOL(expr), 1))
23 #define UNLIKELY(expr) (__builtin_expect(EXPR_BOOL(expr), 0))
24#elif defined(_MSC_VER)
25 // MSVC has no exact cold equivalent. It supports [[likely]]
26 // and [[unlikely]], but we don't use C++20 yet.
27 #define COLD_FUNC __declspec(noinline)
28 #define LIKELY(expr) EXPR_BOOL(expr)
29 #define UNLIKELY(expr) EXPR_BOOL(expr)
30#else
31 #define COLD_FUNC
32 #define LIKELY(expr) EXPR_BOOL(expr)
33 #define UNLIKELY(expr) EXPR_BOOL(expr)
34#endif
35
36// Whether to catch all std::exceptions.
37// When "catching", the program will abort with an error message.
38// In debug mode, leave these for the debugger and don't catch them.
39#ifdef NDEBUG
40 #define CATCH_UNHANDLED_EXCEPTIONS 1
41#else
42 #define CATCH_UNHANDLED_EXCEPTIONS 0
43#endif
44
45/* Abort program execution immediately
46 */
47[[noreturn]] extern COLD_FUNC void fatal_error_fn(
48 const char *msg, const char *file,
49 unsigned int line, const char *function);
50
51#define FATAL_ERROR(msg) \
52 fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME)
53
54#define FATAL_ERROR_IF(expr, msg) \
55 (UNLIKELY(expr) \
56 ? fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME) \
57 : (void)(0))
58
59/*
60 sanity_check()
61 Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
62 defined)
63*/
64
65[[noreturn]] extern COLD_FUNC void sanity_check_fn(
66 const char *assertion, const char *file,
67 unsigned int line, const char *function);
68
69#define SANITY_CHECK(expr) \
70 (LIKELY(expr) \
71 ? (void)(0) \
72 : sanity_check_fn(#expr, __FILE__, __LINE__, FUNCTION_NAME))
73
74#define sanity_check(expr) SANITY_CHECK(expr)
75
76std::string debug_describe_exc(const std::exception &e);
77
79
80/*
81 These should be put into every thread
82*/
83
84#if CATCH_UNHANDLED_EXCEPTIONS == 1
85 #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
86 #define END_DEBUG_EXCEPTION_HANDLER \
87 } catch (std::exception &e) { \
88 std::string e_descr = debug_describe_exc(e); \
89 errorstream << "An unhandled exception occurred: " \
90 << e_descr << std::endl; \
91 FATAL_ERROR(e_descr.c_str()); \
92 }
93#else
94 // Dummy ones
95 #define BEGIN_DEBUG_EXCEPTION_HANDLER
96 #define END_DEBUG_EXCEPTION_HANDLER
97#endif
#define COLD_FUNC
Definition debug.h:31
COLD_FUNC void sanity_check_fn(const char *assertion, const char *file, unsigned int line, const char *function)
Definition debug.cpp:34
std::string debug_describe_exc(const std::exception &e)
Definition debug.cpp:64
void debug_set_exception_handler()
Definition debug.cpp:185
COLD_FUNC void fatal_error_fn(const char *msg, const char *file, unsigned int line, const char *function)
Definition debug.cpp:49