From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by sourceware.org (Postfix) with ESMTPS id C1753386102B for ; Fri, 2 Oct 2020 10:25:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C1753386102B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=andrew.burgess@embecosm.com Received: by mail-wr1-x442.google.com with SMTP id z1so1211254wrt.3 for ; Fri, 02 Oct 2020 03:25:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=zwYs22cJhsEjYMS3GyjdvrRpd4bo/OAOGINQEtTg12M=; b=BCNpnyWEPNni9mkdzHHqeb8omDuuExYIoadtiTUBE7mH6LQ5R9j880vKJtSKnTta4t 5sjBNbvu4B/Sp191gji5ouhisH4HplhpCxw89oav+s4RE4bHS1PyepX4lTikj6/BnZPK Ea/UYQd8EIvoPCZOBuEO4tGvuL9M9pS+9OGG2zHcqFhOenfXbKA7zmjnZPDHmNCPuzNL f7sOGaxrqcfd2/m/3uv2wHS48xLg06Bp3RxyqoWWPbyrFshbcYUuth4WA1azUJPfkQoH V7UB5P8kVp5HPDrPzaTvAe5Dbn91u1MlX2BxGiz7ar7sZ8ts1iSAfG1ifHS1a/NwcZ6v Aj7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=zwYs22cJhsEjYMS3GyjdvrRpd4bo/OAOGINQEtTg12M=; b=hk9yuWDXazZazUP5/nXmO/ZIDwVT0Ug2RjGKsIKJPqgEIFR6dvlHdfqsohoO8kTqau DGYc7YOPiANXzBygA6icjnaA4K07FYJCjpIFHYjAu/ytygqenNrTAkh/hF8qb6Bqg9rN MaurzkT14RprEHr3fYb8AfSnNHeUG1uy6d0g7OLW8xmIDXDXPJt6q+CKkN1z/HCgyO7s HilBvSzB1tBdmhOpvA/J0lVBjeSUPJ82ammlVnn2rTtjlpNLcG+6Xwgxmy07bPy0Ifci kpT9bu6ZbGeW/SMpjYZXayKu8WTqt0oC1q9OYfoEb8DUFyIrwmm32MvsGrHF5iI+fkyA JaXw== X-Gm-Message-State: AOAM531/dNXSJyOi5QgoJ7siYQTFYvcJuipFyBw40KvPT+aLU9RRu1Q3 fv1M6vVLP3D1RY5S/7ZNXJ42UvpeuUQAWA== X-Google-Smtp-Source: ABdhPJzwZfqyhMetlJ1GnqgHRl+tWs530yoBT4AeEs20TmT6qMQNSosXzFc4MP8f8Cjj98e4dsad2A== X-Received: by 2002:adf:ba4f:: with SMTP id t15mr2125714wrg.335.1601634300692; Fri, 02 Oct 2020 03:25:00 -0700 (PDT) Received: from localhost (host109-151-23-62.range109-151.btcentralplus.com. [109.151.23.62]) by smtp.gmail.com with ESMTPSA id z191sm1252374wme.40.2020.10.02.03.24.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Oct 2020 03:24:59 -0700 (PDT) Date: Fri, 2 Oct 2020 11:24:58 +0100 From: Andrew Burgess To: Tom Tromey Cc: gdb-patches@sourceware.org Subject: Re: [PATCH v2 1/7] Introduce read_entire_file Message-ID: <20201002102458.GQ1540618@embecosm.com> References: <20200623132006.15863-1-tom@tromey.com> <20200623132006.15863-2-tom@tromey.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200623132006.15863-2-tom@tromey.com> X-Operating-System: Linux/5.8.9-101.fc31.x86_64 (x86_64) X-Uptime: 11:24:45 up 13 days, 18:32, X-Editor: GNU Emacs [ http://www.gnu.org/software/emacs ] X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org 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: , X-List-Received-Date: Fri, 02 Oct 2020 10:25:03 -0000 * Tom Tromey [2020-06-23 07:20:00 -0600]: > This adds a new function, read_entire_file, and changes the source > cache to use it. This will be used in a subsequent patch as well. > > gdb/ChangeLog > 2020-06-22 Tom Tromey > > * utils.h (myread): Don't declare. > * utils.c (myread): Remove. > * source-cache.c (source_cache::get_plain_source_lines): Use > read_entire_file. > > gdbsupport/ChangeLog > 2020-06-22 Tom Tromey > > * filestuff.h (read_entire_file): Declare. > * filestuff.cc (read_entire_file): New function. LGTM. Thanks, Andrew > --- > gdb/ChangeLog | 7 +++++++ > gdb/source-cache.c | 14 +++++--------- > gdb/utils.c | 22 ---------------------- > gdb/utils.h | 2 -- > gdbsupport/ChangeLog | 5 +++++ > gdbsupport/filestuff.cc | 33 +++++++++++++++++++++++++++++++++ > gdbsupport/filestuff.h | 9 +++++++++ > 7 files changed, 59 insertions(+), 33 deletions(-) > > diff --git a/gdb/source-cache.c b/gdb/source-cache.c > index 9196e3a19e3..63e08c3aeb0 100644 > --- a/gdb/source-cache.c > +++ b/gdb/source-cache.c > @@ -18,6 +18,7 @@ > > #include "defs.h" > #include "source-cache.h" > +#include "gdbsupport/filestuff.h" > #include "gdbsupport/scoped_fd.h" > #include "source.h" > #include "cli/cli-style.h" > @@ -56,14 +57,9 @@ source_cache::get_plain_source_lines (struct symtab *s, > if (desc.get () < 0) > perror_with_name (symtab_to_filename_for_display (s)); > > - struct stat st; > - if (fstat (desc.get (), &st) < 0) > - perror_with_name (symtab_to_filename_for_display (s)); > - > - std::string lines; > - lines.resize (st.st_size); > - if (myread (desc.get (), &lines[0], lines.size ()) < 0) > - perror_with_name (symtab_to_filename_for_display (s)); > + time_t file_mtime; > + std::string lines = read_entire_file (symtab_to_filename_for_display (s), > + desc.get (), &file_mtime); > > time_t mtime = 0; > if (SYMTAB_OBJFILE (s) != NULL && SYMTAB_OBJFILE (s)->obfd != NULL) > @@ -71,7 +67,7 @@ source_cache::get_plain_source_lines (struct symtab *s, > else if (exec_bfd) > mtime = exec_bfd_mtime; > > - if (mtime && mtime < st.st_mtime) > + if (mtime && mtime < file_mtime) > warning (_("Source file is more recent than executable.")); > > std::vector offsets; > diff --git a/gdb/utils.c b/gdb/utils.c > index 102db28787f..023adfae066 100644 > --- a/gdb/utils.c > +++ b/gdb/utils.c > @@ -687,28 +687,6 @@ flush_streams () > gdb_stderr->flush (); > } > > -/* My replacement for the read system call. > - Used like `read' but keeps going if `read' returns too soon. */ > - > -int > -myread (int desc, char *addr, int len) > -{ > - int val; > - int orglen = len; > - > - while (len > 0) > - { > - val = read (desc, addr, len); > - if (val < 0) > - return val; > - if (val == 0) > - return orglen - len; > - len -= val; > - addr += val; > - } > - return orglen; > -} > - > void > print_spaces (int n, struct ui_file *file) > { > diff --git a/gdb/utils.h b/gdb/utils.h > index 3434ff1caa2..d0989204128 100644 > --- a/gdb/utils.h > +++ b/gdb/utils.h > @@ -540,8 +540,6 @@ void dummy_obstack_deallocate (void *object, void *data); > extern pid_t wait_to_die_with_timeout (pid_t pid, int *status, int timeout); > #endif > > -extern int myread (int, char *, int); > - > /* Resource limits used by getrlimit and setrlimit. */ > > enum resource_limit_kind > diff --git a/gdbsupport/filestuff.cc b/gdbsupport/filestuff.cc > index 179c4258918..0f0a238beb0 100644 > --- a/gdbsupport/filestuff.cc > +++ b/gdbsupport/filestuff.cc > @@ -501,3 +501,36 @@ mkdir_recursive (const char *dir) > component_start = component_end; > } > } > + > +/* See filestuff.h. */ > + > +std::string > +read_entire_file (const char *name, int fd, time_t *mtime) > +{ > + struct stat st; > + if (fstat (fd, &st) < 0) > + perror_with_name (name); > + size_t len = st.st_size; > + > + std::string lines; > + lines.resize (len); > + char *addr = &lines[0]; > + > + while (len > 0) > + { > + int val = read (fd, addr, len); > + if (val < 0) > + perror_with_name (name); > + if (val == 0) > + break; > + len -= val; > + addr += val; > + } > + > + if (mtime != nullptr) > + *mtime = st.st_mtime; > + > + /* Just in case we really did end up short. */ > + lines.resize (st.st_size - len); > + return lines; > +} > diff --git a/gdbsupport/filestuff.h b/gdbsupport/filestuff.h > index e36936a84ce..84924f462e2 100644 > --- a/gdbsupport/filestuff.h > +++ b/gdbsupport/filestuff.h > @@ -139,4 +139,13 @@ extern bool is_regular_file (const char *name, int *errno_ptr); > > extern bool mkdir_recursive (const char *dir); > > +/* Read and return the entire contents of a file. The file must > + already be open on FD. NAME is the file name it is used when > + reporting errors, which is done by throwing an exception. The > + mtime of the file is optionally stored in *MTIME; if MTIME is > + nullptr, this is ignored. */ > + > +extern std::string read_entire_file (const char *name, int fd, > + time_t *mtime = nullptr); > + > #endif /* COMMON_FILESTUFF_H */ > -- > 2.17.2 >