trace-event-info.c 11 KB

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