added files
[bcm963xx.git] / userapps / opensource / net-snmp / agent / helpers / debug_handler.c
1 #include <net-snmp/net-snmp-config.h>
2
3 #if HAVE_STRING_H
4 #include <string.h>
5 #else
6 #include <strings.h>
7 #endif
8
9 #include <net-snmp/net-snmp-includes.h>
10 #include <net-snmp/agent/net-snmp-agent-includes.h>
11
12 #include <net-snmp/agent/debug_handler.h>
13
14 #if HAVE_DMALLOC_H
15 #include <dmalloc.h>
16 #endif
17
18 /** @defgroup debug debug: print out debugging information about the handler chain being called.
19  *  This is a useful module for run-time
20  *  debugging of requests as the pass this handler in a calling chain.
21  *  All debugging output is done via the standard debugging routines
22  *  with a token name of "helper:debug", so use the -Dhelper:debug
23  *  command line flag to see the output when running the snmpd
24  *  demon. It's not recommended you compile this into a handler chain
25  *  during compile time, but instead use the "injectHandler" token in
26  *  the snmpd.conf file (or similar) to add it to the chain later:
27  *
28  *     injectHandler debug my_module_name
29  *
30  *  to see an example output, try:
31  *
32  *     injectHandler debug mibII/system
33  *
34  *  and then run snmpwalk on the "system" group.
35  *
36  *  @ingroup handler
37  *  @{
38  */
39
40 /** returns a debug handler that can be injected into a given
41  *  handler chain.
42  */
43 netsnmp_mib_handler *
44 netsnmp_get_debug_handler(void)
45 {
46     return netsnmp_create_handler("debug", netsnmp_debug_helper);
47 }
48
49 /** @internal debug print variables in a chain */
50 void
51 debug_print_requests(netsnmp_request_info *requests)
52 {
53     netsnmp_request_info *request;
54
55     for (request = requests; request; request = request->next) {
56         DEBUGMSGTL(("helper:debug", "      #%2d: ", request->index));
57         DEBUGMSGVAR(("helper:debug", request->requestvb));
58         DEBUGMSG(("helper:debug", "\n"));
59
60         if (request->processed)
61             DEBUGMSGTL(("helper:debug", "        [processed]\n"));
62         if (request->delegated)
63             DEBUGMSGTL(("helper:debug", "        [delegated]\n"));
64         if (request->status)
65             DEBUGMSGTL(("helper:debug", "        [status = %d]\n",
66                         request->status));
67         if (request->parent_data) {
68             netsnmp_data_list *lst;
69             DEBUGMSGTL(("helper:debug", "        [parent data ="));
70             for (lst = request->parent_data; lst; lst = lst->next) {
71                 DEBUGMSG(("helper:debug", " %s", lst->name));
72             }
73             DEBUGMSG(("helper:debug", "]\n"));
74         }
75     }
76 }
77
78
79 /** @internal Implements the debug handler */
80 int
81 netsnmp_debug_helper(netsnmp_mib_handler *handler,
82                      netsnmp_handler_registration *reginfo,
83                      netsnmp_agent_request_info *reqinfo,
84                      netsnmp_request_info *requests)
85 {
86
87     netsnmp_mib_handler *hptr;
88     int             i, ret, count;
89
90     DEBUGMSGTL(("helper:debug", "Entering Debugging Helper:\n"));
91     DEBUGMSGTL(("helper:debug", "  Handler Registration Info:\n"));
92     DEBUGMSGTL(("helper:debug", "    Name:        %s\n",
93                 reginfo->handlerName));
94     DEBUGMSGTL(("helper:debug", "    Context:     %s\n",
95                 reginfo->contextName));
96     DEBUGMSGTL(("helper:debug", "    Base OID:    "));
97     DEBUGMSGOID(("helper:debug", reginfo->rootoid, reginfo->rootoid_len));
98     DEBUGMSG(("helper:debug", "\n"));
99
100     DEBUGMSGTL(("helper:debug", "    Modes:       0x%x = ",
101                 reginfo->modes));
102     for (count = 0, i = reginfo->modes; i; i = i >> 1, count++) {
103         if (i & 0x01) {
104             DEBUGMSG(("helper:debug", "%s | ",
105                       se_find_label_in_slist("handler_can_mode",
106                                              0x01 << count)));
107         }
108     }
109     DEBUGMSG(("helper:debug", "\n"));
110
111     DEBUGMSGTL(("helper:debug", "    Priority:    %d\n",
112                 reginfo->priority));
113
114     DEBUGMSGTL(("helper:debug", "  Handler Calling Chain:\n"));
115     DEBUGMSGTL(("helper:debug", "   "));
116     for (hptr = reginfo->handler; hptr; hptr = hptr->next) {
117         DEBUGMSG(("helper:debug", " -> %s", hptr->handler_name));
118         if (hptr->myvoid)
119             DEBUGMSG(("helper:debug", " [myvoid = %x]", hptr->myvoid));
120     }
121     DEBUGMSG(("helper:debug", "\n"));
122
123     DEBUGMSGTL(("helper:debug", "  Request information:\n"));
124     DEBUGMSGTL(("helper:debug", "    Mode:        %s (%d = 0x%x)\n",
125                 se_find_label_in_slist("agent_mode", reqinfo->mode),
126                 reqinfo->mode, reqinfo->mode));
127     DEBUGMSGTL(("helper:debug", "    Request Variables:\n"));
128     debug_print_requests(requests);
129
130     DEBUGMSGTL(("helper:debug", "  --- calling next handler --- \n"));
131     ret = netsnmp_call_next_handler(handler, reginfo, reqinfo, requests);
132
133     DEBUGMSGTL(("helper:debug", "  Results:\n"));
134     DEBUGMSGTL(("helper:debug", "    Returned code: %d\n", ret));
135     DEBUGMSGTL(("helper:debug", "    Returned Variables:\n"));
136     debug_print_requests(requests);
137
138     DEBUGMSGTL(("helper:debug", "Exiting Debugging Helper:\n"));
139     return ret;
140 }
141
142 /** initializes the debug helper which then registers a debug
143  *  handler as a run-time injectable handler for configuration file
144  *  use.
145  */
146 void
147 netsnmp_init_debug_helper(void)
148 {
149     netsnmp_register_handler_by_name("debug", netsnmp_get_debug_handler());
150 }