diff mbox series

trace-cmd stat: Show all filter functions that are enabled

Message ID 20220607183142.00dac63f@gandalf.local.home (mailing list archive)
State Accepted
Commit 0fc577a64539bc7b9db4de6efedd98602a5fbebb
Headers show
Series trace-cmd stat: Show all filter functions that are enabled | expand

Commit Message

Steven Rostedt June 7, 2022, 10:31 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

The way the files are printed in trace-cmd stat uses a read loop and keeps
allocating memory to read the content. It reads at BUFSIZ increments. But
if a read does not read exactly BUFSIZ it stops reading and returns what
it read but may not return the rest.

This does not work with the filter functions as the read may not fill the
buffer (it does not return an unfinished function). This makes the loop
exit early.

Instead just increase with BUFSIZ against what was allocated and comparing
to the total.

The way to trigger the issue is by:

   # trace-cmd start -p nop -l "*"
   # trace-cmd show --ftrace_filter |wc -l
 50979
   # trace-cmd stat | wc -l
 234

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=212489
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 tracecmd/trace-stat.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/tracecmd/trace-stat.c b/tracecmd/trace-stat.c
index a5fb777b32b8..4c3dc6494922 100644
--- a/tracecmd/trace-stat.c
+++ b/tracecmd/trace-stat.c
@@ -71,21 +71,24 @@  char *append_file(const char *dir, const char *name)
 
 static char *get_fd_content(int fd, const char *file)
 {
+	size_t total = 0;
+	size_t alloc;
 	char *str = NULL;
-	int cnt = 0;
 	int ret;
 
 	for (;;) {
-		str = realloc(str, BUFSIZ * ++cnt);
+		alloc = ((total + BUFSIZ) / BUFSIZ) * BUFSIZ;
+		str = realloc(str, alloc + 1);
 		if (!str)
 			die("malloc");
-		ret = read(fd, str + BUFSIZ * (cnt - 1), BUFSIZ);
+		ret = read(fd, str + total, alloc - total);
 		if (ret < 0)
 			die("reading %s\n", file);
-		if (ret < BUFSIZ)
+		total += ret;
+		if (!ret)
 			break;
 	}
-	str[BUFSIZ * (cnt-1) + ret] = 0;
+	str[total] = 0;
 
 	return str;
 }