10 static const char *argv_exec_path
;
11 static const char *argv0_path
;
13 const char *system_path(const char *path
)
16 static const char *prefix
;
18 static const char *prefix
= PREFIX
;
20 struct strbuf d
= STRBUF_INIT
;
22 if (is_absolute_path(path
))
27 assert(is_absolute_path(argv0_path
));
30 !(prefix
= strip_path_suffix(argv0_path
, PERF_EXEC_PATH
)) &&
31 !(prefix
= strip_path_suffix(argv0_path
, BINDIR
)) &&
32 !(prefix
= strip_path_suffix(argv0_path
, "perf"))) {
34 fprintf(stderr
, "RUNTIME_PREFIX requested, "
35 "but prefix computation failed. "
36 "Using static fallback '%s'.\n", prefix
);
40 strbuf_addf(&d
, "%s/%s", prefix
, path
);
41 path
= strbuf_detach(&d
, NULL
);
45 const char *perf_extract_argv0_path(const char *argv0
)
49 if (!argv0
|| !*argv0
)
51 slash
= argv0
+ strlen(argv0
);
53 while (argv0
<= slash
&& !is_dir_sep(*slash
))
57 argv0_path
= xstrndup(argv0
, slash
- argv0
);
64 void perf_set_argv_exec_path(const char *exec_path
)
66 argv_exec_path
= exec_path
;
68 * Propagate this setting to external programs.
70 setenv(EXEC_PATH_ENVIRONMENT
, exec_path
, 1);
74 /* Returns the highest-priority, location to look for perf programs. */
75 const char *perf_exec_path(void)
80 return argv_exec_path
;
82 env
= getenv(EXEC_PATH_ENVIRONMENT
);
87 return system_path(PERF_EXEC_PATH
);
90 static void add_path(struct strbuf
*out
, const char *path
)
93 if (is_absolute_path(path
))
94 strbuf_addstr(out
, path
);
96 strbuf_addstr(out
, make_nonrelative_path(path
));
98 strbuf_addch(out
, PATH_SEP
);
102 void setup_path(void)
104 const char *old_path
= getenv("PATH");
105 struct strbuf new_path
= STRBUF_INIT
;
107 add_path(&new_path
, perf_exec_path());
108 add_path(&new_path
, argv0_path
);
111 strbuf_addstr(&new_path
, old_path
);
113 strbuf_addstr(&new_path
, "/usr/local/bin:/usr/bin:/bin");
115 setenv("PATH", new_path
.buf
, 1);
117 strbuf_release(&new_path
);
120 const char **prepare_perf_cmd(const char **argv
)
125 for (argc
= 0; argv
[argc
]; argc
++)
126 ; /* just counting */
127 nargv
= malloc(sizeof(*nargv
) * (argc
+ 2));
130 for (argc
= 0; argv
[argc
]; argc
++)
131 nargv
[argc
+ 1] = argv
[argc
];
132 nargv
[argc
+ 1] = NULL
;
136 int execv_perf_cmd(const char **argv
) {
137 const char **nargv
= prepare_perf_cmd(argv
);
139 /* execvp() can only ever return if it fails */
140 execvp("perf", (char **)nargv
);
147 int execl_perf_cmd(const char *cmd
,...)
150 const char *argv
[MAX_ARGS
+ 1];
154 va_start(param
, cmd
);
157 while (argc
< MAX_ARGS
) {
158 arg
= argv
[argc
++] = va_arg(param
, char *);
163 if (MAX_ARGS
<= argc
)
164 return error("too many args to run %s", cmd
);
167 return execv_perf_cmd(argv
);