common-debug.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Declarations for debug printing functions.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_COMMON_DEBUG_H
  15. #define COMMON_COMMON_DEBUG_H
  16. #include "gdbsupport/gdb_optional.h"
  17. #include "gdbsupport/preprocessor.h"
  18. #include <stdarg.h>
  19. /* Set to true to enable debugging of hardware breakpoint/
  20. watchpoint support code. */
  21. extern bool show_debug_regs;
  22. /* Print a formatted message to the appropriate channel for
  23. debugging output for the client. */
  24. extern void debug_printf (const char *format, ...)
  25. ATTRIBUTE_PRINTF (1, 2);
  26. /* Print a formatted message to the appropriate channel for
  27. debugging output for the client. This function must be
  28. provided by the client. */
  29. extern void debug_vprintf (const char *format, va_list ap)
  30. ATTRIBUTE_PRINTF (1, 0);
  31. /* Print a debug statement prefixed with the module and function name, and
  32. with a newline at the end. */
  33. extern void ATTRIBUTE_PRINTF (3, 4) debug_prefixed_printf
  34. (const char *module, const char *func, const char *format, ...);
  35. /* Print a debug statement prefixed with the module and function name, and
  36. with a newline at the end. */
  37. extern void ATTRIBUTE_PRINTF (3, 0) debug_prefixed_vprintf
  38. (const char *module, const char *func, const char *format, va_list args);
  39. /* Helper to define "_debug_print" macros.
  40. DEBUG_ENABLED_COND is an expression that evaluates to true if the debugging
  41. statement is enabled and should be printed.
  42. The other arguments, as well as the name of the current function, are
  43. forwarded to debug_prefixed_printf. */
  44. #define debug_prefixed_printf_cond(debug_enabled_cond, module, fmt, ...) \
  45. do \
  46. { \
  47. if (debug_enabled_cond) \
  48. debug_prefixed_printf (module, __func__, fmt, ##__VA_ARGS__); \
  49. } \
  50. while (0)
  51. #define debug_prefixed_printf_cond_nofunc(debug_enabled_cond, module, fmt, ...) \
  52. do \
  53. { \
  54. if (debug_enabled_cond) \
  55. debug_prefixed_printf (module, nullptr, fmt, ##__VA_ARGS__); \
  56. } \
  57. while (0)
  58. /* Nesting depth of scoped_debug_start_end objects. */
  59. extern int debug_print_depth;
  60. /* Print a message on construction and destruction, to denote the start and end
  61. of an operation. Increment DEBUG_PRINT_DEPTH on construction and decrement
  62. it on destruction, such that nested debug statements will be printed with
  63. an indent and appear "inside" this one. */
  64. struct scoped_debug_start_end
  65. {
  66. /* DEBUG_ENABLED is a reference to a variable that indicates whether debugging
  67. is enabled, so if the debug statements should be printed. Is is read
  68. separately at construction and destruction, such that the start statement
  69. could be printed but not the end statement, or vice-versa.
  70. MODULE and FUNC are forwarded to debug_prefixed_printf.
  71. START_PREFIX and END_PREFIX are the statements to print on construction and
  72. destruction, respectively.
  73. If the FMT format string is non-nullptr, then a `: ` is appended to the
  74. messages, followed by the rendering of that format string. The format
  75. string is rendered during construction and is re-used as is for the
  76. message on exit. */
  77. scoped_debug_start_end (bool &debug_enabled, const char *module,
  78. const char *func, const char *start_prefix,
  79. const char *end_prefix, const char *fmt, ...)
  80. ATTRIBUTE_NULL_PRINTF (7, 8)
  81. : m_debug_enabled (debug_enabled),
  82. m_module (module),
  83. m_func (func),
  84. m_end_prefix (end_prefix),
  85. m_with_format (fmt != nullptr)
  86. {
  87. if (m_debug_enabled)
  88. {
  89. if (fmt != nullptr)
  90. {
  91. va_list args;
  92. va_start (args, fmt);
  93. m_msg = string_vprintf (fmt, args);
  94. va_end (args);
  95. debug_prefixed_printf (m_module, m_func, "%s: %s",
  96. start_prefix, m_msg->c_str ());
  97. }
  98. else
  99. debug_prefixed_printf (m_module, m_func, "%s", start_prefix);
  100. ++debug_print_depth;
  101. m_must_decrement_print_depth = true;
  102. }
  103. }
  104. DISABLE_COPY_AND_ASSIGN (scoped_debug_start_end);
  105. ~scoped_debug_start_end ()
  106. {
  107. if (m_must_decrement_print_depth)
  108. {
  109. gdb_assert (debug_print_depth > 0);
  110. --debug_print_depth;
  111. }
  112. if (m_debug_enabled)
  113. {
  114. if (m_with_format)
  115. {
  116. if (m_msg.has_value ())
  117. debug_prefixed_printf (m_module, m_func, "%s: %s",
  118. m_end_prefix, m_msg->c_str ());
  119. else
  120. {
  121. /* A format string was passed to the constructor, but debug
  122. control variable wasn't set at the time, so we don't have the
  123. rendering of the format string. */
  124. debug_prefixed_printf (m_module, m_func, "%s: <%s debugging was not enabled on entry>",
  125. m_end_prefix, m_module);
  126. }
  127. }
  128. else
  129. debug_prefixed_printf (m_module, m_func, "%s", m_end_prefix);
  130. }
  131. }
  132. private:
  133. bool &m_debug_enabled;
  134. const char *m_module;
  135. const char *m_func;
  136. const char *m_end_prefix;
  137. /* The result of formatting the format string in the constructor. */
  138. gdb::optional<std::string> m_msg;
  139. /* True is a non-nullptr format was passed to the constructor. */
  140. bool m_with_format;
  141. /* This is used to handle the case where debugging is enabled during
  142. construction but not during destruction, or vice-versa. We want to make
  143. sure there are as many increments are there are decrements. */
  144. bool m_must_decrement_print_depth = false;
  145. };
  146. /* Helper to define a module-specific start/end debug macro. */
  147. #define scoped_debug_start_end(debug_enabled, module, fmt, ...) \
  148. scoped_debug_start_end CONCAT(scoped_debug_start_end, __LINE__) \
  149. (debug_enabled, module, __func__, "start", "end", fmt, ##__VA_ARGS__)
  150. /* Helper to define a module-specific enter/exit debug macro. This is a special
  151. case of `scoped_debug_start_end` where the start and end messages are "enter"
  152. and "exit", to denote entry and exit of a function. */
  153. #define scoped_debug_enter_exit(debug_enabled, module) \
  154. scoped_debug_start_end CONCAT(scoped_debug_start_end, __LINE__) \
  155. (debug_enabled, module, __func__, "enter", "exit", nullptr)
  156. #endif /* COMMON_COMMON_DEBUG_H */