Skip to contentSkip to conversation

Integrate stack tracer status in 'stat'

PATCH Superseded Vladislav Valtchev (VMware) 2 participants 8 messages Jan 16, 2018

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Integrate stack tracer status in 'stat'

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(-)
Signature · 1 line
2.14.1
#1 lore raw

PATCH v3 1/3 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 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.

---
 trace-stack.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 63 insertions(+), 16 deletions(-)
trace-stack.c +63 -16
index aa79ae3..c1058ca 100644
@@ -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,79 @@ 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);
-	close(fd);
-	if (n != 1)
+
+	if (fd < 0) {
+		/* we cannot open the file: likely a permission problem. */
+		return -1;
+	}
+
+	n = read(fd, buf, sizeof(buf));
+
+	/* We assume that the file is never empty we got no errors. */
+	if (n <= 0)
 		die("error reading %s", PROC_FILE);
 
-	return buf[0];
+	/* Does this file have more than 63 characters?? */
+	if (n >= sizeof(buf))
+		return -1;
+
+	/* n is guaranteed to be in the range [1, sizeof(buf)-1]. */
+	buf[n] = 0;
+	close(fd);
+
+	errno = 0;
+
+	/* Read an integer from buf ignoring any non-digit trailing characters. */
+	num = strtol(buf, NULL, 10);
+
+	/* strtol() returned 0: we have to check for errors */
+	if (!num && (errno == EINVAL || errno == ERANGE))
+		return -1;
+
+	if (num > INT_MAX || num < INT_MIN)
+		return -1; /* the number is good but does not fit in 'int' */
+
+	*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(int new_status)
 {
 	char buf[1];
+	int status;
 	int fd;
 	int n;
 
-	buf[0] = read_proc();
-	if (buf[0] == val)
-		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 +131,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)
@@ -123,8 +166,12 @@ static void read_trace(void)
 	char *buf = NULL;
 	size_t n;
 	int r;
+	int status;
 
-	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");
Signature · 1 line
2.14.1
#2 lore raw

Re: [PATCH v3 1/3] trace-cmd: Make read_proc() to return int status via OUT arg

trace-stack.c static void test_available(void) · line 101

Quoted 8 lines · On Tue, 16 Jan 2018 09:47:42 +0200 "Vladislav Valtchev (VMware)"…
trace-stack.c +8 -0
+	errno = 0;
+
+	/* Read an integer from buf ignoring any non-digit trailing characters. */
+	num = strtol(buf, NULL, 10);
+
+	/* strtol() returned 0: we have to check for errors */
+	if (!num && (errno == EINVAL || errno == ERANGE))
+		return -1;

Repeating again here. According to the man page of strtol():

RETURN VALUE
The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol()
returns LONG_MIN. If an overflow occurs, strtol() returns LONG_MAX.
In both cases, errno is set to ERANGE. Precisely the same holds for strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX).

and this:

The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned).

Thus, !num is not enough. The example in the man page has:

           errno = 0;    /* To distinguish success/failure after call */
           val = strtol(str, &endptr, base);

           /* Check for various possible errors */

           if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
                   || (errno != 0 && val == 0)) {
               perror("strtol");
               exit(EXIT_FAILURE);
           }

Let's follow this.

-- Steve

#3 lore raw

Re: [PATCH v3 1/3] trace-cmd: Make read_proc() to return int status via OUT arg

Quoted 13 lines · Steven Rostedt

trace-stack.c static void test_available(void) · line 101

Quoted 8 lines · On Tue, 16 Jan 2018 09:47:42 +0200 "Vladislav Valtchev (VMware)"…
trace-stack.c +8 -0
+	errno = 0;
+
+	/* Read an integer from buf ignoring any non-digit trailing characters. */
+	num = strtol(buf, NULL, 10);
+
+	/* strtol() returned 0: we have to check for errors */
+	if (!num && (errno == EINVAL || errno == ERANGE))
+		return -1;

Repeating again here. According to the man page of strtol():

v3 addresses only the comments for patch 3/3.
I'm sorry for that. All the other comments will be addressed in v4.

Quoted 27 lines

RETURN VALUE
The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol()
returns LONG_MIN. If an overflow occurs, strtol() returns LONG_MAX.
In both cases, errno is set to ERANGE. Precisely the same holds for strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX).

and this:

The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned).

Thus, !num is not enough. The example in the man page has:

           errno = 0;    /* To distinguish success/failure after call */
           val = strtol(str, &endptr, base);

           /* Check for various possible errors */

           if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
                   || (errno != 0 && val == 0)) {
               perror("strtol");
               exit(EXIT_FAILURE);
           }
Quoted 3 lines

Let's follow this.

-- Steve

Sure, I thought that:

	errno = 0;
	num = strtol(buf, NULL, 10);

	/* strtol() returned 0: we have to check for errors */
	if (!num && (errno == EINVAL || errno == ERANGE))
		return -1;

	if (num > INT_MAX || num < INT_MIN)
		return -1;

covered all the cases because the case:
(val == LONG_MAX || val == LONG_MIN)

is covered by: if (num > INT_MAX || num < INT_MIN) [no matter the errno]

but that's not true for 32 bit systems where sizeof(long) == sizeof(int). It had to be: if (num >= INT_MAX || num <= INT_MIN), but in that case it would exclude two valid int32 values.

Therefore, let's go with:

	if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
            || (errno != 0 && val == 0))

Just let me keep also the following check:

	if (num > INT_MAX || num < INT_MIN)
		return -1;

since [INT_MIN, INT_MAX] is a subset of [LONG_MIN, LONG_MAX].

Vlad

Signature · 2 lines
Vladislav Valtchev
VMware Open Source Technology Center
#4 lore raw

Re: [PATCH v3 1/3] trace-cmd: Make read_proc() to return int status via OUT arg

Quoted 76 lines · On Tue, 16 Jan 2018 21:10:36 +0200 Vladislav Valtchev…
Quoted 13 lines · Steven Rostedt

trace-stack.c static void test_available(void) · line 101

Quoted 8 lines · On Tue, 16 Jan 2018 09:47:42 +0200 "Vladislav Valtchev (VMware)"…
trace-stack.c +8 -0
+	errno = 0;
+
+	/* Read an integer from buf ignoring any non-digit trailing characters. */
+	num = strtol(buf, NULL, 10);
+
+	/* strtol() returned 0: we have to check for errors */
+	if (!num && (errno == EINVAL || errno == ERANGE))
+		return -1;  

Repeating again here. According to the man page of strtol():

v3 addresses only the comments for patch 3/3.
I'm sorry for that. All the other comments will be addressed in v4.

Quoted 27 lines

RETURN VALUE
The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol()
returns LONG_MIN. If an overflow occurs, strtol() returns LONG_MAX.
In both cases, errno is set to ERANGE. Precisely the same holds for strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX).

and this:

The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned).

Thus, !num is not enough. The example in the man page has:

           errno = 0;    /* To distinguish success/failure after call */
           val = strtol(str, &endptr, base);

           /* Check for various possible errors */

           if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
                   || (errno != 0 && val == 0)) {
               perror("strtol");
               exit(EXIT_FAILURE);
           }
Quoted 3 lines

Let's follow this.

-- Steve

Sure, I thought that:

	errno = 0;
	num = strtol(buf, NULL, 10);

	/* strtol() returned 0: we have to check for errors */
	if (!num && (errno == EINVAL || errno == ERANGE))
		return -1;

	if (num > INT_MAX || num < INT_MIN)
		return -1;

covered all the cases because the case:
(val == LONG_MAX || val == LONG_MIN)

is covered by: if (num > INT_MAX || num < INT_MIN) [no matter the errno]

but that's not true for 32 bit systems where sizeof(long) == sizeof(int). It had to be: if (num >= INT_MAX || num <= INT_MIN), but in that case it would exclude two valid int32 values.

Therefore, let's go with:

	if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
            || (errno != 0 && val == 0))
Quoted 7 lines

Just let me keep also the following check:

	if (num > INT_MAX || num < INT_MIN)
		return -1;

since [INT_MIN, INT_MAX] is a subset of [LONG_MIN, LONG_MAX].

True. What about just doing:

	if (num > INT_MAX || num < INT_MIN || (!num && errno))

That should cover it all, and match what the man pages have.

-- Steve

#5 lore raw

Re: [PATCH v3 1/3] trace-cmd: Make read_proc() to return int status via OUT arg

In reply to Vladislav Valtchev (VMware)
Quoted 39 lines · On Tue, 16 Jan 2018 09:47:42 +0200 "Vladislav Valtchev (VMware)"…
trace-stack.c +17 -2
index aa79ae3..c1058ca 100644
@@ -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,79 @@ 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);

trace-stack.c static void test_available(void) · line 78

Quoted 10 lines
trace-stack.c +5 -5
-	if (fd < 0)
-		die("reading %s", PROC_FILE);
-	n = read(fd, buf, 1);
-	close(fd);
-	if (n != 1)
+
+	if (fd < 0) {
+		/* we cannot open the file: likely a permission problem. */
+		return -1;
+	}

Let's follow Linux coding style. The comment is obvious and can be removed, and we can remove the braces.

	if (fd < 0)
		return -1;

trace-stack.c static void test_available(void) · line 86

Quoted 9 lines
trace-stack.c +6 -1
+
+	n = read(fd, buf, sizeof(buf));
+
+	/* We assume that the file is never empty we got no errors. */
+	if (n <= 0)
 		die("error reading %s", PROC_FILE);
 
-	return buf[0];
+	/* Does this file have more than 63 characters?? */

The comment is too verbose, and not needed.

trace-stack.c static void test_available(void) · line 90

Quoted 4 lines
trace-stack.c +4 -0
+	if (n >= sizeof(buf))
+		return -1;
+
+	/* n is guaranteed to be in the range [1, sizeof(buf)-1]. */

The comment is not needed. It would be a bug if it were not true, and the code directly above the comment shows this guarantee.

trace-stack.c static void test_available(void) · line 96

Quoted 6 lines
trace-stack.c +6 -0
+	buf[n] = 0;
+	close(fd);
+
+	errno = 0;
+
+	/* Read an integer from buf ignoring any non-digit trailing characters. */

Don't need to describe what strtol() does. Remove the comment please.

trace-stack.c static void test_available(void) · line 99

Quoted 3 lines
trace-stack.c +3 -0
+	num = strtol(buf, NULL, 10);
+
+	/* strtol() returned 0: we have to check for errors */

Already commented about this code in my last email.

trace-stack.c static void test_available(void) · line 104

Quoted 5 lines
trace-stack.c +5 -0
+	if (!num && (errno == EINVAL || errno == ERANGE))
+		return -1;
+
+	if (num > INT_MAX || num < INT_MIN)
+		return -1; /* the number is good but does not fit in 'int' */

Comment is redundant and unnecessary, please remove it.

Comments are good, but not if they are at the level of describing the code for a beginner programmer. That is, if the code isn't obvious what it is doing, it should be commented. But comments that describe the definition of the code end up causing more noise than being helpful.

trace-stack.c static void test_available(void) · line 110

Quoted 7 lines
trace-stack.c +4 -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]. */

For example, the above comment is good. It explicitly states that this function requires the parameter new_status be in the range of 0-9, and a reviewer or new developer doesn't have to go read the function to figure that out.

trace-stack.c static void test_available(void) · line 117

Quoted 7 lines
trace-stack.c +2 -0
+static void change_stack_tracer_status(int new_status)
 {
 	char buf[1];
+	int status;
 	int fd;
 	int n;
 

Should enforce that new_status is between 0 and 9, by one of the methods I discussed in the other email.

-- Steve

Quoted 44 lines
trace-stack.c +11 -7
-	buf[0] = read_proc();
-	if (buf[0] == val)
-		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 +131,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)
@@ -123,8 +166,12 @@ static void read_trace(void)
 	char *buf = NULL;
 	size_t n;
 	int r;
+	int status;
 
-	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");
#6 lore raw

PATCH v3 2/3 trace-cmd: Remove the die() call from read_proc()

In reply to 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().

---
 trace-stack.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
trace-stack.c +2 -2
index c1058ca..d55d994 100644
@@ -79,9 +79,9 @@ static int read_proc(int *status)
 
 	n = read(fd, buf, sizeof(buf));
 
-	/* We assume that the file is never empty we got no errors. */
+	/* The file was empty or read() failed with an error. */
 	if (n <= 0)
-		die("error reading %s", PROC_FILE);
+		return -1;
 
 	/* Does this file have more than 63 characters?? */
 	if (n >= sizeof(buf))
Signature · 1 line
2.14.1
#7 lore raw

PATCH v3 3/3 trace-cmd: Making stat to report when the stack tracer is ON

In reply to 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.

---
 trace-cmd.h   | 2 ++
 trace-stack.c | 6 ++++++
 trace-stat.c  | 8 ++++++++
 3 files changed, 16 insertions(+)
trace-cmd.h +2 -0
index 6fd34d7..9704b2e 100644
@@ -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,
trace-stack.c +6 -0
index d55d994..0028ecc 100644
@@ -107,6 +107,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(int new_status)
 {
trace-stat.c +8 -0
index fd16354..23d7dd4 100644
@@ -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);
 }
Signature · 1 line
2.14.1