builtin-trace.c 15 KB

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