From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1028) id 69B4B385354F; Fri, 16 Sep 2022 23:31:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69B4B385354F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1663371083; bh=ZuIA+wuF/Zyp5Uqt7STPL1dNCWTpTsW/cEVbHvI2V4o=; h=From:To:Subject:Date:From; b=cgu13iZj4ze4OUHUimJX4S43i3LP1Cw+Q0H7zUkwJdIqsyws7LA2JwOcr+Iwz52QJ 8czS4dTRWxTnHolBlkI0iZB6Qj0Uvs9UV4dO3PTyTlE8/SIcnYe1RCc+46DQVGzvQJ u5U1/+KtOUjdNtf2PIbxxdl3Qzd53JTOoC9bzODA= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Kevin Buettner To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Suppress printing of superfluous BFD error messages X-Act-Checkin: binutils-gdb X-Git-Author: Kevin Buettner X-Git-Refname: refs/heads/master X-Git-Oldrev: 80eaec735e36598a312f9b184ee4f32bd6c0bab4 X-Git-Newrev: 84e605558eb8a3aeec636e32de5d3573242d462e Message-Id: <20220916233123.69B4B385354F@sourceware.org> Date: Fri, 16 Sep 2022 23:31:23 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D84e605558eb8= a3aeec636e32de5d3573242d462e commit 84e605558eb8a3aeec636e32de5d3573242d462e Author: Kevin Buettner Date: Fri Sep 16 16:13:29 2022 -0700 Suppress printing of superfluous BFD error messages =20 This commit adds a hook to the BFD error handler for suppressing identical messages which have been output once already. =20 It's motivated by this Fedora bug... =20 https://bugzilla.redhat.com/show_bug.cgi?id=3D2083315 =20 ...in which over 900,000 BFD error messages are output when attaching to firefox. From the bug report, the messages all say: =20 BFD: /usr/lib/debug/usr/lib64/firefox/libxul.so-100.0-2.fc35.x86_64.deb= ug: attempt to load strings from a non-string section (number 38) =20 Since there's no (additional) context which might assist the user in determining what's wrong, there's really no point in outputting more than one message. Of course, if BFD should output some other/different message, it should be output too, but all future messages identical to those already output should be suppressed. =20 For the firefox problem, it turned out that there were only 37 sections, but something was referring to section #38. I haven't investigated further to find out how this came to be. =20 Despite this problem, useful debugging might still be done, especially if the user doesn't care about debugging the problematic library. =20 If it turns out that knowing the quantity of messages might be useful, I've implemented the suppression mechanism by keeping a count of each identical message. A new GDB command, perhaps a 'maintenance' command, could be added to print out each message along with the count. I haven't implemented this though because I'm not convinced of its utility. Also, the BFD message printer has support for BFD- specific format specifiers. The BFD message strings that GDB stores in its map are sufficient for distinguishing messages from each other, but are not identical to those output by BFD's default error handler. So, that problem would need to be solved too. Diff: --- gdb/gdb_bfd.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 67 insertions(+) diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c index 6c03ae5ef05..6299148d419 100644 --- a/gdb/gdb_bfd.c +++ b/gdb/gdb_bfd.c @@ -33,6 +33,7 @@ #include "gdb/fileio.h" #include "inferior.h" #include "cli/cli-style.h" +#include =20 /* An object of this type is stored in the section's user data when mapping a section. */ @@ -1125,6 +1126,69 @@ maintenance_info_bfds (const char *arg, int from_tty) htab_traverse (all_bfds, print_one_bfd, uiout); } =20 +/* BFD related per-inferior data. */ + +struct bfd_inferior_data +{ + std::unordered_map bfd_error_string_counts; +}; + +/* Per-inferior data key. */ + +static const registry::key bfd_inferior_data_= key; + +/* Fetch per-inferior BFD data. It always returns a valid pointer to + a bfd_inferior_data struct. */ + +static struct bfd_inferior_data * +get_bfd_inferior_data (struct inferior *inf) +{ + struct bfd_inferior_data *data; + + data =3D bfd_inferior_data_key.get (inf); + if (data =3D=3D nullptr) + data =3D bfd_inferior_data_key.emplace (inf); + + return data; +} + +/* Increment the BFD error count for STR and return the updated + count. */ + +static unsigned long +increment_bfd_error_count (std::string str) +{ + struct bfd_inferior_data *bid =3D get_bfd_inferior_data (current_inferio= r ()); + + auto &map =3D bid->bfd_error_string_counts; + return ++map[std::move (str)]; +} + +static bfd_error_handler_type default_bfd_error_handler; + +/* Define a BFD error handler which will suppress the printing of + messages which have been printed once already. This is done on a + per-inferior basis. */ + +static void +gdb_bfd_error_handler (const char *fmt, va_list ap) +{ + va_list ap_copy; + + va_copy(ap_copy, ap); + const std::string str =3D string_vprintf (fmt, ap_copy); + va_end (ap_copy); + + if (increment_bfd_error_count (std::move (str)) > 1) + return; + + /* We must call the BFD mechanism for printing format strings since + it supports additional format specifiers that GDB's vwarning() doesn't + recognize. It also outputs additional text, i.e. "BFD: ", which + makes it clear that it's a BFD warning/error. */ + (*default_bfd_error_handler) (fmt, ap); +} + void _initialize_gdb_bfd (); void _initialize_gdb_bfd () @@ -1157,4 +1221,7 @@ When non-zero, bfd cache specific debugging is enable= d."), NULL, &show_bfd_cache_debug, &setdebuglist, &showdebuglist); + + /* Hook the BFD error/warning handler to limit amount of output. */ + default_bfd_error_handler =3D bfd_set_error_handler (gdb_bfd_error_handl= er); }