trace-event-info.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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/debugfs.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 int size;
  297. const char *path = "/proc/kallsyms";
  298. struct stat st;
  299. int ret, err = 0;
  300. ret = stat(path, &st);
  301. if (ret < 0) {
  302. /* not found */
  303. size = 0;
  304. if (write(output_fd, &size, 4) != 4)
  305. err = -EIO;
  306. return err;
  307. }
  308. return record_file(path, 4);
  309. }
  310. static int record_ftrace_printk(void)
  311. {
  312. unsigned int size;
  313. char *path;
  314. struct stat st;
  315. int ret, err = 0;
  316. path = get_tracing_file("printk_formats");
  317. if (!path) {
  318. pr_debug("can't get tracing/printk_formats");
  319. return -ENOMEM;
  320. }
  321. ret = stat(path, &st);
  322. if (ret < 0) {
  323. /* not found */
  324. size = 0;
  325. if (write(output_fd, &size, 4) != 4)
  326. err = -EIO;
  327. goto out;
  328. }
  329. err = record_file(path, 4);
  330. out:
  331. put_tracing_file(path);
  332. return err;
  333. }
  334. static void
  335. put_tracepoints_path(struct tracepoint_path *tps)
  336. {
  337. while (tps) {
  338. struct tracepoint_path *t = tps;
  339. tps = tps->next;
  340. zfree(&t->name);
  341. zfree(&t->system);
  342. free(t);
  343. }
  344. }
  345. static struct tracepoint_path *
  346. get_tracepoints_path(struct list_head *pattrs)
  347. {
  348. struct tracepoint_path path, *ppath = &path;
  349. struct perf_evsel *pos;
  350. int nr_tracepoints = 0;
  351. list_for_each_entry(pos, pattrs, node) {
  352. if (pos->attr.type != PERF_TYPE_TRACEPOINT)
  353. continue;
  354. ++nr_tracepoints;
  355. if (pos->name) {
  356. ppath->next = tracepoint_name_to_path(pos->name);
  357. if (ppath->next)
  358. goto next;
  359. if (strchr(pos->name, ':') == NULL)
  360. goto try_id;
  361. goto error;
  362. }
  363. try_id:
  364. ppath->next = tracepoint_id_to_path(pos->attr.config);
  365. if (!ppath->next) {
  366. error:
  367. pr_debug("No memory to alloc tracepoints list\n");
  368. put_tracepoints_path(&path);
  369. return NULL;
  370. }
  371. next:
  372. ppath = ppath->next;
  373. }
  374. return nr_tracepoints > 0 ? path.next : NULL;
  375. }
  376. bool have_tracepoints(struct list_head *pattrs)
  377. {
  378. struct perf_evsel *pos;
  379. list_for_each_entry(pos, pattrs, node)
  380. if (pos->attr.type == PERF_TYPE_TRACEPOINT)
  381. return true;
  382. return false;
  383. }
  384. static int tracing_data_header(void)
  385. {
  386. char buf[20];
  387. ssize_t size;
  388. /* just guessing this is someone's birthday.. ;) */
  389. buf[0] = 23;
  390. buf[1] = 8;
  391. buf[2] = 68;
  392. memcpy(buf + 3, "tracing", 7);
  393. if (write(output_fd, buf, 10) != 10)
  394. return -1;
  395. size = strlen(VERSION) + 1;
  396. if (write(output_fd, VERSION, size) != size)
  397. return -1;
  398. /* save endian */
  399. if (bigendian())
  400. buf[0] = 1;
  401. else
  402. buf[0] = 0;
  403. if (write(output_fd, buf, 1) != 1)
  404. return -1;
  405. /* save size of long */
  406. buf[0] = sizeof(long);
  407. if (write(output_fd, buf, 1) != 1)
  408. return -1;
  409. /* save page_size */
  410. if (write(output_fd, &page_size, 4) != 4)
  411. return -1;
  412. return 0;
  413. }
  414. struct tracing_data *tracing_data_get(struct list_head *pattrs,
  415. int fd, bool temp)
  416. {
  417. struct tracepoint_path *tps;
  418. struct tracing_data *tdata;
  419. int err;
  420. output_fd = fd;
  421. tps = get_tracepoints_path(pattrs);
  422. if (!tps)
  423. return NULL;
  424. tdata = malloc(sizeof(*tdata));
  425. if (!tdata)
  426. return NULL;
  427. tdata->temp = temp;
  428. tdata->size = 0;
  429. if (temp) {
  430. int temp_fd;
  431. snprintf(tdata->temp_file, sizeof(tdata->temp_file),
  432. "/tmp/perf-XXXXXX");
  433. if (!mkstemp(tdata->temp_file)) {
  434. pr_debug("Can't make temp file");
  435. return NULL;
  436. }
  437. temp_fd = open(tdata->temp_file, O_RDWR);
  438. if (temp_fd < 0) {
  439. pr_debug("Can't read '%s'", tdata->temp_file);
  440. return NULL;
  441. }
  442. /*
  443. * Set the temp file the default output, so all the
  444. * tracing data are stored into it.
  445. */
  446. output_fd = temp_fd;
  447. }
  448. err = tracing_data_header();
  449. if (err)
  450. goto out;
  451. err = record_header_files();
  452. if (err)
  453. goto out;
  454. err = record_ftrace_files(tps);
  455. if (err)
  456. goto out;
  457. err = record_event_files(tps);
  458. if (err)
  459. goto out;
  460. err = record_proc_kallsyms();
  461. if (err)
  462. goto out;
  463. err = record_ftrace_printk();
  464. out:
  465. /*
  466. * All tracing data are stored by now, we can restore
  467. * the default output file in case we used temp file.
  468. */
  469. if (temp) {
  470. tdata->size = lseek(output_fd, 0, SEEK_CUR);
  471. close(output_fd);
  472. output_fd = fd;
  473. }
  474. if (err)
  475. zfree(&tdata);
  476. put_tracepoints_path(tps);
  477. return tdata;
  478. }
  479. int tracing_data_put(struct tracing_data *tdata)
  480. {
  481. int err = 0;
  482. if (tdata->temp) {
  483. err = record_file(tdata->temp_file, 0);
  484. unlink(tdata->temp_file);
  485. }
  486. free(tdata);
  487. return err;
  488. }
  489. int read_tracing_data(int fd, struct list_head *pattrs)
  490. {
  491. int err;
  492. struct tracing_data *tdata;
  493. /*
  494. * We work over the real file, so we can write data
  495. * directly, no temp file is needed.
  496. */
  497. tdata = tracing_data_get(pattrs, fd, false);
  498. if (!tdata)
  499. return -ENOMEM;
  500. err = tracing_data_put(tdata);
  501. return err;
  502. }