1 #include <net-snmp/net-snmp-config.h>
12 #include <sys/types.h>
14 #include <netinet/in.h>
29 #include <net-snmp/types.h>
30 #include <net-snmp/output_api.h>
31 #include <net-snmp/library/snmp_debug.h> /* For this file's "internal" definitions */
32 #include <net-snmp/config_api.h>
33 #include <net-snmp/utilities.h>
35 #include <net-snmp/library/mib.h>
36 #include <net-snmp/library/snmp_api.h>
38 static int dodebug
= SNMP_ALWAYS_DEBUG
;
39 static int debug_num_tokens
= 0;
40 static char *debug_tokens
[MAX_DEBUG_TOKENS
];
41 static int debug_print_everything
= 0;
44 * indent debugging: provide a space padded section to return an indent for
46 static int debugindent
= 0;
48 static char debugindentchars
[] =
52 * Prototype definitions
54 void debug_config_register_tokens(const char *configtoken
,
56 void debug_config_turn_on_debugging(const char *configtoken
,
62 return debugindentchars
;
66 debug_indent_add(int amount
)
68 if (debugindent
+ amount
>= 0 && debugindent
+ amount
< 80) {
69 debugindentchars
[debugindent
] = ' ';
70 debugindent
+= amount
;
71 debugindentchars
[debugindent
] = '\0';
76 debug_config_register_tokens(const char *configtoken
, char *tokens
)
78 debug_register_tokens(tokens
);
82 debug_config_turn_on_debugging(const char *configtoken
, char *line
)
84 snmp_set_do_debugging(atoi(line
));
90 debugindentchars
[0] = '\0'; /* zero out the debugging indent array. */
91 register_prenetsnmp_mib_handler("snmp", "doDebugging",
92 debug_config_turn_on_debugging
, NULL
,
94 register_prenetsnmp_mib_handler("snmp", "debugTokens",
95 debug_config_register_tokens
, NULL
,
100 debug_register_tokens(char *tokens
)
104 if (tokens
== 0 || *tokens
== 0)
107 newp
= strdup(tokens
); /* strtok messes it up */
108 cp
= strtok(newp
, DEBUG_TOKEN_DELIMITER
);
110 if (strlen(cp
) < MAX_DEBUG_TOKEN_LEN
) {
111 if (strcasecmp(cp
, DEBUG_ALWAYS_TOKEN
) == 0)
112 debug_print_everything
= 1;
113 else if (debug_num_tokens
< MAX_DEBUG_TOKENS
)
114 debug_tokens
[debug_num_tokens
++] = strdup(cp
);
116 cp
= strtok(NULL
, DEBUG_TOKEN_DELIMITER
);
123 * debug_is_token_registered(char *TOKEN):
125 * returns SNMPERR_SUCCESS
128 * if TOKEN has been registered and debugging support is turned on.
131 debug_is_token_registered(const char *token
)
136 * debugging flag is on or off
139 return SNMPERR_GENERR
;
141 if (debug_num_tokens
== 0 || debug_print_everything
) {
143 * no tokens specified, print everything
145 return SNMPERR_SUCCESS
;
147 for (i
= 0; i
< debug_num_tokens
; i
++) {
148 if (strncmp(debug_tokens
[i
], token
, strlen(debug_tokens
[i
])) ==
150 return SNMPERR_SUCCESS
;
154 return SNMPERR_GENERR
;
159 debugmsg(const char *token
, const char *format
, ...)
168 va_start(debugargs
, format
);
174 token
= va_arg(debugargs
, const char *);
175 format
= va_arg(debugargs
, const char *); /* ??? */
178 if (debug_is_token_registered(token
) == SNMPERR_SUCCESS
) {
179 snmp_vlog(LOG_DEBUG
, format
, debugargs
);
185 debugmsg_oid(const char *token
, const oid
* theoid
, size_t len
)
188 size_t buf_len
= 0, out_len
= 0;
190 if (sprint_realloc_objid(&buf
, &buf_len
, &out_len
, 1, theoid
, len
)) {
192 debugmsg(token
, "%s", buf
);
196 debugmsg(token
, "%s [TRUNCATED]", buf
);
206 debugmsg_var(const char *token
, netsnmp_variable_list
* var
)
209 size_t buf_len
= 0, out_len
= 0;
211 if (var
== NULL
|| token
== NULL
) {
215 if (sprint_realloc_variable(&buf
, &buf_len
, &out_len
, 1,
216 var
->name
, var
->name_length
, var
)) {
218 debugmsg(token
, "%s", buf
);
222 debugmsg(token
, "%s [TRUNCATED]", buf
);
232 debugmsg_oidrange(const char *token
, const oid
* theoid
, size_t len
,
233 size_t var_subid
, oid range_ubound
)
236 size_t buf_len
= 0, out_len
= 0, i
= 0;
239 if (var_subid
== 0) {
240 rc
= sprint_realloc_objid(&buf
, &buf_len
, &out_len
, 1, theoid
,
244 rc
= sprint_realloc_objid(&buf
, &buf_len
, &out_len
, 1, theoid
,
247 sprintf(tmpbuf
, ".%lu--%lu", theoid
[var_subid
- 1],
249 rc
= snmp_strcat(&buf
, &buf_len
, &out_len
, 1, tmpbuf
);
251 for (i
= var_subid
; i
< len
; i
++) {
252 sprintf(tmpbuf
, ".%lu", theoid
[i
]);
253 if (!snmp_strcat(&buf
, &buf_len
, &out_len
, 1, tmpbuf
)) {
263 debugmsg(token
, "%s%s", buf
, rc
? "" : " [TRUNCATED]");
269 debugmsg_hex(const char *token
, u_char
* thedata
, size_t len
)
272 size_t buf_len
= 0, out_len
= 0;
274 if (sprint_realloc_hexstring
275 (&buf
, &buf_len
, &out_len
, 1, thedata
, len
)) {
277 debugmsg(token
, "%s", buf
);
281 debugmsg(token
, "%s [TRUNCATED]", buf
);
291 debugmsg_hextli(const char *token
, u_char
* thedata
, size_t len
)
293 char buf
[SPRINT_MAX_LEN
], token2
[SPRINT_MAX_LEN
];
295 size_t b3_len
= 0, o3_len
= 0;
297 sprintf(token2
, "dumpx_%s", token
);
300 * XX tracing lines removed from this function DEBUGTRACE;
303 for (incr
= 16; len
> 0; len
-= incr
, thedata
+= incr
) {
304 if ((int) len
< incr
) {
308 * XXnext two lines were DEBUGPRINTINDENT(token);
310 sprintf(buf
, "dumpx%s", token
);
311 debugmsg(buf
, "%s: %s", token2
, debug_indent());
312 if (sprint_realloc_hexstring
313 (&b3
, &b3_len
, &o3_len
, 1, thedata
, incr
)) {
315 debugmsg(token2
, "%s", b3
);
319 debugmsg(token2
, "%s [TRUNCATED]", b3
);
332 debugmsgtoken(const char *token
, const char *format
, ...)
334 debugmsgtoken(va_alist
)
341 va_start(debugargs
, format
);
346 token
= va_arg(debugargs
, const char *);
349 debugmsg(token
, "%s: ", token
);
355 * for speed, these shouldn't be in default_storage space
358 snmp_set_do_debugging(int val
)
364 snmp_get_do_debugging(void)