Commit | Line | Data |
---|---|---|
67d2ce02 | 1 | /* |
0235b0db MJ |
2 | * SPDX-License-Identifier: MIT |
3 | * | |
67d2ce02 MJ |
4 | * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation |
5 | * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com> | |
67d2ce02 MJ |
6 | */ |
7 | ||
0235b0db MJ |
8 | #ifndef BABELTRACE_CTF_WRITER_ASSERT_PRE_INTERNAL_H |
9 | #define BABELTRACE_CTF_WRITER_ASSERT_PRE_INTERNAL_H | |
10 | ||
67d2ce02 | 11 | /* |
f2371a94 PP |
12 | * The macros in this header use macros defined in "logging/log.h". We |
13 | * don't want this header to automatically include "logging/log.h" | |
14 | * because you need to manually define BT_LOG_TAG before including | |
15 | * "logging/log.h" and it is unexpected that you also need to define it | |
16 | * before including this header. | |
67d2ce02 | 17 | * |
f2371a94 PP |
18 | * This is a reminder that in order to use "ctf-writer/assert-pre.h", |
19 | * you also need to use logging explicitly. | |
67d2ce02 MJ |
20 | */ |
21 | ||
7151fb67 | 22 | #ifndef BT_LOG_SUPPORTED |
f2371a94 | 23 | # error Include "logging/log.h" before this header. |
67d2ce02 MJ |
24 | #endif |
25 | ||
c4f23e30 | 26 | #include <stdbool.h> |
67d2ce02 MJ |
27 | #include <stdlib.h> |
28 | #include <inttypes.h> | |
91d81473 | 29 | #include "common/macros.h" |
67d2ce02 MJ |
30 | |
31 | #ifdef BT_DEV_MODE | |
34ae0d62 PP |
32 | /* |
33 | * Prints the details of an unsatisfied precondition without immediately | |
34 | * aborting. You should use this within a function which checks | |
35 | * preconditions, but which is called from a BT_CTF_ASSERT_PRE() | |
36 | * context, so that the function can still return its result for | |
37 | * BT_CTF_ASSERT_PRE() to evaluate it. | |
38 | * | |
39 | * Example: | |
40 | * | |
41 | * BT_CTF_ASSERT_PRE_FUNC | |
42 | * static inline bool check_complex_precond(...) | |
43 | * { | |
44 | * ... | |
45 | * | |
46 | * if (...) { | |
47 | * BT_CTF_ASSERT_PRE_MSG("Invalid object: ...", ...); | |
48 | * return false; | |
49 | * } | |
50 | * | |
51 | * ... | |
52 | * } | |
53 | * | |
54 | * ... | |
55 | * | |
56 | * BT_CTF_ASSERT_PRE(check_complex_precond(...), | |
57 | * "Precondition is not satisfied: ...", ...); | |
58 | */ | |
59 | # define BT_CTF_ASSERT_PRE_MSG(_fmt, ...) \ | |
60 | do { \ | |
71436ae4 | 61 | bt_log_write_printf(__FILE__, __func__, \ |
34ae0d62 PP |
62 | __LINE__, BT_LOG_FATAL, BT_LOG_TAG, (_fmt), \ |
63 | ##__VA_ARGS__); \ | |
64 | } while (0) | |
65 | ||
67d2ce02 MJ |
66 | /* |
67 | * Asserts that the library precondition _cond is satisfied. | |
68 | * | |
69 | * If _cond is false, log a fatal statement using _fmt and the optional | |
70 | * arguments using BT_LOGF(), and abort. | |
71 | * | |
72 | * To assert that a postcondition is satisfied or that some internal | |
98b15851 | 73 | * object/context/value is in the expected state, use BT_ASSERT_DBG(). |
67d2ce02 MJ |
74 | */ |
75 | # define BT_CTF_ASSERT_PRE(_cond, _fmt, ...) \ | |
76 | do { \ | |
77 | if (!(_cond)) { \ | |
34ae0d62 PP |
78 | BT_CTF_ASSERT_PRE_MSG("CTF writer precondition not satisfied; error is:"); \ |
79 | BT_CTF_ASSERT_PRE_MSG((_fmt), ##__VA_ARGS__); \ | |
80 | BT_CTF_ASSERT_PRE_MSG("Aborting..."); \ | |
498e7994 | 81 | bt_common_abort(); \ |
67d2ce02 MJ |
82 | } \ |
83 | } while (0) | |
84 | ||
85 | /* | |
86 | * Marks a function as being only used within a BT_CTF_ASSERT_PRE() context. | |
87 | */ | |
88 | # define BT_CTF_ASSERT_PRE_FUNC | |
89 | ||
90 | /* | |
91 | * Prints the details of an unsatisfied precondition without immediately | |
92 | * aborting. You should use this within a function which checks | |
93 | * preconditions, but which is called from a BT_CTF_ASSERT_PRE() context, so | |
94 | * that the function can still return its result for BT_CTF_ASSERT_PRE() to | |
95 | * evaluate it. | |
96 | * | |
97 | * Example: | |
98 | * | |
99 | * BT_CTF_ASSERT_PRE_FUNC | |
100 | * static inline bool check_complex_precond(...) | |
101 | * { | |
102 | * ... | |
103 | * | |
104 | * if (...) { | |
105 | * BT_CTF_ASSERT_PRE_MSG("Invalid object: ...", ...); | |
106 | * return false; | |
107 | * } | |
108 | * | |
109 | * ... | |
110 | * } | |
111 | * | |
112 | * ... | |
113 | * | |
114 | * BT_CTF_ASSERT_PRE(check_complex_precond(...), | |
115 | * "Precondition is not satisfied: ...", ...); | |
116 | */ | |
67d2ce02 MJ |
117 | #else |
118 | # define BT_CTF_ASSERT_PRE(_cond, _fmt, ...) ((void) sizeof((void) (_cond), 0)) | |
91d81473 | 119 | # define BT_CTF_ASSERT_PRE_FUNC __attribute__((unused)) |
67d2ce02 MJ |
120 | # define BT_CTF_ASSERT_PRE_MSG(_fmt, ...) |
121 | #endif /* BT_DEV_MODE */ | |
122 | ||
123 | /* | |
124 | * Developer mode: asserts that a given variable is not NULL. | |
125 | */ | |
126 | #define BT_CTF_ASSERT_PRE_NON_NULL(_obj, _obj_name) \ | |
5084732e | 127 | BT_CTF_ASSERT_PRE((_obj), "%s is NULL: ", _obj_name) |
67d2ce02 MJ |
128 | |
129 | /* | |
130 | * Developer mode: asserts that a given object is NOT frozen. This macro | |
131 | * checks the `frozen` field of _obj. | |
132 | */ | |
133 | #define BT_CTF_ASSERT_PRE_HOT(_obj, _obj_name, _fmt, ...) \ | |
134 | BT_CTF_ASSERT_PRE(!(_obj)->frozen, "%s is frozen" _fmt, _obj_name, \ | |
135 | ##__VA_ARGS__) | |
136 | ||
137 | /* | |
138 | * Developer mode: asserts that a given index is less than a given size. | |
139 | */ | |
140 | #define BT_CTF_ASSERT_PRE_VALID_INDEX(_index, _length) \ | |
141 | BT_CTF_ASSERT_PRE((_index) < (_length), \ | |
142 | "Index is out of bounds: index=%" PRIu64 ", " \ | |
143 | "count=%" PRIu64, (uint64_t) (_index), (uint64_t) (_length)) | |
144 | ||
145 | #endif /* BABELTRACE_CTF_WRITER_ASSERT_PRE_INTERNAL_H */ |