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