Remove vcomplaint
[external/binutils.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 /* See complaints.h.  */
104
105 void
106 complaint_internal (const char *fmt, ...)
107 {
108   va_list args;
109
110   struct complain *complaint = find_complaint (&symfile_complaint_book, NULL,
111                                                0, fmt);
112   enum complaint_series series;
113
114   complaint->counter++;
115   if (complaint->counter > stop_whining)
116     return;
117
118   va_start (args, fmt);
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   va_end (args);
150 }
151
152 /* Clear out / initialize all complaint counters that have ever been
153    incremented.  If LESS_VERBOSE is 1, be less verbose about
154    successive complaints, since the messages are appearing all
155    together during a command that is reporting a contiguous block of
156    complaints (rather than being interleaved with other messages).  */
157
158 void
159 clear_complaints (int less_verbose)
160 {
161   struct complain *p;
162
163   for (p = symfile_complaint_book.root; p != NULL; p = p->next)
164     {
165       p->counter = 0;
166     }
167
168   if (!less_verbose)
169     symfile_complaint_book.series = ISOLATED_MESSAGE;
170   else
171     symfile_complaint_book.series = SHORT_FIRST_MESSAGE;
172 }
173
174 static void
175 complaints_show_value (struct ui_file *file, int from_tty,
176                        struct cmd_list_element *cmd, const char *value)
177 {
178   fprintf_filtered (file, _("Max number of complaints about incorrect"
179                             " symbols is %s.\n"),
180                     value);
181 }
182
183 void
184 _initialize_complaints (void)
185 {
186   add_setshow_zinteger_cmd ("complaints", class_support, 
187                             &stop_whining, _("\
188 Set max number of complaints about incorrect symbols."), _("\
189 Show max number of complaints about incorrect symbols."), NULL,
190                             NULL, complaints_show_value,
191                             &setlist, &showlist);
192 }