1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2018 Cumulus Networks, Inc.
12 #include "frratomic.h"
21 * FRR's convention is that a debug statement issued under the vty CONFIG_NODE
22 * persists to the config file, whereas the same debug statement issued from
23 * the ENABLE_NODE only persists for the current session. These are mapped to
24 * DEBUG_MODE_CONF and DEBUG_MODE_TERM respectively.
26 * They are not mutually exclusive and are placed in the MSB of the flags
27 * field in a debugging record.
29 #define DEBUG_MODE_TERM 0x01000000
30 #define DEBUG_MODE_CONF 0x02000000
31 #define DEBUG_MODE_ALL (DEBUG_MODE_TERM | DEBUG_MODE_CONF)
32 #define DEBUG_MODE_NONE 0x00000000
33 #define DEBUG_OPT_ALL 0x00FFFFFF
34 #define DEBUG_OPT_NONE 0x00000000
40 * All operations on this record exposed in this header are MT-safe.
43 * A bitfield with the following format (bytes high to low)
44 * - [0] Debugging mode field (MSB) | Mode
45 * - [1] Arbitrary flag field | Option
46 * - [2] Arbitrary flag field | Option
47 * - [3] Arbitrary flag field (LSB) | Option
49 * ALL THESE BYTES ARE YOURS - EXCEPT MODE.
50 * ATTEMPT NO BIT OPS THERE.
52 * The MSB of this field determines the debug mode, Use the DEBUG_MODE*
53 * macros to manipulate this byte.
55 * The low 3 bytes of this field may be used to store arbitrary information.
56 * Usually they are used to store flags that tune how detailed the logging
57 * for a particular debug record is. Use the DEBUG_OPT* macros to manipulate
60 * All operations performed on this field should be done using the macros
61 * later in this header file. They are guaranteed to be atomic operations
62 * with respect to this field. Using anything except the macros to
63 * manipulate the flags field in a multithreaded environment results in
67 * Human-readable description of this debugging record.
70 atomic_uint_fast32_t flags
;
74 PREDECL_LIST(debug_cb_list
);
76 * Callback set for debugging code.
79 * Function pointer to call when the user requests that all debugs have a
82 struct debug_callbacks
{
84 * Linked list of Callbacks to call
86 struct debug_cb_list_item item
;
90 * flags to set on debug flag fields
96 void (*debug_set_all
)(uint32_t flags
, bool set
);
100 * Check if a mode is set for a debug.
104 #define DEBUG_MODE_CHECK(name, mode) \
105 CHECK_FLAG_ATOMIC(&(name)->flags, (mode)&DEBUG_MODE_ALL)
108 * Check if an option bit is set for a debug.
112 #define DEBUG_OPT_CHECK(name, opt) \
113 CHECK_FLAG_ATOMIC(&(name)->flags, (opt)&DEBUG_OPT_ALL)
116 * Check if bits are set for a debug.
120 #define DEBUG_FLAGS_CHECK(name, fl) CHECK_FLAG_ATOMIC(&(name)->flags, (fl))
123 * Set modes on a debug.
127 #define DEBUG_MODE_SET(name, mode, onoff) \
130 SET_FLAG_ATOMIC(&(name)->flags, \
131 (mode)&DEBUG_MODE_ALL); \
133 UNSET_FLAG_ATOMIC(&(name)->flags, \
134 (mode)&DEBUG_MODE_ALL); \
137 /* Convenience macros for specific set operations. */
138 #define DEBUG_MODE_ON(name, mode) DEBUG_MODE_SET(name, mode, true)
139 #define DEBUG_MODE_OFF(name, mode) DEBUG_MODE_SET(name, mode, false)
142 * Set options on a debug.
146 #define DEBUG_OPT_SET(name, opt, onoff) \
149 SET_FLAG_ATOMIC(&(name)->flags, (opt)&DEBUG_OPT_ALL); \
151 UNSET_FLAG_ATOMIC(&(name)->flags, \
152 (opt)&DEBUG_OPT_ALL); \
155 /* Convenience macros for specific set operations. */
156 #define DEBUG_OPT_ON(name, opt) DEBUG_OPT_SET(name, opt, true)
157 #define DEBUG_OPT_OFF(name, opt) DEBUG_OPT_SET(name, opt, true)
160 * Set bits on a debug.
164 #define DEBUG_FLAGS_SET(name, fl, onoff) \
167 SET_FLAG_ATOMIC(&(name)->flags, (fl)); \
169 UNSET_FLAG_ATOMIC(&(name)->flags, (fl)); \
172 /* Convenience macros for specific set operations. */
173 #define DEBUG_FLAGS_ON(name, fl) DEBUG_FLAGS_SET(&(name)->flags, (type), true)
174 #define DEBUG_FLAGS_OFF(name, fl) DEBUG_FLAGS_SET(&(name)->flags, (type), false)
177 * Unset all modes and options on a debug.
181 #define DEBUG_CLEAR(name) RESET_FLAG_ATOMIC(&(name)->flags)
184 * Set all modes and options on a debug.
188 #define DEBUG_ON(name) \
189 SET_FLAG_ATOMIC(&(name)->flags, DEBUG_MODE_ALL | DEBUG_OPT_ALL)
192 * Map a vty node to the correct debugging mode flags. FRR behaves such that a
193 * debug statement issued under the config node persists to the config file,
194 * whereas the same debug statement issued from the enable node only persists
195 * for the current session.
199 #define DEBUG_NODE2MODE(vtynode) \
200 (((vtynode) == CONFIG_NODE) ? DEBUG_MODE_ALL : DEBUG_MODE_TERM)
203 * Debug at the given level to the default logging destination.
207 #define DEBUG(level, name, fmt, ...) \
209 if (DEBUG_MODE_CHECK(name, DEBUG_MODE_ALL)) \
210 zlog_##level(fmt, ##__VA_ARGS__); \
213 /* Convenience macros for the various levels. */
214 #define DEBUGE(name, fmt, ...) DEBUG(err, name, fmt, ##__VA_ARGS__)
215 #define DEBUGW(name, fmt, ...) DEBUG(warn, name, fmt, ##__VA_ARGS__)
216 #define DEBUGI(name, fmt, ...) DEBUG(info, name, fmt, ##__VA_ARGS__)
217 #define DEBUGN(name, fmt, ...) DEBUG(notice, name, fmt, ##__VA_ARGS__)
218 #define DEBUGD(name, fmt, ...) DEBUG(debug, name, fmt, ##__VA_ARGS__)
221 * Optional initializer for debugging. Highly recommended.
223 * This function installs common debugging commands and allows the caller to
224 * specify callbacks to take when these commands are issued, allowing the
225 * caller to respond to events such as a request to turn off all debugs.
229 void debug_init(struct debug_callbacks
*cb
);
232 * Turn on the cli to turn on/off debugs.
233 * Should only be called by libfrr
235 void debug_init_cli(void);
241 #endif /* _FRRDEBUG_H */