Remove struct explanation
[binutils-gdb.git] / gdb / complaints.c
1 /* Support for complaint handling during symbol reading in GDB.
2
3 Copyright (C) 1990-2018 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 #include "defs.h"
21 #include "complaints.h"
22 #include "command.h"
23 #include "gdbcmd.h"
24
25 /* Should each complaint message be self explanatory, or should we
26 assume that a series of complaints is being produced? */
27
28 enum complaint_series {
29 /* Isolated self explanatory message. */
30 ISOLATED_MESSAGE,
31
32 /* First message of a series, but does not need to include any sort
33 of explanation. */
34 SHORT_FIRST_MESSAGE,
35 };
36
37 /* Structure to manage complaints about symbol file contents. */
38
39 struct complain
40 {
41 const char *file;
42 int line;
43 const char *fmt;
44 int counter;
45 struct complain *next;
46 };
47
48 struct complaints
49 {
50 struct complain *root;
51
52 enum complaint_series series;
53 };
54
55 static struct complain complaint_sentinel;
56
57 static struct complaints symfile_complaint_book = {
58 &complaint_sentinel,
59 ISOLATED_MESSAGE
60 };
61
62 static struct complain * ATTRIBUTE_PRINTF (4, 0)
63 find_complaint (struct complaints *complaints, const char *file,
64 int line, const char *fmt)
65 {
66 struct complain *complaint;
67
68 /* Find the complaint in the table. A more efficient search
69 algorithm (based on hash table or something) could be used. But
70 that can wait until someone shows evidence that this lookup is
71 a real bottle neck. */
72 for (complaint = complaints->root;
73 complaint != NULL;
74 complaint = complaint->next)
75 {
76 if (complaint->fmt == fmt
77 && complaint->file == file
78 && complaint->line == line)
79 return complaint;
80 }
81
82 /* Oops not seen before, fill in a new complaint. */
83 complaint = XNEW (struct complain);
84 complaint->fmt = fmt;
85 complaint->file = file;
86 complaint->line = line;
87 complaint->counter = 0;
88 complaint->next = NULL;
89
90 /* File it, return it. */
91 complaint->next = complaints->root;
92 complaints->root = complaint;
93 return complaint;
94 }
95
96
97 /* How many complaints about a particular thing should be printed
98 before we stop whining about it? Default is no whining at all,
99 since so many systems have ill-constructed symbol files. */
100
101 int stop_whining = 0;
102
103 /* Print a complaint, and link the complaint block into a chain for
104 later handling. */
105
106 static void ATTRIBUTE_PRINTF (3, 0)
107 vcomplaint (const char *file,
108 int line, const char *fmt,
109 va_list args)
110 {
111 struct complain *complaint = find_complaint (&symfile_complaint_book, file,
112 line, fmt);
113 enum complaint_series series;
114
115 complaint->counter++;
116 if (complaint->counter > stop_whining)
117 return;
118
119 series = symfile_complaint_book.series;
120
121 /* Pass 'fmt' instead of 'complaint->fmt' to printf-like callees
122 from here on, to avoid "format string is not a string literal"
123 warnings. 'fmt' is this function's printf-format parameter, so
124 the compiler can assume the passed in argument is a literal
125 string somewhere up the call chain. */
126 gdb_assert (complaint->fmt == fmt);
127
128 if (complaint->file != NULL)
129 internal_vwarning (complaint->file, complaint->line, fmt, args);
130 else if (deprecated_warning_hook)
131 (*deprecated_warning_hook) (fmt, args);
132 else
133 {
134 std::string msg = string_vprintf (fmt, args);
135 wrap_here ("");
136 begin_line ();
137 if (series == ISOLATED_MESSAGE)
138 fprintf_filtered (gdb_stderr, "During symbol reading, %s.\n",
139 msg.c_str ());
140 else
141 fprintf_filtered (gdb_stderr, "%s...", msg.c_str ());
142 }
143
144 /* If GDB dumps core, we'd like to see the complaints first.
145 Presumably GDB will not be sending so many complaints that this
146 becomes a performance hog. */
147
148 gdb_flush (gdb_stderr);
149 }
150
151 void
152 complaint_internal (const char *fmt, ...)
153 {
154 va_list args;
155
156 va_start (args, fmt);
157 vcomplaint (NULL/*file*/, 0/*line*/, fmt, args);
158 va_end (args);
159 }
160
161 /* Clear out / initialize all complaint counters that have ever been
162 incremented. If LESS_VERBOSE is 1, be less verbose about
163 successive complaints, since the messages are appearing all
164 together during a command that is reporting a contiguous block of
165 complaints (rather than being interleaved with other messages). */
166
167 void
168 clear_complaints (int less_verbose)
169 {
170 struct complain *p;
171
172 for (p = symfile_complaint_book.root; p != NULL; p = p->next)
173 {
174 p->counter = 0;
175 }
176
177 if (!less_verbose)
178 symfile_complaint_book.series = ISOLATED_MESSAGE;
179 else
180 symfile_complaint_book.series = SHORT_FIRST_MESSAGE;
181 }
182
183 static void
184 complaints_show_value (struct ui_file *file, int from_tty,
185 struct cmd_list_element *cmd, const char *value)
186 {
187 fprintf_filtered (file, _("Max number of complaints about incorrect"
188 " symbols is %s.\n"),
189 value);
190 }
191
192 void
193 _initialize_complaints (void)
194 {
195 add_setshow_zinteger_cmd ("complaints", class_support,
196 &stop_whining, _("\
197 Set max number of complaints about incorrect symbols."), _("\
198 Show max number of complaints about incorrect symbols."), NULL,
199 NULL, complaints_show_value,
200 &setlist, &showlist);
201 }