perf annotate: Use pipe + fork instead of popen

We will need to redirect the stderr as well, so open code popen as
a starting point.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-k0zt9svg4bswiglem7ornts4@git.kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 7c48dcfd
...@@ -1134,8 +1134,10 @@ int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize) ...@@ -1134,8 +1134,10 @@ int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
char symfs_filename[PATH_MAX]; char symfs_filename[PATH_MAX];
struct kcore_extract kce; struct kcore_extract kce;
bool delete_extract = false; bool delete_extract = false;
int stdout_fd[2];
int lineno = 0; int lineno = 0;
int nline; int nline;
pid_t pid;
if (filename) if (filename)
symbol__join_symfs(symfs_filename, filename); symbol__join_symfs(symfs_filename, filename);
...@@ -1258,9 +1260,32 @@ int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize) ...@@ -1258,9 +1260,32 @@ int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
pr_debug("Executing: %s\n", command); pr_debug("Executing: %s\n", command);
file = popen(command, "r"); err = -1;
if (pipe(stdout_fd) < 0) {
pr_err("Failure creating the pipe to run %s\n", command);
goto out_remove_tmp;
}
pid = fork();
if (pid < 0) {
pr_err("Failure forking to run %s\n", command);
goto out_close_stdout;
}
if (pid == 0) {
close(stdout_fd[0]);
dup2(stdout_fd[1], 1);
close(stdout_fd[1]);
execl("/bin/sh", "sh", "-c", command, NULL);
perror(command);
exit(-1);
}
close(stdout_fd[1]);
file = fdopen(stdout_fd[0], "r");
if (!file) { if (!file) {
pr_err("Failure running %s\n", command); pr_err("Failure creating FILE stream for %s\n", command);
/* /*
* If we were using debug info should retry with * If we were using debug info should retry with
* original binary. * original binary.
...@@ -1286,9 +1311,11 @@ int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize) ...@@ -1286,9 +1311,11 @@ int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
if (dso__is_kcore(dso)) if (dso__is_kcore(dso))
delete_last_nop(sym); delete_last_nop(sym);
pclose(file); fclose(file);
err = 0;
out_remove_tmp: out_remove_tmp:
close(stdout_fd[0]);
if (dso__needs_decompress(dso)) if (dso__needs_decompress(dso))
unlink(symfs_filename); unlink(symfs_filename);
out_free_filename: out_free_filename:
...@@ -1297,6 +1324,10 @@ int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize) ...@@ -1297,6 +1324,10 @@ int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
if (free_filename) if (free_filename)
free(filename); free(filename);
return err; return err;
out_close_stdout:
close(stdout_fd[1]);
goto out_remove_tmp;
} }
static void insert_source_line(struct rb_root *root, struct source_line *src_line) static void insert_source_line(struct rb_root *root, struct source_line *src_line)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment