linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/3] Integrate stack tracer status in 'stat'
@ 2018-01-16 19:53 Vladislav Valtchev (VMware)
  2018-01-16 19:53 ` [PATCH v4 1/3] trace-cmd: Make read_proc() to return int status via OUT arg Vladislav Valtchev (VMware)
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Vladislav Valtchev (VMware) @ 2018-01-16 19:53 UTC (permalink / raw)
  To: rostedt
  Cc: linux-trace-devel, linux-kernel, y.karadz, Vladislav Valtchev (VMware)

This short patch series makes trace-cmd stat aware of the stack tracer: now,
when the stack tracker is ON, the command will report that.

Vladislav Valtchev (VMware) (3):
  trace-cmd: Make read_proc() to return int status via OUT arg
  trace-cmd: Remove the die() call from read_proc()
  trace-cmd: Making stat to report when the stack tracer is ON

 trace-cmd.h   |  2 ++
 trace-stack.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
 trace-stat.c  |  8 ++++++
 3 files changed, 79 insertions(+), 16 deletions(-)

-- 
2.14.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 1/3] trace-cmd: Make read_proc() to return int status via OUT arg
  2018-01-16 19:53 [PATCH v4 0/3] Integrate stack tracer status in 'stat' Vladislav Valtchev (VMware)
@ 2018-01-16 19:53 ` Vladislav Valtchev (VMware)
  2018-01-19 21:58   ` Steven Rostedt
  2018-01-16 19:53 ` [PATCH v4 2/3] trace-cmd: Remove the die() call from read_proc() Vladislav Valtchev (VMware)
  2018-01-16 19:53 ` [PATCH v4 3/3] trace-cmd: Making stat to report when the stack tracer is ON Vladislav Valtchev (VMware)
  2 siblings, 1 reply; 7+ messages in thread
From: Vladislav Valtchev (VMware) @ 2018-01-16 19:53 UTC (permalink / raw)
  To: rostedt
  Cc: linux-trace-devel, linux-kernel, y.karadz, Vladislav Valtchev (VMware)

This patch changes both the implementation and the interface of read_proc()
in trace-stack.c. First, it makes the function to read a string from the proc
file and then parse it as an integer using strtol(). Then, it makes the function
to return the integer read from the proc file using the int *status OUT
parameter, in order to make possible its return value to be used by the caller
to check if the operation succeeded.

This new implementation relaxes the external contraints the function relies on,
making it possible to be used by trace stat. The point is that 'stat' should not
fail in case there is something wrong with the stack tracer. Only the call to
die() in case the file is empty has been left in this patch: it will be removed
as well in a separate commit.

Signed-off-by: Vladislav Valtchev (VMware) <vladislav.valtchev@gmail.com>
---
 trace-stack.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 57 insertions(+), 15 deletions(-)

diff --git a/trace-stack.c b/trace-stack.c
index aa79ae3..fada62d 100644
--- a/trace-stack.c
+++ b/trace-stack.c
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <limits.h>
 #include <getopt.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -49,37 +50,74 @@ static void test_available(void)
 		die("stack tracer not configured on running kernel");
 }
 
-static char read_proc(void)
+/*
+ * Returns:
+ *   -1 - Something went wrong
+ *    0 - File does not exist (stack tracer not enabled)
+ *    1 - Success
+ */
+static int read_proc(int *status)
 {
-	char buf[1];
+	struct stat stat_buf;
+	char buf[64];
+	long num;
 	int fd;
 	int n;
 
+	if (stat(PROC_FILE, &stat_buf) < 0) {
+		/* stack tracer not configured on running kernel */
+		*status = 0; /* not configured means disabled */
+		return 0;
+	}
+
 	fd = open(PROC_FILE, O_RDONLY);
+
 	if (fd < 0)
-		die("reading %s", PROC_FILE);
-	n = read(fd, buf, 1);
+		return -1;
+
+	n = read(fd, buf, sizeof(buf));
 	close(fd);
-	if (n != 1)
+
+	if (n <= 0)
 		die("error reading %s", PROC_FILE);
 
-	return buf[0];
+	if (n >= sizeof(buf))
+		return -1;
+
+	buf[n] = 0;
+	errno = 0;
+	num = strtol(buf, NULL, 10);
+
+	/* Check for various possible errors */
+	if (num > INT_MAX || num < INT_MIN || (!num && errno))
+		return -1;
+
+	*status = num;
+	return 1; /* full success */
 }
 
-static void start_stop_trace(char val)
+/* NOTE: this implementation only accepts new_status in the range [0..9]. */
+static void change_stack_tracer_status(unsigned new_status)
 {
 	char buf[1];
+	int status;
 	int fd;
 	int n;
 
-	buf[0] = read_proc();
-	if (buf[0] == val)
+	if (new_status > 9) {
+		warning("invalid status %d\n", new_status);
 		return;
+	}
+
+	if (read_proc(&status) > 0 && status == new_status)
+		return; /* nothing to do */
 
 	fd = open(PROC_FILE, O_WRONLY);
 	if (fd < 0)
 		die("writing %s", PROC_FILE);
-	buf[0] = val;
+
+	buf[0] = new_status + '0';
+
 	n = write(fd, buf, 1);
 	if (n < 0)
 		die("writing into %s", PROC_FILE);
@@ -88,12 +126,12 @@ static void start_stop_trace(char val)
 
 static void start_trace(void)
 {
-	start_stop_trace('1');
+	change_stack_tracer_status(1);
 }
 
 static void stop_trace(void)
 {
-	start_stop_trace('0');
+	change_stack_tracer_status(0);
 }
 
 static void reset_trace(void)
@@ -118,13 +156,17 @@ static void reset_trace(void)
 
 static void read_trace(void)
 {
-	FILE *fp;
-	char *path;
 	char *buf = NULL;
+	int status;
+	char *path;
+	FILE *fp;
 	size_t n;
 	int r;
 
-	if (read_proc() == '1')
+	if (read_proc(&status) <= 0)
+		die("Invalid stack tracer state");
+
+	if (status > 0)
 		printf("(stack tracer running)\n");
 	else
 		printf("(stack tracer not running)\n");
-- 
2.14.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v4 2/3] trace-cmd: Remove the die() call from read_proc()
  2018-01-16 19:53 [PATCH v4 0/3] Integrate stack tracer status in 'stat' Vladislav Valtchev (VMware)
  2018-01-16 19:53 ` [PATCH v4 1/3] trace-cmd: Make read_proc() to return int status via OUT arg Vladislav Valtchev (VMware)
@ 2018-01-16 19:53 ` Vladislav Valtchev (VMware)
  2018-01-16 19:53 ` [PATCH v4 3/3] trace-cmd: Making stat to report when the stack tracer is ON Vladislav Valtchev (VMware)
  2 siblings, 0 replies; 7+ messages in thread
From: Vladislav Valtchev (VMware) @ 2018-01-16 19:53 UTC (permalink / raw)
  To: rostedt
  Cc: linux-trace-devel, linux-kernel, y.karadz, Vladislav Valtchev (VMware)

As trace-stack.c's read_proc() function is going to be used by trace-cmd stat,
we don't want it to make the program die in case something went wrong.
Therefore, this simple patch makes read_proc() to just return -1 in case the
proc file was empty or read() failed with an error, instead of using die().

Signed-off-by: Vladislav Valtchev (VMware) <vladislav.valtchev@gmail.com>
---
 trace-stack.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/trace-stack.c b/trace-stack.c
index fada62d..3e1e41b 100644
--- a/trace-stack.c
+++ b/trace-stack.c
@@ -79,7 +79,7 @@ static int read_proc(int *status)
 	close(fd);
 
 	if (n <= 0)
-		die("error reading %s", PROC_FILE);
+		return -1;
 
 	if (n >= sizeof(buf))
 		return -1;
@@ -101,6 +101,7 @@ static void change_stack_tracer_status(unsigned new_status)
 {
 	char buf[1];
 	int status;
+	int ret;
 	int fd;
 	int n;
 
@@ -109,7 +110,11 @@ static void change_stack_tracer_status(unsigned new_status)
 		return;
 	}
 
-	if (read_proc(&status) > 0 && status == new_status)
+	ret = read_proc(&status);
+	if (ret < 0)
+		die("error reading %s", PROC_FILE);
+
+	if (ret > 0 && status == new_status)
 		return; /* nothing to do */
 
 	fd = open(PROC_FILE, O_WRONLY);
-- 
2.14.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v4 3/3] trace-cmd: Making stat to report when the stack tracer is ON
  2018-01-16 19:53 [PATCH v4 0/3] Integrate stack tracer status in 'stat' Vladislav Valtchev (VMware)
  2018-01-16 19:53 ` [PATCH v4 1/3] trace-cmd: Make read_proc() to return int status via OUT arg Vladislav Valtchev (VMware)
  2018-01-16 19:53 ` [PATCH v4 2/3] trace-cmd: Remove the die() call from read_proc() Vladislav Valtchev (VMware)
@ 2018-01-16 19:53 ` Vladislav Valtchev (VMware)
  2 siblings, 0 replies; 7+ messages in thread
From: Vladislav Valtchev (VMware) @ 2018-01-16 19:53 UTC (permalink / raw)
  To: rostedt
  Cc: linux-trace-devel, linux-kernel, y.karadz, Vladislav Valtchev (VMware)

trace-cmd stat is a handy way for users to see what tracing is currently going
on, but currently it does not say anything about the stack tracing. This patch
makes the command to show a message when the stack tracer is ON.

Signed-off-by: Vladislav Valtchev (VMware) <vladislav.valtchev@gmail.com>
---
 trace-cmd.h   | 2 ++
 trace-stack.c | 6 ++++++
 trace-stat.c  | 8 ++++++++
 3 files changed, 16 insertions(+)

diff --git a/trace-cmd.h b/trace-cmd.h
index 6fd34d7..9704b2e 100644
--- a/trace-cmd.h
+++ b/trace-cmd.h
@@ -358,6 +358,8 @@ void tracecmd_free_hooks(struct hook_list *hooks);
 /* --- Hack! --- */
 int tracecmd_blk_hack(struct tracecmd_input *handle);
 
+/* --- Stack tracer functions --- */
+int tracecmd_stack_tracer_status(int *status);
 
 /* --- Debugging --- */
 struct kbuffer *tracecmd_record_kbuf(struct tracecmd_input *handle,
diff --git a/trace-stack.c b/trace-stack.c
index 3e1e41b..65a74ad 100644
--- a/trace-stack.c
+++ b/trace-stack.c
@@ -96,6 +96,12 @@ static int read_proc(int *status)
 	return 1; /* full success */
 }
 
+/* Public wrapper of read_proc() */
+int tracecmd_stack_tracer_status(int *status)
+{
+	return read_proc(status);
+}
+
 /* NOTE: this implementation only accepts new_status in the range [0..9]. */
 static void change_stack_tracer_status(unsigned new_status)
 {
diff --git a/trace-stat.c b/trace-stat.c
index fd16354..23d7dd4 100644
--- a/trace-stat.c
+++ b/trace-stat.c
@@ -893,6 +893,7 @@ void trace_stat (int argc, char **argv)
 {
 	struct buffer_instance *instance = &top_instance;
 	int topt = 0;
+	int status;
 	int c;
 
 	for (;;) {
@@ -928,5 +929,12 @@ void trace_stat (int argc, char **argv)
 		stat_instance(instance);
 	}
 
+	if (tracecmd_stack_tracer_status(&status) >= 0) {
+		if (status > 0)
+			printf("Stack tracing is enabled\n\n");
+	} else {
+		printf("Error reading stack tracer status\n\n");
+	}
+
 	exit(0);
 }
-- 
2.14.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 1/3] trace-cmd: Make read_proc() to return int status via OUT arg
  2018-01-16 19:53 ` [PATCH v4 1/3] trace-cmd: Make read_proc() to return int status via OUT arg Vladislav Valtchev (VMware)
@ 2018-01-19 21:58   ` Steven Rostedt
  2018-01-19 22:02     ` Steven Rostedt
  0 siblings, 1 reply; 7+ messages in thread
From: Steven Rostedt @ 2018-01-19 21:58 UTC (permalink / raw)
  To: Vladislav Valtchev (VMware); +Cc: linux-trace-devel, linux-kernel, y.karadz


Just to let you know. I accepted all three of your patches. I have one
comment, (and changed this myself).

On Tue, 16 Jan 2018 21:53:41 +0200
"Vladislav Valtchev (VMware)" <vladislav.valtchev@gmail.com> wrote:

> file and then parse it as an integer using strtol(). Then, it makes the function

This line is 80 characters. For commit logs, we recommend no more than
76 characters (some people suggest 74). The reason is that a git show
will add 4 characters, to each line of the log, and this will cause an
overflow like this:

commit 68f00f512883fa7f1d04a7d88defff5e687bd98f
Author: Vladislav Valtchev (VMware) <vladislav.valtchev@gmail.com>
Date:   Tue Jan 16 21:53:41 2018 +0200

    trace-cmd: Make read_proc() to return int status via OUT arg

    This patch changes both the implementation and the interface of read_proc()
    in trace-stack.c. First, it makes the function to read a string from the pro
c
    file and then parse it as an integer using strtol(). Then, it makes the func
tion
    to return the integer read from the proc file using the int *status OUT
    parameter, in order to make possible its return value to be used by the call
er
    to check if the operation succeeded.

    This new implementation relaxes the external contraints the function relies
on,
    making it possible to be used by trace stat. The point is that 'stat' should
 not
    fail in case there is something wrong with the stack tracer. Only the call t
o
    die() in case the file is empty has been left in this patch: it will be remo
ved
    as well in a separate commit.


When editing a git log with the vim editor, I usually do :set tw=76

Thanks!

-- Steve

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 1/3] trace-cmd: Make read_proc() to return int status via OUT arg
  2018-01-19 21:58   ` Steven Rostedt
@ 2018-01-19 22:02     ` Steven Rostedt
  2018-01-22  9:03       ` Vladislav K. Valtchev
  0 siblings, 1 reply; 7+ messages in thread
From: Steven Rostedt @ 2018-01-19 22:02 UTC (permalink / raw)
  To: Vladislav Valtchev (VMware); +Cc: linux-trace-devel, linux-kernel, y.karadz

On Fri, 19 Jan 2018 16:58:02 -0500
Steven Rostedt <rostedt@goodmis.org> wrote:

> Just to let you know. I accepted all three of your patches. I have one
> comment, (and changed this myself).

Anyway,

Great job so far! I think I'm all set to start getting trace-cmd 2.7
out now. Unfortunately, I don't think I'll be able to get to that
before I leave on my next trip.

-- Steve

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 1/3] trace-cmd: Make read_proc() to return int status via OUT arg
  2018-01-19 22:02     ` Steven Rostedt
@ 2018-01-22  9:03       ` Vladislav K. Valtchev
  0 siblings, 0 replies; 7+ messages in thread
From: Vladislav K. Valtchev @ 2018-01-22  9:03 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: linux-trace-devel, linux-kernel, y.karadz

On Fri, 2018-01-19 at 17:02 -0500, Steven Rostedt wrote:
> On Fri, 19 Jan 2018 16:58:02 -0500
> Steven Rostedt <rostedt@goodmis.org> wrote:
> 
> > Just to let you know. I accepted all three of your patches. I have one
> > comment, (and changed this myself).
> 
> Anyway,
> 
> Great job so far! I think I'm all set to start getting trace-cmd 2.7
> out now. Unfortunately, I don't think I'll be able to get to that
> before I leave on my next trip.
> 
> -- Steve


Thanks Steven, I'm happy that you accepted my patches!
Sure, the next time I'll use 76 (or maybe even 74) as col limit
for the description.

-- 
Vladislav Valtchev
VMware Open Source Technology Center

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-01-22  9:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-16 19:53 [PATCH v4 0/3] Integrate stack tracer status in 'stat' Vladislav Valtchev (VMware)
2018-01-16 19:53 ` [PATCH v4 1/3] trace-cmd: Make read_proc() to return int status via OUT arg Vladislav Valtchev (VMware)
2018-01-19 21:58   ` Steven Rostedt
2018-01-19 22:02     ` Steven Rostedt
2018-01-22  9:03       ` Vladislav K. Valtchev
2018-01-16 19:53 ` [PATCH v4 2/3] trace-cmd: Remove the die() call from read_proc() Vladislav Valtchev (VMware)
2018-01-16 19:53 ` [PATCH v4 3/3] trace-cmd: Making stat to report when the stack tracer is ON Vladislav Valtchev (VMware)

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