Skip to content

Commit 63cd02d

Browse files
Changbin Duacmel
authored andcommitted
perf ftrace: Append an EOL when write tracing files
Before this change, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the <func> doesn't exist. Because the kernel side hid possible errors. $ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg 0) 0.140 us | rcu_all_qs(); 3) 0.304 us | mutex_unlock(); 0) 0.153 us | find_vma(); 3) 0.088 us | __fsnotify_parent(); 0) 6.145 us | handle_mm_fault(); 3) 0.089 us | fsnotify(); 3) 0.161 us | __sb_end_write(); 3) 0.710 us | SyS_close(); 3) 7.848 us | exit_to_usermode_loop(); On the example above, I specified the function filter 'abcdefg' but all functions are enabled. The expected result is for all functions to be filtered, since there is no such function ('abcdefg') The original fix is to make the kernel support '\0' as end of string: https://lkml.org/lkml/2018/1/16/116 But above fix cannot be compatible with old kernels. Then Namhyung Kim suggest adding a space after function name. This patch will append an '\n' when write tracing file. After this fix, the perf will report correct error state. Also let it print an error if reset_tracing_files() fails. Committer testing: Now it prints: # perf ftrace -a --graph-depth 1 --graph-funcs abcdefg failed to set tracing filters # And for an existing function: # perf ftrace -a --graph-depth 1 --graph-funcs SyS_open 3) | SyS_open() { 3) ! 494.899 us | } 0) + 23.910 us | SyS_open(); 1) + 17.115 us | SyS_open(); 1) + 13.900 us | SyS_open(); ------------------------------------------ 3) qemu-sy-2817 => pickup-1290 ------------------------------------------ 3) + 20.021 us | SyS_open(); # Signed-off-by: Changbin Du <changbin.du@intel.com> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1519007609-14551-1-git-send-email-changbin.du@intel.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 1d12cec commit 63cd02d

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

‎tools/perf/builtin-ftrace.c‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ static int __write_tracing_file(const char *name, const char *val, bool append)
7272
ssize_t size = strlen(val);
7373
int flags = O_WRONLY;
7474
char errbuf[512];
75+
char *val_copy;
7576

7677
file = get_tracing_file(name);
7778
if (!file) {
@@ -91,12 +92,23 @@ static int __write_tracing_file(const char *name, const char *val, bool append)
9192
goto out;
9293
}
9394

94-
if (write(fd, val, size) == size)
95+
/*
96+
* Copy the original value and append a '\n'. Without this,
97+
* the kernel can hide possible errors.
98+
*/
99+
val_copy = strdup(val);
100+
if (!val_copy)
101+
goto out_close;
102+
val_copy[size] = '\n';
103+
104+
if (write(fd, val_copy, size + 1) == size + 1)
95105
ret = 0;
96106
else
97107
pr_debug("write '%s' to tracing/%s failed: %s\n",
98108
val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
99109

110+
free(val_copy);
111+
out_close:
100112
close(fd);
101113
out:
102114
put_tracing_file(file);
@@ -280,8 +292,10 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
280292
signal(SIGCHLD, sig_handler);
281293
signal(SIGPIPE, sig_handler);
282294

283-
if (reset_tracing_files(ftrace) < 0)
295+
if (reset_tracing_files(ftrace) < 0) {
296+
pr_err("failed to reset ftrace\n");
284297
goto out;
298+
}
285299

286300
/* reset ftrace buffer */
287301
if (write_tracing_file("trace", "0") < 0)

0 commit comments

Comments
 (0)