debug.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file debug.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Debugging macros.
  25. */
  26. /**
  27. * @def DEBUG
  28. *
  29. * Macro for printing debugging text. Use the same way as printf,
  30. * but without a newline.
  31. * Prepends "function_name: " and appends a newline.
  32. */
  33. /**
  34. * @def ASSERT_FORCE
  35. *
  36. * Macro for forced assertions.
  37. * Evaluates the argument and terminates the program abnormally
  38. * if the result is false.
  39. * Expands to an expression with the type and value of the result
  40. * of the evaluation.
  41. */
  42. /**
  43. * @def ASSERT
  44. *
  45. * Macro for assertions.
  46. * The argument may or may not be evaluated.
  47. * If the argument is evaluated, it must not evaluate to false.
  48. * Expands to an expression of type void.
  49. */
  50. /**
  51. * @def ASSERT_EXECUTE
  52. *
  53. * Macro for always-evaluated assertions.
  54. * The argument is evaluated.
  55. * The argument must not evaluate to false.
  56. * Expands to an expression with the type and value of the result
  57. * of the evaluation.
  58. */
  59. /**
  60. * @def DEBUG_ZERO_MEMORY
  61. *
  62. * If debugging is enabled, zeroes the given memory region.
  63. * First argument is pointer to the memory region, second is
  64. * number of bytes.
  65. */
  66. /**
  67. * @def WARN_UNUSED
  68. *
  69. * Tells the compiler that the result of a function should not be unused.
  70. * Insert at the end of the declaration of a function before the semicolon.
  71. */
  72. #ifndef BADVPN_MISC_DEBUG_H
  73. #define BADVPN_MISC_DEBUG_H
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76. #include <string.h>
  77. #include <stdint.h>
  78. #include <assert.h>
  79. #define DEBUG(...) \
  80. { \
  81. fprintf(stderr, "%s: ", __FUNCTION__); \
  82. fprintf(stderr, __VA_ARGS__); \
  83. fprintf(stderr, "\n"); \
  84. }
  85. #define ASSERT_FORCE(e) \
  86. { \
  87. typeof (e) _assert_res = (e); \
  88. if (!_assert_res) { \
  89. fprintf(stderr, "%s:%d Assertion failed\n", __FILE__, __LINE__); \
  90. abort(); \
  91. } \
  92. _assert_res; \
  93. }
  94. #ifdef NDEBUG
  95. #define DEBUG_ZERO_MEMORY(buf, len)
  96. #define ASSERT(e) { ; }
  97. #define ASSERT_EXECUTE(e) { (e); }
  98. #else
  99. #define DEBUG_ZERO_MEMORY(buf, len) { memset((buf), 0, (len)); }
  100. #ifdef BADVPN_USE_C_ASSERT
  101. #define ASSERT(e) { assert(e); ; }
  102. #define ASSERT_EXECUTE(e) \
  103. { \
  104. typeof (e) _assert_res = (e); \
  105. assert(_assert_res); \
  106. _assert_res; \
  107. }
  108. #else
  109. #define ASSERT(e) { ASSERT_FORCE(e); ; }
  110. #define ASSERT_EXECUTE(e) ASSERT_FORCE(e)
  111. #endif
  112. #endif
  113. #ifdef __GNUC__
  114. #define WARN_UNUSED __attribute((warn_unused_result))
  115. #else
  116. #define WARN_UNUSED
  117. #endif
  118. #endif