Skip to content

Commit bbbab19

Browse files
rostedtacmel
authored andcommitted
tools lib traceevent: Separate out tep_strerror() for strerror_r() issues
While working on having PowerTop use libtracevent as a shared object library, Tzvetomir hit "str_error_r not defined". This was added by commit c3cec9e ("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, and its in the traceevent library's own tep_strerror() function, we can copy what was done in perf, and create yet another external file that undefs _GNU_SOURCE to use the more portable version of the function. 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> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com> Cc: linux trace devel <linux-trace-devel@vger.kernel.org> Link: http://lkml.kernel.org/r/20181005121816.484e654f@gandalf.local.home Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 8b2f245 commit bbbab19

3 files changed

Lines changed: 54 additions & 30 deletions

File tree

‎tools/lib/traceevent/Build‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ libtraceevent-y += trace-seq.o
44
libtraceevent-y += parse-filter.o
55
libtraceevent-y += parse-utils.o
66
libtraceevent-y += kbuffer-parse.o
7+
libtraceevent-y += tep_strerror.o
78

89
plugin_jbd2-y += plugin_jbd2.o
910
plugin_hrtimer-y += plugin_hrtimer.o

‎tools/lib/traceevent/event-parse.c‎

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <errno.h>
1919
#include <stdint.h>
2020
#include <limits.h>
21-
#include <linux/string.h>
2221
#include <linux/time64.h>
2322

2423
#include <netinet/in.h>
@@ -6201,35 +6200,6 @@ enum tep_errno tep_parse_event(struct tep_handle *pevent, const char *buf,
62016200
return __parse_event(pevent, &event, buf, size, sys);
62026201
}
62036202

6204-
#undef _PE
6205-
#define _PE(code, str) str
6206-
static const char * const tep_error_str[] = {
6207-
TEP_ERRORS
6208-
};
6209-
#undef _PE
6210-
6211-
int tep_strerror(struct tep_handle *pevent __maybe_unused,
6212-
enum tep_errno errnum, char *buf, size_t buflen)
6213-
{
6214-
int idx;
6215-
const char *msg;
6216-
6217-
if (errnum >= 0) {
6218-
str_error_r(errnum, buf, buflen);
6219-
return 0;
6220-
}
6221-
6222-
if (errnum <= __TEP_ERRNO__START ||
6223-
errnum >= __TEP_ERRNO__END)
6224-
return -1;
6225-
6226-
idx = errnum - __TEP_ERRNO__START - 1;
6227-
msg = tep_error_str[idx];
6228-
snprintf(buf, buflen, "%s", msg);
6229-
6230-
return 0;
6231-
}
6232-
62336203
int get_field_val(struct trace_seq *s, struct tep_format_field *field,
62346204
const char *name, struct tep_record *record,
62356205
unsigned long long *val, int err)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-License-Identifier: LGPL-2.1
2+
#undef _GNU_SOURCE
3+
#include <string.h>
4+
#include <stdio.h>
5+
6+
#include "event-parse.h"
7+
8+
#undef _PE
9+
#define _PE(code, str) str
10+
static const char * const tep_error_str[] = {
11+
TEP_ERRORS
12+
};
13+
#undef _PE
14+
15+
/*
16+
* The tools so far have been using the strerror_r() GNU variant, that returns
17+
* a string, be it the buffer passed or something else.
18+
*
19+
* But that, besides being tricky in cases where we expect that the function
20+
* using strerror_r() returns the error formatted in a provided buffer (we have
21+
* to check if it returned something else and copy that instead), breaks the
22+
* build on systems not using glibc, like Alpine Linux, where musl libc is
23+
* used.
24+
*
25+
* So, introduce yet another wrapper, str_error_r(), that has the GNU
26+
* interface, but uses the portable XSI variant of strerror_r(), so that users
27+
* rest asured that the provided buffer is used and it is what is returned.
28+
*/
29+
int tep_strerror(struct tep_handle *tep __maybe_unused,
30+
enum tep_errno errnum, char *buf, size_t buflen)
31+
{
32+
const char *msg;
33+
int idx;
34+
35+
if (!buflen)
36+
return 0;
37+
38+
if (errnum >= 0) {
39+
int err = strerror_r(errnum, buf, buflen);
40+
buf[buflen - 1] = 0;
41+
return err;
42+
}
43+
44+
if (errnum <= __TEP_ERRNO__START ||
45+
errnum >= __TEP_ERRNO__END)
46+
return -1;
47+
48+
idx = errnum - __TEP_ERRNO__START - 1;
49+
msg = tep_error_str[idx];
50+
snprintf(buf, buflen, "%s", msg);
51+
52+
return 0;
53+
}

0 commit comments

Comments
 (0)