From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gateway24.websitewelcome.com (gateway24.websitewelcome.com [192.185.50.93]) by sourceware.org (Postfix) with ESMTPS id 43AD1385DC02 for ; Sat, 4 Apr 2020 14:54:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 43AD1385DC02 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=tom@tromey.com Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway24.websitewelcome.com (Postfix) with ESMTP id EC4066F90 for ; Sat, 4 Apr 2020 09:54:23 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id KkBjj3CPbXVkQKkBjjHTJ6; Sat, 04 Apr 2020 09:54:23 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=yfe+CPIh0LvOdLSihi91RlCfAxMOgi1CbK3Fz665uHY=; b=Es1efddYtCssrxiI/APXouiBaY md9YZ9UaiHg5+qJVpWsEenCYJN5kVSeOMl70lmXjpkdUMWwb9pchJBnzqDiFjV5qIU9n+QP0Bu9q6 1/qSAYjeT0LT9J3/7JvFlnWk2; Received: from 174-16-110-145.hlrn.qwest.net ([174.16.110.145]:58652 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92) (envelope-from ) id 1jKkBj-003pFd-Ny; Sat, 04 Apr 2020 08:54:23 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFC 1/6] Introduce read_entire_file Date: Sat, 4 Apr 2020 08:54:17 -0600 Message-Id: <20200404145422.19917-2-tom@tromey.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20200404145422.19917-1-tom@tromey.com> References: <20200404145422.19917-1-tom@tromey.com> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 174.16.110.145 X-Source-L: No X-Exim-ID: 1jKkBj-003pFd-Ny X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 174-16-110-145.hlrn.qwest.net (bapiya.Home) [174.16.110.145]:58652 X-Source-Auth: tom+tromey.com X-Email-Count: 33 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-18.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, JMQ_SPF_NEUTRAL, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_NEUTRAL, 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: Sat, 04 Apr 2020 14:54:25 -0000 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-03-28 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-03-28 Tom Tromey * filestuff.h (read_entire_file): Declare. * filestuff.cc (read_entire_file): New function. --- 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 bda6bbf5b0e..3fe739eeaf5 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -691,28 +691,6 @@ malloc_failure (long size) } } -/* 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