builtin-trace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. #include "builtin.h"
  2. #include "util/util.h"
  3. #include "util/cache.h"
  4. #include "util/symbol.h"
  5. #include "util/thread.h"
  6. #include "util/header.h"
  7. #include "util/exec_cmd.h"
  8. #include "util/trace-event.h"
  9. #include "util/session.h"
  10. static char const *script_name;
  11. static char const *generate_script_lang;
  12. static bool debug_ordering;
  13. static u64 last_timestamp;
  14. static int default_start_script(const char *script __unused,
  15. int argc __unused,
  16. const char **argv __unused)
  17. {
  18. return 0;
  19. }
  20. static int default_stop_script(void)
  21. {
  22. return 0;
  23. }
  24. static int default_generate_script(const char *outfile __unused)
  25. {
  26. return 0;
  27. }
  28. static struct scripting_ops default_scripting_ops = {
  29. .start_script = default_start_script,
  30. .stop_script = default_stop_script,
  31. .process_event = print_event,
  32. .generate_script = default_generate_script,
  33. };
  34. static struct scripting_ops *scripting_ops;
  35. static void setup_scripting(void)
  36. {
  37. /* make sure PERF_EXEC_PATH is set for scripts */
  38. perf_set_argv_exec_path(perf_exec_path());
  39. setup_perl_scripting();
  40. setup_python_scripting();
  41. scripting_ops = &default_scripting_ops;
  42. }
  43. static int cleanup_scripting(void)
  44. {
  45. pr_debug("\nperf trace script stopped\n");
  46. return scripting_ops->stop_script();
  47. }
  48. #include "util/parse-options.h"
  49. #include "perf.h"
  50. #include "util/debug.h"
  51. #include "util/trace-event.h"
  52. #include "util/exec_cmd.h"
  53. static char const *input_name = "perf.data";
  54. static int process_sample_event(event_t *event, struct perf_session *session)
  55. {
  56. struct sample_data data;
  57. struct thread *thread;
  58. memset(&data, 0, sizeof(data));
  59. data.time = -1;
  60. data.cpu = -1;
  61. data.period = 1;
  62. event__parse_sample(event, session->sample_type, &data);
  63. dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
  64. data.pid, data.tid, data.ip, data.period);
  65. thread = perf_session__findnew(session, event->ip.pid);
  66. if (thread == NULL) {
  67. pr_debug("problem processing %d event, skipping it.\n",
  68. event->header.type);
  69. return -1;
  70. }
  71. if (session->sample_type & PERF_SAMPLE_RAW) {
  72. if (debug_ordering) {
  73. if (data.time < last_timestamp) {
  74. pr_err("Samples misordered, previous: %llu "
  75. "this: %llu\n", last_timestamp,
  76. data.time);
  77. }
  78. last_timestamp = data.time;
  79. }
  80. /*
  81. * FIXME: better resolve from pid from the struct trace_entry
  82. * field, although it should be the same than this perf
  83. * event pid
  84. */
  85. scripting_ops->process_event(data.cpu, data.raw_data,
  86. data.raw_size,
  87. data.time, thread->comm);
  88. }
  89. session->hists.stats.total_period += data.period;
  90. return 0;
  91. }
  92. static struct perf_event_ops event_ops = {
  93. .sample = process_sample_event,
  94. .comm = event__process_comm,
  95. .attr = event__process_attr,
  96. .event_type = event__process_event_type,
  97. .tracing_data = event__process_tracing_data,
  98. .build_id = event__process_build_id,
  99. .ordered_samples = true,
  100. };
  101. extern volatile int session_done;
  102. static void sig_handler(int sig __unused)
  103. {
  104. session_done = 1;
  105. }
  106. static int __cmd_trace(struct perf_session *session)
  107. {
  108. signal(SIGINT, sig_handler);
  109. return perf_session__process_events(session, &event_ops);
  110. }
  111. struct script_spec {
  112. struct list_head node;
  113. struct scripting_ops *ops;
  114. char spec[0];
  115. };
  116. LIST_HEAD(script_specs);
  117. static struct script_spec *script_spec__new(const char *spec,
  118. struct scripting_ops *ops)
  119. {
  120. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  121. if (s != NULL) {
  122. strcpy(s->spec, spec);
  123. s->ops = ops;
  124. }
  125. return s;
  126. }
  127. static void script_spec__delete(struct script_spec *s)
  128. {
  129. free(s->spec);
  130. free(s);
  131. }
  132. static void script_spec__add(struct script_spec *s)
  133. {
  134. list_add_tail(&s->node, &script_specs);
  135. }
  136. static struct script_spec *script_spec__find(const char *spec)
  137. {
  138. struct script_spec *s;
  139. list_for_each_entry(s, &script_specs, node)
  140. if (strcasecmp(s->spec, spec) == 0)
  141. return s;
  142. return NULL;
  143. }
  144. static struct script_spec *script_spec__findnew(const char *spec,
  145. struct scripting_ops *ops)
  146. {
  147. struct script_spec *s = script_spec__find(spec);
  148. if (s)
  149. return s;
  150. s = script_spec__new(spec, ops);
  151. if (!s)
  152. goto out_delete_spec;
  153. script_spec__add(s);
  154. return s;
  155. out_delete_spec:
  156. script_spec__delete(s);
  157. return NULL;
  158. }
  159. int script_spec_register(const char *spec, struct scripting_ops *ops)
  160. {
  161. struct script_spec *s;
  162. s = script_spec__find(spec);
  163. if (s)
  164. return -1;
  165. s = script_spec__findnew(spec, ops);
  166. if (!s)
  167. return -1;
  168. return 0;
  169. }
  170. static struct scripting_ops *script_spec__lookup(const char *spec)
  171. {
  172. struct script_spec *s = script_spec__find(spec);
  173. if (!s)
  174. return NULL;
  175. return s->ops;
  176. }
  177. static void list_available_languages(void)
  178. {
  179. struct script_spec *s;
  180. fprintf(stderr, "\n");
  181. fprintf(stderr, "Scripting language extensions (used in "
  182. "perf trace -s [spec:]script.[spec]):\n\n");
  183. list_for_each_entry(s, &script_specs, node)
  184. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  185. fprintf(stderr, "\n");
  186. }
  187. static int parse_scriptname(const struct option *opt __used,
  188. const char *str, int unset __used)
  189. {
  190. char spec[PATH_MAX];
  191. const char *script, *ext;
  192. int len;
  193. if (strcmp(str, "lang") == 0) {
  194. list_available_languages();
  195. exit(0);
  196. }
  197. script = strchr(str, ':');
  198. if (script) {
  199. len = script - str;
  200. if (len >= PATH_MAX) {
  201. fprintf(stderr, "invalid language specifier");
  202. return -1;
  203. }
  204. strncpy(spec, str, len);
  205. spec[len] = '\0';
  206. scripting_ops = script_spec__lookup(spec);
  207. if (!scripting_ops) {
  208. fprintf(stderr, "invalid language specifier");
  209. return -1;
  210. }
  211. script++;
  212. } else {
  213. script = str;
  214. ext = strchr(script, '.');
  215. if (!ext) {
  216. fprintf(stderr, "invalid script extension");
  217. return -1;
  218. }
  219. scripting_ops = script_spec__lookup(++ext);
  220. if (!scripting_ops) {
  221. fprintf(stderr, "invalid script extension");
  222. return -1;
  223. }
  224. }
  225. script_name = strdup(script);
  226. return 0;
  227. }
  228. #define for_each_lang(scripts_dir, lang_dirent, lang_next) \
  229. while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
  230. lang_next) \
  231. if (lang_dirent.d_type == DT_DIR && \
  232. (strcmp(lang_dirent.d_name, ".")) && \
  233. (strcmp(lang_dirent.d_name, "..")))
  234. #define for_each_script(lang_dir, script_dirent, script_next) \
  235. while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
  236. script_next) \
  237. if (script_dirent.d_type != DT_DIR)
  238. #define RECORD_SUFFIX "-record"
  239. #define REPORT_SUFFIX "-report"
  240. struct script_desc {
  241. struct list_head node;
  242. char *name;
  243. char *half_liner;
  244. char *args;
  245. };
  246. LIST_HEAD(script_descs);
  247. static struct script_desc *script_desc__new(const char *name)
  248. {
  249. struct script_desc *s = zalloc(sizeof(*s));
  250. if (s != NULL)
  251. s->name = strdup(name);
  252. return s;
  253. }
  254. static void script_desc__delete(struct script_desc *s)
  255. {
  256. free(s->name);
  257. free(s);
  258. }
  259. static void script_desc__add(struct script_desc *s)
  260. {
  261. list_add_tail(&s->node, &script_descs);
  262. }
  263. static struct script_desc *script_desc__find(const char *name)
  264. {
  265. struct script_desc *s;
  266. list_for_each_entry(s, &script_descs, node)
  267. if (strcasecmp(s->name, name) == 0)
  268. return s;
  269. return NULL;
  270. }
  271. static struct script_desc *script_desc__findnew(const char *name)
  272. {
  273. struct script_desc *s = script_desc__find(name);
  274. if (s)
  275. return s;
  276. s = script_desc__new(name);
  277. if (!s)
  278. goto out_delete_desc;
  279. script_desc__add(s);
  280. return s;
  281. out_delete_desc:
  282. script_desc__delete(s);
  283. return NULL;
  284. }
  285. static char *ends_with(char *str, const char *suffix)
  286. {
  287. size_t suffix_len = strlen(suffix);
  288. char *p = str;
  289. if (strlen(str) > suffix_len) {
  290. p = str + strlen(str) - suffix_len;
  291. if (!strncmp(p, suffix, suffix_len))
  292. return p;
  293. }
  294. return NULL;
  295. }
  296. static char *ltrim(char *str)
  297. {
  298. int len = strlen(str);
  299. while (len && isspace(*str)) {
  300. len--;
  301. str++;
  302. }
  303. return str;
  304. }
  305. static int read_script_info(struct script_desc *desc, const char *filename)
  306. {
  307. char line[BUFSIZ], *p;
  308. FILE *fp;
  309. fp = fopen(filename, "r");
  310. if (!fp)
  311. return -1;
  312. while (fgets(line, sizeof(line), fp)) {
  313. p = ltrim(line);
  314. if (strlen(p) == 0)
  315. continue;
  316. if (*p != '#')
  317. continue;
  318. p++;
  319. if (strlen(p) && *p == '!')
  320. continue;
  321. p = ltrim(p);
  322. if (strlen(p) && p[strlen(p) - 1] == '\n')
  323. p[strlen(p) - 1] = '\0';
  324. if (!strncmp(p, "description:", strlen("description:"))) {
  325. p += strlen("description:");
  326. desc->half_liner = strdup(ltrim(p));
  327. continue;
  328. }
  329. if (!strncmp(p, "args:", strlen("args:"))) {
  330. p += strlen("args:");
  331. desc->args = strdup(ltrim(p));
  332. continue;
  333. }
  334. }
  335. fclose(fp);
  336. return 0;
  337. }
  338. static int list_available_scripts(const struct option *opt __used,
  339. const char *s __used, int unset __used)
  340. {
  341. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  342. char scripts_path[MAXPATHLEN];
  343. DIR *scripts_dir, *lang_dir;
  344. char script_path[MAXPATHLEN];
  345. char lang_path[MAXPATHLEN];
  346. struct script_desc *desc;
  347. char first_half[BUFSIZ];
  348. char *script_root;
  349. char *str;
  350. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  351. scripts_dir = opendir(scripts_path);
  352. if (!scripts_dir)
  353. return -1;
  354. for_each_lang(scripts_dir, lang_dirent, lang_next) {
  355. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  356. lang_dirent.d_name);
  357. lang_dir = opendir(lang_path);
  358. if (!lang_dir)
  359. continue;
  360. for_each_script(lang_dir, script_dirent, script_next) {
  361. script_root = strdup(script_dirent.d_name);
  362. str = ends_with(script_root, REPORT_SUFFIX);
  363. if (str) {
  364. *str = '\0';
  365. desc = script_desc__findnew(script_root);
  366. snprintf(script_path, MAXPATHLEN, "%s/%s",
  367. lang_path, script_dirent.d_name);
  368. read_script_info(desc, script_path);
  369. }
  370. free(script_root);
  371. }
  372. }
  373. fprintf(stdout, "List of available trace scripts:\n");
  374. list_for_each_entry(desc, &script_descs, node) {
  375. sprintf(first_half, "%s %s", desc->name,
  376. desc->args ? desc->args : "");
  377. fprintf(stdout, " %-36s %s\n", first_half,
  378. desc->half_liner ? desc->half_liner : "");
  379. }
  380. exit(0);
  381. }
  382. static char *get_script_path(const char *script_root, const char *suffix)
  383. {
  384. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  385. char scripts_path[MAXPATHLEN];
  386. char script_path[MAXPATHLEN];
  387. DIR *scripts_dir, *lang_dir;
  388. char lang_path[MAXPATHLEN];
  389. char *str, *__script_root;
  390. char *path = NULL;
  391. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  392. scripts_dir = opendir(scripts_path);
  393. if (!scripts_dir)
  394. return NULL;
  395. for_each_lang(scripts_dir, lang_dirent, lang_next) {
  396. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  397. lang_dirent.d_name);
  398. lang_dir = opendir(lang_path);
  399. if (!lang_dir)
  400. continue;
  401. for_each_script(lang_dir, script_dirent, script_next) {
  402. __script_root = strdup(script_dirent.d_name);
  403. str = ends_with(__script_root, suffix);
  404. if (str) {
  405. *str = '\0';
  406. if (strcmp(__script_root, script_root))
  407. continue;
  408. snprintf(script_path, MAXPATHLEN, "%s/%s",
  409. lang_path, script_dirent.d_name);
  410. path = strdup(script_path);
  411. free(__script_root);
  412. break;
  413. }
  414. free(__script_root);
  415. }
  416. }
  417. return path;
  418. }
  419. static const char * const trace_usage[] = {
  420. "perf trace [<options>] <command>",
  421. NULL
  422. };
  423. static const struct option options[] = {
  424. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  425. "dump raw trace in ASCII"),
  426. OPT_INCR('v', "verbose", &verbose,
  427. "be more verbose (show symbol address, etc)"),
  428. OPT_BOOLEAN('L', "Latency", &latency_format,
  429. "show latency attributes (irqs/preemption disabled, etc)"),
  430. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  431. list_available_scripts),
  432. OPT_CALLBACK('s', "script", NULL, "name",
  433. "script file name (lang:script name, script name, or *)",
  434. parse_scriptname),
  435. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  436. "generate perf-trace.xx script in specified language"),
  437. OPT_STRING('i', "input", &input_name, "file",
  438. "input file name"),
  439. OPT_BOOLEAN('d', "debug-ordering", &debug_ordering,
  440. "check that samples time ordering is monotonic"),
  441. OPT_END()
  442. };
  443. int cmd_trace(int argc, const char **argv, const char *prefix __used)
  444. {
  445. struct perf_session *session;
  446. const char *suffix = NULL;
  447. const char **__argv;
  448. char *script_path;
  449. int i, err;
  450. if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
  451. if (argc < 3) {
  452. fprintf(stderr,
  453. "Please specify a record script\n");
  454. return -1;
  455. }
  456. suffix = RECORD_SUFFIX;
  457. }
  458. if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
  459. if (argc < 3) {
  460. fprintf(stderr,
  461. "Please specify a report script\n");
  462. return -1;
  463. }
  464. suffix = REPORT_SUFFIX;
  465. }
  466. if (!suffix && argc >= 2 && strncmp(argv[1], "-", strlen("-")) != 0) {
  467. char *record_script_path, *report_script_path;
  468. int live_pipe[2];
  469. pid_t pid;
  470. record_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  471. if (!record_script_path) {
  472. fprintf(stderr, "record script not found\n");
  473. return -1;
  474. }
  475. report_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  476. if (!report_script_path) {
  477. fprintf(stderr, "report script not found\n");
  478. return -1;
  479. }
  480. if (pipe(live_pipe) < 0) {
  481. perror("failed to create pipe");
  482. exit(-1);
  483. }
  484. pid = fork();
  485. if (pid < 0) {
  486. perror("failed to fork");
  487. exit(-1);
  488. }
  489. if (!pid) {
  490. dup2(live_pipe[1], 1);
  491. close(live_pipe[0]);
  492. __argv = malloc(5 * sizeof(const char *));
  493. __argv[0] = "/bin/sh";
  494. __argv[1] = record_script_path;
  495. __argv[2] = "-o";
  496. __argv[3] = "-";
  497. __argv[4] = NULL;
  498. execvp("/bin/sh", (char **)__argv);
  499. exit(-1);
  500. }
  501. dup2(live_pipe[0], 0);
  502. close(live_pipe[1]);
  503. __argv = malloc((argc + 3) * sizeof(const char *));
  504. __argv[0] = "/bin/sh";
  505. __argv[1] = report_script_path;
  506. for (i = 2; i < argc; i++)
  507. __argv[i] = argv[i];
  508. __argv[i++] = "-i";
  509. __argv[i++] = "-";
  510. __argv[i++] = NULL;
  511. execvp("/bin/sh", (char **)__argv);
  512. exit(-1);
  513. }
  514. if (suffix) {
  515. script_path = get_script_path(argv[2], suffix);
  516. if (!script_path) {
  517. fprintf(stderr, "script not found\n");
  518. return -1;
  519. }
  520. __argv = malloc((argc + 1) * sizeof(const char *));
  521. __argv[0] = "/bin/sh";
  522. __argv[1] = script_path;
  523. for (i = 3; i < argc; i++)
  524. __argv[i - 1] = argv[i];
  525. __argv[argc - 1] = NULL;
  526. execvp("/bin/sh", (char **)__argv);
  527. exit(-1);
  528. }
  529. setup_scripting();
  530. argc = parse_options(argc, argv, options, trace_usage,
  531. PARSE_OPT_STOP_AT_NON_OPTION);
  532. if (symbol__init() < 0)
  533. return -1;
  534. if (!script_name)
  535. setup_pager();
  536. session = perf_session__new(input_name, O_RDONLY, 0, false);
  537. if (session == NULL)
  538. return -ENOMEM;
  539. if (strcmp(input_name, "-") &&
  540. !perf_session__has_traces(session, "record -R"))
  541. return -EINVAL;
  542. if (generate_script_lang) {
  543. struct stat perf_stat;
  544. int input = open(input_name, O_RDONLY);
  545. if (input < 0) {
  546. perror("failed to open file");
  547. exit(-1);
  548. }
  549. err = fstat(input, &perf_stat);
  550. if (err < 0) {
  551. perror("failed to stat file");
  552. exit(-1);
  553. }
  554. if (!perf_stat.st_size) {
  555. fprintf(stderr, "zero-sized file, nothing to do!\n");
  556. exit(0);
  557. }
  558. scripting_ops = script_spec__lookup(generate_script_lang);
  559. if (!scripting_ops) {
  560. fprintf(stderr, "invalid language specifier");
  561. return -1;
  562. }
  563. err = scripting_ops->generate_script("perf-trace");
  564. goto out;
  565. }
  566. if (script_name) {
  567. err = scripting_ops->start_script(script_name, argc, argv);
  568. if (err)
  569. goto out;
  570. pr_debug("perf trace started with script %s\n\n", script_name);
  571. }
  572. err = __cmd_trace(session);
  573. perf_session__delete(session);
  574. cleanup_scripting();
  575. out:
  576. return err;
  577. }