]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Add "bad" option for --track-fds.
authorAlexandra Hájková <ahajkova@redhat.com>
Fri, 20 Jun 2025 07:21:41 +0000 (03:21 -0400)
committerAlexandra Hájková <ahajkova@redhat.com>
Thu, 26 Jun 2025 18:48:55 +0000 (20:48 +0200)
When --track-fds=bad is specified, do not warn about
leaked file descriptors and only warn about file decriptors
which was not opened or already closed.

Update the documentation in docs/xml/manual-core.xml.

Add none/tests/track_bad test to test the new option.

Adjust none/tests/cmdline1 and  none/tests/cmdline2 expected
outputs.

https://bugs.kde.org/show_bug.cgi?id=493434

12 files changed:
.gitignore
NEWS
coregrind/m_main.c
docs/xml/manual-core.xml
none/tests/Makefile.am
none/tests/cmdline1.stdout.exp
none/tests/cmdline1.stdout.exp-non-linux
none/tests/cmdline2.stdout.exp
none/tests/cmdline2.stdout.exp-non-linux
none/tests/track_bad.c [new file with mode: 0644]
none/tests/track_bad.stderr.exp [new file with mode: 0644]
none/tests/track_bad.vgtest [new file with mode: 0644]

index 8cabb96df49fa5068a4252aa0531d8f90368c706..32cedb0d7c6b269f26e962d14de3731bf855dd66 100644 (file)
 /none/tests/track-fds-exec-children
 /none/tests/track_new
 /none/tests/track_std
+/none/tests/track_bad
 /none/tests/unit_debuglog
 /none/tests/use_after_close
 /none/tests/valgrind_cpp_test
diff --git a/NEWS b/NEWS
index 0e011826516cc3dfc31caaa0e209024df5fa3908..0d9cc798ec8b819319aab0875e696f6d72a4bb07 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -24,6 +24,7 @@ than mailing the developers (or mailing lists) directly -- bugs that
 are not entered into bugzilla tend to get forgotten about or ignored.
 
 338803  Handling of dwz debug alt files or cross-CU is broken
+493434  Add --track-fds=bad mode (no "leak" tracking)
 503098  Incorrect NAN-boxing for float registers in RISC-V
 503641  close_range syscalls started failing with 3.25.0
 503677  duplicated-cond compiler warning in dis_RV64M
index 128e4298c18872c11bd74835183abfcf96e5a417..f7fd20dbae3ff8c83cf198aca3572365ade64439 100644 (file)
@@ -113,8 +113,12 @@ static void usage_NORETURN ( int need_help )
 "    --vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none]\n"
 "         where event is one of:\n"
 "           startup exit abexit valgrindabexit all none\n"
-"    --track-fds=no|yes|all    track open file descriptors? [no]\n"
-"                              all includes reporting inherited file descriptors\n"
+"    --track-fds=no|yes|all|bad track open file descriptors? [no]\n"
+"                              all also reports on open inherited file\n"
+"                              descriptors at exit (e.g. stdin/out/err)\n"
+"                              bad only reports on file descriptor usage\n"
+"                              errors and doesn't list open file descriptors\n"
+"                              at exit\n"
 "    --modify-fds=no|yes|high  modify newly open file descriptors? [no]\n"
 "    --time-stamp=no|yes       add timestamps to log messages? [no]\n"
 "    --log-fd=<number>         log messages to file descriptor [2=stderr]\n"
@@ -643,6 +647,8 @@ static void process_option (Clo_Mode mode,
          VG_(clo_track_fds) = 2;
       else if (VG_(strcmp)(tmp_str, "no") == 0)
          VG_(clo_track_fds) = 0;
+      else if (VG_(strcmp)(tmp_str, "bad") == 0)
+         VG_(clo_track_fds) = 3;
       else
          VG_(fmsg_bad_option)(arg,
             "Bad argument, should be 'yes', 'all' or 'no'\n");
@@ -2324,7 +2330,7 @@ void shutdown_actions_NORETURN( ThreadId tid,
    }
 
    /* Print out file descriptor summary and stats. */
-   if (VG_(clo_track_fds))
+   if (VG_(clo_track_fds) && VG_(clo_track_fds) < 3)
       VG_(show_open_fds)("at exit");
 
    /* Call the tool's finalisation function.  This makes Memcheck's
index 7d18d46f391a935ac7c51e5f55266cda5e4df688..9ab09b51bf00cdba4b4342cc26f3c548c406af9a 100644 (file)
@@ -886,18 +886,31 @@ in most cases.  We group the available options by rough categories.</para>
 
   <varlistentry id="opt.track-fds" xreflabel="--track-fds">
     <term>
-      <option><![CDATA[--track-fds=<yes|no|all> [default: no] ]]></option>
-    </term>
-    <listitem>
-      <para>When enabled, Valgrind will print out a list of open file
-      descriptors on exit or on request, via the gdbserver monitor
-      command <varname>v.info open_fds</varname>.  Along with each
-      file descriptor is printed a stack backtrace of where the file
-      was opened and any details relating to the file descriptor such
-      as the file name or socket details. Use <option>all</option> to
-      include reporting on <computeroutput>stdin</computeroutput>,
+      <option><![CDATA[--track-fds=<yes|no|all|bad> [default: no] ]]></option>
+    </term>
+    <listitem>
+      <para>When enabled, Valgrind will track all file descriptor
+      usage. It will produce errors for bad file descriptor usage like
+      closing a file descriptor twice, using a file descriptor
+      (number) that was never created, or passing an already closed
+      file descriptor to a system call. It will also print out a list
+      of open file descriptors on exit. Or on request, via the
+      gdbserver monitor command <varname>v.info open_fds</varname>.</para>
+
+      <para>When an error is generated, or when listing the still open
+      file descriptors at exit, a stack backtrace of where the file
+      was opened is printed. If the file descriptor has already been
+      closed, it will also include a backtrace of where it was
+      previously closed. Any error will include details relating to
+      the file descriptor such as the file name or socket details.</para>
+
+      <para>Use <option>all</option> to also include reporting
+      on <computeroutput>stdin</computeroutput>,
       <computeroutput>stdout</computeroutput> and
-      <computeroutput>stderr</computeroutput>.</para>
+      <computeroutput>stderr</computeroutput> (or other inherited file
+      descriptors) at exit.  If <option>bad</option> option is used,
+      then exclude reporting on leaked file descriptors at exit and
+      only produce errors about bad file decriptors usage.</para>
     </listitem>
   </varlistentry>
 
index ca01f9e42da91f1e2a25576b80cd4ecdc7f09559..716ce000d39a95375c90e73ad662353e8511d3f6 100644 (file)
@@ -275,7 +275,8 @@ EXTRA_DIST = \
        track_new.stderr.exp track_new.stdout.exp \
        track_new.vgtest track_new.stderr.exp-illumos \
        track_yes.vgtest track_high.vgtest \
-       track_yes.stderr.exp track_high.stderr.exp
+       track_yes.stderr.exp track_high.stderr.exp \
+       track_bad.vgtest track_bad.stderr.exp
 
 check_PROGRAMS = \
        args \
@@ -334,7 +335,8 @@ check_PROGRAMS = \
        fdbaduse \
         use_after_close \
        track_new \
-       track_std
+       track_std \
+       track_bad
 
 if HAVE_STATIC_LIBC
 if ! VGCONF_OS_IS_LINUX
index 06a679a11112dd6b57302a4e656201da5c6498ad..5d3ea056977e9ffc388ada53590c288893d9bec8 100644 (file)
@@ -28,8 +28,12 @@ usage: valgrind [options] prog-and-args
     --vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none]
          where event is one of:
            startup exit abexit valgrindabexit all none
-    --track-fds=no|yes|all    track open file descriptors? [no]
-                              all includes reporting inherited file descriptors
+    --track-fds=no|yes|all|bad track open file descriptors? [no]
+                              all also reports on open inherited file
+                              descriptors at exit (e.g. stdin/out/err)
+                              bad only reports on file descriptor usage
+                              errors and doesn't list open file descriptors
+                              at exit
     --modify-fds=no|yes|high  modify newly open file descriptors? [no]
     --time-stamp=no|yes       add timestamps to log messages? [no]
     --log-fd=<number>         log messages to file descriptor [2=stderr]
index 4049d5ab2fb0e11b924604e10f2ae45e0bce0673..9b044e78a17cc654f74181178074b0ab96073334 100644 (file)
@@ -28,8 +28,12 @@ usage: valgrind [options] prog-and-args
     --vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none]
          where event is one of:
            startup exit abexit valgrindabexit all none
-    --track-fds=no|yes|all    track open file descriptors? [no]
-                              all includes reporting inherited file descriptors
+    --track-fds=no|yes|all|bad track open file descriptors? [no]
+                              all also reports on open inherited file
+                              descriptors at exit (e.g. stdin/out/err)
+                              bad only reports on file descriptor usage
+                              errors and doesn't list open file descriptors
+                              at exit
     --modify-fds=no|yes|high  modify newly open file descriptors? [no]
     --time-stamp=no|yes       add timestamps to log messages? [no]
     --log-fd=<number>         log messages to file descriptor [2=stderr]
index d7914ae0101bbc53f2a262e4ca21b0aff5756917..618c43acbcd8b3b0c408a406d90e0670f17d3fae 100644 (file)
@@ -28,8 +28,12 @@ usage: valgrind [options] prog-and-args
     --vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none]
          where event is one of:
            startup exit abexit valgrindabexit all none
-    --track-fds=no|yes|all    track open file descriptors? [no]
-                              all includes reporting inherited file descriptors
+    --track-fds=no|yes|all|bad track open file descriptors? [no]
+                              all also reports on open inherited file
+                              descriptors at exit (e.g. stdin/out/err)
+                              bad only reports on file descriptor usage
+                              errors and doesn't list open file descriptors
+                              at exit
     --modify-fds=no|yes|high  modify newly open file descriptors? [no]
     --time-stamp=no|yes       add timestamps to log messages? [no]
     --log-fd=<number>         log messages to file descriptor [2=stderr]
index d709ea365dd8b62635646a556dd82ee99e3e67b4..767f1cb9c03381eae5ec30633e7f69699eee820a 100644 (file)
@@ -28,7 +28,7 @@ usage: valgrind [options] prog-and-args
     --vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none]
          where event is one of:
            startup exit abexit valgrindabexit all none
-    --track-fds=no|yes|all    track open file descriptors? [no]
+    --track-fds=no|yes|all|bad track open file descriptors? [no]
                               all includes reporting inherited file descriptors
     --modify-fds=no|yes|high  modify newly open file descriptors? [no]
     --time-stamp=no|yes       add timestamps to log messages? [no]
diff --git a/none/tests/track_bad.c b/none/tests/track_bad.c
new file mode 100644 (file)
index 0000000..b1674d6
--- /dev/null
@@ -0,0 +1,34 @@
+#include<stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "fdleak.h"
+
+int main(void)
+{
+   char *string = "bad\n";
+   int fd = dup(2);
+
+   /* OK. */
+   write(fd, string, 4);
+   close(fd);
+
+   /* Already closed. */
+   write(fd, string, 4);
+
+   /* Never created. */
+   write(7, string, 4);
+
+   /* Invalid. */
+   write(-7, string, 4);
+
+   /* Double double close. */
+   close(fd);
+
+   /* Invalid close. */
+   close (-7);
+
+   (void) DO( open("/dev/null", O_RDONLY) );
+
+   return 0;
+}
diff --git a/none/tests/track_bad.stderr.exp b/none/tests/track_bad.stderr.exp
new file mode 100644 (file)
index 0000000..620a6b8
--- /dev/null
@@ -0,0 +1,28 @@
+bad
+File descriptor was closed already
+   at 0x........: write (in /...libc...)
+   by 0x........: main
+ Previously closed
+   at 0x........: close (in /...libc...)
+   by 0x........: main
+ Originally opened
+   at 0x........: dup (in /...libc...)
+   by 0x........: main
+File descriptor 7 was never created
+   at 0x........: write (in /...libc...)
+   by 0x........: main
+File descriptor -7 Invalid file descriptor
+   at 0x........: write (in /...libc...)
+   by 0x........: main
+File descriptor ...: ... is already closed
+   at 0x........: close (in /...libc...)
+   by 0x........: main
+ Previously closed
+   at 0x........: close (in /...libc...)
+   by 0x........: main
+ Originally opened
+   at 0x........: dup (in /...libc...)
+   by 0x........: main
+File descriptor -7 Invalid file descriptor
+   at 0x........: close (in /...libc...)
+   by 0x........: main
diff --git a/none/tests/track_bad.vgtest b/none/tests/track_bad.vgtest
new file mode 100644 (file)
index 0000000..77daf8f
--- /dev/null
@@ -0,0 +1,3 @@
+prog: track_bad
+vgopts: -q --track-fds=bad
+stderr_filter: filter_fdleak