From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTPS id B69A23858D35 for ; Thu, 4 Nov 2021 11:31:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B69A23858D35 Received: from mail-vk1-f197.google.com (mail-vk1-f197.google.com [209.85.221.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-399-NDzIyeOXPky2tP1wZXcs1A-1; Thu, 04 Nov 2021 07:30:55 -0400 X-MC-Unique: NDzIyeOXPky2tP1wZXcs1A-1 Received: by mail-vk1-f197.google.com with SMTP id y15-20020a1f7d0f000000b002f244d4c479so1195012vkc.9 for ; Thu, 04 Nov 2021 04:30:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=IoBRROdxoFCfn8E1k7ee/QY5g2gw5rlsTcjRZ4GmvW0=; b=YfDsM5lPvESpGkht+QbR5nCDYX5lUaet/+GSq5UbGXWuQCaWT9hkAUxBGr9LIPxG0i Gcvu3aHBO66weGvmX2htFVJHv10GUZMMX/p+kPpro1cCQvjsgZvuNeTCtG5eRXzfwq2U MHkP+Iiyy+xv4Pp/uempjxb7SUddi8rU230BTAQ84Q+PzMkBwtRB4M8Jz9UjicDg3cRV wwnda7NGhCTlCw24ETtsgDoOuWiCXaIcAtb9h5g7/OkBf5ULLkWfzH18tGs3aBdgLUzD 2FCYEgv/u8MV9v8F7uoMpbehAt2thQ1OD3VRmrDiOmdeNd7a0EWf0HegZvlF4k2JPhij lgJw== X-Gm-Message-State: AOAM532HL7Gt13DMW2YuWLsfZTaGL1pmIObT4eGYCRHvOLvEUjvUcbPc ay7TBMjQmvggLRFUCFJlxlRkP3g04KJzjy1GT/F4ct8n1Ho4Pgj0Z6Sf/opsM9Lr9G9HsyF0ACh i8mz93yqbiTuM/J9x+ZqR4e5K7fboeh4= X-Received: by 2002:a05:6102:c8a:: with SMTP id f10mr58441150vst.11.1636025455448; Thu, 04 Nov 2021 04:30:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzlZtr+hrCqoCA1hW2nr1xIaTNzwU37OEwNeEN3JlWYynh6yjjxK++hymGqVXU9YmX7G5I9q+76BW6UNmBUtDc= X-Received: by 2002:a05:6102:c8a:: with SMTP id f10mr58441104vst.11.1636025455158; Thu, 04 Nov 2021 04:30:55 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Thu, 4 Nov 2021 11:30:43 +0000 Message-ID: Subject: Re: [PATCH] print extended assertion failures to stderr To: Jay Feldblum Cc: "libstdc++" , gcc Patches X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="00000000000019bd6605cff4dc4a" X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, KAM_LOTSOFHASH, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Nov 2021 11:31:05 -0000 --00000000000019bd6605cff4dc4a Content-Type: text/plain; charset="UTF-8" On Wed, 27 Oct 2021 at 09:27, Jay Feldblum via Libstdc++ < libstdc++@gcc.gnu.org> wrote: > From: yfeldblum > > The stdout stream is reserved for output intentionally produced by the > application. Assertion failures and other forms of logging must be > emitted to stderr, not to stdout. > > It is common for testing and monitoring infrastructure to scan stderr > for errors, such as for assertion failures, and to collect or retain > them for analysis or observation. It is a norm that assertion failures > match this expectation in practice. > > While `__builtin_fprintf` is available as a builtin, there is no > equivalent builtin for `stderr`. The only option in practice is to use > the macro `stderr`, which requires `#include `. It is desired > not to add such an include to `bits/c++config` so the solution is to > write and export a function which may be called by `bits/c++config`. > > This is expected to be API-compatible and ABI-compatible with caveats. > Code compiled against an earlier libstdc++ will work when linked into a > later libstdc++ but the stream to which assertion failures are logged is > anybody's guess, and in practice will be determined by the link line and > the choice of linker. This fix targets builds for which all C++ code is > built against a libstdc++ with the fix. > Thanks for the patch! Comments below. > > Alternatives: > * This, which is the smallest change. > * This, but also defining symbols `std::__stdin` and `std::__stdout` for > completeness. > * Define a symbol like `std::__printf_stderr` which prints any message > with any formatting to stderr, just as `std::printf` does to stdout, > and call that from `std::__replacement_assert` instead of calling > `__builtin_printf`. > * Move `std::__replacement_assert` into libstdc++.so and no longer mark > it as weak. This allows an application with some parts built against a > previous libstdc++ to guarantee that the fix will be applied at least > to the parts that are built against a libstdc++ containing the fix. > > libstdc++-v3/ChangeLog: > include/bits/c++config (__glibcxx_assert): print to stderr. > --- > libstdc++-v3/include/bits/c++config | 8 ++++-- > libstdc++-v3/src/c++98/Makefile.am | 1 + > libstdc++-v3/src/c++98/Makefile.in | 2 +- > libstdc++-v3/src/c++98/stdio.cc | 39 +++++++++++++++++++++++++++++ > 4 files changed, 47 insertions(+), 3 deletions(-) > create mode 100644 libstdc++-v3/src/c++98/stdio.cc > > diff --git a/libstdc++-v3/include/bits/c++config > b/libstdc++-v3/include/bits/c++config > index > a64958096718126a49e8767694e913ed96108df2..d821ba09d88dc3e42ff1807200cfece71cc18bd9 > 100644 > --- a/libstdc++-v3/include/bits/c++config > +++ b/libstdc++-v3/include/bits/c++config > @@ -523,6 +523,10 @@ namespace std > # ifdef _GLIBCXX_VERBOSE_ASSERT > namespace std > { > + // Avoid the use of stderr, because we're trying to keep the > + // include out of the mix. > + extern "C++" void* __stderr() _GLIBCXX_NOEXCEPT; > We can declare this locally in __replacement_assert, so it isn't made visible in namespace std. + > // Avoid the use of assert, because we're trying to keep the > // include out of the mix. > extern "C++" _GLIBCXX_NORETURN > @@ -531,8 +535,8 @@ namespace std > const char* __function, const char* __condition) > _GLIBCXX_NOEXCEPT > { > - __builtin_printf("%s:%d: %s: Assertion '%s' failed.\n", __file, > __line, > - __function, __condition); > + __builtin_fprintf(__stderr(), "%s:%d: %s: Assertion '%s' failed.\n", > + __file, __line, __function, __condition); > __builtin_abort(); > } > } > diff --git a/libstdc++-v3/src/c++98/Makefile.am > b/libstdc++-v3/src/c++98/Makefile.am > index > b48b57a2945780bb48496d3b5e76de4be61f836e..4032f914ea20344f51f2f219c5575d2a3858c44c > 100644 > --- a/libstdc++-v3/src/c++98/Makefile.am > +++ b/libstdc++-v3/src/c++98/Makefile.am > @@ -136,6 +136,7 @@ sources = \ > math_stubs_float.cc \ > math_stubs_long_double.cc \ > stdexcept.cc \ > + stdio.cc \ > I think adding it to src/c++11/debug.cc makes sense. That file already uses stderr itself, and is where we define other utilities for printing assertions. We also need to add it to the linker script, so that the symbol gets exported from the shared library. Otherwise any use of -D_GLIBCXX_ASSERTIONS or -D_GLIBCXX_DEBUG results in linker errors. The attached patch does that. --00000000000019bd6605cff4dc4a Content-Type: text/plain; charset="US-ASCII"; name="patch.txt" Content-Disposition: attachment; filename="patch.txt" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_kvk1i0td0 Y29tbWl0IDc1ZTc2MTI0MzdmNjVhYmUyMTY4OTg0NWIxODU2YmIzMDhjNmNiODEKQXV0aG9yOiBK b25hdGhhbiBXYWtlbHkgPGp3YWtlbHlAcmVkaGF0LmNvbT4KRGF0ZTogICBXZWQgTm92IDMgMTY6 MDY6MjkgMjAyMQoKICAgIGxpYnN0ZGMrKzogUHJpbnQgYXNzZXJ0aW9uIG1lc3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