builtin-trace.c 13 KB

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