From patchwork Fri Nov 30 21:23:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Baldwin X-Patchwork-Id: 30483 Received: (qmail 116488 invoked by alias); 30 Nov 2018 21:28:02 -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 116436 invoked by uid 89); 30 Nov 2018 21:28:01 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS, SPF_SOFTFAIL autolearn=ham version=3.3.2 spammy=Hx-languages-length:1872, sk:unique_, HContent-Transfer-Encoding:8bit X-HELO: mail.baldwin.cx Received: from bigwig.baldwin.cx (HELO mail.baldwin.cx) (96.47.65.170) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 30 Nov 2018 21:27:58 +0000 Received: from ralph.com (ralph.baldwin.cx [66.234.199.215]) by mail.baldwin.cx (Postfix) with ESMTPSA id C071A10B670 for ; Fri, 30 Nov 2018 16:27:55 -0500 (EST) From: John Baldwin To: gdb-patches@sourceware.org Subject: [PATCH] Use kinfo_getfile to implement fdwalk on FreeBSD. Date: Fri, 30 Nov 2018 13:23:31 -0800 Message-Id: <20181130212331.59989-1-jhb@FreeBSD.org> MIME-Version: 1.0 X-IsSubscribed: yes kinfo_getfile() requires a couple of system calls to fetch the list of open file descriptors. This can be much cheaper than invoking fstat on all of the values from 0 to the open file resource limit maximum. gdb/ChangeLog: * common/filestuff.c [HAVE_KINFO_GETFILE]: Include headers. (fdwalk) [HAVE_KINFO_GETFILE]: Use kinfo_getfile. --- gdb/ChangeLog | 5 +++++ gdb/common/filestuff.c | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 348eb65ec7..f88cc16b4d 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2018-11-30 John Baldwin + + * common/filestuff.c [HAVE_KINFO_GETFILE]: Include headers. + (fdwalk) [HAVE_KINFO_GETFILE]: Use kinfo_getfile. + 2018-11-30 John Baldwin * fbsd-nat.c [__FreeBSD_version >= 700009] (USE_SIGINFO): Macro diff --git a/gdb/common/filestuff.c b/gdb/common/filestuff.c index 0db5c6936b..48b0487f54 100644 --- a/gdb/common/filestuff.c +++ b/gdb/common/filestuff.c @@ -36,6 +36,11 @@ #define HAVE_SOCKETS 1 #endif +#ifdef HAVE_KINFO_GETFILE +#include +#include +#endif + #ifdef HAVE_SYS_RESOURCE_H #include #endif /* HAVE_SYS_RESOURCE_H */ @@ -108,6 +113,27 @@ fdwalk (int (*func) (void *, int), void *arg) } /* We may fall through to the next case. */ #endif +#ifdef HAVE_KINFO_GETFILE + gdb::unique_xmalloc_ptr fdtbl; + int nfd; + fdtbl.reset (kinfo_getfile (getpid (), &nfd)); + if (fdtbl != NULL) + { + int result = 0; + struct kinfo_file *kf = fdtbl.get (); + for (int i = 0; i < nfd; i++, kf++) + { + if (kf->kf_fd >= 0) + { + result = func (arg, kf->kf_fd); + if (result != 0) + break; + } + } + return result; + } + /* We may fall through to the next case. */ +#endif { int max, fd;