trace-event-info.c 11 KB

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