trace-event-info.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. #define _GNU_SOURCE
  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 <ctype.h>
  35. #include <errno.h>
  36. #include <stdbool.h>
  37. #include <linux/kernel.h>
  38. #include "../perf.h"
  39. #include "trace-event.h"
  40. #include "debugfs.h"
  41. #define VERSION "0.5"
  42. #define _STR(x) #x
  43. #define STR(x) _STR(x)
  44. #define MAX_PATH 256
  45. #define TRACE_CTRL "tracing_on"
  46. #define TRACE "trace"
  47. #define AVAILABLE "available_tracers"
  48. #define CURRENT "current_tracer"
  49. #define ITER_CTRL "trace_options"
  50. #define MAX_LATENCY "tracing_max_latency"
  51. unsigned int page_size;
  52. static const char *output_file = "trace.info";
  53. static int output_fd;
  54. struct event_list {
  55. struct event_list *next;
  56. const char *event;
  57. };
  58. struct events {
  59. struct events *sibling;
  60. struct events *children;
  61. struct events *next;
  62. char *name;
  63. };
  64. static void die(const char *fmt, ...)
  65. {
  66. va_list ap;
  67. int ret = errno;
  68. if (errno)
  69. perror("trace-cmd");
  70. else
  71. ret = -1;
  72. va_start(ap, fmt);
  73. fprintf(stderr, " ");
  74. vfprintf(stderr, fmt, ap);
  75. va_end(ap);
  76. fprintf(stderr, "\n");
  77. exit(ret);
  78. }
  79. void *malloc_or_die(unsigned int size)
  80. {
  81. void *data;
  82. data = malloc(size);
  83. if (!data)
  84. die("malloc");
  85. return data;
  86. }
  87. static const char *find_debugfs(void)
  88. {
  89. const char *path = debugfs_mount(NULL);
  90. if (!path)
  91. die("Your kernel not support debugfs filesystem");
  92. return path;
  93. }
  94. /*
  95. * Finds the path to the debugfs/tracing
  96. * Allocates the string and stores it.
  97. */
  98. static const char *find_tracing_dir(void)
  99. {
  100. static char *tracing;
  101. static int tracing_found;
  102. const char *debugfs;
  103. if (tracing_found)
  104. return tracing;
  105. debugfs = find_debugfs();
  106. tracing = malloc_or_die(strlen(debugfs) + 9);
  107. sprintf(tracing, "%s/tracing", debugfs);
  108. tracing_found = 1;
  109. return tracing;
  110. }
  111. static char *get_tracing_file(const char *name)
  112. {
  113. const char *tracing;
  114. char *file;
  115. tracing = find_tracing_dir();
  116. if (!tracing)
  117. return NULL;
  118. file = malloc_or_die(strlen(tracing) + strlen(name) + 2);
  119. sprintf(file, "%s/%s", tracing, name);
  120. return file;
  121. }
  122. static void put_tracing_file(char *file)
  123. {
  124. free(file);
  125. }
  126. static ssize_t write_or_die(const void *buf, size_t len)
  127. {
  128. int ret;
  129. ret = write(output_fd, buf, len);
  130. if (ret < 0)
  131. die("writing to '%s'", output_file);
  132. return ret;
  133. }
  134. int bigendian(void)
  135. {
  136. unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
  137. unsigned int *ptr;
  138. ptr = (unsigned int *)(void *)str;
  139. return *ptr == 0x01020304;
  140. }
  141. static unsigned long long copy_file_fd(int fd)
  142. {
  143. unsigned long long size = 0;
  144. char buf[BUFSIZ];
  145. int r;
  146. do {
  147. r = read(fd, buf, BUFSIZ);
  148. if (r > 0) {
  149. size += r;
  150. write_or_die(buf, r);
  151. }
  152. } while (r > 0);
  153. return size;
  154. }
  155. static unsigned long long copy_file(const char *file)
  156. {
  157. unsigned long long size = 0;
  158. int fd;
  159. fd = open(file, O_RDONLY);
  160. if (fd < 0)
  161. die("Can't read '%s'", file);
  162. size = copy_file_fd(fd);
  163. close(fd);
  164. return size;
  165. }
  166. static unsigned long get_size_fd(int fd)
  167. {
  168. unsigned long long size = 0;
  169. char buf[BUFSIZ];
  170. int r;
  171. do {
  172. r = read(fd, buf, BUFSIZ);
  173. if (r > 0)
  174. size += r;
  175. } while (r > 0);
  176. lseek(fd, 0, SEEK_SET);
  177. return size;
  178. }
  179. static unsigned long get_size(const char *file)
  180. {
  181. unsigned long long size = 0;
  182. int fd;
  183. fd = open(file, O_RDONLY);
  184. if (fd < 0)
  185. die("Can't read '%s'", file);
  186. size = get_size_fd(fd);
  187. close(fd);
  188. return size;
  189. }
  190. static void read_header_files(void)
  191. {
  192. unsigned long long size, check_size;
  193. char *path;
  194. int fd;
  195. path = get_tracing_file("events/header_page");
  196. fd = open(path, O_RDONLY);
  197. if (fd < 0)
  198. die("can't read '%s'", path);
  199. /* unfortunately, you can not stat debugfs files for size */
  200. size = get_size_fd(fd);
  201. write_or_die("header_page", 12);
  202. write_or_die(&size, 8);
  203. check_size = copy_file_fd(fd);
  204. close(fd);
  205. if (size != check_size)
  206. die("wrong size for '%s' size=%lld read=%lld",
  207. path, size, check_size);
  208. put_tracing_file(path);
  209. path = get_tracing_file("events/header_event");
  210. fd = open(path, O_RDONLY);
  211. if (fd < 0)
  212. die("can't read '%s'", path);
  213. size = get_size_fd(fd);
  214. write_or_die("header_event", 13);
  215. write_or_die(&size, 8);
  216. check_size = copy_file_fd(fd);
  217. if (size != check_size)
  218. die("wrong size for '%s'", path);
  219. put_tracing_file(path);
  220. close(fd);
  221. }
  222. static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
  223. {
  224. while (tps) {
  225. if (!strcmp(sys, tps->name))
  226. return true;
  227. tps = tps->next;
  228. }
  229. return false;
  230. }
  231. static void copy_event_system(const char *sys, struct tracepoint_path *tps)
  232. {
  233. unsigned long long size, check_size;
  234. struct dirent *dent;
  235. struct stat st;
  236. char *format;
  237. DIR *dir;
  238. int count = 0;
  239. int ret;
  240. dir = opendir(sys);
  241. if (!dir)
  242. die("can't read directory '%s'", sys);
  243. while ((dent = readdir(dir))) {
  244. if (dent->d_type != DT_DIR ||
  245. strcmp(dent->d_name, ".") == 0 ||
  246. strcmp(dent->d_name, "..") == 0 ||
  247. !name_in_tp_list(dent->d_name, tps))
  248. continue;
  249. format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
  250. sprintf(format, "%s/%s/format", sys, dent->d_name);
  251. ret = stat(format, &st);
  252. free(format);
  253. if (ret < 0)
  254. continue;
  255. count++;
  256. }
  257. write_or_die(&count, 4);
  258. rewinddir(dir);
  259. while ((dent = readdir(dir))) {
  260. if (dent->d_type != DT_DIR ||
  261. strcmp(dent->d_name, ".") == 0 ||
  262. strcmp(dent->d_name, "..") == 0 ||
  263. !name_in_tp_list(dent->d_name, tps))
  264. continue;
  265. format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
  266. sprintf(format, "%s/%s/format", sys, dent->d_name);
  267. ret = stat(format, &st);
  268. if (ret >= 0) {
  269. /* unfortunately, you can not stat debugfs files for size */
  270. size = get_size(format);
  271. write_or_die(&size, 8);
  272. check_size = copy_file(format);
  273. if (size != check_size)
  274. die("error in size of file '%s'", format);
  275. }
  276. free(format);
  277. }
  278. closedir(dir);
  279. }
  280. static void read_ftrace_files(struct tracepoint_path *tps)
  281. {
  282. char *path;
  283. path = get_tracing_file("events/ftrace");
  284. copy_event_system(path, tps);
  285. put_tracing_file(path);
  286. }
  287. static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
  288. {
  289. while (tps) {
  290. if (!strcmp(sys, tps->system))
  291. return true;
  292. tps = tps->next;
  293. }
  294. return false;
  295. }
  296. static void read_event_files(struct tracepoint_path *tps)
  297. {
  298. struct dirent *dent;
  299. struct stat st;
  300. char *path;
  301. char *sys;
  302. DIR *dir;
  303. int count = 0;
  304. int ret;
  305. path = get_tracing_file("events");
  306. dir = opendir(path);
  307. if (!dir)
  308. die("can't read directory '%s'", path);
  309. while ((dent = readdir(dir))) {
  310. if (dent->d_type != DT_DIR ||
  311. strcmp(dent->d_name, ".") == 0 ||
  312. strcmp(dent->d_name, "..") == 0 ||
  313. strcmp(dent->d_name, "ftrace") == 0 ||
  314. !system_in_tp_list(dent->d_name, tps))
  315. continue;
  316. count++;
  317. }
  318. write_or_die(&count, 4);
  319. rewinddir(dir);
  320. while ((dent = readdir(dir))) {
  321. if (dent->d_type != DT_DIR ||
  322. strcmp(dent->d_name, ".") == 0 ||
  323. strcmp(dent->d_name, "..") == 0 ||
  324. strcmp(dent->d_name, "ftrace") == 0 ||
  325. !system_in_tp_list(dent->d_name, tps))
  326. continue;
  327. sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
  328. sprintf(sys, "%s/%s", path, dent->d_name);
  329. ret = stat(sys, &st);
  330. if (ret >= 0) {
  331. write_or_die(dent->d_name, strlen(dent->d_name) + 1);
  332. copy_event_system(sys, tps);
  333. }
  334. free(sys);
  335. }
  336. closedir(dir);
  337. put_tracing_file(path);
  338. }
  339. static void read_proc_kallsyms(void)
  340. {
  341. unsigned int size, check_size;
  342. const char *path = "/proc/kallsyms";
  343. struct stat st;
  344. int ret;
  345. ret = stat(path, &st);
  346. if (ret < 0) {
  347. /* not found */
  348. size = 0;
  349. write_or_die(&size, 4);
  350. return;
  351. }
  352. size = get_size(path);
  353. write_or_die(&size, 4);
  354. check_size = copy_file(path);
  355. if (size != check_size)
  356. die("error in size of file '%s'", path);
  357. }
  358. static void read_ftrace_printk(void)
  359. {
  360. unsigned int size, check_size;
  361. char *path;
  362. struct stat st;
  363. int ret;
  364. path = get_tracing_file("printk_formats");
  365. ret = stat(path, &st);
  366. if (ret < 0) {
  367. /* not found */
  368. size = 0;
  369. write_or_die(&size, 4);
  370. goto out;
  371. }
  372. size = get_size(path);
  373. write_or_die(&size, 4);
  374. check_size = copy_file(path);
  375. if (size != check_size)
  376. die("error in size of file '%s'", path);
  377. out:
  378. put_tracing_file(path);
  379. }
  380. static struct tracepoint_path *
  381. get_tracepoints_path(struct perf_event_attr *pattrs, int nb_events)
  382. {
  383. struct tracepoint_path path, *ppath = &path;
  384. int i, nr_tracepoints = 0;
  385. for (i = 0; i < nb_events; i++) {
  386. if (pattrs[i].type != PERF_TYPE_TRACEPOINT)
  387. continue;
  388. ++nr_tracepoints;
  389. ppath->next = tracepoint_id_to_path(pattrs[i].config);
  390. if (!ppath->next)
  391. die("%s\n", "No memory to alloc tracepoints list");
  392. ppath = ppath->next;
  393. }
  394. return nr_tracepoints > 0 ? path.next : NULL;
  395. }
  396. int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events)
  397. {
  398. char buf[BUFSIZ];
  399. struct tracepoint_path *tps = get_tracepoints_path(pattrs, nb_events);
  400. /*
  401. * What? No tracepoints? No sense writing anything here, bail out.
  402. */
  403. if (tps == NULL)
  404. return -1;
  405. output_fd = fd;
  406. buf[0] = 23;
  407. buf[1] = 8;
  408. buf[2] = 68;
  409. memcpy(buf + 3, "tracing", 7);
  410. write_or_die(buf, 10);
  411. write_or_die(VERSION, strlen(VERSION) + 1);
  412. /* save endian */
  413. if (bigendian())
  414. buf[0] = 1;
  415. else
  416. buf[0] = 0;
  417. write_or_die(buf, 1);
  418. /* save size of long */
  419. buf[0] = sizeof(long);
  420. write_or_die(buf, 1);
  421. /* save page_size */
  422. page_size = sysconf(_SC_PAGESIZE);
  423. write_or_die(&page_size, 4);
  424. read_header_files();
  425. read_ftrace_files(tps);
  426. read_event_files(tps);
  427. read_proc_kallsyms();
  428. read_ftrace_printk();
  429. return 0;
  430. }