From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id tJ9EG9zX1WFwWAAAWB0awg (envelope-from ) for ; Wed, 05 Jan 2022 12:39:40 -0500 Received: by simark.ca (Postfix, from userid 112) id 65AB11EE18; Wed, 5 Jan 2022 12:39:40 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 13B0B1E61F for ; Wed, 5 Jan 2022 12:39:39 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4DFDC385800A for ; Wed, 5 Jan 2022 17:39:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4DFDC385800A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1641404378; bh=0HpP2hZcfCUOHYBvjPRD9WLpecowlaPYHbfzUcVCygk=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=q18uIpRLsGxyFyx5zm5X1azPIHM1y5RaljJhD8clTbrA6TcuunysH6AcFXQFWU6YX 94YaRZCZgnd6iI9l0VVb4jIpmQUm6wg7plTMGN2jowDVfVwQMTuBIxBOFWXtP/nYNd cEJ8GbcKn7yaDZFCeZQqZrUqKuwByVXUgtapRoxs= Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 8FE943858410 for ; Wed, 5 Jan 2022 17:39:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8FE943858410 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-486-0q4B7YHYP2uaoxTJ3fG00Q-1; Wed, 05 Jan 2022 12:39:15 -0500 X-MC-Unique: 0q4B7YHYP2uaoxTJ3fG00Q-1 Received: by mail-wm1-f69.google.com with SMTP id l20-20020a05600c1d1400b003458e02cea0so2149616wms.7 for ; Wed, 05 Jan 2022 09:39:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=0HpP2hZcfCUOHYBvjPRD9WLpecowlaPYHbfzUcVCygk=; b=izgdUwP/iYG4NHS2pgkfj7rxndynR9dSSYBqYDyNwwx1yZZSd9Bm0e/1zypXpluxUk RKlAaZr2i1By6IEqTjTjsuIzjb67PnR7jPrDdOUbDsr0xGZeBiGQrBhS4GSH5qitp7fk YQ1ciEEf9dMaN9xJUI/TX4QknEGKvFFTtcf0YuIyimPMy7yGSIU0QW3VEjhsdDTgZmjM FQeCV+uS732Kiatl7tUsfcaM3YgesSEiEVRHdpcY6U1O5cZZ2wPvhkbxLJi0siNff2Qv mnU8ySTJqIHAc7qEzsU+PRD9X9suZzzcJ3swaTtITfRI6VWzlqPymKqSExbjocgiJ7AQ LK0g== X-Gm-Message-State: AOAM531aQz61rkYtj7YcrYZR4f21jb5B8FMHBflNeNq1fdqTAuONOSzO 6o5mzWz9o1J9DEH4XuylwsGXJA5YlK/8F6/xpwOOjjWm/DqVfApA4GrCmYhFvcSOrBWZlfLrj9x 8OYZHB9Lu/AP8qEbUYgaNmw== X-Received: by 2002:a5d:5049:: with SMTP id h9mr48722155wrt.382.1641404354071; Wed, 05 Jan 2022 09:39:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJxxdW6twLyKr6jcR9qmzL2xQrMe/lwyuOsupziBmufQsuWQty30fp4AU72mcef9S6I4YS0XFg== X-Received: by 2002:a5d:5049:: with SMTP id h9mr48722127wrt.382.1641404353726; Wed, 05 Jan 2022 09:39:13 -0800 (PST) Received: from localhost (host109-154-163-67.range109-154.btcentralplus.com. [109.154.163.67]) by smtp.gmail.com with ESMTPSA id l14sm36788667wrr.53.2022.01.05.09.39.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jan 2022 09:39:13 -0800 (PST) Date: Wed, 5 Jan 2022 17:39:12 +0000 To: Tom Tromey Subject: Re: [PATCH] Implement putstr and putstrn in ui_file Message-ID: <20220105173912.GA828155@redhat.com> References: <20211231180157.946230-1-tom@tromey.com> <20211231192735.4ljnjbztxs3dfg7b@Plymouth> <87tuel7lbq.fsf@tromey.com> MIME-Version: 1.0 In-Reply-To: <87tuel7lbq.fsf@tromey.com> X-Operating-System: Linux/5.8.18-100.fc31.x86_64 (x86_64) X-Uptime: 17:27:53 up 4 days, 2:21, X-Editor: GNU Emacs [ http://www.gnu.org/software/emacs ] X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Andrew Burgess via Gdb-patches Reply-To: Andrew Burgess Cc: gdb-patches@sourceware.org Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" * Tom Tromey [2022-01-02 16:46:17 -0700]: > Lancelot> I have included comments and questions below. > > Thanks for the review. > > >> + buf[out++] = '0'; > >> + buf[out++] = '0'; > >> + buf[out++] = '0'; > > Lancelot> This is different from the printchar implementation you moved (and it > Lancelot> seems that always printing '\000' is a loss of information). Maybe you > Lancelot> forgot to finalize this part? > > Yikes, I certainly did. This means that there aren't any tests for > this, so in v2 of the patch (appended) I added a self-test. > > Lancelot> I?find the comment slightly misleading (or incomplete). The function > Lancelot> prints exactly N?chars. So if 'strlen (str) < n', putstrn prints > Lancelot> more stuff than putstrn. > > Lancelot> Maybe ?Like putstr, but print exactly N characters, starting at the > Lancelot> position pointed by STR?, or something similar? > > I rewrote this to try to clarify it, and also rearranged so that the > more detailed comment from printchar is now by putstr. > > >> + usual, but not any other quoting character. */ > > Lancelot> You are missing one space after the '.'. > > Fixed (well, avoided) by the comment rearrangement. > > Tom > > commit a87c1495874162fa41171ff8a43dcfa1ce2e48e2 > Author: Tom Tromey > Date: Fri Dec 31 10:40:02 2021 -0700 > > Implement putstr and putstrn in ui_file > > In my tour of the ui_file subsystem, I found that fputstr and fputstrn > can be simplified. The _filtered forms are never used (and IMO > unlikely to ever be used) and so can be removed. And, the interface > can be simplified by removing a callback function and moving the > implementation directly to ui_file. > > A new self-test is included. Previously, I think nothing was testing > this code. > > Regression tested on x86-64 Fedora 34. > > diff --git a/gdb/Makefile.in b/gdb/Makefile.in > index 8cd97475e86..d0db5fbdee1 100644 > --- a/gdb/Makefile.in > +++ b/gdb/Makefile.in > @@ -477,6 +477,7 @@ SELFTESTS_SRCS = \ > unittests/style-selftests.c \ > unittests/tracepoint-selftests.c \ > unittests/tui-selftests.c \ > + unittests/ui-file-selftests.c \ > unittests/unpack-selftests.c \ > unittests/utils-selftests.c \ > unittests/vec-utils-selftests.c \ > diff --git a/gdb/guile/scm-ports.c b/gdb/guile/scm-ports.c > index bc6266ae055..9ce9cb4f678 100644 > --- a/gdb/guile/scm-ports.c > +++ b/gdb/guile/scm-ports.c > @@ -191,7 +191,7 @@ ioscm_open_port (scm_t_port_type *port_type, long mode_bits, scm_t_bits stream) > > > /* Support for connecting Guile's stdio ports to GDB's stdio ports. */ > > -/* Like fputstrn_filtered, but don't escape characters, except nul. > +/* Print a string, but don't escape characters, except nul. > Also like fputs_filtered, but a length is specified. */ Given you're changing this comment anyway, please could you get rid of the "Also like fputs_filtered...", we can just say something like: /* Print a string S, length SIZE, but don't escape characters, except nul. */ Then, everything is self contained. Otherwise, this version looks great. Thanks, Andrew > > static void > diff --git a/gdb/mi/mi-console.c b/gdb/mi/mi-console.c > index 157154b361c..9db87fe180c 100644 > --- a/gdb/mi/mi-console.c > +++ b/gdb/mi/mi-console.c > @@ -48,16 +48,6 @@ mi_console_file::write (const char *buf, long length_buf) > this->flush (); > } > > -/* Write C to STREAM's in an async-safe way. */ > - > -static int > -do_fputc_async_safe (int c, ui_file *stream) > -{ > - char ch = c; > - stream->write_async_safe (&ch, 1); > - return c; > -} > - > void > mi_console_file::write_async_safe (const char *buf, long length_buf) > { > @@ -65,12 +55,11 @@ mi_console_file::write_async_safe (const char *buf, long length_buf) > if (m_quote) > { > m_raw->write_async_safe (&m_quote, 1); > - fputstrn_unfiltered (buf, length_buf, m_quote, do_fputc_async_safe, > - m_raw); > + m_raw->putstrn (buf, length_buf, m_quote, true); > m_raw->write_async_safe (&m_quote, 1); > } > else > - fputstrn_unfiltered (buf, length_buf, 0, do_fputc_async_safe, m_raw); > + m_raw->putstrn (buf, length_buf, 0, true); > > char nl = '\n'; > m_raw->write_async_safe (&nl, 1); > @@ -91,14 +80,13 @@ mi_console_file::flush () > if (m_quote) > { > fputc_unfiltered (m_quote, m_raw); > - fputstrn_unfiltered (buf, length_buf, m_quote, fputc_unfiltered, > - m_raw); > + m_raw->putstrn (buf, length_buf, m_quote); > fputc_unfiltered (m_quote, m_raw); > fputc_unfiltered ('\n', m_raw); > } > else > { > - fputstrn_unfiltered (buf, length_buf, 0, fputc_unfiltered, m_raw); > + m_raw->putstrn (buf, length_buf, 0); > fputc_unfiltered ('\n', m_raw); > } > gdb_flush (m_raw); > diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c > index e729a861c61..4860da7536a 100644 > --- a/gdb/mi/mi-main.c > +++ b/gdb/mi/mi-main.c > @@ -1866,7 +1866,7 @@ mi_print_exception (const char *token, const struct gdb_exception &exception) > if (exception.message == NULL) > fputs_unfiltered ("unknown error", mi->raw_stdout); > else > - fputstr_unfiltered (exception.what (), '"', mi->raw_stdout); > + mi->raw_stdout->putstr (exception.what (), '"'); > fputs_unfiltered ("\"", mi->raw_stdout); > > switch (exception.error) > diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c > index e9a44437a90..20c6f0f9194 100644 > --- a/gdb/mi/mi-out.c > +++ b/gdb/mi/mi-out.c > @@ -135,7 +135,7 @@ mi_ui_out::do_field_string (int fldno, int width, ui_align align, > fprintf_unfiltered (stream, "%s=", fldname); > fprintf_unfiltered (stream, "\""); > if (string) > - fputstr_unfiltered (string, '"', stream); > + stream->putstr (string, '"'); > fprintf_unfiltered (stream, "\""); > } > > diff --git a/gdb/ui-file.c b/gdb/ui-file.c > index 57352e44a34..c6a4888ed48 100644 > --- a/gdb/ui-file.c > +++ b/gdb/ui-file.c > @@ -47,13 +47,15 @@ ui_file::printf (const char *format, ...) > void > ui_file::putstr (const char *str, int quoter) > { > - fputstr_unfiltered (str, quoter, this); > + while (*str) > + printchar (*str++, quoter, false); > } > > void > -ui_file::putstrn (const char *str, int n, int quoter) > +ui_file::putstrn (const char *str, int n, int quoter, bool async_safe) > { > - fputstrn_unfiltered (str, n, quoter, fputc_unfiltered, this); > + for (int i = 0; i < n; i++) > + printchar (str[i], quoter, async_safe); > } > > int > @@ -68,6 +70,67 @@ ui_file::vprintf (const char *format, va_list args) > vfprintf_unfiltered (this, format, args); > } > > +/* See ui-file.h. */ > + > +void > +ui_file::printchar (int c, int quoter, bool async_safe) > +{ > + char buf[4]; > + int out = 0; > + > + c &= 0xFF; /* Avoid sign bit follies */ > + > + if (c < 0x20 /* Low control chars */ > + || (c >= 0x7F && c < 0xA0) /* DEL, High controls */ > + || (sevenbit_strings && c >= 0x80)) > + { /* high order bit set */ > + buf[out++] = '\\'; > + > + switch (c) > + { > + case '\n': > + buf[out++] = 'n'; > + break; > + case '\b': > + buf[out++] = 'b'; > + break; > + case '\t': > + buf[out++] = 't'; > + break; > + case '\f': > + buf[out++] = 'f'; > + break; > + case '\r': > + buf[out++] = 'r'; > + break; > + case '\033': > + buf[out++] = 'e'; > + break; > + case '\007': > + buf[out++] = 'a'; > + break; > + default: > + { > + buf[out++] = '0' + ((c >> 6) & 0x7); > + buf[out++] = '0' + ((c >> 3) & 0x7); > + buf[out++] = '0' + ((c >> 0) & 0x7); > + break; > + } > + } > + } > + else > + { > + if (quoter != 0 && (c == '\\' || c == quoter)) > + buf[out++] = '\\'; > + buf[out++] = c; > + } > + > + if (async_safe) > + this->write_async_safe (buf, out); > + else > + this->write (buf, out); > +} > + > > > > void > diff --git a/gdb/ui-file.h b/gdb/ui-file.h > index 89f249ead5e..c097abf0c29 100644 > --- a/gdb/ui-file.h > +++ b/gdb/ui-file.h > @@ -34,12 +34,22 @@ class ui_file > > void printf (const char *, ...) ATTRIBUTE_PRINTF (2, 3); > > - /* Print a string whose delimiter is QUOTER. Note that these > - routines should only be called for printing things which are > - independent of the language of the program being debugged. */ > + /* Print a NUL-terminated string whose delimiter is QUOTER. Note > + that these routines should only be called for printing things > + which are independent of the language of the program being > + debugged. > + > + This will normally escape backslashes and instances of QUOTER. > + If QUOTER is 0, it won't escape backslashes or any quoting > + character. As a side effect, if you pass the backslash character > + as the QUOTER, this will escape backslashes as usual, but not any > + other quoting character. */ > void putstr (const char *str, int quoter); > > - void putstrn (const char *str, int n, int quoter); > + /* Like putstr, but only print the first N characters of STR. If > + ASYNC_SAFE is true, then the output is done via the > + write_async_safe method. */ > + void putstrn (const char *str, int n, int quoter, bool async_safe = false); > > int putc (int c); > > @@ -96,6 +106,13 @@ class ui_file > default. */ > return false; > } > + > +private: > + > + /* Helper function for putstr and putstrn. Print the character C on > + this stream as part of the contents of a literal string whose > + delimiter is QUOTER. */ > + void printchar (int c, int quoter, bool async_safe); > }; > > typedef std::unique_ptr ui_file_up; > diff --git a/gdb/unittests/ui-file-selftests.c b/gdb/unittests/ui-file-selftests.c > new file mode 100644 > index 00000000000..55854386169 > --- /dev/null > +++ b/gdb/unittests/ui-file-selftests.c > @@ -0,0 +1,62 @@ > +/* Self tests for ui_file > + > + Copyright (C) 2022 Free Software Foundation, Inc. > + > + This file is part of GDB. > + > + This program is free software; you can redistribute it and/or modify > + it under the terms of the GNU General Public License as published by > + the Free Software Foundation; either version 3 of the License, or > + (at your option) any later version. > + > + This program is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + GNU General Public License for more details. > + > + You should have received a copy of the GNU General Public License > + along with this program. If not, see . */ > + > +#include "defs.h" > +#include "gdbsupport/selftest.h" > +#include "ui-file.h" > + > +namespace selftests { > +namespace file { > + > +static void > +check_one (const char *str, int quoter, const char *result) > +{ > + string_file out; > + out.putstr (str, quoter); > + SELF_CHECK (out.string () == result); > +} > + > +static void > +run_tests () > +{ > + check_one ("basic stuff: \\", '\\', > + "basic stuff: \\\\"); > + check_one ("more basic stuff: \\Q", 'Q', > + "more basic stuff: \\\\\\Q"); > + check_one ("more basic stuff: \\Q", '\0', > + "more basic stuff: \\Q"); > + > + check_one ("weird stuff: \x1f\x90\n\b\t\f\r\033\007", '\\', > + "weird stuff: \\037\\220\\n\\b\\t\\f\\r\\e\\a"); > + > + scoped_restore save_7 = make_scoped_restore (&sevenbit_strings, true); > + check_one ("more weird stuff: \xa5", '\\', > + "more weird stuff: \\245"); > +} > + > +} /* namespace file*/ > +} /* namespace selftests */ > + > +void _initialize_ui_file_selftest (); > +void > +_initialize_ui_file_selftest () > +{ > + selftests::register_test ("ui-file", > + selftests::file::run_tests); > +} > diff --git a/gdb/utils.c b/gdb/utils.c > index 11f88d6f991..91662dc5fd8 100644 > --- a/gdb/utils.c > +++ b/gdb/utils.c > @@ -1129,103 +1129,6 @@ parse_escape (struct gdbarch *gdbarch, const char **string_ptr) > return target_char; > } > > > -/* Print the character C on STREAM as part of the contents of a literal > - string whose delimiter is QUOTER. Note that this routine should only > - be called for printing things which are independent of the language > - of the program being debugged. > - > - printchar will normally escape backslashes and instances of QUOTER. If > - QUOTER is 0, printchar won't escape backslashes or any quoting character. > - As a side effect, if you pass the backslash character as the QUOTER, > - printchar will escape backslashes as usual, but not any other quoting > - character. */ > - > -static void > -printchar (int c, do_fputc_ftype do_fputc, ui_file *stream, int quoter) > -{ > - c &= 0xFF; /* Avoid sign bit follies */ > - > - if (c < 0x20 || /* Low control chars */ > - (c >= 0x7F && c < 0xA0) || /* DEL, High controls */ > - (sevenbit_strings && c >= 0x80)) > - { /* high order bit set */ > - do_fputc ('\\', stream); > - > - switch (c) > - { > - case '\n': > - do_fputc ('n', stream); > - break; > - case '\b': > - do_fputc ('b', stream); > - break; > - case '\t': > - do_fputc ('t', stream); > - break; > - case '\f': > - do_fputc ('f', stream); > - break; > - case '\r': > - do_fputc ('r', stream); > - break; > - case '\033': > - do_fputc ('e', stream); > - break; > - case '\007': > - do_fputc ('a', stream); > - break; > - default: > - { > - do_fputc ('0' + ((c >> 6) & 0x7), stream); > - do_fputc ('0' + ((c >> 3) & 0x7), stream); > - do_fputc ('0' + ((c >> 0) & 0x7), stream); > - break; > - } > - } > - } > - else > - { > - if (quoter != 0 && (c == '\\' || c == quoter)) > - do_fputc ('\\', stream); > - do_fputc (c, stream); > - } > -} > - > -/* Print the character C on STREAM as part of the contents of a > - literal string whose delimiter is QUOTER. Note that these routines > - should only be call for printing things which are independent of > - the language of the program being debugged. */ > - > -void > -fputstr_filtered (const char *str, int quoter, struct ui_file *stream) > -{ > - while (*str) > - printchar (*str++, fputc_filtered, stream, quoter); > -} > - > -void > -fputstr_unfiltered (const char *str, int quoter, struct ui_file *stream) > -{ > - while (*str) > - printchar (*str++, fputc_unfiltered, stream, quoter); > -} > - > -void > -fputstrn_filtered (const char *str, int n, int quoter, > - struct ui_file *stream) > -{ > - for (int i = 0; i < n; i++) > - printchar (str[i], fputc_filtered, stream, quoter); > -} > - > -void > -fputstrn_unfiltered (const char *str, int n, int quoter, > - do_fputc_ftype do_fputc, struct ui_file *stream) > -{ > - for (int i = 0; i < n; i++) > - printchar (str[i], do_fputc, stream, quoter); > -} > - > > > /* Number of lines per page or UINT_MAX if paging is disabled. */ > static unsigned int lines_per_page; > diff --git a/gdb/utils.h b/gdb/utils.h > index 69f84e0489c..ac30fd5f114 100644 > --- a/gdb/utils.h > +++ b/gdb/utils.h > @@ -464,21 +464,6 @@ extern void print_spaces_filtered (int, struct ui_file *); > > extern const char *n_spaces (int); > > -extern void fputstr_filtered (const char *str, int quotr, > - struct ui_file * stream); > - > -extern void fputstr_unfiltered (const char *str, int quotr, > - struct ui_file * stream); > - > -extern void fputstrn_filtered (const char *str, int n, int quotr, > - struct ui_file * stream); > - > -typedef int (*do_fputc_ftype) (int c, ui_file *stream); > - > -extern void fputstrn_unfiltered (const char *str, int n, int quotr, > - do_fputc_ftype do_fputc, > - struct ui_file * stream); > - > /* Return nonzero if filtered printing is initialized. */ > extern int filtered_printing_initialized (void); > >