trace-event-info.c 11 KB

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