Skip to content

Commit

Permalink
tools: Introduce str_error_r()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
acmel committed Jul 12, 2016
1 parent ffe3a28 commit c8b5f2c
Show file tree
Hide file tree
Showing 41 changed files with 113 additions and 77 deletions.
2 changes: 2 additions & 0 deletions tools/include/linux/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ int strtobool(const char *s, bool *res);
extern size_t strlcpy(char *dest, const char *src, size_t size);
#endif

char *str_error_r(int errnum, char *buf, size_t buflen);

#endif /* _LINUX_STRING_H_ */
26 changes: 26 additions & 0 deletions tools/lib/str_error_r.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#undef _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <linux/string.h>

/*
* 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.
*/
char *str_error_r(int errnum, char *buf, size_t buflen)
{
int err = strerror_r(errnum, buf, buflen);
if (err)
snprintf(buf, buflen, "INTERNAL ERROR: strerror_r(%d, %p, %zd)=%d", errnum, buf, buflen, err);
return buf;
}
1 change: 1 addition & 0 deletions tools/perf/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tools/lib/symbol/kallsyms.c
tools/lib/symbol/kallsyms.h
tools/lib/find_bit.c
tools/lib/bitmap.c
tools/lib/str_error_r.c
tools/include/asm/atomic.h
tools/include/asm/barrier.h
tools/include/asm/bug.h
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/arch/x86/tests/rdpmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ static int __test__rdpmc(void)
if (fd < 0) {
pr_err("Error: sys_perf_event_open() syscall returned "
"with %d (%s)\n", fd,
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
return -1;
}

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

Expand Down
8 changes: 4 additions & 4 deletions tools/perf/builtin-buildid-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ int cmd_buildid_cache(int argc, const char **argv,
continue;
}
pr_warning("Couldn't add %s: %s\n",
pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
}

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

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

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

strlist__delete(list);
Expand Down
8 changes: 4 additions & 4 deletions tools/perf/builtin-help.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static void exec_woman_emacs(const char *path, const char *page)
free(man_page);
}
warning("failed to exec '%s': %s", path,
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@ static void exec_man_konqueror(const char *path, const char *page)
free(man_page);
}
warning("failed to exec '%s': %s", path,
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
}
}

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

static void exec_man_cmd(const char *cmd, const char *page)
Expand All @@ -175,7 +175,7 @@ static void exec_man_cmd(const char *cmd, const char *page)
free(shell_cmd);
}
warning("failed to exec '%s': %s", cmd,
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
}

static void add_man_viewer(const char *name)
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/builtin-kvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1018,13 +1018,13 @@ static int kvm_live_open_events(struct perf_kvm_stat *kvm)
err = perf_evlist__open(evlist);
if (err < 0) {
printf("Couldn't create the events: %s\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out;
}

if (perf_evlist__mmap(evlist, kvm->opts.mmap_pages, false) < 0) {
ui__error("Failed to mmap the events: %s\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
perf_evlist__close(evlist);
goto out;
}
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/builtin-probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ static void pr_err_with_code(const char *msg, int err)

pr_err("%s", msg);
pr_debug(" Reason: %s (Code: %d)",
strerror_r(-err, sbuf, sizeof(sbuf)), err);
str_error_r(-err, sbuf, sizeof(sbuf)), err);
pr_err("\n");
}

Expand Down
6 changes: 3 additions & 3 deletions tools/perf/builtin-record.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ static int record__mmap_evlist(struct record *rec,
return -errno;
} else {
pr_err("failed to mmap with %d (%s)\n", errno,
strerror_r(errno, msg, sizeof(msg)));
str_error_r(errno, msg, sizeof(msg)));
if (errno)
return -errno;
else
Expand Down Expand Up @@ -407,7 +407,7 @@ static int record__open(struct record *rec)
if (perf_evlist__apply_filters(evlist, &pos)) {
error("failed to set filter \"%s\" on event %s with %d (%s)\n",
pos->filter, perf_evsel__name(pos), errno,
strerror_r(errno, msg, sizeof(msg)));
str_error_r(errno, msg, sizeof(msg)));
rc = -1;
goto out;
}
Expand Down Expand Up @@ -1003,7 +1003,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)

if (forks && workload_exec_errno) {
char msg[STRERR_BUFSIZE];
const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
pr_err("Workload failed: %s\n", emsg);
err = -1;
goto out_child;
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/builtin-sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
}
pr_err("Error: sys_perf_event_open() syscall returned "
"with %d (%s)\n%s", fd,
strerror_r(errno, sbuf, sizeof(sbuf)), info);
str_error_r(errno, sbuf, sizeof(sbuf)), info);
exit(EXIT_FAILURE);
}
return fd;
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/builtin-stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ static int __run_perf_stat(int argc, const char **argv)
if (perf_evlist__apply_filters(evsel_list, &counter)) {
error("failed to set filter \"%s\" on event %s with %d (%s)\n",
counter->filter, perf_evsel__name(counter), errno,
strerror_r(errno, msg, sizeof(msg)));
str_error_r(errno, msg, sizeof(msg)));
return -1;
}

Expand Down Expand Up @@ -637,7 +637,7 @@ static int __run_perf_stat(int argc, const char **argv)
wait(&status);

if (workload_exec_errno) {
const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
pr_err("Workload failed: %s\n", emsg);
return -1;
}
Expand Down
6 changes: 3 additions & 3 deletions tools/perf/builtin-top.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ static int perf_top__start_counters(struct perf_top *top)

if (perf_evlist__mmap(evlist, opts->mmap_pages, false) < 0) {
ui__error("Failed to mmap with %d (%s)\n",
errno, strerror_r(errno, msg, sizeof(msg)));
errno, str_error_r(errno, msg, sizeof(msg)));
goto out_err;
}

Expand Down Expand Up @@ -1028,7 +1028,7 @@ static int __cmd_top(struct perf_top *top)

out_err_cpu_topo: {
char errbuf[BUFSIZ];
const char *err = strerror_r(-ret, errbuf, sizeof(errbuf));
const char *err = str_error_r(-ret, errbuf, sizeof(errbuf));

ui__error("Could not read the CPU topology map: %s\n", err);
goto out_delete;
Expand Down Expand Up @@ -1295,7 +1295,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)

if (perf_evlist__create_maps(top.evlist, target) < 0) {
ui__error("Couldn't create thread/CPU maps: %s\n",
errno == ENOENT ? "No such process" : strerror_r(errno, errbuf, sizeof(errbuf)));
errno == ENOENT ? "No such process" : str_error_r(errno, errbuf, sizeof(errbuf)));
goto out_delete_evlist;
}

Expand Down
4 changes: 2 additions & 2 deletions tools/perf/builtin-trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -1601,7 +1601,7 @@ static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
fprintf(trace->output, ") = %ld", ret);
} else if (ret < 0 && (sc->fmt->errmsg || sc->fmt->errpid)) {
char bf[STRERR_BUFSIZE];
const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
*e = audit_errno_to_name(-ret);

fprintf(trace->output, ") = -1 %s %s", e, emsg);
Expand Down Expand Up @@ -2402,7 +2402,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
fprintf(trace->output,
"Failed to set filter \"%s\" on event %s with %d (%s)\n",
evsel->filter, perf_evsel__name(evsel), errno,
strerror_r(errno, errbuf, sizeof(errbuf)));
str_error_r(errno, errbuf, sizeof(errbuf)));
goto out_delete_evlist;
}
out_error_mem:
Expand Down
6 changes: 3 additions & 3 deletions tools/perf/perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
/* Check for ENOSPC and EIO errors.. */
if (fflush(stdout)) {
fprintf(stderr, "write failure on standard output: %s",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out;
}
if (ferror(stdout)) {
Expand All @@ -383,7 +383,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
}
if (fclose(stdout)) {
fprintf(stderr, "close failed on standard output: %s",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out;
}
status = 0;
Expand Down Expand Up @@ -615,7 +615,7 @@ int main(int argc, const char **argv)
}

fprintf(stderr, "Failed to run command '%s': %s\n",
cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
out:
return 1;
}
4 changes: 2 additions & 2 deletions tools/perf/tests/backward-ring-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static int do_test(struct perf_evlist *evlist, int mmap_pages,
err = perf_evlist__mmap(evlist, mmap_pages, true);
if (err < 0) {
pr_debug("perf_evlist__mmap: %s\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
return TEST_FAIL;
}

Expand Down Expand Up @@ -124,7 +124,7 @@ int test__backward_ring_buffer(int subtest __maybe_unused)
err = perf_evlist__open(evlist);
if (err < 0) {
pr_debug("perf_evlist__open: %s\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out_delete_evlist;
}

Expand Down
4 changes: 2 additions & 2 deletions tools/perf/tests/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ static int do_test(struct bpf_object *obj, int (*func)(void),
err = perf_evlist__open(evlist);
if (err < 0) {
pr_debug("perf_evlist__open: %s\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out_delete_evlist;
}

err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
if (err < 0) {
pr_debug("perf_evlist__mmap: %s\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out_delete_evlist;
}

Expand Down
2 changes: 1 addition & 1 deletion tools/perf/tests/builtin-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ static int run_test(struct test *test, int subtest)

if (child < 0) {
pr_err("failed to fork test: %s\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion tools/perf/tests/event-times.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static int attach__enable_on_exec(struct perf_evlist *evlist)
err = perf_evlist__open(evlist);
if (err < 0) {
pr_debug("perf_evlist__open: %s\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
return err;
}

Expand Down
6 changes: 3 additions & 3 deletions tools/perf/tests/mmap-basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int test__basic_mmap(int subtest __maybe_unused)
sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
pr_debug("sched_setaffinity() failed on CPU %d: %s ",
cpus->map[0], strerror_r(errno, sbuf, sizeof(sbuf)));
cpus->map[0], str_error_r(errno, sbuf, sizeof(sbuf)));
goto out_free_cpus;
}

Expand Down Expand Up @@ -79,7 +79,7 @@ int test__basic_mmap(int subtest __maybe_unused)
if (perf_evsel__open(evsels[i], cpus, threads) < 0) {
pr_debug("failed to open counter: %s, "
"tweak /proc/sys/kernel/perf_event_paranoid?\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out_delete_evlist;
}

Expand All @@ -89,7 +89,7 @@ int test__basic_mmap(int subtest __maybe_unused)

if (perf_evlist__mmap(evlist, 128, true) < 0) {
pr_debug("failed to mmap events: %d (%s)\n", errno,
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out_delete_evlist;
}

Expand Down
4 changes: 2 additions & 2 deletions tools/perf/tests/openat-syscall-all-cpus.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int test__openat_syscall_event_on_all_cpus(int subtest __maybe_unused)
if (perf_evsel__open(evsel, cpus, threads) < 0) {
pr_debug("failed to open counter: %s, "
"tweak /proc/sys/kernel/perf_event_paranoid?\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out_evsel_delete;
}

Expand All @@ -62,7 +62,7 @@ int test__openat_syscall_event_on_all_cpus(int subtest __maybe_unused)
if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
pr_debug("sched_setaffinity() failed on CPU %d: %s ",
cpus->map[cpu],
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out_close_fd;
}
for (i = 0; i < ncalls; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/tests/openat-syscall-tp-fields.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ int test__syscall_openat_tp_fields(int subtest __maybe_unused)
err = perf_evlist__open(evlist);
if (err < 0) {
pr_debug("perf_evlist__open: %s\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out_delete_evlist;
}

err = perf_evlist__mmap(evlist, UINT_MAX, false);
if (err < 0) {
pr_debug("perf_evlist__mmap: %s\n",
strerror_r(errno, sbuf, sizeof(sbuf)));
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out_delete_evlist;
}

Expand Down
Loading

0 comments on commit c8b5f2c

Please sign in to comment.