builtin-trace.c 13 KB

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