trace-event-info.c 11 KB

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