snmp_debug.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Portions of this file are subject to the following copyright(s). See
  3. * the Net-SNMP's COPYING file for more details and other copyrights
  4. * that may apply:
  5. *
  6. * Portions of this file are copyrighted by:
  7. * Copyright (c) 2016 VMware, Inc. All rights reserved.
  8. * Use is subject to license terms specified in the COPYING file
  9. * distributed with the Net-SNMP package.
  10. */
  11. #ifndef SNMP_DEBUG_H
  12. #define SNMP_DEBUG_H
  13. #include <net-snmp/library/netsnmp-attribute-format.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /*
  18. * snmp_debug.h:
  19. *
  20. * - prototypes for snmp debugging routines.
  21. * - easy to use macros to wrap around the functions. This also provides
  22. * the ability to remove debugging code easily from the applications at
  23. * compile time.
  24. */
  25. /*
  26. * These functions should not be used, if at all possible. Instead, use
  27. * the macros below.
  28. */
  29. NETSNMP_IMPORT
  30. void debugmsg(const char *token, const char *format, ...)
  31. NETSNMP_ATTRIBUTE_FORMAT(printf, 2, 3);
  32. NETSNMP_IMPORT
  33. void debugmsgtoken(const char *token, const char *format,
  34. ...)
  35. NETSNMP_ATTRIBUTE_FORMAT(printf, 2, 3);
  36. NETSNMP_IMPORT
  37. void debug_combo_nc(const char *token, const char *format,
  38. ...)
  39. NETSNMP_ATTRIBUTE_FORMAT(printf, 2, 3);
  40. NETSNMP_IMPORT
  41. void debugmsg_oid(const char *token, const oid * theoid,
  42. size_t len);
  43. NETSNMP_IMPORT
  44. void debugmsg_suboid(const char *token, const oid * theoid,
  45. size_t len);
  46. NETSNMP_IMPORT
  47. void debugmsg_var(const char *token,
  48. netsnmp_variable_list * var);
  49. NETSNMP_IMPORT
  50. void debugmsg_oidrange(const char *token,
  51. const oid * theoid, size_t len,
  52. size_t var_subid, oid range_ubound);
  53. NETSNMP_IMPORT
  54. void debugmsg_hex(const char *token, const u_char * thedata,
  55. size_t len);
  56. NETSNMP_IMPORT
  57. void debugmsg_hextli(const char *token, const u_char * thedata,
  58. size_t len);
  59. NETSNMP_IMPORT
  60. void debug_indent_add(int amount);
  61. NETSNMP_IMPORT
  62. void debug_indent_reset(void);
  63. NETSNMP_IMPORT
  64. int debug_indent_get(void);
  65. NETSNMP_IMPORT
  66. void debug_indent_reset(void);
  67. /*
  68. * What is said above is true for this function as well. Further this
  69. * function is deprecated and only provided for backwards compatibility.
  70. * Please use "%*s", debug_indent_get(), "" if you used this one before.
  71. */
  72. NETSNMP_IMPORT
  73. const char *debug_indent(void);
  74. /*
  75. * Use these macros instead of the functions above to allow them to be
  76. * re-defined at compile time to NOP for speed optimization.
  77. *
  78. * They need to be called enclosing all the arguments in a single set of ()s.
  79. * Example:
  80. * DEBUGMSGTL(("token", "debugging of something %s related\n", "snmp"));
  81. *
  82. * Usage:
  83. * All of the functions take a "token" argument that helps determine when
  84. * the output in question should be printed. See the snmpcmd.1 manual page
  85. * on the -D flag to turn on/off output for a given token on the command line.
  86. *
  87. * DEBUGMSG((token, format, ...)): equivalent to printf(format, ...)
  88. * (if "token" debugging output
  89. * is requested by the user)
  90. *
  91. * DEBUGMSGT((token, format, ...)): equivalent to DEBUGMSG, but prints
  92. * "token: " at the beginning of the
  93. * line for you.
  94. *
  95. * DEBUGTRACE Insert this token anywhere you want
  96. * tracing output displayed when the
  97. * "trace" debugging token is selected.
  98. *
  99. * DEBUGMSGL((token, format, ...)): equivalent to DEBUGMSG, but includes
  100. * DEBUGTRACE debugging line just before
  101. * yours.
  102. *
  103. * DEBUGMSGTL((token, format, ...)): Same as DEBUGMSGL and DEBUGMSGT
  104. * combined.
  105. *
  106. * Important:
  107. * It is considered best if you use DEBUGMSGTL() everywhere possible, as it
  108. * gives the nicest format output and provides tracing support just before
  109. * every debugging statement output.
  110. *
  111. * To print multiple pieces to a single line in one call, use:
  112. *
  113. * DEBUGMSGTL(("token", "line part 1"));
  114. * DEBUGMSG (("token", " and part 2\n"));
  115. *
  116. * to get:
  117. *
  118. * token: line part 1 and part 2
  119. *
  120. * as debugging output.
  121. *
  122. *
  123. * Each of these macros also have a version with a suffix of '_NC'. The
  124. * NC suffix stands for 'No Check', which means that no check will be
  125. * performed to see if debug is enabled or if the token has been turned
  126. * on. These NC versions are intended for use within a DEBUG_IF {} block,
  127. * where the debug/token check has already been performed.
  128. */
  129. #ifndef NETSNMP_NO_DEBUGGING /* make sure we're wanted */
  130. /*
  131. * define two macros : one macro with, one without,
  132. * a test if debugging is enabled.
  133. *
  134. * Generally, use the macro with _DBG_IF_
  135. */
  136. /******************* Start private macros ************************/
  137. #define _DBG_IF_ snmp_get_do_debugging()
  138. #define DEBUGIF(x) if (_DBG_IF_ && debug_is_token_registered(x) == SNMPERR_SUCCESS)
  139. #define __DBGMSGT(x) debugmsgtoken x, debugmsg x
  140. #define __DBGMSG_NC(x) debugmsg x
  141. #define __DBGMSGT_NC(x) debug_combo_nc x
  142. #define __DBGMSGL_NC(x) __DBGTRACE; debugmsg x
  143. #define __DBGMSGTL_NC(x) __DBGTRACE; debug_combo_nc x
  144. #ifdef NETSNMP_FUNCTION
  145. #define __DBGTRACE __DBGMSGT(("trace","%s(): %s, %d:\n",\
  146. NETSNMP_FUNCTION,__FILE__,__LINE__))
  147. #define __DBGTRACETOK(x) __DBGMSGT((x,"%s(): %s, %d:\n", \
  148. NETSNMP_FUNCTION,__FILE__,__LINE__))
  149. #else
  150. #define __DBGTRACE __DBGMSGT(("trace"," %s, %d:\n", __FILE__,__LINE__))
  151. #define __DBGTRACETOK(x) __DBGMSGT((x," %s, %d:\n", __FILE__,__LINE__))
  152. #endif
  153. #define __DBGMSGL(x) __DBGTRACE, debugmsg x
  154. #define __DBGMSGTL(x) __DBGTRACE, debugmsgtoken x, debugmsg x
  155. #define __DBGMSGOID(x) debugmsg_oid x
  156. #define __DBGMSGSUBOID(x) debugmsg_suboid x
  157. #define __DBGMSGVAR(x) debugmsg_var x
  158. #define __DBGMSGOIDRANGE(x) debugmsg_oidrange x
  159. #define __DBGMSGHEX(x) debugmsg_hex x
  160. #define __DBGMSGHEXTLI(x) debugmsg_hextli x
  161. #define __DBGINDENT() debug_indent_get()
  162. #define __DBGINDENTADD(x) debug_indent_add(x)
  163. #define __DBGINDENTMORE() debug_indent_add(2)
  164. #define __DBGINDENTLESS() debug_indent_add(-2)
  165. #define __DBGPRINTINDENT(token) __DBGMSGTL((token, "%*s", __DBGINDENT(), ""))
  166. #define __DBGDUMPHEADER(token,x) \
  167. __DBGPRINTINDENT("dumph_" token); \
  168. debugmsg("dumph_" token,x); \
  169. if (debug_is_token_registered("dumpx" token) == SNMPERR_SUCCESS || \
  170. debug_is_token_registered("dumpv" token) == SNMPERR_SUCCESS || \
  171. (debug_is_token_registered("dumpx_" token) != SNMPERR_SUCCESS && \
  172. debug_is_token_registered("dumpv_" token) != SNMPERR_SUCCESS)) { \
  173. debugmsg("dumph_" token,"\n"); \
  174. } else { \
  175. debugmsg("dumph_" token," "); \
  176. } \
  177. __DBGINDENTMORE()
  178. #define __DBGDUMPSECTION(token,x) \
  179. __DBGPRINTINDENT("dumph_" token); \
  180. debugmsg("dumph_" token,"%s\n",x);\
  181. __DBGINDENTMORE()
  182. #define __DBGDUMPSETUP(token,buf,len) \
  183. debugmsg("dumpx" token, "dumpx_%s:%*s", token, __DBGINDENT(), ""); \
  184. __DBGMSGHEX(("dumpx_" token,buf,len)); \
  185. if (debug_is_token_registered("dumpv" token) == SNMPERR_SUCCESS || \
  186. debug_is_token_registered("dumpv_" token) != SNMPERR_SUCCESS) { \
  187. debugmsg("dumpx_" token,"\n"); \
  188. } else { \
  189. debugmsg("dumpx_" token," "); \
  190. } \
  191. debugmsg("dumpv" token, "dumpv_%s:%*s", token, __DBGINDENT(), "");
  192. /******************* End private macros ************************/
  193. /*****************************************************************/
  194. #endif /* NETSNMP_NO_DEBUGGING */
  195. #ifdef __cplusplus
  196. }
  197. #endif
  198. /* Public macros moved to top-level API header file */
  199. #include <net-snmp/output_api.h>
  200. #ifdef __cplusplus
  201. extern "C" {
  202. #endif
  203. void snmp_debug_init(void);
  204. void snmp_debug_shutdown(void);
  205. #define MAX_DEBUG_TOKENS 256
  206. #define MAX_DEBUG_TOKEN_LEN 128
  207. #define DEBUG_TOKEN_DELIMITER ","
  208. #define DEBUG_ALWAYS_TOKEN "all"
  209. #ifndef NETSNMP_NO_DEBUGGING
  210. /*
  211. * internal:
  212. * You probably shouldn't be using this information unless the word
  213. * "expert" applies to you. I know it looks tempting.
  214. */
  215. typedef struct netsnmp_token_descr_s {
  216. char *token_name;
  217. char enabled;
  218. } netsnmp_token_descr;
  219. NETSNMP_IMPORT int debug_num_tokens;
  220. NETSNMP_IMPORT netsnmp_token_descr dbg_tokens[MAX_DEBUG_TOKENS];
  221. #endif /* NETSNMP_NO_DEBUGGING */
  222. #ifdef __cplusplus
  223. }
  224. #endif
  225. #endif /* SNMP_DEBUG_H */