perf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. { "top", cmd_top, 0 },
  258. { "annotate", cmd_annotate, 0 },
  259. { "version", cmd_version, 0 },
  260. };
  261. unsigned int i;
  262. static const char ext[] = STRIP_EXTENSION;
  263. if (sizeof(ext) > 1) {
  264. i = strlen(argv[0]) - strlen(ext);
  265. if (i > 0 && !strcmp(argv[0] + i, ext)) {
  266. char *argv0 = strdup(argv[0]);
  267. argv[0] = cmd = argv0;
  268. argv0[i] = '\0';
  269. }
  270. }
  271. /* Turn "perf cmd --help" into "perf help cmd" */
  272. if (argc > 1 && !strcmp(argv[1], "--help")) {
  273. argv[1] = argv[0];
  274. argv[0] = cmd = "help";
  275. }
  276. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  277. struct cmd_struct *p = commands+i;
  278. if (strcmp(p->cmd, cmd))
  279. continue;
  280. exit(run_builtin(p, argc, argv));
  281. }
  282. }
  283. static void execv_dashed_external(const char **argv)
  284. {
  285. struct strbuf cmd = STRBUF_INIT;
  286. const char *tmp;
  287. int status;
  288. strbuf_addf(&cmd, "perf-%s", argv[0]);
  289. /*
  290. * argv[0] must be the perf command, but the argv array
  291. * belongs to the caller, and may be reused in
  292. * subsequent loop iterations. Save argv[0] and
  293. * restore it on error.
  294. */
  295. tmp = argv[0];
  296. argv[0] = cmd.buf;
  297. /*
  298. * if we fail because the command is not found, it is
  299. * OK to return. Otherwise, we just pass along the status code.
  300. */
  301. status = run_command_v_opt(argv, 0);
  302. if (status != -ERR_RUN_COMMAND_EXEC) {
  303. if (IS_RUN_COMMAND_ERR(status))
  304. die("unable to run '%s'", argv[0]);
  305. exit(-status);
  306. }
  307. errno = ENOENT; /* as if we called execvp */
  308. argv[0] = tmp;
  309. strbuf_release(&cmd);
  310. }
  311. static int run_argv(int *argcp, const char ***argv)
  312. {
  313. int done_alias = 0;
  314. while (1) {
  315. /* See if it's an internal command */
  316. handle_internal_command(*argcp, *argv);
  317. /* .. then try the external ones */
  318. execv_dashed_external(*argv);
  319. /* It could be an alias -- this works around the insanity
  320. * of overriding "perf log" with "perf show" by having
  321. * alias.log = show
  322. */
  323. if (done_alias || !handle_alias(argcp, argv))
  324. break;
  325. done_alias = 1;
  326. }
  327. return done_alias;
  328. }
  329. /* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
  330. static void get_debugfs_mntpt(void)
  331. {
  332. FILE *file;
  333. char fs_type[100];
  334. char debugfs[MAXPATHLEN];
  335. /*
  336. * try the standard location
  337. */
  338. if (valid_debugfs_mount("/sys/kernel/debug/") == 0) {
  339. strcpy(debugfs_mntpt, "/sys/kernel/debug/");
  340. return;
  341. }
  342. /*
  343. * try the sane location
  344. */
  345. if (valid_debugfs_mount("/debug/") == 0) {
  346. strcpy(debugfs_mntpt, "/debug/");
  347. return;
  348. }
  349. /*
  350. * give up and parse /proc/mounts
  351. */
  352. file = fopen("/proc/mounts", "r");
  353. if (file == NULL)
  354. return;
  355. while (fscanf(file, "%*s %"
  356. STR(MAXPATHLEN)
  357. "s %99s %*s %*d %*d\n",
  358. debugfs, fs_type) == 2) {
  359. if (strcmp(fs_type, "debugfs") == 0)
  360. break;
  361. }
  362. fclose(file);
  363. if (strcmp(fs_type, "debugfs") == 0) {
  364. strncpy(debugfs_mntpt, debugfs, MAXPATHLEN);
  365. debugfs_mntpt[MAXPATHLEN - 1] = '\0';
  366. }
  367. }
  368. int main(int argc, const char **argv)
  369. {
  370. const char *cmd;
  371. cmd = perf_extract_argv0_path(argv[0]);
  372. if (!cmd)
  373. cmd = "perf-help";
  374. /* get debugfs mount point from /proc/mounts */
  375. get_debugfs_mntpt();
  376. /*
  377. * "perf-xxxx" is the same as "perf xxxx", but we obviously:
  378. *
  379. * - cannot take flags in between the "perf" and the "xxxx".
  380. * - cannot execute it externally (since it would just do
  381. * the same thing over again)
  382. *
  383. * So we just directly call the internal command handler, and
  384. * die if that one cannot handle it.
  385. */
  386. if (!prefixcmp(cmd, "perf-")) {
  387. cmd += 5;
  388. argv[0] = cmd;
  389. handle_internal_command(argc, argv);
  390. die("cannot handle %s internally", cmd);
  391. }
  392. /* Look for flags.. */
  393. argv++;
  394. argc--;
  395. handle_options(&argv, &argc, NULL);
  396. commit_pager_choice();
  397. set_debugfs_path();
  398. if (argc > 0) {
  399. if (!prefixcmp(argv[0], "--"))
  400. argv[0] += 2;
  401. } else {
  402. /* The user didn't specify a command; give them help */
  403. printf("\n usage: %s\n\n", perf_usage_string);
  404. list_common_cmds_help();
  405. printf("\n %s\n\n", perf_more_info_string);
  406. exit(1);
  407. }
  408. cmd = argv[0];
  409. /*
  410. * We use PATH to find perf commands, but we prepend some higher
  411. * precidence paths: the "--exec-path" option, the PERF_EXEC_PATH
  412. * environment, and the $(perfexecdir) from the Makefile at build
  413. * time.
  414. */
  415. setup_path();
  416. while (1) {
  417. static int done_help = 0;
  418. static int was_alias = 0;
  419. was_alias = run_argv(&argc, &argv);
  420. if (errno != ENOENT)
  421. break;
  422. if (was_alias) {
  423. fprintf(stderr, "Expansion of alias '%s' failed; "
  424. "'%s' is not a perf-command\n",
  425. cmd, argv[0]);
  426. exit(1);
  427. }
  428. if (!done_help) {
  429. cmd = argv[0] = help_unknown_cmd(cmd);
  430. done_help = 1;
  431. } else
  432. break;
  433. }
  434. fprintf(stderr, "Failed to run command '%s': %s\n",
  435. cmd, strerror(errno));
  436. return 1;
  437. }