builtin-trace.c 13 KB

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