3 * Copyright (C) 2018 Cumulus Networks, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "frratomic.h"
34 * FRR's convention is that a debug statement issued under the vty CONFIG_NODE
35 * persists to the config file, whereas the same debug statement issued from
36 * the ENABLE_NODE only persists for the current session. These are mapped to
37 * DEBUG_MODE_CONF and DEBUG_MODE_TERM respectively.
39 * They are not mutually exclusive and are placed in the MSB of the flags
40 * field in a debugging record.
42 #define DEBUG_MODE_TERM 0x01000000
43 #define DEBUG_MODE_CONF 0x02000000
44 #define DEBUG_MODE_ALL (DEBUG_MODE_TERM | DEBUG_MODE_CONF)
45 #define DEBUG_MODE_NONE 0x00000000
46 #define DEBUG_OPT_ALL 0x00FFFFFF
47 #define DEBUG_OPT_NONE 0x00000000
53 * All operations on this record exposed in this header are MT-safe.
56 * A bitfield with the following format (bytes high to low)
57 * - [0] Debugging mode field (MSB) | Mode
58 * - [1] Arbitrary flag field | Option
59 * - [2] Arbitrary flag field | Option
60 * - [3] Arbitrary flag field (LSB) | Option
62 * ALL THESE BYTES ARE YOURS - EXCEPT MODE.
63 * ATTEMPT NO BIT OPS THERE.
65 * The MSB of this field determines the debug mode, Use the DEBUG_MODE*
66 * macros to manipulate this byte.
68 * The low 3 bytes of this field may be used to store arbitrary information.
69 * Usually they are used to store flags that tune how detailed the logging
70 * for a particular debug record is. Use the DEBUG_OPT* macros to manipulate
73 * All operations performed on this field should be done using the macros
74 * later in this header file. They are guaranteed to be atomic operations
75 * with respect to this field. Using anything except the macros to
76 * manipulate the flags field in a multithreaded environment results in
80 * Human-readable description of this debugging record.
83 atomic_uint_fast32_t flags
;
88 * Callback set for debugging code.
91 * Function pointer to call when the user requests that all debugs have a
94 struct debug_callbacks
{
97 * flags to set on debug flag fields
103 void (*debug_set_all
)(uint32_t flags
, bool set
);
107 * Check if a mode is set for a debug.
111 #define DEBUG_MODE_CHECK(name, mode) \
112 CHECK_FLAG_ATOMIC(&(name)->flags, (mode)&DEBUG_MODE_ALL)
115 * Check if an option bit is set for a debug.
119 #define DEBUG_OPT_CHECK(name, opt) \
120 CHECK_FLAG_ATOMIC(&(name)->flags, (opt)&DEBUG_OPT_ALL)
123 * Check if bits are set for a debug.
127 #define DEBUG_FLAGS_CHECK(name, fl) CHECK_FLAG_ATOMIC(&(name)->flags, (fl))
130 * Set modes on a debug.
134 #define DEBUG_MODE_SET(name, mode, onoff) \
137 SET_FLAG_ATOMIC(&(name)->flags, \
138 (mode)&DEBUG_MODE_ALL); \
140 UNSET_FLAG_ATOMIC(&(name)->flags, \
141 (mode)&DEBUG_MODE_ALL); \
144 /* Convenience macros for specific set operations. */
145 #define DEBUG_MODE_ON(name, mode) DEBUG_MODE_SET(name, mode, true)
146 #define DEBUG_MODE_OFF(name, mode) DEBUG_MODE_SET(name, mode, false)
149 * Set options on a debug.
153 #define DEBUG_OPT_SET(name, opt, onoff) \
156 SET_FLAG_ATOMIC(&(name)->flags, (opt)&DEBUG_OPT_ALL); \
158 UNSET_FLAG_ATOMIC(&(name)->flags, \
159 (opt)&DEBUG_OPT_ALL); \
162 /* Convenience macros for specific set operations. */
163 #define DEBUG_OPT_ON(name, opt) DEBUG_OPT_SET(name, opt, true)
164 #define DEBUG_OPT_OFF(name, opt) DEBUG_OPT_SET(name, opt, true)
167 * Set bits on a debug.
171 #define DEBUG_FLAGS_SET(name, fl, onoff) \
174 SET_FLAG_ATOMIC(&(name)->flags, (fl)); \
176 UNSET_FLAG_ATOMIC(&(name)->flags, (fl)); \
179 /* Convenience macros for specific set operations. */
180 #define DEBUG_FLAGS_ON(name, fl) DEBUG_FLAGS_SET(&(name)->flags, (type), true)
181 #define DEBUG_FLAGS_OFF(name, fl) DEBUG_FLAGS_SET(&(name)->flags, (type), false)
184 * Unset all modes and options on a debug.
188 #define DEBUG_CLEAR(name) RESET_FLAG_ATOMIC(&(name)->flags)
191 * Set all modes and options on a debug.
195 #define DEBUG_ON(name) \
196 SET_FLAG_ATOMIC(&(name)->flags, DEBUG_MODE_ALL | DEBUG_OPT_ALL)
199 * Map a vty node to the correct debugging mode flags. FRR behaves such that a
200 * debug statement issued under the config node persists to the config file,
201 * whereas the same debug statement issued from the enable node only persists
202 * for the current session.
206 #define DEBUG_NODE2MODE(vtynode) \
207 (((vtynode) == CONFIG_NODE) ? DEBUG_MODE_ALL : DEBUG_MODE_TERM)
210 * Debug at the given level to the default logging destination.
214 #define DEBUG(level, name, fmt, ...) \
216 if (DEBUG_MODE_CHECK(name, DEBUG_MODE_ALL)) \
217 zlog_##level(fmt, ##__VA_ARGS__); \
220 /* Convenience macros for the various levels. */
221 #define DEBUGE(name, fmt, ...) DEBUG(err, name, fmt, ##__VA_ARGS__)
222 #define DEBUGW(name, fmt, ...) DEBUG(warn, name, fmt, ##__VA_ARGS__)
223 #define DEBUGI(name, fmt, ...) DEBUG(info, name, fmt, ##__VA_ARGS__)
224 #define DEBUGN(name, fmt, ...) DEBUG(notice, name, fmt, ##__VA_ARGS__)
225 #define DEBUGD(name, fmt, ...) DEBUG(debug, name, fmt, ##__VA_ARGS__)
228 * Optional initializer for debugging. Highly recommended.
230 * This function installs common debugging commands and allows the caller to
231 * specify callbacks to take when these commands are issued, allowing the
232 * caller to respond to events such as a request to turn off all debugs.
236 void debug_init(const struct debug_callbacks
*cb
);
242 #endif /* _FRRDEBUG_H */