Fix test for sections with different VMA<->LMA relationships so that it only applies...
[binutils-gdb.git] / gdb / complaints.c
blobd3c72df6d41f0056798560b39fdf45dfd09caae0
1 /* Support for complaint handling during symbol reading in GDB.
3 Copyright (C) 1990-2024 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/>. */
20 #include "complaints.h"
21 #include "command.h"
22 #include "gdbcmd.h"
23 #include "run-on-main-thread.h"
24 #include "gdbsupport/selftest.h"
25 #include <unordered_map>
26 #include <mutex>
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. */
36 int stop_whining = 0;
38 #if CXX_STD_THREAD
39 static std::mutex complaint_mutex;
40 #endif /* CXX_STD_THREAD */
42 /* See complaints.h. */
44 void
45 complaint_internal (const char *fmt, ...)
47 va_list args;
50 #if CXX_STD_THREAD
51 std::lock_guard<std::mutex> guard (complaint_mutex);
52 #endif
53 if (++counters[fmt] > stop_whining)
54 return;
57 va_start (args, fmt);
59 warning_hook_handler handler = get_warning_hook_handler ();
60 if (handler != nullptr)
61 handler->warn (fmt, args);
62 else
64 gdb_puts (_("During symbol reading: "), gdb_stderr);
65 gdb_vprintf (gdb_stderr, fmt, args);
66 gdb_puts ("\n", gdb_stderr);
69 va_end (args);
72 /* See complaints.h. */
74 void
75 clear_complaints ()
77 counters.clear ();
80 /* See complaints.h. */
82 thread_local complaint_interceptor *complaint_interceptor::g_complaint_interceptor;
84 /* See complaints.h. */
86 complaint_interceptor::complaint_interceptor ()
87 : m_saved_complaint_interceptor (&g_complaint_interceptor, this),
88 m_saved_warning_hook (this)
92 /* A helper that wraps a warning hook. */
94 static void
95 wrap_warning_hook (warning_hook_handler hook, ...)
97 va_list args;
98 va_start (args, hook);
99 hook->warn ("%s", args);
100 va_end (args);
103 /* See complaints.h. */
105 void
106 re_emit_complaints (const complaint_collection &complaints)
108 gdb_assert (is_main_thread ());
110 for (const std::string &str : complaints)
112 warning_hook_handler handler = get_warning_hook_handler ();
113 if (handler != nullptr)
114 wrap_warning_hook (handler, str.c_str ());
115 else
116 gdb_printf (gdb_stderr, _("During symbol reading: %s\n"),
117 str.c_str ());
121 /* See complaints.h. */
123 void
124 complaint_interceptor::warn (const char *fmt, va_list args)
126 #if CXX_STD_THREAD
127 std::lock_guard<std::mutex> guard (complaint_mutex);
128 #endif
129 g_complaint_interceptor->m_complaints.insert (string_vprintf (fmt, args));
132 static void
133 complaints_show_value (struct ui_file *file, int from_tty,
134 struct cmd_list_element *cmd, const char *value)
136 gdb_printf (file, _("Max number of complaints about incorrect"
137 " symbols is %s.\n"),
138 value);
141 #if GDB_SELF_TEST
142 namespace selftests {
144 /* Entry point for complaints unit tests. */
146 static void
147 test_complaints ()
149 std::unordered_map<const char *, int> tmp;
150 scoped_restore reset_counters = make_scoped_restore (&counters, tmp);
151 scoped_restore reset_stop_whining = make_scoped_restore (&stop_whining, 2);
153 #define CHECK_COMPLAINT(STR, CNT) \
154 do \
156 std::string output; \
157 execute_fn_to_string (output, []() { complaint (STR); }, false); \
158 std::string expected \
159 = _("During symbol reading: ") + std::string (STR "\n"); \
160 SELF_CHECK (output == expected); \
161 SELF_CHECK (counters[STR] == CNT); \
162 } while (0)
164 #define CHECK_COMPLAINT_SILENT(STR, CNT) \
165 do \
167 std::string output; \
168 execute_fn_to_string (output, []() { complaint (STR); }, false); \
169 SELF_CHECK (output.empty ()); \
170 SELF_CHECK (counters[STR] == CNT); \
171 } while (0)
173 CHECK_COMPLAINT ("maintenance complaint 0", 1);
174 CHECK_COMPLAINT ("maintenance complaint 0", 2);
175 CHECK_COMPLAINT_SILENT ("maintenance complaint 0", 3);
176 CHECK_COMPLAINT ("maintenance complaint 1", 1);
177 clear_complaints ();
178 CHECK_COMPLAINT ("maintenance complaint 0", 1);
180 #undef CHECK_COMPLAINT
181 #undef CHECK_COMPLAINT_SILENT
185 } // namespace selftests
186 #endif /* GDB_SELF_TEST */
188 void _initialize_complaints ();
189 void
190 _initialize_complaints ()
192 add_setshow_zinteger_cmd ("complaints", class_support,
193 &stop_whining, _("\
194 Set max number of complaints about incorrect symbols."), _("\
195 Show max number of complaints about incorrect symbols."), NULL,
196 NULL, complaints_show_value,
197 &setlist, &showlist);
199 #if GDB_SELF_TEST
200 selftests::register_test ("complaints", selftests::test_complaints);
201 #endif /* GDB_SELF_TEST */