perf_counter tools: Add more warnings and fix/annotate them
[linux-2.6/verdex.git] / tools / perf / util / exec_cmd.c
blob34a3528673822622019687929f0499c4ebc0de1a
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "quote.h"
5 #include <string.h>
7 #define MAX_ARGS 32
9 extern char **environ;
10 static const char *argv_exec_path;
11 static const char *argv0_path;
13 const char *system_path(const char *path)
15 #ifdef RUNTIME_PREFIX
16 static const char *prefix;
17 #else
18 static const char *prefix = PREFIX;
19 #endif
20 struct strbuf d = STRBUF_INIT;
22 if (is_absolute_path(path))
23 return path;
25 #ifdef RUNTIME_PREFIX
26 assert(argv0_path);
27 assert(is_absolute_path(argv0_path));
29 if (!prefix &&
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"))) {
33 prefix = PREFIX;
34 fprintf(stderr, "RUNTIME_PREFIX requested, "
35 "but prefix computation failed. "
36 "Using static fallback '%s'.\n", prefix);
38 #endif
40 strbuf_addf(&d, "%s/%s", prefix, path);
41 path = strbuf_detach(&d, NULL);
42 return path;
45 const char *perf_extract_argv0_path(const char *argv0)
47 const char *slash;
49 if (!argv0 || !*argv0)
50 return NULL;
51 slash = argv0 + strlen(argv0);
53 while (argv0 <= slash && !is_dir_sep(*slash))
54 slash--;
56 if (slash >= argv0) {
57 argv0_path = xstrndup(argv0, slash - argv0);
58 return slash + 1;
61 return 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)
77 const char *env;
79 if (argv_exec_path)
80 return argv_exec_path;
82 env = getenv(EXEC_PATH_ENVIRONMENT);
83 if (env && *env) {
84 return env;
87 return system_path(PERF_EXEC_PATH);
90 static void add_path(struct strbuf *out, const char *path)
92 if (path && *path) {
93 if (is_absolute_path(path))
94 strbuf_addstr(out, path);
95 else
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);
110 if (old_path)
111 strbuf_addstr(&new_path, old_path);
112 else
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)
122 int argc;
123 const char **nargv;
125 for (argc = 0; argv[argc]; argc++)
126 ; /* just counting */
127 nargv = malloc(sizeof(*nargv) * (argc + 2));
129 nargv[0] = "perf";
130 for (argc = 0; argv[argc]; argc++)
131 nargv[argc + 1] = argv[argc];
132 nargv[argc + 1] = NULL;
133 return nargv;
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);
142 free(nargv);
143 return -1;
147 int execl_perf_cmd(const char *cmd,...)
149 int argc;
150 const char *argv[MAX_ARGS + 1];
151 const char *arg;
152 va_list param;
154 va_start(param, cmd);
155 argv[0] = cmd;
156 argc = 1;
157 while (argc < MAX_ARGS) {
158 arg = argv[argc++] = va_arg(param, char *);
159 if (!arg)
160 break;
162 va_end(param);
163 if (MAX_ARGS <= argc)
164 return error("too many args to run %s", cmd);
166 argv[argc] = NULL;
167 return execv_perf_cmd(argv);