From: Mark Wielaard Date: Sun, 7 Feb 2021 23:25:52 +0000 (+0100) Subject: PR140939 --track-fds reports leakage of stdout/in/err and doesn't respect -q X-Git-Tag: VALGRIND_3_17_0~62 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c9781cc97e719c1ecf0cfad9ca2f86631c017268;p=thirdparty%2Fvalgrind.git PR140939 --track-fds reports leakage of stdout/in/err and doesn't respect -q Make --track-fds=yes not report on file descriptors 0, 1, and 2 (stdin, stdout, and stderr) by default. Add a new option --track-fds=all that does report on the std file descriptors still being open. Update testsuite and documentation. Original patch by Peter Kelly Updated by Daniel Fahlgren https://bugs.kde.org/show_bug.cgi?id=140939 --- diff --git a/NEWS b/NEWS index 5342336ecc..cd372235f2 100644 --- a/NEWS +++ b/NEWS @@ -52,6 +52,7 @@ To see details of a given bug, visit where XXXXXX is the bug number as listed below. 140178 open("/proc/self/exe", ...); doesn't quite work +140939 --track-fds reports leakage of stdout/in/err and doesn't respect -q 345077 linux syscall execveat support (linux 3.19) 369029 handle linux syscalls sched_getattr and sched_setattr n-i-bz helgrind: If hg_cli__realloc fails, return NULL. diff --git a/coregrind/m_gdbserver/server.c b/coregrind/m_gdbserver/server.c index cc809a8b63..8e4bae8567 100644 --- a/coregrind/m_gdbserver/server.c +++ b/coregrind/m_gdbserver/server.c @@ -234,7 +234,7 @@ int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return) " v.info last_error : show last error found\n" " v.info location : show information about location \n" " v.info n_errs_found [msg] : show the nr of errors found so far and the given msg\n" -" v.info open_fds : show open file descriptors (only if --track-fds=yes)\n" +" v.info open_fds : show open file descriptors (only if --track-fds=[yes|all])\n" " v.kill : kill the Valgrind process\n" " v.clo ... : changes one or more dynamic command line options\n" " with no clo_option, show the dynamically changeable options.\n" @@ -427,7 +427,7 @@ int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return) VG_(show_open_fds) (""); else VG_(gdb_printf) - ("Valgrind must be started with --track-fds=yes" + ("Valgrind must be started with --track-fds=[yes|all]" " to show open fds\n"); ret = 1; break; diff --git a/coregrind/m_main.c b/coregrind/m_main.c index c182fd90e9..68edc4d499 100644 --- a/coregrind/m_main.c +++ b/coregrind/m_main.c @@ -110,7 +110,8 @@ 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 valgrindabexit all none\n" -" --track-fds=no|yes track open file descriptors? [no]\n" +" --track-fds=no|yes|all track open file descriptors? [no]\n" +" all includes reporting stdin, stdout and stderr\n" " --time-stamp=no|yes add timestamps to log messages? [no]\n" " --log-fd= log messages to file descriptor [2=stderr]\n" " --log-file= log messages to \n" @@ -600,7 +601,17 @@ static void process_option (Clo_Mode mode, else if VG_BOOL_CLOM(cloPD, arg, "--show-below-main", VG_(clo_show_below_main)) {} else if VG_BOOL_CLO(arg, "--keep-debuginfo", VG_(clo_keep_debuginfo)) {} else if VG_BOOL_CLOM(cloPD, arg, "--time-stamp", VG_(clo_time_stamp)) {} - else if VG_BOOL_CLO(arg, "--track-fds", VG_(clo_track_fds)) {} + else if VG_STR_CLO(arg, "--track-fds", tmp_str) { + if (VG_(strcmp)(tmp_str, "yes") == 0) + VG_(clo_track_fds) = 1; + else if (VG_(strcmp)(tmp_str, "all") == 0) + VG_(clo_track_fds) = 2; + else if (VG_(strcmp)(tmp_str, "no") == 0) + VG_(clo_track_fds) = 0; + else + VG_(fmsg_bad_option)(arg, + "Bad argument, should be 'yes', 'all' or 'no'\n"); + } else if VG_BOOL_CLOM(cloPD, arg, "--trace-children", VG_(clo_trace_children)) {} else if VG_BOOL_CLOM(cloPD, arg, "--child-silent-after-fork", VG_(clo_child_silent_after_fork)) {} diff --git a/coregrind/m_options.c b/coregrind/m_options.c index 10641d4faa..688523324e 100644 --- a/coregrind/m_options.c +++ b/coregrind/m_options.c @@ -172,7 +172,7 @@ Bool VG_(clo_read_var_info) = False; XArray *VG_(clo_req_tsyms); // array of strings Bool VG_(clo_run_libc_freeres) = True; Bool VG_(clo_run_cxx_freeres) = True; -Bool VG_(clo_track_fds) = False; +UInt VG_(clo_track_fds) = 0; Bool VG_(clo_show_below_main)= False; Bool VG_(clo_keep_debuginfo) = False; Bool VG_(clo_show_emwarns) = False; diff --git a/coregrind/m_syswrap/syswrap-generic.c b/coregrind/m_syswrap/syswrap-generic.c index 3810f74744..0739ccc9af 100644 --- a/coregrind/m_syswrap/syswrap-generic.c +++ b/coregrind/m_syswrap/syswrap-generic.c @@ -823,11 +823,28 @@ getsockdetails(Int fd) /* Dump out a summary, and a more detailed list, of open file descriptors. */ void VG_(show_open_fds) (const HChar* when) { - OpenFd *i = allocated_fds; + OpenFd *i; + int non_std = 0; - VG_(message)(Vg_UserMsg, "FILE DESCRIPTORS: %d open %s.\n", fd_count, when); + for (i = allocated_fds; i; i = i->next) { + if (i->fd > 2) + non_std++; + } + + /* If we are running quiet and there are either no open file descriptors + or not tracking all fds, then don't report anything. */ + if ((fd_count == 0 + || ((non_std == 0) && (VG_(clo_track_fds) < 2))) + && (VG_(clo_verbosity) == 0)) + return; + + VG_(message)(Vg_UserMsg, "FILE DESCRIPTORS: %d open (%d std) %s.\n", + fd_count, fd_count - non_std, when); + + for (i = allocated_fds; i; i = i->next) { + if (i->fd <= 2 && VG_(clo_track_fds) < 2) + continue; - while (i) { if (i->pathname) { VG_(message)(Vg_UserMsg, "Open file descriptor %d: %s\n", i->fd, i->pathname); @@ -850,8 +867,6 @@ void VG_(show_open_fds) (const HChar* when) VG_(message)(Vg_UserMsg, " \n"); VG_(message)(Vg_UserMsg, "\n"); } - - i = i->next; } VG_(message)(Vg_UserMsg, "\n"); diff --git a/coregrind/pub_core_options.h b/coregrind/pub_core_options.h index 136c0ff6e0..5dd01addcc 100644 --- a/coregrind/pub_core_options.h +++ b/coregrind/pub_core_options.h @@ -283,8 +283,8 @@ extern const HChar* VG_(clo_prefix_to_strip); wildcards. */ extern XArray *VG_(clo_req_tsyms); -/* Track open file descriptors? */ -extern Bool VG_(clo_track_fds); +/* Track open file descriptors? 0 = No, 1 = Yes, 2 = All (including std) */ +extern UInt VG_(clo_track_fds); /* Should we run __libc_freeres at exit? Sometimes causes crashes. Default: YES. Note this is subservient to VG_(needs).libc_freeres; diff --git a/docs/xml/manual-core-adv.xml b/docs/xml/manual-core-adv.xml index 8d2e905e42..1fa801edc1 100644 --- a/docs/xml/manual-core-adv.xml +++ b/docs/xml/manual-core-adv.xml @@ -1371,8 +1371,12 @@ client request. v.info open_fds shows the list of open file descriptors and details related to the file descriptor. - This only works if - was given at Valgrind startup. + This only works if or + (to include + stdin, + stdout and + stderr) was given at Valgrindr + startup. diff --git a/docs/xml/manual-core.xml b/docs/xml/manual-core.xml index 2e90ab7b8c..5d52d2e3be 100644 --- a/docs/xml/manual-core.xml +++ b/docs/xml/manual-core.xml @@ -852,7 +852,7 @@ in most cases. We group the available options by rough categories. - + When enabled, Valgrind will print out a list of open file @@ -860,7 +860,10 @@ in most cases. We group the available options by rough categories. command v.info open_fds. 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. + as the file name or socket details. Use to + include reporting on stdin, + stdout and + stderr. diff --git a/gdbserver_tests/mchelp.stdoutB.exp b/gdbserver_tests/mchelp.stdoutB.exp index f8582de345..6f45932d10 100644 --- a/gdbserver_tests/mchelp.stdoutB.exp +++ b/gdbserver_tests/mchelp.stdoutB.exp @@ -5,7 +5,7 @@ general valgrind monitor commands: v.info last_error : show last error found v.info location : show information about location v.info n_errs_found [msg] : show the nr of errors found so far and the given msg - v.info open_fds : show open file descriptors (only if --track-fds=yes) + v.info open_fds : show open file descriptors (only if --track-fds=[yes|all]) v.kill : kill the Valgrind process v.clo ... : changes one or more dynamic command line options with no clo_option, show the dynamically changeable options. @@ -64,7 +64,7 @@ general valgrind monitor commands: v.info last_error : show last error found v.info location : show information about location v.info n_errs_found [msg] : show the nr of errors found so far and the given msg - v.info open_fds : show open file descriptors (only if --track-fds=yes) + v.info open_fds : show open file descriptors (only if --track-fds=[yes|all]) v.kill : kill the Valgrind process v.clo ... : changes one or more dynamic command line options with no clo_option, show the dynamically changeable options. diff --git a/gdbserver_tests/mssnapshot.stderrB.exp b/gdbserver_tests/mssnapshot.stderrB.exp index 067303ef14..8d463a4a71 100644 --- a/gdbserver_tests/mssnapshot.stderrB.exp +++ b/gdbserver_tests/mssnapshot.stderrB.exp @@ -6,7 +6,7 @@ general valgrind monitor commands: v.info last_error : show last error found v.info location : show information about location v.info n_errs_found [msg] : show the nr of errors found so far and the given msg - v.info open_fds : show open file descriptors (only if --track-fds=yes) + v.info open_fds : show open file descriptors (only if --track-fds=[yes|all]) v.kill : kill the Valgrind process v.clo ... : changes one or more dynamic command line options with no clo_option, show the dynamically changeable options. diff --git a/none/tests/cmdline1.stdout.exp b/none/tests/cmdline1.stdout.exp index 4b98743a66..7f05709548 100644 --- a/none/tests/cmdline1.stdout.exp +++ b/none/tests/cmdline1.stdout.exp @@ -25,7 +25,8 @@ usage: valgrind [options] prog-and-args --vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none] where event is one of: startup exit valgrindabexit all none - --track-fds=no|yes track open file descriptors? [no] + --track-fds=no|yes|all track open file descriptors? [no] + all includes reporting stdin, stdout and stderr --time-stamp=no|yes add timestamps to log messages? [no] --log-fd= log messages to file descriptor [2=stderr] --log-file= log messages to diff --git a/none/tests/cmdline2.stdout.exp b/none/tests/cmdline2.stdout.exp index 9e8e3df01f..7c8a23f8b0 100644 --- a/none/tests/cmdline2.stdout.exp +++ b/none/tests/cmdline2.stdout.exp @@ -25,7 +25,8 @@ usage: valgrind [options] prog-and-args --vgdb-stop-at=event1,event2,... invoke gdbserver for given events [none] where event is one of: startup exit valgrindabexit all none - --track-fds=no|yes track open file descriptors? [no] + --track-fds=no|yes|all track open file descriptors? [no] + all includes reporting stdin, stdout and stderr --time-stamp=no|yes add timestamps to log messages? [no] --log-fd= log messages to file descriptor [2=stderr] --log-file= log messages to diff --git a/none/tests/fdleak_cmsg.stderr.exp b/none/tests/fdleak_cmsg.stderr.exp index 6e5a797b9f..0fe56eaeed 100644 --- a/none/tests/fdleak_cmsg.stderr.exp +++ b/none/tests/fdleak_cmsg.stderr.exp @@ -1,6 +1,6 @@ -FILE DESCRIPTORS: 7 open at exit. +FILE DESCRIPTORS: 7 open (3 std) at exit. Open AF_UNIX socket ...: /tmp/sock ... @@ -24,7 +24,7 @@ Open file descriptor ...: /dev/null -FILE DESCRIPTORS: 6 open at exit. +FILE DESCRIPTORS: 6 open (3 std) at exit. Open file descriptor ...: /tmp/data2 ... diff --git a/none/tests/fdleak_cmsg.vgtest b/none/tests/fdleak_cmsg.vgtest index 5083320443..4b4bd8ab06 100644 --- a/none/tests/fdleak_cmsg.vgtest +++ b/none/tests/fdleak_cmsg.vgtest @@ -1,4 +1,4 @@ prog: fdleak_cmsg -vgopts: --track-fds=yes +vgopts: --track-fds=all stderr_filter: filter_fdleak args: < /dev/null diff --git a/none/tests/fdleak_creat.stderr.exp b/none/tests/fdleak_creat.stderr.exp index e7a6e918af..7b70478b9d 100644 --- a/none/tests/fdleak_creat.stderr.exp +++ b/none/tests/fdleak_creat.stderr.exp @@ -1,6 +1,6 @@ -FILE DESCRIPTORS: 4 open at exit. +FILE DESCRIPTORS: 4 open (3 std) at exit. Open file descriptor ...: /tmp/file ... diff --git a/none/tests/fdleak_creat.vgtest b/none/tests/fdleak_creat.vgtest index ffa412a456..25c243e024 100644 --- a/none/tests/fdleak_creat.vgtest +++ b/none/tests/fdleak_creat.vgtest @@ -1,4 +1,4 @@ prog: fdleak_creat -vgopts: --track-fds=yes +vgopts: --track-fds=all stderr_filter: filter_fdleak args: < /dev/null diff --git a/none/tests/fdleak_dup.stderr.exp b/none/tests/fdleak_dup.stderr.exp index 7737a7d9dc..8ceada9230 100644 --- a/none/tests/fdleak_dup.stderr.exp +++ b/none/tests/fdleak_dup.stderr.exp @@ -1,6 +1,6 @@ -FILE DESCRIPTORS: 5 open at exit. +FILE DESCRIPTORS: 5 open (3 std) at exit. Open file descriptor ...: /dev/null ... diff --git a/none/tests/fdleak_dup.vgtest b/none/tests/fdleak_dup.vgtest index d150dfef0a..012fbe02eb 100644 --- a/none/tests/fdleak_dup.vgtest +++ b/none/tests/fdleak_dup.vgtest @@ -1,4 +1,4 @@ prog: fdleak_dup -vgopts: --track-fds=yes +vgopts: --track-fds=all stderr_filter: filter_fdleak args: < /dev/null diff --git a/none/tests/fdleak_dup2.stderr.exp b/none/tests/fdleak_dup2.stderr.exp index 98d61a92a4..4576a578b3 100644 --- a/none/tests/fdleak_dup2.stderr.exp +++ b/none/tests/fdleak_dup2.stderr.exp @@ -1,6 +1,6 @@ -FILE DESCRIPTORS: 6 open at exit. +FILE DESCRIPTORS: 6 open (3 std) at exit. Open file descriptor ...: /dev/null ... diff --git a/none/tests/fdleak_dup2.vgtest b/none/tests/fdleak_dup2.vgtest index 7b0d95eeac..8d5e287b8e 100644 --- a/none/tests/fdleak_dup2.vgtest +++ b/none/tests/fdleak_dup2.vgtest @@ -1,4 +1,4 @@ prog: fdleak_dup2 -vgopts: --track-fds=yes +vgopts: --track-fds=all stderr_filter: filter_fdleak args: < /dev/null diff --git a/none/tests/fdleak_fcntl.stderr.exp b/none/tests/fdleak_fcntl.stderr.exp index 7737a7d9dc..8ceada9230 100644 --- a/none/tests/fdleak_fcntl.stderr.exp +++ b/none/tests/fdleak_fcntl.stderr.exp @@ -1,6 +1,6 @@ -FILE DESCRIPTORS: 5 open at exit. +FILE DESCRIPTORS: 5 open (3 std) at exit. Open file descriptor ...: /dev/null ... diff --git a/none/tests/fdleak_fcntl.vgtest b/none/tests/fdleak_fcntl.vgtest index 5b506eaafc..9ee14740be 100644 --- a/none/tests/fdleak_fcntl.vgtest +++ b/none/tests/fdleak_fcntl.vgtest @@ -1,4 +1,4 @@ prog: fdleak_fcntl -vgopts: --track-fds=yes +vgopts: --track-fds=all stderr_filter: filter_fdleak args: < /dev/null diff --git a/none/tests/fdleak_ipv4.stderr.exp b/none/tests/fdleak_ipv4.stderr.exp index 80c62019f1..72c2b46859 100644 --- a/none/tests/fdleak_ipv4.stderr.exp +++ b/none/tests/fdleak_ipv4.stderr.exp @@ -1,34 +1,16 @@ -FILE DESCRIPTORS: 5 open at exit. +FILE DESCRIPTORS: 5 open (3 std) at exit. Open AF_INET socket 4: 127.0.0.1:... <-> 127.0.0.1:... ... Open AF_INET socket 3: 127.0.0.1:... <-> unbound ... -Open file descriptor ...: ... - -Open file descriptor ...: ... - -Open file descriptor ...: /dev/null - - - - -FILE DESCRIPTORS: 4 open at exit. +FILE DESCRIPTORS: 4 open (3 std) at exit. Open AF_INET socket 3: 127.0.0.1:... <-> 127.0.0.1:... ... -Open file descriptor ...: ... - - -Open file descriptor ...: ... - - -Open file descriptor ...: /dev/null - - diff --git a/none/tests/fdleak_open.stderr.exp b/none/tests/fdleak_open.stderr.exp index 948ed97d4e..f3f51167f1 100644 --- a/none/tests/fdleak_open.stderr.exp +++ b/none/tests/fdleak_open.stderr.exp @@ -1,16 +1,7 @@ -FILE DESCRIPTORS: 4 open at exit. +FILE DESCRIPTORS: 4 open (3 std) at exit. Open file descriptor ...: /dev/null ... -Open file descriptor ...: ... - - -Open file descriptor ...: ... - - -Open file descriptor ...: /dev/null - - diff --git a/none/tests/fdleak_pipe.stderr.exp b/none/tests/fdleak_pipe.stderr.exp index 832fd7e11c..5f92ea69c2 100644 --- a/none/tests/fdleak_pipe.stderr.exp +++ b/none/tests/fdleak_pipe.stderr.exp @@ -1,19 +1,10 @@ -FILE DESCRIPTORS: 5 open at exit. +FILE DESCRIPTORS: 5 open (3 std) at exit. Open file descriptor ...: ... Open file descriptor ...: ... -Open file descriptor ...: ... - - -Open file descriptor ...: ... - - -Open file descriptor ...: /dev/null - - diff --git a/none/tests/fdleak_socketpair.stderr.exp b/none/tests/fdleak_socketpair.stderr.exp index 377b1318ee..73832a108b 100644 --- a/none/tests/fdleak_socketpair.stderr.exp +++ b/none/tests/fdleak_socketpair.stderr.exp @@ -1,19 +1,10 @@ -FILE DESCRIPTORS: 5 open at exit. +FILE DESCRIPTORS: 5 open (3 std) at exit. Open AF_UNIX socket ...: ... Open AF_UNIX socket ...: ... -Open file descriptor ...: ... - - -Open file descriptor ...: ... - - -Open file descriptor ...: /dev/null - -