builtin-trace.c 13 KB

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