linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] trace-cmd stat: Show all filter functions that are enabled
@ 2022-06-07 22:31 Steven Rostedt
  0 siblings, 0 replies; only message in thread
From: Steven Rostedt @ 2022-06-07 22:31 UTC (permalink / raw)
  To: Linux Trace Devel

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 --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;
 }
-- 
2.35.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08  1:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-07 22:31 [PATCH] trace-cmd stat: Show all filter functions that are enabled Steven Rostedt

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).