trace-event-info.c 11 KB

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