9 static const char *argv_exec_path
;
10 static const char *argv0_path
;
12 const char *system_path(const char *path
)
14 static const char *prefix
= PREFIX
;
15 struct strbuf d
= STRBUF_INIT
;
17 if (is_absolute_path(path
))
20 strbuf_addf(&d
, "%s/%s", prefix
, path
);
21 path
= strbuf_detach(&d
, NULL
);
25 const char *perf_extract_argv0_path(const char *argv0
)
29 if (!argv0
|| !*argv0
)
31 slash
= argv0
+ strlen(argv0
);
33 while (argv0
<= slash
&& !is_dir_sep(*slash
))
37 argv0_path
= strndup(argv0
, slash
- argv0
);
38 return argv0_path
? slash
+ 1 : NULL
;
44 void perf_set_argv_exec_path(const char *exec_path
)
46 argv_exec_path
= exec_path
;
48 * Propagate this setting to external programs.
50 setenv(EXEC_PATH_ENVIRONMENT
, exec_path
, 1);
54 /* Returns the highest-priority, location to look for perf programs. */
55 const char *perf_exec_path(void)
60 return argv_exec_path
;
62 env
= getenv(EXEC_PATH_ENVIRONMENT
);
67 return system_path(PERF_EXEC_PATH
);
70 static void add_path(struct strbuf
*out
, const char *path
)
73 if (is_absolute_path(path
))
74 strbuf_addstr(out
, path
);
76 strbuf_addstr(out
, make_nonrelative_path(path
));
78 strbuf_addch(out
, PATH_SEP
);
84 const char *old_path
= getenv("PATH");
85 struct strbuf new_path
= STRBUF_INIT
;
87 add_path(&new_path
, perf_exec_path());
88 add_path(&new_path
, argv0_path
);
91 strbuf_addstr(&new_path
, old_path
);
93 strbuf_addstr(&new_path
, "/usr/local/bin:/usr/bin:/bin");
95 setenv("PATH", new_path
.buf
, 1);
97 strbuf_release(&new_path
);
100 static const char **prepare_perf_cmd(const char **argv
)
105 for (argc
= 0; argv
[argc
]; argc
++)
106 ; /* just counting */
107 nargv
= malloc(sizeof(*nargv
) * (argc
+ 2));
110 for (argc
= 0; argv
[argc
]; argc
++)
111 nargv
[argc
+ 1] = argv
[argc
];
112 nargv
[argc
+ 1] = NULL
;
116 int execv_perf_cmd(const char **argv
) {
117 const char **nargv
= prepare_perf_cmd(argv
);
119 /* execvp() can only ever return if it fails */
120 execvp("perf", (char **)nargv
);
127 int execl_perf_cmd(const char *cmd
,...)
130 const char *argv
[MAX_ARGS
+ 1];
134 va_start(param
, cmd
);
137 while (argc
< MAX_ARGS
) {
138 arg
= argv
[argc
++] = va_arg(param
, char *);
143 if (MAX_ARGS
<= argc
)
144 return error("too many args to run %s", cmd
);
147 return execv_perf_cmd(argv
);