builtin-trace.c 16 KB

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