Skip to content

Commit

Permalink
Merge pull request #16664 from mjstapp/igor_debug_simplify
Browse files Browse the repository at this point in the history
*: simplify frrlib debug
  • Loading branch information
Jafaral authored Aug 29, 2024
2 parents f1e4ef4 + 830972c commit 77e1a26
Show file tree
Hide file tree
Showing 39 changed files with 222 additions and 772 deletions.
4 changes: 2 additions & 2 deletions lib/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <lib/version.h>

#include "command.h"
#include "debug.h"
#include "frrstr.h"
#include "memory.h"
#include "log.h"
Expand Down Expand Up @@ -2463,8 +2464,7 @@ const char *host_config_get(void)
void cmd_show_lib_debugs(struct vty *vty)
{
route_map_show_debug(vty);
mgmt_debug_be_client_show_debug(vty);
mgmt_debug_fe_client_show_debug(vty);
debug_status_write(vty);
}

void install_default(enum node_type node)
Expand Down
4 changes: 1 addition & 3 deletions lib/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ enum node_type {
CONFIG_NODE, /* Config node. Default mode of config file. */
PREFIX_NODE, /* ip prefix-list node. */
PREFIX_IPV6_NODE, /* ipv6 prefix-list node. */
LIB_DEBUG_NODE, /* frrlib debug node. */
DEBUG_NODE, /* Debug node. */
VRF_DEBUG_NODE, /* Vrf Debug node. */
NORTHBOUND_DEBUG_NODE, /* Northbound Debug node. */
DEBUG_VNC_NODE, /* Debug VNC node. */
RMAP_DEBUG_NODE, /* Route-map debug node */
RESOLVER_DEBUG_NODE, /* Resolver debug node */
MGMT_BE_DEBUG_NODE, /* mgmtd backend-client debug node */
MGMT_FE_DEBUG_NODE, /* mgmtd frontend-client debug node */
AAA_NODE, /* AAA node. */
EXTLOG_NODE, /* RFC5424 & co. extended syslog */
KEYCHAIN_NODE, /* Key-chain node. */
Expand Down
63 changes: 48 additions & 15 deletions lib/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,75 @@
#include "debug.h"
#include "command.h"

static struct debug_cb_list_head cb_head;
static struct debug_list_head debug_head;

DECLARE_LIST(debug_cb_list, struct debug_callbacks, item);
DECLARE_LIST(debug_list, struct debug, item);

/* All code in this section should be reentrant and MT-safe */

DEFUN_NOSH(debug_all, debug_all_cmd, "[no] debug all",
NO_STR DEBUG_STR "Toggle all debugging output\n")
DEFUN_NOSH (debug_all,
debug_all_cmd,
"[no] debug all",
NO_STR DEBUG_STR
"Toggle all debugging output\n")
{
struct debug_callbacks *cb;

struct debug *debug;
bool set = !strmatch(argv[0]->text, "no");
uint32_t mode = DEBUG_NODE2MODE(vty->node);

frr_each (debug_cb_list, &cb_head, cb)
cb->debug_set_all(mode, set);
frr_each (debug_list, &debug_head, debug) {
DEBUG_MODE_SET(debug, mode, set);

/* If all modes have been turned off, don't preserve options. */
if (!DEBUG_MODE_CHECK(debug, DEBUG_MODE_ALL))
DEBUG_CLEAR(debug);
}

return CMD_SUCCESS;
}

/* ------------------------------------------------------------------------- */

void debug_init(struct debug_callbacks *cb)
void debug_status_write(struct vty *vty)
{
struct debug *debug;

frr_each (debug_list, &debug_head, debug) {
if (DEBUG_MODE_CHECK(debug, DEBUG_MODE_ALL))
vty_out(vty, " %s debugging is on\n", debug->desc);
}
}

static int config_write_debug(struct vty *vty)
{
static bool inited = false;
struct debug *debug;

if (!inited) {
inited = true;
debug_cb_list_init(&cb_head);
frr_each (debug_list, &debug_head, debug) {
if (DEBUG_MODE_CHECK(debug, DEBUG_MODE_CONF))
vty_out(vty, "%s\n", debug->conf);
}

debug_cb_list_add_head(&cb_head, cb);
return 0;
}

void debug_init_cli(void)
static struct cmd_node debug_node = {
.name = "debug",
.node = LIB_DEBUG_NODE,
.prompt = "",
.config_write = config_write_debug,
};

void debug_install(struct debug *debug)
{
debug_list_add_tail(&debug_head, debug);
}

void debug_init(void)
{
debug_list_init(&debug_head);

install_node(&debug_node);

install_element(ENABLE_NODE, &debug_all_cmd);
install_element(CONFIG_NODE, &debug_all_cmd);
}
49 changes: 14 additions & 35 deletions lib/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern "C" {
#define DEBUG_OPT_NONE 0x00000000


PREDECL_LIST(debug_list);
/*
* Debugging record.
*
Expand Down Expand Up @@ -63,37 +64,18 @@ extern "C" {
* manipulate the flags field in a multithreaded environment results in
* undefined behavior.
*
* conf
* The configuration string that will be written to the config file.
*
* desc
* Human-readable description of this debugging record.
*/
struct debug {
atomic_uint_fast32_t flags;
const char *conf;
const char *desc;
};

PREDECL_LIST(debug_cb_list);
/*
* Callback set for debugging code.
*
* debug_set_all
* Function pointer to call when the user requests that all debugs have a
* mode set.
*/
struct debug_callbacks {
/*
* Linked list of Callbacks to call
*/
struct debug_cb_list_item item;

/*
* flags
* flags to set on debug flag fields
*
* set
* true: set flags
* false: unset flags
*/
void (*debug_set_all)(uint32_t flags, bool set);
struct debug_list_item item;
};

/*
Expand Down Expand Up @@ -217,22 +199,19 @@ struct debug_callbacks {
#define DEBUGN(name, fmt, ...) DEBUG(notice, name, fmt, ##__VA_ARGS__)
#define DEBUGD(name, fmt, ...) DEBUG(debug, name, fmt, ##__VA_ARGS__)

/* Show current debugging status. */
void debug_status_write(struct vty *vty);

/*
* Optional initializer for debugging. Highly recommended.
*
* This function installs common debugging commands and allows the caller to
* specify callbacks to take when these commands are issued, allowing the
* caller to respond to events such as a request to turn off all debugs.
*
* MT-Safe
* Register a debug item.
*/
void debug_init(struct debug_callbacks *cb);
void debug_install(struct debug *debug);

/*
* Turn on the cli to turn on/off debugs.
* Should only be called by libfrr
* Initialize debugging.
* Should only be called by libfrr.
*/
void debug_init_cli(void);
void debug_init(void);

#ifdef __cplusplus
}
Expand Down
3 changes: 1 addition & 2 deletions lib/libfrr.c
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ struct event_loop *frr_init(void)

vty_init(master, di->log_always);
lib_cmd_init();
debug_init();

frr_pthread_init();
#ifdef HAVE_SCRIPTING
Expand All @@ -825,8 +826,6 @@ struct event_loop *frr_init(void)
"%s: failed to initialize northbound database",
__func__);

debug_init_cli();

return master;
}

Expand Down
30 changes: 3 additions & 27 deletions lib/mgmt_be_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ struct mgmt_be_client {
frr_each_safe (mgmt_be_txns, &(client_ctx)->txn_head, (txn))

struct debug mgmt_dbg_be_client = {
.conf = "debug mgmt client backend",
.desc = "Management backend client operations"
};

Expand Down Expand Up @@ -1258,31 +1259,6 @@ DEFPY(debug_mgmt_client_be, debug_mgmt_client_be_cmd,
return CMD_SUCCESS;
}

static int mgmt_debug_be_client_config_write(struct vty *vty)
{
if (DEBUG_MODE_CHECK(&mgmt_dbg_be_client, DEBUG_MODE_CONF))
vty_out(vty, "debug mgmt client backend\n");

return 1;
}

void mgmt_debug_be_client_show_debug(struct vty *vty)
{
if (debug_check_be_client())
vty_out(vty, "debug mgmt client backend\n");
}

static struct debug_callbacks mgmt_dbg_be_client_cbs = {
.debug_set_all = mgmt_debug_client_be_set
};

static struct cmd_node mgmt_dbg_node = {
.name = "debug mgmt client backend",
.node = MGMT_BE_DEBUG_NODE,
.prompt = "",
.config_write = mgmt_debug_be_client_config_write,
};

struct mgmt_be_client *mgmt_be_client_create(const char *client_name,
struct mgmt_be_client_cbs *cbs,
uintptr_t user_data,
Expand Down Expand Up @@ -1328,8 +1304,8 @@ struct mgmt_be_client *mgmt_be_client_create(const char *client_name,

void mgmt_be_client_lib_vty_init(void)
{
debug_init(&mgmt_dbg_be_client_cbs);
install_node(&mgmt_dbg_node);
debug_install(&mgmt_dbg_be_client);

install_element(ENABLE_NODE, &debug_mgmt_client_be_cmd);
install_element(CONFIG_NODE, &debug_mgmt_client_be_cmd);
}
Expand Down
5 changes: 0 additions & 5 deletions lib/mgmt_be_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ mgmt_be_client_create(const char *name, struct mgmt_be_client_cbs *cbs,
*/
extern void mgmt_be_client_lib_vty_init(void);

/*
* Print enabled debugging commands.
*/
extern void mgmt_debug_be_client_show_debug(struct vty *vty);

/*
* [Un]-subscribe with MGMTD for one or more YANG subtree(s).
*
Expand Down
30 changes: 3 additions & 27 deletions lib/mgmt_fe_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct mgmt_fe_client {
frr_each_safe (mgmt_sessions, &(client)->sessions, (session))

struct debug mgmt_dbg_fe_client = {
.conf = "debug mgmt client frontend",
.desc = "Management frontend client operations"
};

Expand Down Expand Up @@ -805,31 +806,6 @@ DEFPY(debug_mgmt_client_fe, debug_mgmt_client_fe_cmd,
return CMD_SUCCESS;
}

static int mgmt_debug_fe_client_config_write(struct vty *vty)
{
if (DEBUG_MODE_CHECK(&mgmt_dbg_fe_client, DEBUG_MODE_CONF))
vty_out(vty, "debug mgmt client frontend\n");

return CMD_SUCCESS;
}

void mgmt_debug_fe_client_show_debug(struct vty *vty)
{
if (debug_check_fe_client())
vty_out(vty, "debug mgmt client frontend\n");
}

static struct debug_callbacks mgmt_dbg_fe_client_cbs = {
.debug_set_all = mgmt_debug_client_fe_set
};

static struct cmd_node mgmt_dbg_node = {
.name = "debug mgmt client frontend",
.node = MGMT_FE_DEBUG_NODE,
.prompt = "",
.config_write = mgmt_debug_fe_client_config_write,
};

/*
* Initialize library and try connecting with MGMTD.
*/
Expand Down Expand Up @@ -870,8 +846,8 @@ struct mgmt_fe_client *mgmt_fe_client_create(const char *client_name,

void mgmt_fe_client_lib_vty_init(void)
{
debug_init(&mgmt_dbg_fe_client_cbs);
install_node(&mgmt_dbg_node);
debug_install(&mgmt_dbg_fe_client);

install_element(ENABLE_NODE, &debug_mgmt_client_fe_cmd);
install_element(CONFIG_NODE, &debug_mgmt_client_fe_cmd);
}
Expand Down
5 changes: 0 additions & 5 deletions lib/mgmt_fe_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,6 @@ mgmt_fe_client_create(const char *client_name, struct mgmt_fe_client_cbs *cbs,
*/
extern void mgmt_fe_client_lib_vty_init(void);

/*
* Print enabled debugging commands.
*/
extern void mgmt_debug_fe_client_show_debug(struct vty *vty);

/*
* Create a new Session for a Frontend Client connection.
*
Expand Down
2 changes: 0 additions & 2 deletions lib/northbound.h
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,6 @@ DECLARE_HOOK(nb_notification_send, (const char *xpath, struct list *arguments),
(xpath, arguments));
DECLARE_HOOK(nb_notification_tree_send,
(const char *xpath, const struct lyd_node *tree), (xpath, tree));
DECLARE_HOOK(nb_client_debug_config_write, (struct vty *vty), (vty));
DECLARE_HOOK(nb_client_debug_set_all, (uint32_t flags, bool set), (flags, set));

/* Northbound debugging records */
extern struct debug nb_dbg_cbs_config;
Expand Down
Loading

0 comments on commit 77e1a26

Please sign in to comment.