perf.c 9.8 KB

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