7 static const char *argv_exec_path;
8 static const char *argv0_path;
10 const char *system_path(const char *path)
13 static const char *prefix;
15 static const char *prefix = PREFIX;
17 struct strbuf d = STRBUF_INIT;
19 if (is_absolute_path(path))
24 assert(is_absolute_path(argv0_path));
27 !(prefix = strip_path_suffix(argv0_path, PERF_EXEC_PATH)) &&
28 !(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
29 !(prefix = strip_path_suffix(argv0_path, "perf"))) {
31 fprintf(stderr, "RUNTIME_PREFIX requested, "
32 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix);
37 strbuf_addf(&d, "%s/%s", prefix, path);
38 path = strbuf_detach(&d, NULL);
42 const char *perf_extract_argv0_path(const char *argv0)
46 if (!argv0 || !*argv0)
48 slash = argv0 + strlen(argv0);
50 while (argv0 <= slash && !is_dir_sep(*slash))
54 argv0_path = strndup(argv0, slash - argv0);
61 void perf_set_argv_exec_path(const char *exec_path)
63 argv_exec_path = exec_path;
65 * Propagate this setting to external programs.
67 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
71 /* Returns the highest-priority, location to look for perf programs. */
72 const char *perf_exec_path(void)
77 return argv_exec_path;
79 env = getenv(EXEC_PATH_ENVIRONMENT);
84 return system_path(PERF_EXEC_PATH);
87 static void add_path(struct strbuf *out, const char *path)
90 if (is_absolute_path(path))
91 strbuf_addstr(out, path);
93 strbuf_addstr(out, make_nonrelative_path(path));
95 strbuf_addch(out, PATH_SEP);
101 const char *old_path = getenv("PATH");
102 struct strbuf new_path = STRBUF_INIT;
104 add_path(&new_path, perf_exec_path());
105 add_path(&new_path, argv0_path);
108 strbuf_addstr(&new_path, old_path);
110 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
112 setenv("PATH", new_path.buf, 1);
114 strbuf_release(&new_path);
117 const char **prepare_perf_cmd(const char **argv)
122 for (argc = 0; argv[argc]; argc++)
123 ; /* just counting */
124 nargv = malloc(sizeof(*nargv) * (argc + 2));
127 for (argc = 0; argv[argc]; argc++)
128 nargv[argc + 1] = argv[argc];
129 nargv[argc + 1] = NULL;
133 int execv_perf_cmd(const char **argv) {
134 const char **nargv = prepare_perf_cmd(argv);
136 /* execvp() can only ever return if it fails */
137 execvp("perf", (char **)nargv);
144 int execl_perf_cmd(const char *cmd,...)
147 const char *argv[MAX_ARGS + 1];
151 va_start(param, cmd);
154 while (argc < MAX_ARGS) {
155 arg = argv[argc++] = va_arg(param, char *);
160 if (MAX_ARGS <= argc)
161 return error("too many args to run %s", cmd);
164 return execv_perf_cmd(argv);