trace-event-info.c 10 KB

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