From patchwork Mon Jul 1 12:23:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 33495 Received: (qmail 82074 invoked by alias); 1 Jul 2019 12:23:35 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 82047 invoked by uid 89); 1 Jul 2019 12:23:34 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=HX-Spam-Relays-External:209.85.221.68, H*RU:209.85.221.68, UD:utils.c, utilsc X-HELO: mail-wr1-f68.google.com Received: from mail-wr1-f68.google.com (HELO mail-wr1-f68.google.com) (209.85.221.68) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 01 Jul 2019 12:23:33 +0000 Received: by mail-wr1-f68.google.com with SMTP id p13so13587256wru.10 for ; Mon, 01 Jul 2019 05:23:32 -0700 (PDT) Return-Path: Received: from ?IPv6:2001:8a0:f913:f700:4c97:6d52:2cea:997b? ([2001:8a0:f913:f700:4c97:6d52:2cea:997b]) by smtp.gmail.com with ESMTPSA id x3sm10482264wrp.78.2019.07.01.05.23.30 (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Mon, 01 Jul 2019 05:23:30 -0700 (PDT) Subject: Re: ui_out format strings for fields and styles (Re: [PATCH] Style "pwd" output) To: Tom Tromey References: <20190605020116.1550-1-tom@tromey.com> <1ee4bd6b-4cdf-f3a9-74af-0843bf123a8b@redhat.com> <87lfygi1x0.fsf@tromey.com> <32872d6a-15d6-9718-59ae-957694e114c9@redhat.com> <87imtjhj6b.fsf@tromey.com> <625cd0ba-058d-d4bf-8ba3-8676f335b0f3@redhat.com> <87blzbep47.fsf@tromey.com> <2180f72f-da10-5333-90a1-666ba3bd145e@redhat.com> <87imtjbrmx.fsf@tromey.com> <871s056yjw.fsf@tromey.com> <87wohx5hir.fsf@tromey.com> Cc: gdb-patches@sourceware.org From: Pedro Alves Message-ID: Date: Mon, 1 Jul 2019 13:23:29 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <87wohx5hir.fsf@tromey.com> On 6/7/19 8:20 PM, Tom Tromey wrote: >>>>>> "Tom" == Tom Tromey writes: > > Tom> My patch also removes (really just comments out) %pS and %pN in favor of > Tom> requiring %ps. Those could be restored with a bit of effort I guess. > > Tom> I think we do need these. > > I pushed a couple more patches to the branch, including one intended to > fix this. > > What's left to do on that branch in order to merge it? Well, you used printf_filtered the new patches, but that doesn't work with the new format specifiers, yet. :-) @@ -12112,23 +12112,21 @@ say_where (struct breakpoint *b) else { if (opts.addressprint || b->loc->symtab == NULL) - { - printf_filtered (" at "); - fputs_styled (paddress (b->loc->gdbarch, b->loc->address), - address_style.style (), - gdb_stdout); - } + printf_filtered (" at %ps", + styled_string (address_style.style (), + paddress (b->loc->gdbarch, + b->loc->address)).ptr ()); branch, buggy: (top-gdb) start Temporary breakpoint 3 at 0x7fffc8e67590s: file 0x7fffc8e675e0s, line 28. fixed: (top-gdb) start Temporary breakpoint 3 at 0x469a06: file /home/pedro/gdb/mygit/src/gdb/gdb.c, line 28. If we want the new new formatters to work with printf_(un)filtered, the patch below fixes it by making printf_(un)filtered defer to cli_ui_out::message for format processing, instead of to string_printf (and thus to vsnprintf). It's maybe a bit roundabout, but I couldn't think of a better/easier way. From de45ea7e7544f1cb3b7facc2fa47609006d1aeb0 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Fri, 28 Jun 2019 22:40:50 +0100 Subject: [PATCH] printf_filtered --- gdb/cli-out.c | 5 ++++- gdb/ui-out.c | 14 ++++++++++---- gdb/ui-out.h | 1 + gdb/utils.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++--------- gdb/utils.h | 7 +++++++ 5 files changed, 69 insertions(+), 14 deletions(-) diff --git a/gdb/cli-out.c b/gdb/cli-out.c index aaea20e1512..1f95e4b8ddd 100644 --- a/gdb/cli-out.c +++ b/gdb/cli-out.c @@ -209,7 +209,10 @@ cli_ui_out::do_message (const ui_file_style &style, if (m_suppress_output) return; - vfprintf_styled (m_streams.back (), style, format, args); + /* Use the "no_gdbfmt" variant here to avoid recursion. + vfprintf_styled calls into cli_ui_out::message to handle the + gdb-specific printf formats. */ + vfprintf_styled_no_gdbfmt (m_streams.back (), style, format, args); } void diff --git a/gdb/ui-out.c b/gdb/ui-out.c index 4116c47ea43..d50fb3a3e3f 100644 --- a/gdb/ui-out.c +++ b/gdb/ui-out.c @@ -560,15 +560,12 @@ ui_out::call_do_message (const ui_file_style &style, const char *format, } void -ui_out::message (const char *format, ...) +ui_out::vmessage (const char *format, va_list args) { format_pieces fpieces (&format, true); ui_file_style style; - va_list args; - va_start (args, format); - for (auto &&piece : fpieces) { const char *current_substring = piece.string; @@ -647,6 +644,15 @@ ui_out::message (const char *format, ...) _("failed internal consistency check")); } } +} + +void +ui_out::message (const char *format, ...) +{ + va_list args; + va_start (args, format); + + vmessage (format, args); va_end (args); } diff --git a/gdb/ui-out.h b/gdb/ui-out.h index f4c3857fe2d..35bc8b21824 100644 --- a/gdb/ui-out.h +++ b/gdb/ui-out.h @@ -149,6 +149,7 @@ class ui_out void spaces (int numspaces); void text (const char *string); void message (const char *format, ...) ATTRIBUTE_PRINTF (2, 3); + void vmessage (const char *format, va_list args) ATTRIBUTE_PRINTF (2, 0); void wrap_hint (const char *identstring); void flush (); diff --git a/gdb/utils.c b/gdb/utils.c index 6ba4e41faa2..22badda7713 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -73,13 +73,15 @@ #include "common/pathstuff.h" #include "cli/cli-style.h" #include "common/scope-exit.h" +#include "cli-out.h" void (*deprecated_error_begin_hook) (void); /* Prototypes for local functions */ static void vfprintf_maybe_filtered (struct ui_file *, const char *, - va_list, int) ATTRIBUTE_PRINTF (2, 0); + va_list, bool, bool) + ATTRIBUTE_PRINTF (2, 0); static void fputs_maybe_filtered (const char *, struct ui_file *, int); @@ -2027,6 +2029,26 @@ puts_debug (char *prefix, char *string, char *suffix) } } +/* Like string_vprintf, but if GDBFMT is true, also process the + gdb-specific format specifiers. */ + +static std::string string_vprintf_maybe_gdbfmt (const char *format, + va_list args, + bool gdbfmt) + ATTRIBUTE_PRINTF (1, 0); + +static std::string +string_vprintf_maybe_gdbfmt (const char *format, va_list args, bool gdbfmt) +{ + if (gdbfmt) + { + string_file sfile; + cli_ui_out (&sfile, 0).vmessage (format, args); + return std::move (sfile.string ()); + } + else + return string_vprintf (format, args); +} /* Print a variable number of ARGS using format FORMAT. If this information is going to put the amount written (since the last call @@ -2038,15 +2060,14 @@ puts_debug (char *prefix, char *string, char *suffix) We implement three variants, vfprintf (takes a vararg list and stream), fprintf (takes a stream to write on), and printf (the usual). - Note also that a longjmp to top level may occur in this routine - (since prompt_for_continue may do so) so this routine should not be - called when cleanups are not in place. */ + Note also that this may throw a quit (since prompt_for_continue may + do so). */ static void vfprintf_maybe_filtered (struct ui_file *stream, const char *format, - va_list args, int filter) + va_list args, bool filter, bool gdbfmt) { - std::string linebuffer = string_vprintf (format, args); + std::string linebuffer = string_vprintf_maybe_gdbfmt (format, args, gdbfmt); fputs_maybe_filtered (linebuffer.c_str (), stream, filter); } @@ -2054,13 +2075,19 @@ vfprintf_maybe_filtered (struct ui_file *stream, const char *format, void vfprintf_filtered (struct ui_file *stream, const char *format, va_list args) { - vfprintf_maybe_filtered (stream, format, args, 1); + vfprintf_maybe_filtered (stream, format, args, true, true); +} + +static void +vfprintf_filtered_no_gdbfmt (struct ui_file *stream, const char *format, va_list args) +{ + vfprintf_maybe_filtered (stream, format, args, true, false); } void vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args) { - std::string linebuffer = string_vprintf (format, args); + std::string linebuffer = string_vprintf_maybe_gdbfmt (format, args, true); if (debug_timestamp && stream == gdb_stdlog) { using namespace std::chrono; @@ -2087,7 +2114,7 @@ vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args) void vprintf_filtered (const char *format, va_list args) { - vfprintf_maybe_filtered (gdb_stdout, format, args, 1); + vfprintf_maybe_filtered (gdb_stdout, format, args, true, false); } void @@ -2158,6 +2185,17 @@ vfprintf_styled (struct ui_file *stream, const ui_file_style &style, set_output_style (stream, ui_file_style ()); } +/* See utils.h. */ + +void +vfprintf_styled_no_gdbfmt (struct ui_file *stream, const ui_file_style &style, + const char *format, va_list args) +{ + set_output_style (stream, style); + vfprintf_filtered_no_gdbfmt (stream, format, args); + set_output_style (stream, ui_file_style ()); +} + void printf_filtered (const char *format, ...) { diff --git a/gdb/utils.h b/gdb/utils.h index bb73d4a8e72..61b7b5e3bb3 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -435,6 +435,13 @@ extern void vfprintf_styled (struct ui_file *stream, va_list args) ATTRIBUTE_PRINTF (3, 0); +/* Like vfprintf_styled, but do not process gdb-specific format + specifiers. */ +extern void vfprintf_styled_no_gdbfmt (struct ui_file *stream, + const ui_file_style &style, + const char *fmt, va_list args) + ATTRIBUTE_PRINTF (3, 0); + /* Like fputs_filtered, but styles the output according to STYLE, when appropriate. */