trace-event-info.c 11 KB

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