Skip to content

Commit c8b5f2c

Browse files
committed
tools: Introduce str_error_r()
The tools so far have been using the strerror_r() GNU variant, that returns a string, be it the buffer passed or something else. But that, besides being tricky in cases where we expect that the function using strerror_r() returns the error formatted in a provided buffer (we have to check if it returned something else and copy that instead), breaks the build on systems not using glibc, like Alpine Linux, where musl libc is used. So, introduce yet another wrapper, str_error_r(), that has the GNU interface, but uses the portable XSI variant of strerror_r(), so that users rest asured that the provided buffer is used and it is what is returned. Cc: Adrian Hunter <[email protected]> Cc: David Ahern <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Wang Nan <[email protected]> Link: http://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent ffe3a28 commit c8b5f2c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+113
-77
lines changed

tools/include/linux/string.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ int strtobool(const char *s, bool *res);
1212
extern size_t strlcpy(char *dest, const char *src, size_t size);
1313
#endif
1414

15+
char *str_error_r(int errnum, char *buf, size_t buflen);
16+
1517
#endif /* _LINUX_STRING_H_ */

tools/lib/str_error_r.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#undef _GNU_SOURCE
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include <linux/string.h>
5+
6+
/*
7+
* The tools so far have been using the strerror_r() GNU variant, that returns
8+
* a string, be it the buffer passed or something else.
9+
*
10+
* But that, besides being tricky in cases where we expect that the function
11+
* using strerror_r() returns the error formatted in a provided buffer (we have
12+
* to check if it returned something else and copy that instead), breaks the
13+
* build on systems not using glibc, like Alpine Linux, where musl libc is
14+
* used.
15+
*
16+
* So, introduce yet another wrapper, str_error_r(), that has the GNU
17+
* interface, but uses the portable XSI variant of strerror_r(), so that users
18+
* rest asured that the provided buffer is used and it is what is returned.
19+
*/
20+
char *str_error_r(int errnum, char *buf, size_t buflen)
21+
{
22+
int err = strerror_r(errnum, buf, buflen);
23+
if (err)
24+
snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d", errnum, buf, buflen, err);
25+
return buf;
26+
}

tools/perf/MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ tools/lib/symbol/kallsyms.c
2929
tools/lib/symbol/kallsyms.h
3030
tools/lib/find_bit.c
3131
tools/lib/bitmap.c
32+
tools/lib/str_error_r.c
3233
tools/include/asm/atomic.h
3334
tools/include/asm/barrier.h
3435
tools/include/asm/bug.h

tools/perf/arch/x86/tests/rdpmc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ static int __test__rdpmc(void)
111111
if (fd < 0) {
112112
pr_err("Error: sys_perf_event_open() syscall returned "
113113
"with %d (%s)\n", fd,
114-
strerror_r(errno, sbuf, sizeof(sbuf)));
114+
str_error_r(errno, sbuf, sizeof(sbuf)));
115115
return -1;
116116
}
117117

118118
addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
119119
if (addr == (void *)(-1)) {
120120
pr_err("Error: mmap() syscall returned with (%s)\n",
121-
strerror_r(errno, sbuf, sizeof(sbuf)));
121+
str_error_r(errno, sbuf, sizeof(sbuf)));
122122
goto out_close;
123123
}
124124

tools/perf/builtin-buildid-cache.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ int cmd_buildid_cache(int argc, const char **argv,
351351
continue;
352352
}
353353
pr_warning("Couldn't add %s: %s\n",
354-
pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
354+
pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
355355
}
356356

357357
strlist__delete(list);
@@ -369,7 +369,7 @@ int cmd_buildid_cache(int argc, const char **argv,
369369
continue;
370370
}
371371
pr_warning("Couldn't remove %s: %s\n",
372-
pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
372+
pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
373373
}
374374

375375
strlist__delete(list);
@@ -387,7 +387,7 @@ int cmd_buildid_cache(int argc, const char **argv,
387387
continue;
388388
}
389389
pr_warning("Couldn't remove %s: %s\n",
390-
pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
390+
pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
391391
}
392392

393393
strlist__delete(list);
@@ -408,7 +408,7 @@ int cmd_buildid_cache(int argc, const char **argv,
408408
continue;
409409
}
410410
pr_warning("Couldn't update %s: %s\n",
411-
pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
411+
pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
412412
}
413413

414414
strlist__delete(list);

tools/perf/builtin-help.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static void exec_woman_emacs(const char *path, const char *page)
117117
free(man_page);
118118
}
119119
warning("failed to exec '%s': %s", path,
120-
strerror_r(errno, sbuf, sizeof(sbuf)));
120+
str_error_r(errno, sbuf, sizeof(sbuf)));
121121
}
122122
}
123123

@@ -150,7 +150,7 @@ static void exec_man_konqueror(const char *path, const char *page)
150150
free(man_page);
151151
}
152152
warning("failed to exec '%s': %s", path,
153-
strerror_r(errno, sbuf, sizeof(sbuf)));
153+
str_error_r(errno, sbuf, sizeof(sbuf)));
154154
}
155155
}
156156

@@ -162,7 +162,7 @@ static void exec_man_man(const char *path, const char *page)
162162
path = "man";
163163
execlp(path, "man", page, NULL);
164164
warning("failed to exec '%s': %s", path,
165-
strerror_r(errno, sbuf, sizeof(sbuf)));
165+
str_error_r(errno, sbuf, sizeof(sbuf)));
166166
}
167167

168168
static void exec_man_cmd(const char *cmd, const char *page)
@@ -175,7 +175,7 @@ static void exec_man_cmd(const char *cmd, const char *page)
175175
free(shell_cmd);
176176
}
177177
warning("failed to exec '%s': %s", cmd,
178-
strerror_r(errno, sbuf, sizeof(sbuf)));
178+
str_error_r(errno, sbuf, sizeof(sbuf)));
179179
}
180180

181181
static void add_man_viewer(const char *name)

tools/perf/builtin-kvm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,13 +1018,13 @@ static int kvm_live_open_events(struct perf_kvm_stat *kvm)
10181018
err = perf_evlist__open(evlist);
10191019
if (err < 0) {
10201020
printf("Couldn't create the events: %s\n",
1021-
strerror_r(errno, sbuf, sizeof(sbuf)));
1021+
str_error_r(errno, sbuf, sizeof(sbuf)));
10221022
goto out;
10231023
}
10241024

10251025
if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
10261026
ui__error("Failed to mmap the events: %s\n",
1027-
strerror_r(errno, sbuf, sizeof(sbuf)));
1027+
str_error_r(errno, sbuf, sizeof(sbuf)));
10281028
perf_evlist__close(evlist);
10291029
goto out;
10301030
}

tools/perf/builtin-probe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ static void pr_err_with_code(const char *msg, int err)
308308

309309
pr_err("%s", msg);
310310
pr_debug(" Reason: %s (Code: %d)",
311-
strerror_r(-err, sbuf, sizeof(sbuf)), err);
311+
str_error_r(-err, sbuf, sizeof(sbuf)), err);
312312
pr_err("\n");
313313
}
314314

tools/perf/builtin-record.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ static int record__mmap_evlist(struct record *rec,
361361
return -errno;
362362
} else {
363363
pr_err("failed to mmap with %d (%s)\n", errno,
364-
strerror_r(errno, msg, sizeof(msg)));
364+
str_error_r(errno, msg, sizeof(msg)));
365365
if (errno)
366366
return -errno;
367367
else
@@ -407,7 +407,7 @@ static int record__open(struct record *rec)
407407
if (perf_evlist__apply_filters(evlist, &pos)) {
408408
error("failed to set filter \"%s\" on event %s with %d (%s)\n",
409409
pos->filter, perf_evsel__name(pos), errno,
410-
strerror_r(errno, msg, sizeof(msg)));
410+
str_error_r(errno, msg, sizeof(msg)));
411411
rc = -1;
412412
goto out;
413413
}
@@ -1003,7 +1003,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
10031003

10041004
if (forks && workload_exec_errno) {
10051005
char msg[STRERR_BUFSIZE];
1006-
const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
1006+
const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
10071007
pr_err("Workload failed: %s\n", emsg);
10081008
err = -1;
10091009
goto out_child;

tools/perf/builtin-sched.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
494494
}
495495
pr_err("Error: sys_perf_event_open() syscall returned "
496496
"with %d (%s)\n%s", fd,
497-
strerror_r(errno, sbuf, sizeof(sbuf)), info);
497+
str_error_r(errno, sbuf, sizeof(sbuf)), info);
498498
exit(EXIT_FAILURE);
499499
}
500500
return fd;

0 commit comments

Comments
 (0)