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