perf.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include "builtin.h"
  2. #include "exec_cmd.h"
  3. #include "cache.h"
  4. #include "quote.h"
  5. #include "run-command.h"
  6. const char perf_usage_string[] =
  7. "perf [--version] [--exec-path[=PERF_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--perf-dir=PERF_DIR] [--work-tree=PERF_WORK_TREE] [--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. { "top", cmd_top, 0 },
  220. { "stat", cmd_stat, 0 },
  221. };
  222. int i;
  223. static const char ext[] = STRIP_EXTENSION;
  224. if (sizeof(ext) > 1) {
  225. i = strlen(argv[0]) - strlen(ext);
  226. if (i > 0 && !strcmp(argv[0] + i, ext)) {
  227. char *argv0 = strdup(argv[0]);
  228. argv[0] = cmd = argv0;
  229. argv0[i] = '\0';
  230. }
  231. }
  232. /* Turn "perf cmd --help" into "perf help cmd" */
  233. if (argc > 1 && !strcmp(argv[1], "--help")) {
  234. argv[1] = argv[0];
  235. argv[0] = cmd = "help";
  236. }
  237. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  238. struct cmd_struct *p = commands+i;
  239. if (strcmp(p->cmd, cmd))
  240. continue;
  241. exit(run_builtin(p, argc, argv));
  242. }
  243. }
  244. static void execv_dashed_external(const char **argv)
  245. {
  246. struct strbuf cmd = STRBUF_INIT;
  247. const char *tmp;
  248. int status;
  249. strbuf_addf(&cmd, "perf-%s", argv[0]);
  250. /*
  251. * argv[0] must be the perf command, but the argv array
  252. * belongs to the caller, and may be reused in
  253. * subsequent loop iterations. Save argv[0] and
  254. * restore it on error.
  255. */
  256. tmp = argv[0];
  257. argv[0] = cmd.buf;
  258. /*
  259. * if we fail because the command is not found, it is
  260. * OK to return. Otherwise, we just pass along the status code.
  261. */
  262. status = run_command_v_opt(argv, 0);
  263. if (status != -ERR_RUN_COMMAND_EXEC) {
  264. if (IS_RUN_COMMAND_ERR(status))
  265. die("unable to run '%s'", argv[0]);
  266. exit(-status);
  267. }
  268. errno = ENOENT; /* as if we called execvp */
  269. argv[0] = tmp;
  270. strbuf_release(&cmd);
  271. }
  272. static int run_argv(int *argcp, const char ***argv)
  273. {
  274. int done_alias = 0;
  275. while (1) {
  276. /* See if it's an internal command */
  277. handle_internal_command(*argcp, *argv);
  278. /* .. then try the external ones */
  279. execv_dashed_external(*argv);
  280. /* It could be an alias -- this works around the insanity
  281. * of overriding "perf log" with "perf show" by having
  282. * alias.log = show
  283. */
  284. if (done_alias || !handle_alias(argcp, argv))
  285. break;
  286. done_alias = 1;
  287. }
  288. return done_alias;
  289. }
  290. int main(int argc, const char **argv)
  291. {
  292. const char *cmd;
  293. cmd = perf_extract_argv0_path(argv[0]);
  294. if (!cmd)
  295. cmd = "perf-help";
  296. /*
  297. * "perf-xxxx" is the same as "perf xxxx", but we obviously:
  298. *
  299. * - cannot take flags in between the "perf" and the "xxxx".
  300. * - cannot execute it externally (since it would just do
  301. * the same thing over again)
  302. *
  303. * So we just directly call the internal command handler, and
  304. * die if that one cannot handle it.
  305. */
  306. if (!prefixcmp(cmd, "perf-")) {
  307. cmd += 4;
  308. argv[0] = cmd;
  309. handle_internal_command(argc, argv);
  310. die("cannot handle %s internally", cmd);
  311. }
  312. /* Look for flags.. */
  313. argv++;
  314. argc--;
  315. handle_options(&argv, &argc, NULL);
  316. commit_pager_choice();
  317. if (argc > 0) {
  318. if (!prefixcmp(argv[0], "--"))
  319. argv[0] += 2;
  320. } else {
  321. /* The user didn't specify a command; give them help */
  322. printf("usage: %s\n\n", perf_usage_string);
  323. list_common_cmds_help();
  324. printf("\n%s\n", perf_more_info_string);
  325. exit(1);
  326. }
  327. cmd = argv[0];
  328. /*
  329. * We use PATH to find perf commands, but we prepend some higher
  330. * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
  331. * environment, and the $(perfexecdir) from the Makefile at build
  332. * time.
  333. */
  334. setup_path();
  335. while (1) {
  336. static int done_help = 0;
  337. static int was_alias = 0;
  338. was_alias = run_argv(&argc, &argv);
  339. if (errno != ENOENT)
  340. break;
  341. if (was_alias) {
  342. fprintf(stderr, "Expansion of alias '%s' failed; "
  343. "'%s' is not a perf-command\n",
  344. cmd, argv[0]);
  345. exit(1);
  346. }
  347. if (!done_help) {
  348. cmd = argv[0] = help_unknown_cmd(cmd);
  349. done_help = 1;
  350. } else
  351. break;
  352. }
  353. fprintf(stderr, "Failed to run command '%s': %s\n",
  354. cmd, strerror(errno));
  355. return 1;
  356. }