debug.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @file debug.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Debugging macros.
  32. */
  33. /**
  34. * @def DEBUG
  35. *
  36. * Macro for printing debugging text. Use the same way as printf,
  37. * but without a newline.
  38. * Prepends "function_name: " and appends a newline.
  39. */
  40. /**
  41. * @def ASSERT_FORCE
  42. *
  43. * Macro for forced assertions.
  44. * Evaluates the argument and terminates the program abnormally
  45. * if the result is false.
  46. */
  47. /**
  48. * @def ASSERT
  49. *
  50. * Macro for assertions.
  51. * The argument may or may not be evaluated.
  52. * If the argument is evaluated, it must not evaluate to false.
  53. */
  54. /**
  55. * @def ASSERT_EXECUTE
  56. *
  57. * Macro for always-evaluated assertions.
  58. * The argument is evaluated.
  59. * The argument must not evaluate to false.
  60. */
  61. /**
  62. * @def DEBUG_ZERO_MEMORY
  63. *
  64. * If debugging is enabled, zeroes the given memory region.
  65. * First argument is pointer to the memory region, second is
  66. * number of bytes.
  67. */
  68. /**
  69. * @def WARN_UNUSED
  70. *
  71. * Tells the compiler that the result of a function should not be unused.
  72. * Insert at the end of the declaration of a function before the semicolon.
  73. */
  74. /**
  75. * @def B_USE
  76. *
  77. * This can be used to suppress warnings about unused variables. It can
  78. * be applied to a variable or any expression. It does not evaluate the
  79. * expression.
  80. */
  81. #ifndef BADVPN_MISC_DEBUG_H
  82. #define BADVPN_MISC_DEBUG_H
  83. #include <stdio.h>
  84. #include <stdlib.h>
  85. #include <string.h>
  86. #include <stdint.h>
  87. #include <assert.h>
  88. #define DEBUG(...) \
  89. { \
  90. fprintf(stderr, "%s: ", __FUNCTION__); \
  91. fprintf(stderr, __VA_ARGS__); \
  92. fprintf(stderr, "\n"); \
  93. }
  94. #define ASSERT_FORCE(e) \
  95. { \
  96. if (!(e)) { \
  97. fprintf(stderr, "%s:%d Assertion failed\n", __FILE__, __LINE__); \
  98. abort(); \
  99. } \
  100. }
  101. #ifdef NDEBUG
  102. #define DEBUG_ZERO_MEMORY(buf, len) {}
  103. #define ASSERT(e) {}
  104. #define ASSERT_EXECUTE(e) { (e); }
  105. #else
  106. #define DEBUG_ZERO_MEMORY(buf, len) { memset((buf), 0, (len)); }
  107. #ifdef BADVPN_USE_C_ASSERT
  108. #define ASSERT(e) { assert(e); }
  109. #define ASSERT_EXECUTE(e) \
  110. { \
  111. int _assert_res = !!(e); \
  112. assert(_assert_res); \
  113. }
  114. #else
  115. #define ASSERT(e) ASSERT_FORCE(e)
  116. #define ASSERT_EXECUTE(e) ASSERT_FORCE(e)
  117. #endif
  118. #endif
  119. #ifdef __GNUC__
  120. #define WARN_UNUSED __attribute__((warn_unused_result))
  121. #else
  122. #define WARN_UNUSED
  123. #endif
  124. #define B_USE(expr) (void)(sizeof((expr)));
  125. #define B_ASSERT_USE(expr) { ASSERT(expr) B_USE(expr) }
  126. #endif