9 static const char *argv_exec_path;
10 static const char *argv0_path;
12 const char *system_path(const char *path)
15 static const char *prefix;
17 static const char *prefix = PREFIX;
19 struct strbuf d = STRBUF_INIT;
21 if (is_absolute_path(path))
26 assert(is_absolute_path(argv0_path));
29 !(prefix = strip_path_suffix(argv0_path, PERF_EXEC_PATH)) &&
30 !(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
31 !(prefix = strip_path_suffix(argv0_path, "perf"))) {
33 fprintf(stderr, "RUNTIME_PREFIX requested, "
34 "but prefix computation failed. "
35 "Using static fallback '%s'.\n", prefix);
39 strbuf_addf(&d, "%s/%s", prefix, path);
40 path = strbuf_detach(&d, NULL);
44 const char *perf_extract_argv0_path(const char *argv0)
48 if (!argv0 || !*argv0)
50 slash = argv0 + strlen(argv0);
52 while (argv0 <= slash && !is_dir_sep(*slash))
56 argv0_path = strndup(argv0, slash - argv0);
57 return argv0_path ? slash + 1 : NULL;
63 void perf_set_argv_exec_path(const char *exec_path)
65 argv_exec_path = exec_path;
67 * Propagate this setting to external programs.
69 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
73 /* Returns the highest-priority, location to look for perf programs. */
74 const char *perf_exec_path(void)
79 return argv_exec_path;
81 env = getenv(EXEC_PATH_ENVIRONMENT);
86 return system_path(PERF_EXEC_PATH);
89 static void add_path(struct strbuf *out, const char *path)
92 if (is_absolute_path(path))
93 strbuf_addstr(out, path);
95 strbuf_addstr(out, make_nonrelative_path(path));
97 strbuf_addch(out, PATH_SEP);
101 void setup_path(void)
103 const char *old_path = getenv("PATH");
104 struct strbuf new_path = STRBUF_INIT;
106 add_path(&new_path, perf_exec_path());
107 add_path(&new_path, argv0_path);
110 strbuf_addstr(&new_path, old_path);
112 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
114 setenv("PATH", new_path.buf, 1);
116 strbuf_release(&new_path);
119 static const char **prepare_perf_cmd(const char **argv)
124 for (argc = 0; argv[argc]; argc++)
125 ; /* just counting */
126 nargv = malloc(sizeof(*nargv) * (argc + 2));
129 for (argc = 0; argv[argc]; argc++)
130 nargv[argc + 1] = argv[argc];
131 nargv[argc + 1] = NULL;
135 int execv_perf_cmd(const char **argv) {
136 const char **nargv = prepare_perf_cmd(argv);
138 /* execvp() can only ever return if it fails */
139 execvp("perf", (char **)nargv);
146 int execl_perf_cmd(const char *cmd,...)
149 const char *argv[MAX_ARGS + 1];
153 va_start(param, cmd);
156 while (argc < MAX_ARGS) {
157 arg = argv[argc++] = va_arg(param, char *);
162 if (MAX_ARGS <= argc)
163 return error("too many args to run %s", cmd);
166 return execv_perf_cmd(argv);