perf.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #include "builtin.h"
  2. #include "util/exec_cmd.h"
  3. #include "util/cache.h"
  4. #include "util/quote.h"
  5. #include "util/run-command.h"
  6. const char perf_usage_string[] =
  7. "perf [--version] [--help] COMMAND [ARGS]";
  8. const char perf_more_info_string[] =
  9. "See 'perf help COMMAND' for more information on a specific command.";
  10. static int use_pager = -1;
  11. struct pager_config {
  12. const char *cmd;
  13. int val;
  14. };
  15. static int pager_command_config(const char *var, const char *value, void *data)
  16. {
  17. struct pager_config *c = data;
  18. if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
  19. c->val = perf_config_bool(var, value);
  20. return 0;
  21. }
  22. /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
  23. int check_pager_config(const char *cmd)
  24. {
  25. struct pager_config c;
  26. c.cmd = cmd;
  27. c.val = -1;
  28. perf_config(pager_command_config, &c);
  29. return c.val;
  30. }
  31. static void commit_pager_choice(void) {
  32. switch (use_pager) {
  33. case 0:
  34. setenv("PERF_PAGER", "cat", 1);
  35. break;
  36. case 1:
  37. /* setup_pager(); */
  38. break;
  39. default:
  40. break;
  41. }
  42. }
  43. static int handle_options(const char*** argv, int* argc, int* envchanged)
  44. {
  45. int handled = 0;
  46. while (*argc > 0) {
  47. const char *cmd = (*argv)[0];
  48. if (cmd[0] != '-')
  49. break;
  50. /*
  51. * For legacy reasons, the "version" and "help"
  52. * commands can be written with "--" prepended
  53. * to make them look like flags.
  54. */
  55. if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
  56. break;
  57. /*
  58. * Check remaining flags.
  59. */
  60. if (!prefixcmp(cmd, "--exec-path")) {
  61. cmd += 11;
  62. if (*cmd == '=')
  63. perf_set_argv_exec_path(cmd + 1);
  64. else {
  65. puts(perf_exec_path());
  66. exit(0);
  67. }
  68. } else if (!strcmp(cmd, "--html-path")) {
  69. puts(system_path(PERF_HTML_PATH));
  70. exit(0);
  71. } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
  72. use_pager = 1;
  73. } else if (!strcmp(cmd, "--no-pager")) {
  74. use_pager = 0;
  75. if (envchanged)
  76. *envchanged = 1;
  77. } else if (!strcmp(cmd, "--perf-dir")) {
  78. if (*argc < 2) {
  79. fprintf(stderr, "No directory given for --perf-dir.\n" );
  80. usage(perf_usage_string);
  81. }
  82. setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
  83. if (envchanged)
  84. *envchanged = 1;
  85. (*argv)++;
  86. (*argc)--;
  87. handled++;
  88. } else if (!prefixcmp(cmd, "--perf-dir=")) {
  89. setenv(PERF_DIR_ENVIRONMENT, cmd + 10, 1);
  90. if (envchanged)
  91. *envchanged = 1;
  92. } else if (!strcmp(cmd, "--work-tree")) {
  93. if (*argc < 2) {
  94. fprintf(stderr, "No directory given for --work-tree.\n" );
  95. usage(perf_usage_string);
  96. }
  97. setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
  98. if (envchanged)
  99. *envchanged = 1;
  100. (*argv)++;
  101. (*argc)--;
  102. } else if (!prefixcmp(cmd, "--work-tree=")) {
  103. setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
  104. if (envchanged)
  105. *envchanged = 1;
  106. } else {
  107. fprintf(stderr, "Unknown option: %s\n", cmd);
  108. usage(perf_usage_string);
  109. }
  110. (*argv)++;
  111. (*argc)--;
  112. handled++;
  113. }
  114. return handled;
  115. }
  116. static int handle_alias(int *argcp, const char ***argv)
  117. {
  118. int envchanged = 0, ret = 0, saved_errno = errno;
  119. int count, option_count;
  120. const char** new_argv;
  121. const char *alias_command;
  122. char *alias_string;
  123. alias_command = (*argv)[0];
  124. alias_string = alias_lookup(alias_command);
  125. if (alias_string) {
  126. if (alias_string[0] == '!') {
  127. if (*argcp > 1) {
  128. struct strbuf buf;
  129. strbuf_init(&buf, PATH_MAX);
  130. strbuf_addstr(&buf, alias_string);
  131. sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
  132. free(alias_string);
  133. alias_string = buf.buf;
  134. }
  135. ret = system(alias_string + 1);
  136. if (ret >= 0 && WIFEXITED(ret) &&
  137. WEXITSTATUS(ret) != 127)
  138. exit(WEXITSTATUS(ret));
  139. die("Failed to run '%s' when expanding alias '%s'",
  140. alias_string + 1, alias_command);
  141. }
  142. count = split_cmdline(alias_string, &new_argv);
  143. if (count < 0)
  144. die("Bad alias.%s string", alias_command);
  145. option_count = handle_options(&new_argv, &count, &envchanged);
  146. if (envchanged)
  147. die("alias '%s' changes environment variables\n"
  148. "You can use '!perf' in the alias to do this.",
  149. alias_command);
  150. memmove(new_argv - option_count, new_argv,
  151. count * sizeof(char *));
  152. new_argv -= option_count;
  153. if (count < 1)
  154. die("empty alias for %s", alias_command);
  155. if (!strcmp(alias_command, new_argv[0]))
  156. die("recursive alias: %s", alias_command);
  157. new_argv = realloc(new_argv, sizeof(char*) *
  158. (count + *argcp + 1));
  159. /* insert after command name */
  160. memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp);
  161. new_argv[count+*argcp] = NULL;
  162. *argv = new_argv;
  163. *argcp += count - 1;
  164. ret = 1;
  165. }
  166. errno = saved_errno;
  167. return ret;
  168. }
  169. const char perf_version_string[] = PERF_VERSION;
  170. #define RUN_SETUP (1<<0)
  171. #define USE_PAGER (1<<1)
  172. /*
  173. * require working tree to be present -- anything uses this needs
  174. * RUN_SETUP for reading from the configuration file.
  175. */
  176. #define NEED_WORK_TREE (1<<2)
  177. struct cmd_struct {
  178. const char *cmd;
  179. int (*fn)(int, const char **, const char *);
  180. int option;
  181. };
  182. static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
  183. {
  184. int status;
  185. struct stat st;
  186. const char *prefix;
  187. prefix = NULL;
  188. if (p->option & RUN_SETUP)
  189. prefix = NULL; /* setup_perf_directory(); */
  190. if (use_pager == -1 && p->option & RUN_SETUP)
  191. use_pager = check_pager_config(p->cmd);
  192. if (use_pager == -1 && p->option & USE_PAGER)
  193. use_pager = 1;
  194. commit_pager_choice();
  195. if (p->option & NEED_WORK_TREE)
  196. /* setup_work_tree() */;
  197. status = p->fn(argc, argv, prefix);
  198. if (status)
  199. return status & 0xff;
  200. /* Somebody closed stdout? */
  201. if (fstat(fileno(stdout), &st))
  202. return 0;
  203. /* Ignore write errors for pipes and sockets.. */
  204. if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
  205. return 0;
  206. /* Check for ENOSPC and EIO errors.. */
  207. if (fflush(stdout))
  208. die("write failure on standard output: %s", strerror(errno));
  209. if (ferror(stdout))
  210. die("unknown write failure on standard output");
  211. if (fclose(stdout))
  212. die("close failed on standard output: %s", strerror(errno));
  213. return 0;
  214. }
  215. static void handle_internal_command(int argc, const char **argv)
  216. {
  217. const char *cmd = argv[0];
  218. static struct cmd_struct commands[] = {
  219. { "help", cmd_help, 0 },
  220. { "record", cmd_record, 0 },
  221. { "stat", cmd_stat, 0 },
  222. { "top", cmd_top, 0 },
  223. { "version", cmd_version, 0 },
  224. };
  225. int i;
  226. static const char ext[] = STRIP_EXTENSION;
  227. if (sizeof(ext) > 1) {
  228. i = strlen(argv[0]) - strlen(ext);
  229. if (i > 0 && !strcmp(argv[0] + i, ext)) {
  230. char *argv0 = strdup(argv[0]);
  231. argv[0] = cmd = argv0;
  232. argv0[i] = '\0';
  233. }
  234. }
  235. /* Turn "perf cmd --help" into "perf help cmd" */
  236. if (argc > 1 && !strcmp(argv[1], "--help")) {
  237. argv[1] = argv[0];
  238. argv[0] = cmd = "help";
  239. }
  240. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  241. struct cmd_struct *p = commands+i;
  242. if (strcmp(p->cmd, cmd))
  243. continue;
  244. exit(run_builtin(p, argc, argv));
  245. }
  246. }
  247. static void execv_dashed_external(const char **argv)
  248. {
  249. struct strbuf cmd = STRBUF_INIT;
  250. const char *tmp;
  251. int status;
  252. strbuf_addf(&cmd, "perf-%s", argv[0]);
  253. /*
  254. * argv[0] must be the perf command, but the argv array
  255. * belongs to the caller, and may be reused in
  256. * subsequent loop iterations. Save argv[0] and
  257. * restore it on error.
  258. */
  259. tmp = argv[0];
  260. argv[0] = cmd.buf;
  261. /*
  262. * if we fail because the command is not found, it is
  263. * OK to return. Otherwise, we just pass along the status code.
  264. */
  265. status = run_command_v_opt(argv, 0);
  266. if (status != -ERR_RUN_COMMAND_EXEC) {
  267. if (IS_RUN_COMMAND_ERR(status))
  268. die("unable to run '%s'", argv[0]);
  269. exit(-status);
  270. }
  271. errno = ENOENT; /* as if we called execvp */
  272. argv[0] = tmp;
  273. strbuf_release(&cmd);
  274. }
  275. static int run_argv(int *argcp, const char ***argv)
  276. {
  277. int done_alias = 0;
  278. while (1) {
  279. /* See if it's an internal command */
  280. handle_internal_command(*argcp, *argv);
  281. /* .. then try the external ones */
  282. execv_dashed_external(*argv);
  283. /* It could be an alias -- this works around the insanity
  284. * of overriding "perf log" with "perf show" by having
  285. * alias.log = show
  286. */
  287. if (done_alias || !handle_alias(argcp, argv))
  288. break;
  289. done_alias = 1;
  290. }
  291. return done_alias;
  292. }
  293. int main(int argc, const char **argv)
  294. {
  295. const char *cmd;
  296. cmd = perf_extract_argv0_path(argv[0]);
  297. if (!cmd)
  298. cmd = "perf-help";
  299. /*
  300. * "perf-xxxx" is the same as "perf xxxx", but we obviously:
  301. *
  302. * - cannot take flags in between the "perf" and the "xxxx".
  303. * - cannot execute it externally (since it would just do
  304. * the same thing over again)
  305. *
  306. * So we just directly call the internal command handler, and
  307. * die if that one cannot handle it.
  308. */
  309. if (!prefixcmp(cmd, "perf-")) {
  310. cmd += 5;
  311. argv[0] = cmd;
  312. handle_internal_command(argc, argv);
  313. die("cannot handle %s internally", cmd);
  314. }
  315. /* Look for flags.. */
  316. argv++;
  317. argc--;
  318. handle_options(&argv, &argc, NULL);
  319. commit_pager_choice();
  320. if (argc > 0) {
  321. if (!prefixcmp(argv[0], "--"))
  322. argv[0] += 2;
  323. } else {
  324. /* The user didn't specify a command; give them help */
  325. printf("usage: %s\n\n", perf_usage_string);
  326. list_common_cmds_help();
  327. printf("\n%s\n", perf_more_info_string);
  328. exit(1);
  329. }
  330. cmd = argv[0];
  331. /*
  332. * We use PATH to find perf commands, but we prepend some higher
  333. * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
  334. * environment, and the $(perfexecdir) from the Makefile at build
  335. * time.
  336. */
  337. setup_path();
  338. while (1) {
  339. static int done_help = 0;
  340. static int was_alias = 0;
  341. was_alias = run_argv(&argc, &argv);
  342. if (errno != ENOENT)
  343. break;
  344. if (was_alias) {
  345. fprintf(stderr, "Expansion of alias '%s' failed; "
  346. "'%s' is not a perf-command\n",
  347. cmd, argv[0]);
  348. exit(1);
  349. }
  350. if (!done_help) {
  351. cmd = argv[0] = help_unknown_cmd(cmd);
  352. done_help = 1;
  353. } else
  354. break;
  355. }
  356. fprintf(stderr, "Failed to run command '%s': %s\n",
  357. cmd, strerror(errno));
  358. return 1;
  359. }