builtin-trace.c 15 KB

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