trace-event-info.c 9.4 KB

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