linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Linux Trace Devel <linux-trace-devel@vger.kernel.org>
Cc: Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
Subject: [PATCH v2] trace-cmd test: Quiet valgrind from reporting forked children
Date: Wed, 7 Jun 2023 14:01:53 -0400	[thread overview]
Message-ID: <20230607140153.161b43a0@gandalf.local.home> (raw)

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

valgrind triggers its reports when the application exits. If the application
does a fork() and the child exits, it will still trigger a report saying that
the memory that was currently allocated by the parent has leaked.

To prevent this, call execl() on a shell script that will simply exit with the
proper error code. This will keep valgrind from producing a report on the
child for the memory that is still allocated by the parent.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
Changes since v1: https://lore.kernel.org/linux-trace-devel/20230607135256.41a9ab6b@gandalf.local.home/

  - test "/bin/sh" too, as not all systems have "/usr/bin/sh"

 utest/tracecmd-utest.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/utest/tracecmd-utest.c b/utest/tracecmd-utest.c
index 34fed1e1..6ba2318e 100644
--- a/utest/tracecmd-utest.c
+++ b/utest/tracecmd-utest.c
@@ -131,6 +131,7 @@ static int pipe_it(int *ofd, int *efd, int (*func)(void *),
 		goto fail;
 
 	if (!pid) {
+		char shret[32];
 
 		close(obrass[0]);
 		close(STDOUT_FILENO);
@@ -143,6 +144,23 @@ static int pipe_it(int *ofd, int *efd, int (*func)(void *),
 			exit(-1);
 
 		ret = func(data);
+
+		/*
+		 * valgrind triggers its reports when the application
+		 * exits. If the application does a fork() and the child
+		 * exits, it will still trigger the valgrind report for
+		 * all the allocations that were not freed by the parent.
+		 *
+		 * To prevent valgrind from triggering, do an execl() on
+		 * a basic shell that will simply exit with the return value.
+		 * This will quiet valgrind from reporting memory that has
+		 * been allocated by the parent up to here.
+		 */
+		snprintf(shret, 32, "exit %d", ret);
+		execl("/usr/bin/sh", "/usr/bin/sh", "-c", shret, NULL);
+		execl("/bin/sh", "/bin/sh", "-c", shret, NULL);
+
+		/* If the above execl() fails, simply do an exit */
 		exit(ret);
 	}
 
-- 
2.35.1


                 reply	other threads:[~2023-06-07 18:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230607140153.161b43a0@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=tz.stoyanov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).