perf.c 12 KB

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