gdbsupport: include preprocessor.h in common-debug.h
[deliverable/binutils-gdb.git] / gdbsupport / common-debug.h
CommitLineData
34abf635
GB
1/* Declarations for debug printing functions.
2
3666a048 3 Copyright (C) 2014-2021 Free Software Foundation, Inc.
34abf635
GB
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
1a5c2598
TT
20#ifndef COMMON_COMMON_DEBUG_H
21#define COMMON_COMMON_DEBUG_H
34abf635 22
4d6840c3
SM
23#include "gdbsupport/preprocessor.h"
24
491144b5 25/* Set to true to enable debugging of hardware breakpoint/
c5e92cca
GB
26 watchpoint support code. */
27
491144b5 28extern bool show_debug_regs;
c5e92cca 29
34abf635
GB
30/* Print a formatted message to the appropriate channel for
31 debugging output for the client. */
32
33extern void debug_printf (const char *format, ...)
34 ATTRIBUTE_PRINTF (1, 2);
35
36/* Print a formatted message to the appropriate channel for
37 debugging output for the client. This function must be
38 provided by the client. */
39
40extern void debug_vprintf (const char *format, va_list ap)
41 ATTRIBUTE_PRINTF (1, 0);
42
ba988419
SM
43/* Print a debug statement prefixed with the module and function name, and
44 with a newline at the end. */
45
17417fb0
SM
46extern void ATTRIBUTE_PRINTF (3, 4) debug_prefixed_printf
47 (const char *module, const char *func, const char *format, ...);
48
49/* Print a debug statement prefixed with the module and function name, and
50 with a newline at the end. */
51
ba988419
SM
52extern void ATTRIBUTE_PRINTF (3, 0) debug_prefixed_vprintf
53 (const char *module, const char *func, const char *format, va_list args);
54
74b773fc
SM
55/* Helper to define "_debug_print" macros.
56
57 DEBUG_ENABLED_COND is an expression that evaluates to true if the debugging
58 statement is enabled and should be printed.
59
60 The other arguments, as well as the name of the current function, are
61 forwarded to debug_prefixed_printf. */
62
63#define debug_prefixed_printf_cond(debug_enabled_cond, module, fmt, ...) \
64 do \
65 { \
66 if (debug_enabled_cond) \
67 debug_prefixed_printf (module, __func__, fmt, ##__VA_ARGS__); \
68 } \
69 while (0)
70
2189c312
SM
71#define debug_prefixed_printf_cond_nofunc(debug_enabled_cond, module, fmt, ...) \
72 do \
73 { \
74 if (debug_enabled_cond) \
75 debug_prefixed_printf (module, nullptr, fmt, ##__VA_ARGS__); \
76 } \
77 while (0)
78
3ec3145c
SM
79/* Nesting depth of scoped_debug_start_end objects. */
80
81extern int debug_print_depth;
82
83/* Print a message on construction and destruction, to denote the start and end
84 of an operation. Increment DEBUG_PRINT_DEPTH on construction and decrement
85 it on destruction, such that nested debug statements will be printed with
86 an indent and appear "inside" this one. */
87
88struct scoped_debug_start_end
89{
90 /* DEBUG_ENABLED is a reference to a variable that indicates whether debugging
91 is enabled, so if the debug statements should be printed. Is is read
92 separately at construction and destruction, such that the start statement
93 could be printed but not the end statement, or vice-versa.
94
95 MODULE and FUNC are forwarded to debug_prefixed_printf.
96
97 START_MSG and END_MSG are the statements to print on construction and
98 destruction, respectively. */
99
100 scoped_debug_start_end (bool &debug_enabled, const char *module,
101 const char *func, const char *start_msg,
102 const char *end_msg)
103 : m_debug_enabled (debug_enabled),
104 m_module (module),
105 m_func (func),
106 m_end_msg (end_msg)
107 {
108 if (m_debug_enabled)
109 {
110 debug_prefixed_printf (m_module, m_func, "%s", start_msg);
111 ++debug_print_depth;
112 m_must_decrement_print_depth = true;
113 }
114 }
115
116 DISABLE_COPY_AND_ASSIGN (scoped_debug_start_end);
117
118 ~scoped_debug_start_end ()
119 {
120 if (m_must_decrement_print_depth)
121 {
122 gdb_assert (debug_print_depth > 0);
123 --debug_print_depth;
124 }
125
126 if (m_debug_enabled)
127 {
128 debug_prefixed_printf (m_module, m_func, "%s", m_end_msg);
129 }
130 }
131
132private:
133 bool &m_debug_enabled;
134 const char *m_module;
135 const char *m_func;
136 const char *m_end_msg;
137
138 /* This is used to handle the case where debugging is enabled during
139 construction but not during destruction, or vice-versa. We want to make
140 sure there are as many increments are there are decrements. */
141
142 bool m_must_decrement_print_depth = false;
143};
144
145/* Helper to define a module-specific start/end debug macro. */
146
147#define scoped_debug_start_end(debug_enabled, module, msg) \
148 scoped_debug_start_end CONCAT(scoped_debug_start_end, __LINE__) \
149 (debug_enabled, module, __func__, "start: " msg, "end: " msg)
150
151/* Helper to define a module-specific enter/exit debug macro. This is a special
152 case of `scoped_debug_start_end` where the start and end messages are "enter"
153 and "exit", to denote entry and exit of a function. */
154
155#define scoped_debug_enter_exit(debug_enabled, module) \
156 scoped_debug_start_end CONCAT(scoped_debug_start_end, __LINE__) \
157 (debug_enabled, module, __func__, "enter", "exit")
158
1a5c2598 159#endif /* COMMON_COMMON_DEBUG_H */
This page took 0.457434 seconds and 4 git commands to generate.