trace-event-info.c 11 KB

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