Commit bac74dcb authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo
Browse files

perf tools: Switch printf("...%s", strerror(errno)) to printf("...%m")



strerror() has thread safety issues, strerror_r() requires stack
allocated buffers.

Code in perf has already been using the "%m" formatting flag that is a
widely support glibc extension to print the current errno's description.

Expand the usage of this formatting flag and remove usage of
strerror()/strerror_r().

Signed-off-by: default avatarIan Rogers <irogers@google.com>
Acked-by: default avatarNamhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexandre Ghiti <alexghiti@rivosinc.com>
Cc: Blake Jones <blakejones@google.com>
Cc: Chun-Tse Shao <ctshao@google.com>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Haibo Xu <haibo1.xu@intel.com>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Leo Yan <leo.yan@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>
Cc: Thomas Falcon <thomas.falcon@intel.com>
Cc: Yunseong Kim <ysk@kzalloc.com>
Cc: Zhongqiu Han <quic_zhonhan@quicinc.com>
Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 47d3545f
Loading
Loading
Loading
Loading
+12 −18
Original line number Diff line number Diff line
@@ -80,26 +80,24 @@ static int bp_modify1(void)
	 */
	if (ptrace(PTRACE_POKEUSER, child,
		   offsetof(struct user, u_debugreg[0]), bp_2)) {
		pr_debug("failed to set breakpoint, 1st time: %s\n",
			 strerror(errno));
		pr_debug("failed to set breakpoint, 1st time: %m\n");
		goto out;
	}

	if (ptrace(PTRACE_POKEUSER, child,
		   offsetof(struct user, u_debugreg[0]), bp_1)) {
		pr_debug("failed to set breakpoint, 2nd time: %s\n",
			 strerror(errno));
		pr_debug("failed to set breakpoint, 2nd time: %m\n");
		goto out;
	}

	if (ptrace(PTRACE_POKEUSER, child,
		   offsetof(struct user, u_debugreg[7]), dr7)) {
		pr_debug("failed to set dr7: %s\n", strerror(errno));
		pr_debug("failed to set dr7: %m\n");
		goto out;
	}

	if (ptrace(PTRACE_CONT, child, NULL, NULL)) {
		pr_debug("failed to PTRACE_CONT: %s\n", strerror(errno));
		pr_debug("failed to PTRACE_CONT: %m\n");
		goto out;
	}

@@ -112,19 +110,17 @@ static int bp_modify1(void)
	rip = ptrace(PTRACE_PEEKUSER, child,
		     offsetof(struct user_regs_struct, rip), NULL);
	if (rip == (unsigned long) -1) {
		pr_debug("failed to PTRACE_PEEKUSER: %s\n",
			 strerror(errno));
		pr_debug("failed to PTRACE_PEEKUSER: %m\n");
		goto out;
	}

	pr_debug("rip %lx, bp_1 %p\n", rip, bp_1);

out:
	if (ptrace(PTRACE_DETACH, child, NULL, NULL)) {
		pr_debug("failed to PTRACE_DETACH: %s", strerror(errno));
		pr_debug("failed to PTRACE_DETACH: %m\n");
		return TEST_FAIL;
	}

	}
	return rip == (unsigned long) bp_1 ? TEST_OK : TEST_FAIL;
}

@@ -157,14 +153,13 @@ static int bp_modify2(void)
	 */
	if (ptrace(PTRACE_POKEUSER, child,
		   offsetof(struct user, u_debugreg[0]), bp_1)) {
		pr_debug("failed to set breakpoint: %s\n",
			 strerror(errno));
		pr_debug("failed to set breakpoint: %m\n");
		goto out;
	}

	if (ptrace(PTRACE_POKEUSER, child,
		   offsetof(struct user, u_debugreg[7]), dr7)) {
		pr_debug("failed to set dr7: %s\n", strerror(errno));
		pr_debug("failed to set dr7: %m\n");
		goto out;
	}

@@ -175,7 +170,7 @@ static int bp_modify2(void)
	}

	if (ptrace(PTRACE_CONT, child, NULL, NULL)) {
		pr_debug("failed to PTRACE_CONT: %s\n", strerror(errno));
		pr_debug("failed to PTRACE_CONT: %m\n");
		goto out;
	}

@@ -188,8 +183,7 @@ static int bp_modify2(void)
	rip = ptrace(PTRACE_PEEKUSER, child,
		     offsetof(struct user_regs_struct, rip), NULL);
	if (rip == (unsigned long) -1) {
		pr_debug("failed to PTRACE_PEEKUSER: %s\n",
			 strerror(errno));
		pr_debug("failed to PTRACE_PEEKUSER: %m\n");
		goto out;
	}

@@ -197,7 +191,7 @@ static int bp_modify2(void)

out:
	if (ptrace(PTRACE_DETACH, child, NULL, NULL)) {
		pr_debug("failed to PTRACE_DETACH: %s", strerror(errno));
		pr_debug("failed to PTRACE_DETACH: %m\n");
		return TEST_FAIL;
	}

+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ static const char * const bench_uprobe_usage[] = {
							   /*opts=*/&uprobe_opts); \
	if (!skel->links.prog) { \
		err = -errno; \
		fprintf(stderr, "Failed to attach bench uprobe \"%s\": %s\n", #prog, strerror(errno)); \
		fprintf(stderr, "Failed to attach bench uprobe \"%s\": %m\n", #prog); \
		goto cleanup; \
	}

+3 −5
Original line number Diff line number Diff line
@@ -265,8 +265,7 @@ static int check_base(struct daemon *daemon)
			       daemon->base);
			return -EACCES;
		default:
			pr_err("failed: can't access base '%s': %s\n",
			       daemon->base, strerror(errno));
			pr_err("failed: can't access base '%s': %m\n", daemon->base);
			return -errno;
		}
	}
@@ -544,8 +543,7 @@ static int daemon_session__control(struct daemon_session *session,

	err = writen(control, msg, len);
	if (err != len) {
		pr_err("failed: write to control pipe: %d (%s)\n",
		       errno, control_path);
		pr_err("failed: write to control pipe: %m (%s)\n", control_path);
		goto out;
	}

@@ -586,7 +584,7 @@ static int setup_server_socket(struct daemon *daemon)
	int fd = socket(AF_UNIX, SOCK_STREAM, 0);

	if (fd < 0) {
		fprintf(stderr, "socket: %s\n", strerror(errno));
		fprintf(stderr, "socket: %m\n");
		return -1;
	}

+1 −2
Original line number Diff line number Diff line
@@ -211,8 +211,7 @@ static int opt_set_target_ns(const struct option *opt __maybe_unused,
		ns_pid = (pid_t)strtol(str, NULL, 10);
		if (errno != 0) {
			ret = -errno;
			pr_warning("Failed to parse %s as a pid: %s\n", str,
				   strerror(errno));
			pr_warning("Failed to parse %s as a pid: %m\n", str);
			return ret;
		}
		nsip = nsinfo__new(ns_pid);
+10 −14
Original line number Diff line number Diff line
@@ -1286,7 +1286,6 @@ static int record__mmap_evlist(struct record *rec,
	struct record_opts *opts = &rec->opts;
	bool auxtrace_overwrite = opts->auxtrace_snapshot_mode ||
				  opts->auxtrace_sample_mode;
	char msg[512];

	if (opts->affinity != PERF_AFFINITY_SYS)
		cpu__setup_cpunode_map();
@@ -1305,8 +1304,7 @@ static int record__mmap_evlist(struct record *rec,
			       opts->mmap_pages, opts->auxtrace_mmap_pages);
			return -errno;
		} else {
			pr_err("failed to mmap with %d (%s)\n", errno,
				str_error_r(errno, msg, sizeof(msg)));
			pr_err("failed to mmap: %m\n");
			if (errno)
				return -errno;
			else
@@ -1324,7 +1322,8 @@ static int record__mmap_evlist(struct record *rec,
	if (record__threads_enabled(rec)) {
		ret = perf_data__create_dir(&rec->data, evlist->core.nr_mmaps);
		if (ret) {
			pr_err("Failed to create data directory: %s\n", strerror(-ret));
			errno = -ret;
			pr_err("Failed to create data directory: %m\n");
			return ret;
		}
		for (i = 0; i < evlist->core.nr_mmaps; i++) {
@@ -1461,9 +1460,8 @@ static int record__open(struct record *rec)
	}

	if (evlist__apply_filters(evlist, &pos, &opts->target)) {
		pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
			pos->filter ?: "BPF", evsel__name(pos), errno,
			str_error_r(errno, msg, sizeof(msg)));
		pr_err("failed to set filter \"%s\" on event %s: %m\n",
			pos->filter ?: "BPF", evsel__name(pos));
		rc = -1;
		goto out;
	}
@@ -1748,8 +1746,7 @@ static void *record__thread(void *arg)

	err = write(thread->pipes.ack[1], &msg, sizeof(msg));
	if (err == -1)
		pr_warning("threads[%d]: failed to notify on start: %s\n",
			   thread->tid, strerror(errno));
		pr_warning("threads[%d]: failed to notify on start: %m\n", thread->tid);

	pr_debug("threads[%d]: started on cpu%d\n", thread->tid, sched_getcpu());

@@ -1792,8 +1789,7 @@ static void *record__thread(void *arg)

	err = write(thread->pipes.ack[1], &msg, sizeof(msg));
	if (err == -1)
		pr_warning("threads[%d]: failed to notify on termination: %s\n",
			   thread->tid, strerror(errno));
		pr_warning("threads[%d]: failed to notify on termination: %m\n", thread->tid);

	return NULL;
}
@@ -2338,7 +2334,7 @@ static int record__start_threads(struct record *rec)

	sigfillset(&full);
	if (sigprocmask(SIG_SETMASK, &full, &mask)) {
		pr_err("Failed to block signals on threads start: %s\n", strerror(errno));
		pr_err("Failed to block signals on threads start: %m\n");
		return -1;
	}

@@ -2356,7 +2352,7 @@ static int record__start_threads(struct record *rec)
		if (pthread_create(&handle, &attrs, record__thread, &thread_data[t])) {
			for (tt = 1; tt < t; tt++)
				record__terminate_thread(&thread_data[t]);
			pr_err("Failed to start threads: %s\n", strerror(errno));
			pr_err("Failed to start threads: %m\n");
			ret = -1;
			goto out_err;
		}
@@ -2379,7 +2375,7 @@ static int record__start_threads(struct record *rec)
	pthread_attr_destroy(&attrs);

	if (sigprocmask(SIG_SETMASK, &mask, NULL)) {
		pr_err("Failed to unblock signals on threads start: %s\n", strerror(errno));
		pr_err("Failed to unblock signals on threads start: %m\n");
		ret = -1;
	}

Loading