builtin-trace.c 15 KB

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