1 /* Support for complaint handling during symbol reading in GDB.
3 Copyright (C) 1990-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "complaints.h"
24 #include "gdbsupport/selftest.h"
25 #include <unordered_map>
28 /* Map format strings to counters. */
30 static std::unordered_map<const char *, int> counters;
32 /* How many complaints about a particular thing should be printed
33 before we stop whining about it? Default is no whining at all,
34 since so many systems have ill-constructed symbol files. */
39 static std::mutex complaint_mutex;
40 #endif /* CXX_STD_THREAD */
42 /* See complaints.h. */
45 complaint_internal (const char *fmt, ...)
51 std::lock_guard<std::mutex> guard (complaint_mutex);
53 if (++counters[fmt] > stop_whining)
59 if (deprecated_warning_hook)
60 (*deprecated_warning_hook) (fmt, args);
63 gdb_puts (_("During symbol reading: "), gdb_stderr);
64 gdb_vprintf (gdb_stderr, fmt, args);
65 gdb_puts ("\n", gdb_stderr);
71 /* See complaints.h. */
79 /* See complaints.h. */
81 complaint_interceptor *complaint_interceptor::g_complaint_interceptor;
83 /* See complaints.h. */
85 complaint_interceptor::complaint_interceptor ()
86 : m_saved_warning_hook (deprecated_warning_hook)
88 /* These cannot be stacked. */
89 gdb_assert (g_complaint_interceptor == nullptr);
90 g_complaint_interceptor = this;
91 deprecated_warning_hook = issue_complaint;
94 /* A helper that wraps a warning hook. */
97 wrap_warning_hook (void (*hook) (const char *, va_list), ...)
100 va_start (args, hook);
105 /* See complaints.h. */
107 complaint_interceptor::~complaint_interceptor ()
109 for (const std::string &str : m_complaints)
111 if (m_saved_warning_hook)
112 wrap_warning_hook (m_saved_warning_hook, str.c_str ());
114 gdb_printf (gdb_stderr, _("During symbol reading: %s\n"),
118 g_complaint_interceptor = nullptr;
119 deprecated_warning_hook = m_saved_warning_hook;
122 /* See complaints.h. */
125 complaint_interceptor::issue_complaint (const char *fmt, va_list args)
128 std::lock_guard<std::mutex> guard (complaint_mutex);
130 g_complaint_interceptor->m_complaints.insert (string_vprintf (fmt, args));
134 complaints_show_value (struct ui_file *file, int from_tty,
135 struct cmd_list_element *cmd, const char *value)
137 gdb_printf (file, _("Max number of complaints about incorrect"
138 " symbols is %s.\n"),
143 namespace selftests {
145 /* Entry point for complaints unit tests. */
150 std::unordered_map<const char *, int> tmp;
151 scoped_restore reset_counters = make_scoped_restore (&counters, tmp);
152 scoped_restore reset_stop_whining = make_scoped_restore (&stop_whining, 2);
154 #define CHECK_COMPLAINT(STR, CNT) \
157 std::string output; \
158 execute_fn_to_string (output, []() { complaint (STR); }, false); \
159 std::string expected \
160 = _("During symbol reading: ") + std::string (STR "\n"); \
161 SELF_CHECK (output == expected); \
162 SELF_CHECK (counters[STR] == CNT); \
165 #define CHECK_COMPLAINT_SILENT(STR, CNT) \
168 std::string output; \
169 execute_fn_to_string (output, []() { complaint (STR); }, false); \
170 SELF_CHECK (output.empty ()); \
171 SELF_CHECK (counters[STR] == CNT); \
174 CHECK_COMPLAINT ("maintenance complaint 0", 1);
175 CHECK_COMPLAINT ("maintenance complaint 0", 2);
176 CHECK_COMPLAINT_SILENT ("maintenance complaint 0", 3);
177 CHECK_COMPLAINT ("maintenance complaint 1", 1);
179 CHECK_COMPLAINT ("maintenance complaint 0", 1);
181 #undef CHECK_COMPLAINT
182 #undef CHECK_COMPLAINT_SILENT
186 } // namespace selftests
187 #endif /* GDB_SELF_TEST */
189 void _initialize_complaints ();
191 _initialize_complaints ()
193 add_setshow_zinteger_cmd ("complaints", class_support,
195 Set max number of complaints about incorrect symbols."), _("\
196 Show max number of complaints about incorrect symbols."), NULL,
197 NULL, complaints_show_value,
198 &setlist, &showlist);
201 selftests::register_test ("complaints", selftests::test_complaints);
202 #endif /* GDB_SELF_TEST */