builtin-trace.c 15 KB

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