trace-event-info.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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 = get_events_file("header_page");
  93. struct stat st;
  94. int err = -EIO;
  95. if (!path) {
  96. pr_debug("can't get tracing/events/header_page");
  97. return -ENOMEM;
  98. }
  99. if (stat(path, &st) < 0) {
  100. pr_debug("can't read '%s'", path);
  101. goto out;
  102. }
  103. if (write(output_fd, "header_page", 12) != 12) {
  104. pr_debug("can't write header_page\n");
  105. goto out;
  106. }
  107. if (record_file(path, 8) < 0) {
  108. pr_debug("can't record header_page file\n");
  109. goto out;
  110. }
  111. put_events_file(path);
  112. path = get_events_file("header_event");
  113. if (!path) {
  114. pr_debug("can't get tracing/events/header_event");
  115. err = -ENOMEM;
  116. goto out;
  117. }
  118. if (stat(path, &st) < 0) {
  119. pr_debug("can't read '%s'", path);
  120. goto out;
  121. }
  122. if (write(output_fd, "header_event", 13) != 13) {
  123. pr_debug("can't write header_event\n");
  124. goto out;
  125. }
  126. if (record_file(path, 8) < 0) {
  127. pr_debug("can't record header_event file\n");
  128. goto out;
  129. }
  130. err = 0;
  131. out:
  132. put_events_file(path);
  133. return err;
  134. }
  135. static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
  136. {
  137. while (tps) {
  138. if (!strcmp(sys, tps->name))
  139. return true;
  140. tps = tps->next;
  141. }
  142. return false;
  143. }
  144. #define for_each_event(dir, dent, tps) \
  145. while ((dent = readdir(dir))) \
  146. if (dent->d_type == DT_DIR && \
  147. (strcmp(dent->d_name, ".")) && \
  148. (strcmp(dent->d_name, ".."))) \
  149. static int copy_event_system(const char *sys, struct tracepoint_path *tps)
  150. {
  151. struct dirent *dent;
  152. struct stat st;
  153. char *format;
  154. DIR *dir;
  155. int count = 0;
  156. int ret;
  157. int err;
  158. dir = opendir(sys);
  159. if (!dir) {
  160. pr_debug("can't read directory '%s'", sys);
  161. return -errno;
  162. }
  163. for_each_event(dir, dent, tps) {
  164. if (!name_in_tp_list(dent->d_name, tps))
  165. continue;
  166. if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
  167. err = -ENOMEM;
  168. goto out;
  169. }
  170. ret = stat(format, &st);
  171. free(format);
  172. if (ret < 0)
  173. continue;
  174. count++;
  175. }
  176. if (write(output_fd, &count, 4) != 4) {
  177. err = -EIO;
  178. pr_debug("can't write count\n");
  179. goto out;
  180. }
  181. rewinddir(dir);
  182. for_each_event(dir, dent, tps) {
  183. if (!name_in_tp_list(dent->d_name, tps))
  184. continue;
  185. if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
  186. err = -ENOMEM;
  187. goto out;
  188. }
  189. ret = stat(format, &st);
  190. if (ret >= 0) {
  191. err = record_file(format, 8);
  192. if (err) {
  193. free(format);
  194. goto out;
  195. }
  196. }
  197. free(format);
  198. }
  199. err = 0;
  200. out:
  201. closedir(dir);
  202. return err;
  203. }
  204. static int record_ftrace_files(struct tracepoint_path *tps)
  205. {
  206. char *path;
  207. int ret;
  208. path = get_events_file("ftrace");
  209. if (!path) {
  210. pr_debug("can't get tracing/events/ftrace");
  211. return -ENOMEM;
  212. }
  213. ret = copy_event_system(path, tps);
  214. put_tracing_file(path);
  215. return ret;
  216. }
  217. static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
  218. {
  219. while (tps) {
  220. if (!strcmp(sys, tps->system))
  221. return true;
  222. tps = tps->next;
  223. }
  224. return false;
  225. }
  226. static int record_event_files(struct tracepoint_path *tps)
  227. {
  228. struct dirent *dent;
  229. struct stat st;
  230. char *path;
  231. char *sys;
  232. DIR *dir;
  233. int count = 0;
  234. int ret;
  235. int err;
  236. path = get_tracing_file("events");
  237. if (!path) {
  238. pr_debug("can't get tracing/events");
  239. return -ENOMEM;
  240. }
  241. dir = opendir(path);
  242. if (!dir) {
  243. err = -errno;
  244. pr_debug("can't read directory '%s'", path);
  245. goto out;
  246. }
  247. for_each_event(dir, dent, tps) {
  248. if (strcmp(dent->d_name, "ftrace") == 0 ||
  249. !system_in_tp_list(dent->d_name, tps))
  250. continue;
  251. count++;
  252. }
  253. if (write(output_fd, &count, 4) != 4) {
  254. err = -EIO;
  255. pr_debug("can't write count\n");
  256. goto out;
  257. }
  258. rewinddir(dir);
  259. for_each_event(dir, dent, tps) {
  260. if (strcmp(dent->d_name, "ftrace") == 0 ||
  261. !system_in_tp_list(dent->d_name, tps))
  262. continue;
  263. if (asprintf(&sys, "%s/%s", path, dent->d_name) < 0) {
  264. err = -ENOMEM;
  265. goto out;
  266. }
  267. ret = stat(sys, &st);
  268. if (ret >= 0) {
  269. ssize_t size = strlen(dent->d_name) + 1;
  270. if (write(output_fd, dent->d_name, size) != size ||
  271. copy_event_system(sys, tps) < 0) {
  272. err = -EIO;
  273. free(sys);
  274. goto out;
  275. }
  276. }
  277. free(sys);
  278. }
  279. err = 0;
  280. out:
  281. closedir(dir);
  282. put_tracing_file(path);
  283. return err;
  284. }
  285. static int record_proc_kallsyms(void)
  286. {
  287. unsigned long long size = 0;
  288. /*
  289. * Just to keep older perf.data file parsers happy, record a zero
  290. * sized kallsyms file, i.e. do the same thing that was done when
  291. * /proc/kallsyms (or something specified via --kallsyms, in a
  292. * different path) couldn't be read.
  293. */
  294. return write(output_fd, &size, 4) != 4 ? -EIO : 0;
  295. }
  296. static int record_ftrace_printk(void)
  297. {
  298. unsigned int size;
  299. char *path;
  300. struct stat st;
  301. int ret, err = 0;
  302. path = get_tracing_file("printk_formats");
  303. if (!path) {
  304. pr_debug("can't get tracing/printk_formats");
  305. return -ENOMEM;
  306. }
  307. ret = stat(path, &st);
  308. if (ret < 0) {
  309. /* not found */
  310. size = 0;
  311. if (write(output_fd, &size, 4) != 4)
  312. err = -EIO;
  313. goto out;
  314. }
  315. err = record_file(path, 4);
  316. out:
  317. put_tracing_file(path);
  318. return err;
  319. }
  320. static int record_saved_cmdline(void)
  321. {
  322. unsigned long long size;
  323. char *path;
  324. struct stat st;
  325. int ret, err = 0;
  326. path = get_tracing_file("saved_cmdlines");
  327. if (!path) {
  328. pr_debug("can't get tracing/saved_cmdline");
  329. return -ENOMEM;
  330. }
  331. ret = stat(path, &st);
  332. if (ret < 0) {
  333. /* not found */
  334. size = 0;
  335. if (write(output_fd, &size, 8) != 8)
  336. err = -EIO;
  337. goto out;
  338. }
  339. err = record_file(path, 8);
  340. out:
  341. put_tracing_file(path);
  342. return err;
  343. }
  344. static void
  345. put_tracepoints_path(struct tracepoint_path *tps)
  346. {
  347. while (tps) {
  348. struct tracepoint_path *t = tps;
  349. tps = tps->next;
  350. zfree(&t->name);
  351. zfree(&t->system);
  352. free(t);
  353. }
  354. }
  355. static struct tracepoint_path *
  356. get_tracepoints_path(struct list_head *pattrs)
  357. {
  358. struct tracepoint_path path, *ppath = &path;
  359. struct perf_evsel *pos;
  360. int nr_tracepoints = 0;
  361. list_for_each_entry(pos, pattrs, node) {
  362. if (pos->attr.type != PERF_TYPE_TRACEPOINT)
  363. continue;
  364. ++nr_tracepoints;
  365. if (pos->name) {
  366. ppath->next = tracepoint_name_to_path(pos->name);
  367. if (ppath->next)
  368. goto next;
  369. if (strchr(pos->name, ':') == NULL)
  370. goto try_id;
  371. goto error;
  372. }
  373. try_id:
  374. ppath->next = tracepoint_id_to_path(pos->attr.config);
  375. if (!ppath->next) {
  376. error:
  377. pr_debug("No memory to alloc tracepoints list\n");
  378. put_tracepoints_path(&path);
  379. return NULL;
  380. }
  381. next:
  382. ppath = ppath->next;
  383. }
  384. return nr_tracepoints > 0 ? path.next : NULL;
  385. }
  386. bool have_tracepoints(struct list_head *pattrs)
  387. {
  388. struct perf_evsel *pos;
  389. list_for_each_entry(pos, pattrs, node)
  390. if (pos->attr.type == PERF_TYPE_TRACEPOINT)
  391. return true;
  392. return false;
  393. }
  394. static int tracing_data_header(void)
  395. {
  396. char buf[20];
  397. ssize_t size;
  398. /* just guessing this is someone's birthday.. ;) */
  399. buf[0] = 23;
  400. buf[1] = 8;
  401. buf[2] = 68;
  402. memcpy(buf + 3, "tracing", 7);
  403. if (write(output_fd, buf, 10) != 10)
  404. return -1;
  405. size = strlen(VERSION) + 1;
  406. if (write(output_fd, VERSION, size) != size)
  407. return -1;
  408. /* save endian */
  409. if (bigendian())
  410. buf[0] = 1;
  411. else
  412. buf[0] = 0;
  413. if (write(output_fd, buf, 1) != 1)
  414. return -1;
  415. /* save size of long */
  416. buf[0] = sizeof(long);
  417. if (write(output_fd, buf, 1) != 1)
  418. return -1;
  419. /* save page_size */
  420. if (write(output_fd, &page_size, 4) != 4)
  421. return -1;
  422. return 0;
  423. }
  424. struct tracing_data *tracing_data_get(struct list_head *pattrs,
  425. int fd, bool temp)
  426. {
  427. struct tracepoint_path *tps;
  428. struct tracing_data *tdata;
  429. int err;
  430. output_fd = fd;
  431. tps = get_tracepoints_path(pattrs);
  432. if (!tps)
  433. return NULL;
  434. tdata = malloc(sizeof(*tdata));
  435. if (!tdata)
  436. return NULL;
  437. tdata->temp = temp;
  438. tdata->size = 0;
  439. if (temp) {
  440. int temp_fd;
  441. snprintf(tdata->temp_file, sizeof(tdata->temp_file),
  442. "/tmp/perf-XXXXXX");
  443. if (!mkstemp(tdata->temp_file)) {
  444. pr_debug("Can't make temp file");
  445. free(tdata);
  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. free(tdata);
  452. return NULL;
  453. }
  454. /*
  455. * Set the temp file the default output, so all the
  456. * tracing data are stored into it.
  457. */
  458. output_fd = temp_fd;
  459. }
  460. err = tracing_data_header();
  461. if (err)
  462. goto out;
  463. err = record_header_files();
  464. if (err)
  465. goto out;
  466. err = record_ftrace_files(tps);
  467. if (err)
  468. goto out;
  469. err = record_event_files(tps);
  470. if (err)
  471. goto out;
  472. err = record_proc_kallsyms();
  473. if (err)
  474. goto out;
  475. err = record_ftrace_printk();
  476. if (err)
  477. goto out;
  478. err = record_saved_cmdline();
  479. out:
  480. /*
  481. * All tracing data are stored by now, we can restore
  482. * the default output file in case we used temp file.
  483. */
  484. if (temp) {
  485. tdata->size = lseek(output_fd, 0, SEEK_CUR);
  486. close(output_fd);
  487. output_fd = fd;
  488. }
  489. if (err)
  490. zfree(&tdata);
  491. put_tracepoints_path(tps);
  492. return tdata;
  493. }
  494. int tracing_data_put(struct tracing_data *tdata)
  495. {
  496. int err = 0;
  497. if (tdata->temp) {
  498. err = record_file(tdata->temp_file, 0);
  499. unlink(tdata->temp_file);
  500. }
  501. free(tdata);
  502. return err;
  503. }
  504. int read_tracing_data(int fd, struct list_head *pattrs)
  505. {
  506. int err;
  507. struct tracing_data *tdata;
  508. /*
  509. * We work over the real file, so we can write data
  510. * directly, no temp file is needed.
  511. */
  512. tdata = tracing_data_get(pattrs, fd, false);
  513. if (!tdata)
  514. return -ENOMEM;
  515. err = tracing_data_put(tdata);
  516. return err;
  517. }