trace-event-info.c 11 KB

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