public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] tools/lib/traceevent: Replace str_error_r() with an open coded implementation
@ 2018-09-21 19:20 Steven Rostedt
  2018-09-24 18:07 ` Jiri Olsa
  0 siblings, 1 reply; 2+ messages in thread
From: Steven Rostedt @ 2018-09-21 19:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: LKML, Ingo Molnar, Jiri Olsa, Namhyung Kim, Tzvetomir Stoyanov

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

While working on having PowerTop use libtracevent as a shared object
library, Tzvetomir hit "str_error_r not defined". This was added by commit
c3cec9e68f12d ("tools lib traceevent: Use str_error_r()") because
strerror_r() has two definitions, where one is GNU specific, and the other
is XSI complient. The strerror_r() is in a wrapper str_error_r() to keep the
code from having to worry about which compiler is being used.

The problem is that str_error_r() is external to libtraceevent, and not part
of the library. If it is used as a shared object then the tools using it
will need to define that function. I do not want that function defined in
libtraceevent itself, as it is out of scope for that library.

As there's only a single instance of this call, I replaced it with an open
coded algorithm that just uses strerror() and strncpy() to place the error
message in the given buffer. We don't need to worry about the errors that
strerror_r() returns. If the buffer isn't big enough, we simply truncate it.

Reported-by: Tzvetomir Stoyanov <tstoyanov@vmware.com>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---

Changes from v1:

 - Account for buflen being zero (do nothing).

 tools/lib/traceevent/event-parse.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tools/lib/traceevent/event-parse.c b/tools/lib/traceevent/event-parse.c
index 7980fc6c3bac..ba7570646d2c 100644
--- a/tools/lib/traceevent/event-parse.c
+++ b/tools/lib/traceevent/event-parse.c
@@ -18,7 +18,6 @@
 #include <errno.h>
 #include <stdint.h>
 #include <limits.h>
-#include <linux/string.h>
 #include <linux/time64.h>
 
 #include <netinet/in.h>
@@ -6215,7 +6214,12 @@ int tep_strerror(struct tep_handle *pevent __maybe_unused,
 	const char *msg;
 
 	if (errnum >= 0) {
-		str_error_r(errnum, buf, buflen);
+		const char *error_str = strerror(errnum);
+
+		if (buflen > 0) {
+			strncpy(buf, error_str, buflen);
+			buf[buflen - 1] = 0;
+		}
 		return 0;
 	}
 
-- 
2.13.6


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

* Re: [PATCH v2] tools/lib/traceevent: Replace str_error_r() with an open coded implementation
  2018-09-21 19:20 [PATCH v2] tools/lib/traceevent: Replace str_error_r() with an open coded implementation Steven Rostedt
@ 2018-09-24 18:07 ` Jiri Olsa
  0 siblings, 0 replies; 2+ messages in thread
From: Jiri Olsa @ 2018-09-24 18:07 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Arnaldo Carvalho de Melo, LKML, Ingo Molnar, Namhyung Kim,
	Tzvetomir Stoyanov

On Fri, Sep 21, 2018 at 03:20:37PM -0400, Steven Rostedt wrote:
> From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>
> 
> While working on having PowerTop use libtracevent as a shared object
> library, Tzvetomir hit "str_error_r not defined". This was added by commit
> c3cec9e68f12d ("tools lib traceevent: Use str_error_r()") because
> strerror_r() has two definitions, where one is GNU specific, and the other
> is XSI complient. The strerror_r() is in a wrapper str_error_r() to keep the
> code from having to worry about which compiler is being used.
> 
> The problem is that str_error_r() is external to libtraceevent, and not part
> of the library. If it is used as a shared object then the tools using it
> will need to define that function. I do not want that function defined in
> libtraceevent itself, as it is out of scope for that library.
> 
> As there's only a single instance of this call, I replaced it with an open
> coded algorithm that just uses strerror() and strncpy() to place the error
> message in the given buffer. We don't need to worry about the errors that
> strerror_r() returns. If the buffer isn't big enough, we simply truncate it.
> 
> Reported-by: Tzvetomir Stoyanov <tstoyanov@vmware.com>
> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>

Acked-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka

> ---
> 
> Changes from v1:
> 
>  - Account for buflen being zero (do nothing).
> 
>  tools/lib/traceevent/event-parse.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/lib/traceevent/event-parse.c b/tools/lib/traceevent/event-parse.c
> index 7980fc6c3bac..ba7570646d2c 100644
> --- a/tools/lib/traceevent/event-parse.c
> +++ b/tools/lib/traceevent/event-parse.c
> @@ -18,7 +18,6 @@
>  #include <errno.h>
>  #include <stdint.h>
>  #include <limits.h>
> -#include <linux/string.h>
>  #include <linux/time64.h>
>  
>  #include <netinet/in.h>
> @@ -6215,7 +6214,12 @@ int tep_strerror(struct tep_handle *pevent __maybe_unused,
>  	const char *msg;
>  
>  	if (errnum >= 0) {
> -		str_error_r(errnum, buf, buflen);
> +		const char *error_str = strerror(errnum);
> +
> +		if (buflen > 0) {
> +			strncpy(buf, error_str, buflen);
> +			buf[buflen - 1] = 0;
> +		}
>  		return 0;
>  	}
>  
> -- 
> 2.13.6
> 

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

end of thread, other threads:[~2018-09-24 18:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-09-21 19:20 [PATCH v2] tools/lib/traceevent: Replace str_error_r() with an open coded implementation Steven Rostedt
2018-09-24 18:07 ` Jiri Olsa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox