]>
git.proxmox.com Git - mirror_frr.git/blob - lib/debug.h
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"
30 * FRR's convention is that a debug statement issued under the vty CONFIG_NODE
31 * persists to the config file, whereas the same debug statement issued from
32 * the ENABLE_NODE only persists for the current session. These are mapped to
33 * DEBUG_MODE_CONF and DEBUG_MODE_TERM respectively.
35 * They are not mutually exclusive and are placed in the MSB of the flags
36 * field in a debugging record.
38 #define DEBUG_MODE_TERM 0x01000000
39 #define DEBUG_MODE_CONF 0x02000000
40 #define DEBUG_MODE_ALL (DEBUG_MODE_TERM | DEBUG_MODE_CONF)
41 #define DEBUG_MODE_NONE 0x00000000
42 #define DEBUG_OPT_ALL 0x00FFFFFF
43 #define DEBUG_OPT_NONE 0x00000000
49 * All operations on this record exposed in this header are MT-safe.
52 * A bitfield with the following format (bytes high to low)
53 * - [0] Debugging mode field (MSB) | Mode
54 * - [1] Arbitrary flag field | Option
55 * - [2] Arbitrary flag field | Option
56 * - [3] Arbitrary flag field (LSB) | Option
58 * ALL THESE BYTES ARE YOURS - EXCEPT MODE.
59 * ATTEMPT NO BIT OPS THERE.
61 * The MSB of this field determines the debug mode, Use the DEBUG_MODE*
62 * macros to manipulate this byte.
64 * The low 3 bytes of this field may be used to store arbitrary information.
65 * Usually they are used to store flags that tune how detailed the logging
66 * for a particular debug record is. Use the DEBUG_OPT* macros to manipulate
69 * All operations performed on this field should be done using the macros
70 * later in this header file. They are guaranteed to be atomic operations
71 * with respect to this field. Using anything except the macros to
72 * manipulate the flags field in a multithreaded environment results in
76 * Human-readable description of this debugging record.
79 _Atomic
uint32_t flags
;
84 * Callback set for debugging code.
87 * Function pointer to call when the user requests that all debugs have a
90 struct debug_callbacks
{
93 * flags to set on debug flag fields
99 void (*debug_set_all
)(uint32_t flags
, bool set
);
103 * Check if a mode is set for a debug.
107 #define DEBUG_MODE_CHECK(name, mode) \
108 CHECK_FLAG_ATOMIC(&(name)->flags, (mode)&DEBUG_MODE_ALL)
111 * Check if an option bit is set for a debug.
115 #define DEBUG_OPT_CHECK(name, opt) \
116 CHECK_FLAG_ATOMIC(&(name)->flags, (opt)&DEBUG_OPT_ALL)
119 * Check if bits are set for a debug.
123 #define DEBUG_FLAGS_CHECK(name, fl) CHECK_FLAG_ATOMIC(&(name)->flags, (fl))
126 * Set modes on a debug.
130 #define DEBUG_MODE_SET(name, mode, onoff) \
133 SET_FLAG_ATOMIC(&(name)->flags, \
134 (mode)&DEBUG_MODE_ALL); \
136 UNSET_FLAG_ATOMIC(&(name)->flags, \
137 (mode)&DEBUG_MODE_ALL); \
140 /* Convenience macros for specific set operations. */
141 #define DEBUG_MODE_ON(name, mode) DEBUG_MODE_SET(name, mode, true)
142 #define DEBUG_MODE_OFF(name, mode) DEBUG_MODE_SET(name, mode, false)
145 * Set options on a debug.
149 #define DEBUG_OPT_SET(name, opt, onoff) \
152 SET_FLAG_ATOMIC(&(name)->flags, (opt)&DEBUG_OPT_ALL); \
154 UNSET_FLAG_ATOMIC(&(name)->flags, \
155 (opt)&DEBUG_OPT_ALL); \
158 /* Convenience macros for specific set operations. */
159 #define DEBUG_OPT_ON(name, opt) DEBUG_OPT_SET(name, opt, true)
160 #define DEBUG_OPT_OFF(name, opt) DEBUG_OPT_SET(name, opt, true)
163 * Set bits on a debug.
167 #define DEBUG_FLAGS_SET(name, fl, onoff) \
170 SET_FLAG_ATOMIC(&(name)->flags, (fl)); \
172 UNSET_FLAG_ATOMIC(&(name)->flags, (fl)); \
175 /* Convenience macros for specific set operations. */
176 #define DEBUG_FLAGS_ON(name, fl) DEBUG_FLAGS_SET(&(name)->flags, (type), true)
177 #define DEBUG_FLAGS_OFF(name, fl) DEBUG_FLAGS_SET(&(name)->flags, (type), false)
180 * Unset all modes and options on a debug.
184 #define DEBUG_CLEAR(name) RESET_FLAG_ATOMIC(&(name)->flags)
187 * Set all modes and options on a debug.
191 #define DEBUG_ON(name) \
192 SET_FLAG_ATOMIC(&(name)->flags, DEBUG_MODE_ALL | DEBUG_OPT_ALL)
195 * Map a vty node to the correct debugging mode flags. FRR behaves such that a
196 * debug statement issued under the config node persists to the config file,
197 * whereas the same debug statement issued from the enable node only persists
198 * for the current session.
202 #define DEBUG_NODE2MODE(vtynode) \
203 (((vtynode) == CONFIG_NODE) ? DEBUG_MODE_ALL : DEBUG_MODE_TERM)
206 * Debug at the given level to the default logging destination.
210 #define DEBUG(level, name, fmt, ...) \
212 if (DEBUG_MODE_CHECK(name, DEBUG_MODE_ALL)) \
213 zlog_##level(fmt, ##__VA_ARGS__); \
216 /* Convenience macros for the various levels. */
217 #define DEBUGE(name, fmt, ...) DEBUG(err, name, fmt, ##__VA_ARGS__)
218 #define DEBUGW(name, fmt, ...) DEBUG(warn, name, fmt, ##__VA_ARGS__)
219 #define DEBUGI(name, fmt, ...) DEBUG(info, name, fmt, ##__VA_ARGS__)
220 #define DEBUGN(name, fmt, ...) DEBUG(notice, name, fmt, ##__VA_ARGS__)
221 #define DEBUGD(name, fmt, ...) DEBUG(debug, name, fmt, ##__VA_ARGS__)
224 * Optional initializer for debugging. Highly recommended.
226 * This function installs common debugging commands and allows the caller to
227 * specify callbacks to take when these commands are issued, allowing the
228 * caller to respond to events such as a request to turn off all debugs.
232 void debug_init(const struct debug_callbacks
*cb
);
234 #endif /* _FRRDEBUG_H */