debug.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. */
  40. /**
  41. * @def ASSERT
  42. *
  43. * Macro for assertions.
  44. * The argument may or may not be evaluated.
  45. * If the argument is evaluated, it must not evaluate to false.
  46. */
  47. /**
  48. * @def ASSERT_EXECUTE
  49. *
  50. * Macro for always-evaluated assertions.
  51. * The argument is evaluated.
  52. * The argument must not evaluate to false.
  53. */
  54. /**
  55. * @def DEBUG_ZERO_MEMORY
  56. *
  57. * If debugging is enabled, zeroes the given memory region.
  58. * First argument is pointer to the memory region, second is
  59. * number of bytes.
  60. */
  61. /**
  62. * @def WARN_UNUSED
  63. *
  64. * Tells the compiler that the result of a function should not be unused.
  65. * Insert at the end of the declaration of a function before the semicolon.
  66. */
  67. #ifndef BADVPN_MISC_DEBUG_H
  68. #define BADVPN_MISC_DEBUG_H
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #include <stdint.h>
  73. #include <assert.h>
  74. #define DEBUG(...) \
  75. { \
  76. fprintf(stderr, "%s: ", __FUNCTION__); \
  77. fprintf(stderr, __VA_ARGS__); \
  78. fprintf(stderr, "\n"); \
  79. }
  80. #define ASSERT_FORCE(e) \
  81. { \
  82. if (!(e)) { \
  83. fprintf(stderr, "%s:%d Assertion failed\n", __FILE__, __LINE__); \
  84. abort(); \
  85. } \
  86. }
  87. #ifdef NDEBUG
  88. #define DEBUG_ZERO_MEMORY(buf, len) {}
  89. #define ASSERT(e) {}
  90. #define ASSERT_EXECUTE(e) { (e); }
  91. #else
  92. #define DEBUG_ZERO_MEMORY(buf, len) { memset((buf), 0, (len)); }
  93. #ifdef BADVPN_USE_C_ASSERT
  94. #define ASSERT(e) { assert(e); }
  95. #define ASSERT_EXECUTE(e) \
  96. { \
  97. int _assert_res = !!(e); \
  98. assert(_assert_res); \
  99. }
  100. #else
  101. #define ASSERT(e) ASSERT_FORCE(e)
  102. #define ASSERT_EXECUTE(e) ASSERT_FORCE(e)
  103. #endif
  104. #endif
  105. #ifdef __GNUC__
  106. #define WARN_UNUSED __attribute((warn_unused_result))
  107. #else
  108. #define WARN_UNUSED
  109. #endif
  110. #endif