perf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 "perf.h"
  11. #include "util/build-id.h"
  12. #include "util/cache.h"
  13. #include "util/env.h"
  14. #include <internal/lib.h> // page_size
  15. #include <subcmd/exec-cmd.h>
  16. #include "util/config.h"
  17. #include <subcmd/run-command.h>
  18. #include "util/parse-events.h"
  19. #include <subcmd/parse-options.h>
  20. #include "util/bpf-loader.h"
  21. #include "util/debug.h"
  22. #include "util/event.h"
  23. #include "util/util.h" // usage()
  24. #include "ui/ui.h"
  25. #include "perf-sys.h"
  26. #include <api/fs/fs.h>
  27. #include <api/fs/tracing_path.h>
  28. #include <perf/core.h>
  29. #include <errno.h>
  30. #include <pthread.h>
  31. #include <signal.h>
  32. #include <stdlib.h>
  33. #include <time.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <unistd.h>
  37. #include <linux/kernel.h>
  38. #include <linux/string.h>
  39. #include <linux/zalloc.h>
  40. const char perf_usage_string[] =
  41. "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
  42. const char perf_more_info_string[] =
  43. "See 'perf help COMMAND' for more information on a specific command.";
  44. static int use_pager = -1;
  45. const char *input_name;
  46. struct cmd_struct {
  47. const char *cmd;
  48. int (*fn)(int, const char **);
  49. int option;
  50. };
  51. static struct cmd_struct commands[] = {
  52. { "buildid-cache", cmd_buildid_cache, 0 },
  53. { "buildid-list", cmd_buildid_list, 0 },
  54. { "config", cmd_config, 0 },
  55. { "c2c", cmd_c2c, 0 },
  56. { "diff", cmd_diff, 0 },
  57. { "evlist", cmd_evlist, 0 },
  58. { "help", cmd_help, 0 },
  59. { "kallsyms", cmd_kallsyms, 0 },
  60. { "list", cmd_list, 0 },
  61. { "record", cmd_record, 0 },
  62. { "report", cmd_report, 0 },
  63. { "bench", cmd_bench, 0 },
  64. { "stat", cmd_stat, 0 },
  65. { "timechart", cmd_timechart, 0 },
  66. { "top", cmd_top, 0 },
  67. { "annotate", cmd_annotate, 0 },
  68. { "version", cmd_version, 0 },
  69. { "script", cmd_script, 0 },
  70. { "sched", cmd_sched, 0 },
  71. #ifdef HAVE_LIBELF_SUPPORT
  72. { "probe", cmd_probe, 0 },
  73. #endif
  74. { "kmem", cmd_kmem, 0 },
  75. { "lock", cmd_lock, 0 },
  76. { "kvm", cmd_kvm, 0 },
  77. { "test", cmd_test, 0 },
  78. #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
  79. { "trace", cmd_trace, 0 },
  80. #endif
  81. { "inject", cmd_inject, 0 },
  82. { "mem", cmd_mem, 0 },
  83. { "data", cmd_data, 0 },
  84. { "ftrace", cmd_ftrace, 0 },
  85. };
  86. struct pager_config {
  87. const char *cmd;
  88. int val;
  89. };
  90. static int pager_command_config(const char *var, const char *value, void *data)
  91. {
  92. struct pager_config *c = data;
  93. if (strstarts(var, "pager.") && !strcmp(var + 6, c->cmd))
  94. c->val = perf_config_bool(var, value);
  95. return 0;
  96. }
  97. /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
  98. static int check_pager_config(const char *cmd)
  99. {
  100. int err;
  101. struct pager_config c;
  102. c.cmd = cmd;
  103. c.val = -1;
  104. err = perf_config(pager_command_config, &c);
  105. return err ?: c.val;
  106. }
  107. static int browser_command_config(const char *var, const char *value, void *data)
  108. {
  109. struct pager_config *c = data;
  110. if (strstarts(var, "tui.") && !strcmp(var + 4, c->cmd))
  111. c->val = perf_config_bool(var, value);
  112. if (strstarts(var, "gtk.") && !strcmp(var + 4, c->cmd))
  113. c->val = perf_config_bool(var, value) ? 2 : 0;
  114. return 0;
  115. }
  116. /*
  117. * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
  118. * and -1 for "not specified"
  119. */
  120. static int check_browser_config(const char *cmd)
  121. {
  122. int err;
  123. struct pager_config c;
  124. c.cmd = cmd;
  125. c.val = -1;
  126. err = perf_config(browser_command_config, &c);
  127. return err ?: c.val;
  128. }
  129. static void commit_pager_choice(void)
  130. {
  131. switch (use_pager) {
  132. case 0:
  133. setenv(PERF_PAGER_ENVIRONMENT, "cat", 1);
  134. break;
  135. case 1:
  136. /* setup_pager(); */
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. struct option options[] = {
  143. OPT_ARGUMENT("help", "help"),
  144. OPT_ARGUMENT("version", "version"),
  145. OPT_ARGUMENT("exec-path", "exec-path"),
  146. OPT_ARGUMENT("html-path", "html-path"),
  147. OPT_ARGUMENT("paginate", "paginate"),
  148. OPT_ARGUMENT("no-pager", "no-pager"),
  149. OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
  150. OPT_ARGUMENT("buildid-dir", "buildid-dir"),
  151. OPT_ARGUMENT("list-cmds", "list-cmds"),
  152. OPT_ARGUMENT("list-opts", "list-opts"),
  153. OPT_ARGUMENT("debug", "debug"),
  154. OPT_END()
  155. };
  156. static int handle_options(const char ***argv, int *argc, int *envchanged)
  157. {
  158. int handled = 0;
  159. while (*argc > 0) {
  160. const char *cmd = (*argv)[0];
  161. if (cmd[0] != '-')
  162. break;
  163. /*
  164. * For legacy reasons, the "version" and "help"
  165. * commands can be written with "--" prepended
  166. * to make them look like flags.
  167. */
  168. if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
  169. break;
  170. /*
  171. * Shortcut for '-h' and '-v' options to invoke help
  172. * and version command.
  173. */
  174. if (!strcmp(cmd, "-h")) {
  175. (*argv)[0] = "--help";
  176. break;
  177. }
  178. if (!strcmp(cmd, "-v")) {
  179. (*argv)[0] = "--version";
  180. break;
  181. }
  182. if (!strcmp(cmd, "-vv")) {
  183. (*argv)[0] = "version";
  184. version_verbose = 1;
  185. break;
  186. }
  187. /*
  188. * Check remaining flags.
  189. */
  190. if (strstarts(cmd, CMD_EXEC_PATH)) {
  191. cmd += strlen(CMD_EXEC_PATH);
  192. if (*cmd == '=')
  193. set_argv_exec_path(cmd + 1);
  194. else {
  195. puts(get_argv_exec_path());
  196. exit(0);
  197. }
  198. } else if (!strcmp(cmd, "--html-path")) {
  199. puts(system_path(PERF_HTML_PATH));
  200. exit(0);
  201. } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
  202. use_pager = 1;
  203. } else if (!strcmp(cmd, "--no-pager")) {
  204. use_pager = 0;
  205. if (envchanged)
  206. *envchanged = 1;
  207. } else if (!strcmp(cmd, "--debugfs-dir")) {
  208. if (*argc < 2) {
  209. fprintf(stderr, "No directory given for --debugfs-dir.\n");
  210. usage(perf_usage_string);
  211. }
  212. tracing_path_set((*argv)[1]);
  213. if (envchanged)
  214. *envchanged = 1;
  215. (*argv)++;
  216. (*argc)--;
  217. } else if (!strcmp(cmd, "--buildid-dir")) {
  218. if (*argc < 2) {
  219. fprintf(stderr, "No directory given for --buildid-dir.\n");
  220. usage(perf_usage_string);
  221. }
  222. set_buildid_dir((*argv)[1]);
  223. if (envchanged)
  224. *envchanged = 1;
  225. (*argv)++;
  226. (*argc)--;
  227. } else if (strstarts(cmd, CMD_DEBUGFS_DIR)) {
  228. tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR));
  229. fprintf(stderr, "dir: %s\n", tracing_path_mount());
  230. if (envchanged)
  231. *envchanged = 1;
  232. } else if (!strcmp(cmd, "--list-cmds")) {
  233. unsigned int i;
  234. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  235. struct cmd_struct *p = commands+i;
  236. printf("%s ", p->cmd);
  237. }
  238. putchar('\n');
  239. exit(0);
  240. } else if (!strcmp(cmd, "--list-opts")) {
  241. unsigned int i;
  242. for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
  243. struct option *p = options+i;
  244. printf("--%s ", p->long_name);
  245. }
  246. putchar('\n');
  247. exit(0);
  248. } else if (!strcmp(cmd, "--debug")) {
  249. if (*argc < 2) {
  250. fprintf(stderr, "No variable specified for --debug.\n");
  251. usage(perf_usage_string);
  252. }
  253. if (perf_debug_option((*argv)[1]))
  254. usage(perf_usage_string);
  255. (*argv)++;
  256. (*argc)--;
  257. } else {
  258. fprintf(stderr, "Unknown option: %s\n", cmd);
  259. usage(perf_usage_string);
  260. }
  261. (*argv)++;
  262. (*argc)--;
  263. handled++;
  264. }
  265. return handled;
  266. }
  267. #define RUN_SETUP (1<<0)
  268. #define USE_PAGER (1<<1)
  269. static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
  270. {
  271. int status;
  272. struct stat st;
  273. char sbuf[STRERR_BUFSIZE];
  274. if (use_browser == -1)
  275. use_browser = check_browser_config(p->cmd);
  276. if (use_pager == -1 && p->option & RUN_SETUP)
  277. use_pager = check_pager_config(p->cmd);
  278. if (use_pager == -1 && p->option & USE_PAGER)
  279. use_pager = 1;
  280. commit_pager_choice();
  281. perf_env__init(&perf_env);
  282. perf_env__set_cmdline(&perf_env, argc, argv);
  283. status = p->fn(argc, argv);
  284. perf_config__exit();
  285. exit_browser(status);
  286. perf_env__exit(&perf_env);
  287. bpf__clear();
  288. if (status)
  289. return status & 0xff;
  290. /* Somebody closed stdout? */
  291. if (fstat(fileno(stdout), &st))
  292. return 0;
  293. /* Ignore write errors for pipes and sockets.. */
  294. if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
  295. return 0;
  296. status = 1;
  297. /* Check for ENOSPC and EIO errors.. */
  298. if (fflush(stdout)) {
  299. fprintf(stderr, "write failure on standard output: %s",
  300. str_error_r(errno, sbuf, sizeof(sbuf)));
  301. goto out;
  302. }
  303. if (ferror(stdout)) {
  304. fprintf(stderr, "unknown write failure on standard output");
  305. goto out;
  306. }
  307. if (fclose(stdout)) {
  308. fprintf(stderr, "close failed on standard output: %s",
  309. str_error_r(errno, sbuf, sizeof(sbuf)));
  310. goto out;
  311. }
  312. status = 0;
  313. out:
  314. return status;
  315. }
  316. static void handle_internal_command(int argc, const char **argv)
  317. {
  318. const char *cmd = argv[0];
  319. unsigned int i;
  320. /* Turn "perf cmd --help" into "perf help cmd" */
  321. if (argc > 1 && !strcmp(argv[1], "--help")) {
  322. argv[1] = argv[0];
  323. argv[0] = cmd = "help";
  324. }
  325. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  326. struct cmd_struct *p = commands+i;
  327. if (strcmp(p->cmd, cmd))
  328. continue;
  329. exit(run_builtin(p, argc, argv));
  330. }
  331. }
  332. static void execv_dashed_external(const char **argv)
  333. {
  334. char *cmd;
  335. const char *tmp;
  336. int status;
  337. if (asprintf(&cmd, "perf-%s", argv[0]) < 0)
  338. goto do_die;
  339. /*
  340. * argv[0] must be the perf command, but the argv array
  341. * belongs to the caller, and may be reused in
  342. * subsequent loop iterations. Save argv[0] and
  343. * restore it on error.
  344. */
  345. tmp = argv[0];
  346. argv[0] = cmd;
  347. /*
  348. * if we fail because the command is not found, it is
  349. * OK to return. Otherwise, we just pass along the status code.
  350. */
  351. status = run_command_v_opt(argv, 0);
  352. if (status != -ERR_RUN_COMMAND_EXEC) {
  353. if (IS_RUN_COMMAND_ERR(status)) {
  354. do_die:
  355. pr_err("FATAL: unable to run '%s'", argv[0]);
  356. status = -128;
  357. }
  358. exit(-status);
  359. }
  360. errno = ENOENT; /* as if we called execvp */
  361. argv[0] = tmp;
  362. zfree(&cmd);
  363. }
  364. static int run_argv(int *argcp, const char ***argv)
  365. {
  366. /* See if it's an internal command */
  367. handle_internal_command(*argcp, *argv);
  368. /* .. then try the external ones */
  369. execv_dashed_external(*argv);
  370. return 0;
  371. }
  372. static void pthread__block_sigwinch(void)
  373. {
  374. sigset_t set;
  375. sigemptyset(&set);
  376. sigaddset(&set, SIGWINCH);
  377. pthread_sigmask(SIG_BLOCK, &set, NULL);
  378. }
  379. void pthread__unblock_sigwinch(void)
  380. {
  381. sigset_t set;
  382. sigemptyset(&set);
  383. sigaddset(&set, SIGWINCH);
  384. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  385. }
  386. static int libperf_print(enum libperf_print_level level,
  387. const char *fmt, va_list ap)
  388. {
  389. return eprintf(level, verbose, fmt, ap);
  390. }
  391. int main(int argc, const char **argv)
  392. {
  393. int err;
  394. const char *cmd;
  395. char sbuf[STRERR_BUFSIZE];
  396. /* libsubcmd init */
  397. exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT);
  398. pager_init(PERF_PAGER_ENVIRONMENT);
  399. libperf_init(libperf_print);
  400. cmd = extract_argv0_path(argv[0]);
  401. if (!cmd)
  402. cmd = "perf-help";
  403. srandom(time(NULL));
  404. /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
  405. config_exclusive_filename = getenv("PERF_CONFIG");
  406. err = perf_config(perf_default_config, NULL);
  407. if (err)
  408. return err;
  409. set_buildid_dir(NULL);
  410. /*
  411. * "perf-xxxx" is the same as "perf xxxx", but we obviously:
  412. *
  413. * - cannot take flags in between the "perf" and the "xxxx".
  414. * - cannot execute it externally (since it would just do
  415. * the same thing over again)
  416. *
  417. * So we just directly call the internal command handler. If that one
  418. * fails to handle this, then maybe we just run a renamed perf binary
  419. * that contains a dash in its name. To handle this scenario, we just
  420. * fall through and ignore the "xxxx" part of the command string.
  421. */
  422. if (strstarts(cmd, "perf-")) {
  423. cmd += 5;
  424. argv[0] = cmd;
  425. handle_internal_command(argc, argv);
  426. /*
  427. * If the command is handled, the above function does not
  428. * return undo changes and fall through in such a case.
  429. */
  430. cmd -= 5;
  431. argv[0] = cmd;
  432. }
  433. if (strstarts(cmd, "trace")) {
  434. #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
  435. setup_path();
  436. argv[0] = "trace";
  437. return cmd_trace(argc, argv);
  438. #else
  439. fprintf(stderr,
  440. "trace command not available: missing audit-libs devel package at build time.\n");
  441. goto out;
  442. #endif
  443. }
  444. /* Look for flags.. */
  445. argv++;
  446. argc--;
  447. handle_options(&argv, &argc, NULL);
  448. commit_pager_choice();
  449. if (argc > 0) {
  450. if (strstarts(argv[0], "--"))
  451. argv[0] += 2;
  452. } else {
  453. /* The user didn't specify a command; give them help */
  454. printf("\n usage: %s\n\n", perf_usage_string);
  455. list_common_cmds_help();
  456. printf("\n %s\n\n", perf_more_info_string);
  457. goto out;
  458. }
  459. cmd = argv[0];
  460. test_attr__init();
  461. /*
  462. * We use PATH to find perf commands, but we prepend some higher
  463. * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
  464. * environment, and the $(perfexecdir) from the Makefile at build
  465. * time.
  466. */
  467. setup_path();
  468. /*
  469. * Block SIGWINCH notifications so that the thread that wants it can
  470. * unblock and get syscalls like select interrupted instead of waiting
  471. * forever while the signal goes to some other non interested thread.
  472. */
  473. pthread__block_sigwinch();
  474. perf_debug_setup();
  475. while (1) {
  476. static int done_help;
  477. run_argv(&argc, &argv);
  478. if (errno != ENOENT)
  479. break;
  480. if (!done_help) {
  481. cmd = argv[0] = help_unknown_cmd(cmd);
  482. done_help = 1;
  483. } else
  484. break;
  485. }
  486. fprintf(stderr, "Failed to run command '%s': %s\n",
  487. cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
  488. out:
  489. return 1;
  490. }