trace-event-info.c 12 KB

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