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