From 93579f6f908fa6010b141fd5da2974d878869c80 Mon Sep 17 00:00:00 2001 From: John Baldwin Date: Fri, 30 Nov 2018 15:14:18 -0800 Subject: [PATCH] Use kinfo_getfile to implement fdwalk on FreeBSD. 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 | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 778eebc1b10..83eabc43c56 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 Philippe Waroquiers Simon Marchi diff --git a/gdb/common/filestuff.c b/gdb/common/filestuff.c index 0db5c6936bc..f4d5e38f072 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,25 @@ fdwalk (int (*func) (void *, int), void *arg) } /* We may fall through to the next case. */ #endif +#ifdef HAVE_KINFO_GETFILE + int nfd; + gdb::unique_xmalloc_ptr fdtbl + (kinfo_getfile (getpid (), &nfd)); + if (fdtbl != NULL) + { + for (int i = 0; i < nfd; i++) + { + if (fdtbl[i].kf_fd >= 0) + { + int result = func (arg, fdtbl[i].kf_fd); + if (result != 0) + return result; + } + } + return 0; + } + /* We may fall through to the next case. */ +#endif { int max, fd; -- 2.30.2