trace-event-info.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008,2009, Steven Rostedt <srostedt@redhat.com>
  4. */
  5. #include <dirent.h>
  6. #include <mntent.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdarg.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <sys/wait.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <stdbool.h>
  18. #include <linux/list.h>
  19. #include <linux/kernel.h>
  20. #include <linux/zalloc.h>
  21. #include <internal/lib.h> // page_size
  22. #include "trace-event.h"
  23. #include <api/fs/tracing_path.h>
  24. #include "evsel.h"
  25. #include "debug.h"
  26. #define VERSION "0.6"
  27. static int output_fd;
  28. int bigendian(void)
  29. {
  30. unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
  31. unsigned int *ptr;
  32. ptr = (unsigned int *)(void *)str;
  33. return *ptr == 0x01020304;
  34. }
  35. /* unfortunately, you can not stat debugfs or proc files for size */
  36. static int record_file(const char *file, ssize_t hdr_sz)
  37. {
  38. unsigned long long size = 0;
  39. char buf[BUFSIZ], *sizep;
  40. off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
  41. int r, fd;
  42. int err = -EIO;
  43. fd = open(file, O_RDONLY);
  44. if (fd < 0) {
  45. pr_debug("Can't read '%s'", file);
  46. return -errno;
  47. }
  48. /* put in zeros for file size, then fill true size later */
  49. if (hdr_sz) {
  50. if (write(output_fd, &size, hdr_sz) != hdr_sz)
  51. goto out;
  52. }
  53. do {
  54. r = read(fd, buf, BUFSIZ);
  55. if (r > 0) {
  56. size += r;
  57. if (write(output_fd, buf, r) != r)
  58. goto out;
  59. }
  60. } while (r > 0);
  61. /* ugh, handle big-endian hdr_size == 4 */
  62. sizep = (char*)&size;
  63. if (bigendian())
  64. sizep += sizeof(u64) - hdr_sz;
  65. if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0) {
  66. pr_debug("writing file size failed\n");
  67. goto out;
  68. }
  69. err = 0;
  70. out:
  71. close(fd);
  72. return err;
  73. }
  74. static int record_header_files(void)
  75. {
  76. char *path = get_events_file("header_page");
  77. struct stat st;
  78. int err = -EIO;
  79. if (!path) {
  80. pr_debug("can't get tracing/events/header_page");
  81. return -ENOMEM;
  82. }
  83. if (stat(path, &st) < 0) {
  84. pr_debug("can't read '%s'", path);
  85. goto out;
  86. }
  87. if (write(output_fd, "header_page", 12) != 12) {
  88. pr_debug("can't write header_page\n");
  89. goto out;
  90. }
  91. if (record_file(path, 8) < 0) {
  92. pr_debug("can't record header_page file\n");
  93. goto out;
  94. }
  95. put_events_file(path);
  96. path = get_events_file("header_event");
  97. if (!path) {
  98. pr_debug("can't get tracing/events/header_event");
  99. err = -ENOMEM;
  100. goto out;
  101. }
  102. if (stat(path, &st) < 0) {
  103. pr_debug("can't read '%s'", path);
  104. goto out;
  105. }
  106. if (write(output_fd, "header_event", 13) != 13) {
  107. pr_debug("can't write header_event\n");
  108. goto out;
  109. }
  110. if (record_file(path, 8) < 0) {
  111. pr_debug("can't record header_event file\n");
  112. goto out;
  113. }
  114. err = 0;
  115. out:
  116. put_events_file(path);
  117. return err;
  118. }
  119. static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
  120. {
  121. while (tps) {
  122. if (!strcmp(sys, tps->name))
  123. return true;
  124. tps = tps->next;
  125. }
  126. return false;
  127. }
  128. #define for_each_event(dir, dent, tps) \
  129. while ((dent = readdir(dir))) \
  130. if (dent->d_type == DT_DIR && \
  131. (strcmp(dent->d_name, ".")) && \
  132. (strcmp(dent->d_name, ".."))) \
  133. static int copy_event_system(const char *sys, struct tracepoint_path *tps)
  134. {
  135. struct dirent *dent;
  136. struct stat st;
  137. char *format;
  138. DIR *dir;
  139. int count = 0;
  140. int ret;
  141. int err;
  142. dir = opendir(sys);
  143. if (!dir) {
  144. pr_debug("can't read directory '%s'", sys);
  145. return -errno;
  146. }
  147. for_each_event(dir, dent, tps) {
  148. if (!name_in_tp_list(dent->d_name, tps))
  149. continue;
  150. if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
  151. err = -ENOMEM;
  152. goto out;
  153. }
  154. ret = stat(format, &st);
  155. free(format);
  156. if (ret < 0)
  157. continue;
  158. count++;
  159. }
  160. if (write(output_fd, &count, 4) != 4) {
  161. err = -EIO;
  162. pr_debug("can't write count\n");
  163. goto out;
  164. }
  165. rewinddir(dir);
  166. for_each_event(dir, dent, tps) {
  167. if (!name_in_tp_list(dent->d_name, tps))
  168. continue;
  169. if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
  170. err = -ENOMEM;
  171. goto out;
  172. }
  173. ret = stat(format, &st);
  174. if (ret >= 0) {
  175. err = record_file(format, 8);
  176. if (err) {
  177. free(format);
  178. goto out;
  179. }
  180. }
  181. free(format);
  182. }
  183. err = 0;
  184. out:
  185. closedir(dir);
  186. return err;
  187. }
  188. static int record_ftrace_files(struct tracepoint_path *tps)
  189. {
  190. char *path;
  191. int ret;
  192. path = get_events_file("ftrace");
  193. if (!path) {
  194. pr_debug("can't get tracing/events/ftrace");
  195. return -ENOMEM;
  196. }
  197. ret = copy_event_system(path, tps);
  198. put_tracing_file(path);
  199. return ret;
  200. }
  201. static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
  202. {
  203. while (tps) {
  204. if (!strcmp(sys, tps->system))
  205. return true;
  206. tps = tps->next;
  207. }
  208. return false;
  209. }
  210. static int record_event_files(struct tracepoint_path *tps)
  211. {
  212. struct dirent *dent;
  213. struct stat st;
  214. char *path;
  215. char *sys;
  216. DIR *dir;
  217. int count = 0;
  218. int ret;
  219. int err;
  220. path = get_tracing_file("events");
  221. if (!path) {
  222. pr_debug("can't get tracing/events");
  223. return -ENOMEM;
  224. }
  225. dir = opendir(path);
  226. if (!dir) {
  227. err = -errno;
  228. pr_debug("can't read directory '%s'", path);
  229. goto out;
  230. }
  231. for_each_event(dir, dent, tps) {
  232. if (strcmp(dent->d_name, "ftrace") == 0 ||
  233. !system_in_tp_list(dent->d_name, tps))
  234. continue;
  235. count++;
  236. }
  237. if (write(output_fd, &count, 4) != 4) {
  238. err = -EIO;
  239. pr_debug("can't write count\n");
  240. goto out;
  241. }
  242. rewinddir(dir);
  243. for_each_event(dir, dent, tps) {
  244. if (strcmp(dent->d_name, "ftrace") == 0 ||
  245. !system_in_tp_list(dent->d_name, tps))
  246. continue;
  247. if (asprintf(&sys, "%s/%s", path, dent->d_name) < 0) {
  248. err = -ENOMEM;
  249. goto out;
  250. }
  251. ret = stat(sys, &st);
  252. if (ret >= 0) {
  253. ssize_t size = strlen(dent->d_name) + 1;
  254. if (write(output_fd, dent->d_name, size) != size ||
  255. copy_event_system(sys, tps) < 0) {
  256. err = -EIO;
  257. free(sys);
  258. goto out;
  259. }
  260. }
  261. free(sys);
  262. }
  263. err = 0;
  264. out:
  265. closedir(dir);
  266. put_tracing_file(path);
  267. return err;
  268. }
  269. static int record_proc_kallsyms(void)
  270. {
  271. unsigned long long size = 0;
  272. /*
  273. * Just to keep older perf.data file parsers happy, record a zero
  274. * sized kallsyms file, i.e. do the same thing that was done when
  275. * /proc/kallsyms (or something specified via --kallsyms, in a
  276. * different path) couldn't be read.
  277. */
  278. return write(output_fd, &size, 4) != 4 ? -EIO : 0;
  279. }
  280. static int record_ftrace_printk(void)
  281. {
  282. unsigned int size;
  283. char *path;
  284. struct stat st;
  285. int ret, err = 0;
  286. path = get_tracing_file("printk_formats");
  287. if (!path) {
  288. pr_debug("can't get tracing/printk_formats");
  289. return -ENOMEM;
  290. }
  291. ret = stat(path, &st);
  292. if (ret < 0) {
  293. /* not found */
  294. size = 0;
  295. if (write(output_fd, &size, 4) != 4)
  296. err = -EIO;
  297. goto out;
  298. }
  299. err = record_file(path, 4);
  300. out:
  301. put_tracing_file(path);
  302. return err;
  303. }
  304. static int record_saved_cmdline(void)
  305. {
  306. unsigned long long size;
  307. char *path;
  308. struct stat st;
  309. int ret, err = 0;
  310. path = get_tracing_file("saved_cmdlines");
  311. if (!path) {
  312. pr_debug("can't get tracing/saved_cmdline");
  313. return -ENOMEM;
  314. }
  315. ret = stat(path, &st);
  316. if (ret < 0) {
  317. /* not found */
  318. size = 0;
  319. if (write(output_fd, &size, 8) != 8)
  320. err = -EIO;
  321. goto out;
  322. }
  323. err = record_file(path, 8);
  324. out:
  325. put_tracing_file(path);
  326. return err;
  327. }
  328. static void
  329. put_tracepoints_path(struct tracepoint_path *tps)
  330. {
  331. while (tps) {
  332. struct tracepoint_path *t = tps;
  333. tps = tps->next;
  334. zfree(&t->name);
  335. zfree(&t->system);
  336. free(t);
  337. }
  338. }
  339. static struct tracepoint_path *
  340. get_tracepoints_path(struct list_head *pattrs)
  341. {
  342. struct tracepoint_path path, *ppath = &path;
  343. struct evsel *pos;
  344. int nr_tracepoints = 0;
  345. list_for_each_entry(pos, pattrs, core.node) {
  346. if (pos->core.attr.type != PERF_TYPE_TRACEPOINT)
  347. continue;
  348. ++nr_tracepoints;
  349. if (pos->name) {
  350. ppath->next = tracepoint_name_to_path(pos->name);
  351. if (ppath->next)
  352. goto next;
  353. if (strchr(pos->name, ':') == NULL)
  354. goto try_id;
  355. goto error;
  356. }
  357. try_id:
  358. ppath->next = tracepoint_id_to_path(pos->core.attr.config);
  359. if (!ppath->next) {
  360. error:
  361. pr_debug("No memory to alloc tracepoints list\n");
  362. put_tracepoints_path(path.next);
  363. return NULL;
  364. }
  365. next:
  366. ppath = ppath->next;
  367. }
  368. return nr_tracepoints > 0 ? path.next : NULL;
  369. }
  370. bool have_tracepoints(struct list_head *pattrs)
  371. {
  372. struct evsel *pos;
  373. list_for_each_entry(pos, pattrs, core.node)
  374. if (pos->core.attr.type == PERF_TYPE_TRACEPOINT)
  375. return true;
  376. return false;
  377. }
  378. static int tracing_data_header(void)
  379. {
  380. char buf[20];
  381. ssize_t size;
  382. /* just guessing this is someone's birthday.. ;) */
  383. buf[0] = 23;
  384. buf[1] = 8;
  385. buf[2] = 68;
  386. memcpy(buf + 3, "tracing", 7);
  387. if (write(output_fd, buf, 10) != 10)
  388. return -1;
  389. size = strlen(VERSION) + 1;
  390. if (write(output_fd, VERSION, size) != size)
  391. return -1;
  392. /* save endian */
  393. if (bigendian())
  394. buf[0] = 1;
  395. else
  396. buf[0] = 0;
  397. if (write(output_fd, buf, 1) != 1)
  398. return -1;
  399. /* save size of long */
  400. buf[0] = sizeof(long);
  401. if (write(output_fd, buf, 1) != 1)
  402. return -1;
  403. /* save page_size */
  404. if (write(output_fd, &page_size, 4) != 4)
  405. return -1;
  406. return 0;
  407. }
  408. struct tracing_data *tracing_data_get(struct list_head *pattrs,
  409. int fd, bool temp)
  410. {
  411. struct tracepoint_path *tps;
  412. struct tracing_data *tdata;
  413. int err;
  414. output_fd = fd;
  415. tps = get_tracepoints_path(pattrs);
  416. if (!tps)
  417. return NULL;
  418. tdata = malloc(sizeof(*tdata));
  419. if (!tdata)
  420. return NULL;
  421. tdata->temp = temp;
  422. tdata->size = 0;
  423. if (temp) {
  424. int temp_fd;
  425. snprintf(tdata->temp_file, sizeof(tdata->temp_file),
  426. "/tmp/perf-XXXXXX");
  427. if (!mkstemp(tdata->temp_file)) {
  428. pr_debug("Can't make temp file");
  429. free(tdata);
  430. return NULL;
  431. }
  432. temp_fd = open(tdata->temp_file, O_RDWR);
  433. if (temp_fd < 0) {
  434. pr_debug("Can't read '%s'", tdata->temp_file);
  435. free(tdata);
  436. return NULL;
  437. }
  438. /*
  439. * Set the temp file the default output, so all the
  440. * tracing data are stored into it.
  441. */
  442. output_fd = temp_fd;
  443. }
  444. err = tracing_data_header();
  445. if (err)
  446. goto out;
  447. err = record_header_files();
  448. if (err)
  449. goto out;
  450. err = record_ftrace_files(tps);
  451. if (err)
  452. goto out;
  453. err = record_event_files(tps);
  454. if (err)
  455. goto out;
  456. err = record_proc_kallsyms();
  457. if (err)
  458. goto out;
  459. err = record_ftrace_printk();
  460. if (err)
  461. goto out;
  462. err = record_saved_cmdline();
  463. out:
  464. /*
  465. * All tracing data are stored by now, we can restore
  466. * the default output file in case we used temp file.
  467. */
  468. if (temp) {
  469. tdata->size = lseek(output_fd, 0, SEEK_CUR);
  470. close(output_fd);
  471. output_fd = fd;
  472. }
  473. if (err)
  474. zfree(&tdata);
  475. put_tracepoints_path(tps);
  476. return tdata;
  477. }
  478. int tracing_data_put(struct tracing_data *tdata)
  479. {
  480. int err = 0;
  481. if (tdata->temp) {
  482. err = record_file(tdata->temp_file, 0);
  483. unlink(tdata->temp_file);
  484. }
  485. free(tdata);
  486. return err;
  487. }
  488. int read_tracing_data(int fd, struct list_head *pattrs)
  489. {
  490. int err;
  491. struct tracing_data *tdata;
  492. /*
  493. * We work over the real file, so we can write data
  494. * directly, no temp file is needed.
  495. */
  496. tdata = tracing_data_get(pattrs, fd, false);
  497. if (!tdata)
  498. return -ENOMEM;
  499. err = tracing_data_put(tdata);
  500. return err;
  501. }