trace-event-info.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 calc_data_size;
  127. static ssize_t write_or_die(const void *buf, size_t len)
  128. {
  129. int ret;
  130. if (calc_data_size) {
  131. calc_data_size += len;
  132. return len;
  133. }
  134. ret = write(output_fd, buf, len);
  135. if (ret < 0)
  136. die("writing to '%s'", output_file);
  137. return ret;
  138. }
  139. int bigendian(void)
  140. {
  141. unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
  142. unsigned int *ptr;
  143. ptr = (unsigned int *)(void *)str;
  144. return *ptr == 0x01020304;
  145. }
  146. static unsigned long long copy_file_fd(int fd)
  147. {
  148. unsigned long long size = 0;
  149. char buf[BUFSIZ];
  150. int r;
  151. do {
  152. r = read(fd, buf, BUFSIZ);
  153. if (r > 0) {
  154. size += r;
  155. write_or_die(buf, r);
  156. }
  157. } while (r > 0);
  158. return size;
  159. }
  160. static unsigned long long copy_file(const char *file)
  161. {
  162. unsigned long long size = 0;
  163. int fd;
  164. fd = open(file, O_RDONLY);
  165. if (fd < 0)
  166. die("Can't read '%s'", file);
  167. size = copy_file_fd(fd);
  168. close(fd);
  169. return size;
  170. }
  171. static unsigned long get_size_fd(int fd)
  172. {
  173. unsigned long long size = 0;
  174. char buf[BUFSIZ];
  175. int r;
  176. do {
  177. r = read(fd, buf, BUFSIZ);
  178. if (r > 0)
  179. size += r;
  180. } while (r > 0);
  181. lseek(fd, 0, SEEK_SET);
  182. return size;
  183. }
  184. static unsigned long get_size(const char *file)
  185. {
  186. unsigned long long size = 0;
  187. int fd;
  188. fd = open(file, O_RDONLY);
  189. if (fd < 0)
  190. die("Can't read '%s'", file);
  191. size = get_size_fd(fd);
  192. close(fd);
  193. return size;
  194. }
  195. static void read_header_files(void)
  196. {
  197. unsigned long long size, check_size;
  198. char *path;
  199. int fd;
  200. path = get_tracing_file("events/header_page");
  201. fd = open(path, O_RDONLY);
  202. if (fd < 0)
  203. die("can't read '%s'", path);
  204. /* unfortunately, you can not stat debugfs files for size */
  205. size = get_size_fd(fd);
  206. write_or_die("header_page", 12);
  207. write_or_die(&size, 8);
  208. check_size = copy_file_fd(fd);
  209. close(fd);
  210. if (size != check_size)
  211. die("wrong size for '%s' size=%lld read=%lld",
  212. path, size, check_size);
  213. put_tracing_file(path);
  214. path = get_tracing_file("events/header_event");
  215. fd = open(path, O_RDONLY);
  216. if (fd < 0)
  217. die("can't read '%s'", path);
  218. size = get_size_fd(fd);
  219. write_or_die("header_event", 13);
  220. write_or_die(&size, 8);
  221. check_size = copy_file_fd(fd);
  222. if (size != check_size)
  223. die("wrong size for '%s'", path);
  224. put_tracing_file(path);
  225. close(fd);
  226. }
  227. static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
  228. {
  229. while (tps) {
  230. if (!strcmp(sys, tps->name))
  231. return true;
  232. tps = tps->next;
  233. }
  234. return false;
  235. }
  236. static void copy_event_system(const char *sys, struct tracepoint_path *tps)
  237. {
  238. unsigned long long size, check_size;
  239. struct dirent *dent;
  240. struct stat st;
  241. char *format;
  242. DIR *dir;
  243. int count = 0;
  244. int ret;
  245. dir = opendir(sys);
  246. if (!dir)
  247. die("can't read directory '%s'", sys);
  248. while ((dent = readdir(dir))) {
  249. if (dent->d_type != DT_DIR ||
  250. strcmp(dent->d_name, ".") == 0 ||
  251. strcmp(dent->d_name, "..") == 0 ||
  252. !name_in_tp_list(dent->d_name, tps))
  253. continue;
  254. format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
  255. sprintf(format, "%s/%s/format", sys, dent->d_name);
  256. ret = stat(format, &st);
  257. free(format);
  258. if (ret < 0)
  259. continue;
  260. count++;
  261. }
  262. write_or_die(&count, 4);
  263. rewinddir(dir);
  264. while ((dent = readdir(dir))) {
  265. if (dent->d_type != DT_DIR ||
  266. strcmp(dent->d_name, ".") == 0 ||
  267. strcmp(dent->d_name, "..") == 0 ||
  268. !name_in_tp_list(dent->d_name, tps))
  269. continue;
  270. format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
  271. sprintf(format, "%s/%s/format", sys, dent->d_name);
  272. ret = stat(format, &st);
  273. if (ret >= 0) {
  274. /* unfortunately, you can not stat debugfs files for size */
  275. size = get_size(format);
  276. write_or_die(&size, 8);
  277. check_size = copy_file(format);
  278. if (size != check_size)
  279. die("error in size of file '%s'", format);
  280. }
  281. free(format);
  282. }
  283. closedir(dir);
  284. }
  285. static void read_ftrace_files(struct tracepoint_path *tps)
  286. {
  287. char *path;
  288. path = get_tracing_file("events/ftrace");
  289. copy_event_system(path, tps);
  290. put_tracing_file(path);
  291. }
  292. static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
  293. {
  294. while (tps) {
  295. if (!strcmp(sys, tps->system))
  296. return true;
  297. tps = tps->next;
  298. }
  299. return false;
  300. }
  301. static void read_event_files(struct tracepoint_path *tps)
  302. {
  303. struct dirent *dent;
  304. struct stat st;
  305. char *path;
  306. char *sys;
  307. DIR *dir;
  308. int count = 0;
  309. int ret;
  310. path = get_tracing_file("events");
  311. dir = opendir(path);
  312. if (!dir)
  313. die("can't read directory '%s'", path);
  314. while ((dent = readdir(dir))) {
  315. if (dent->d_type != DT_DIR ||
  316. strcmp(dent->d_name, ".") == 0 ||
  317. strcmp(dent->d_name, "..") == 0 ||
  318. strcmp(dent->d_name, "ftrace") == 0 ||
  319. !system_in_tp_list(dent->d_name, tps))
  320. continue;
  321. count++;
  322. }
  323. write_or_die(&count, 4);
  324. rewinddir(dir);
  325. while ((dent = readdir(dir))) {
  326. if (dent->d_type != DT_DIR ||
  327. strcmp(dent->d_name, ".") == 0 ||
  328. strcmp(dent->d_name, "..") == 0 ||
  329. strcmp(dent->d_name, "ftrace") == 0 ||
  330. !system_in_tp_list(dent->d_name, tps))
  331. continue;
  332. sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
  333. sprintf(sys, "%s/%s", path, dent->d_name);
  334. ret = stat(sys, &st);
  335. if (ret >= 0) {
  336. write_or_die(dent->d_name, strlen(dent->d_name) + 1);
  337. copy_event_system(sys, tps);
  338. }
  339. free(sys);
  340. }
  341. closedir(dir);
  342. put_tracing_file(path);
  343. }
  344. static void read_proc_kallsyms(void)
  345. {
  346. unsigned int size, check_size;
  347. const char *path = "/proc/kallsyms";
  348. struct stat st;
  349. int ret;
  350. ret = stat(path, &st);
  351. if (ret < 0) {
  352. /* not found */
  353. size = 0;
  354. write_or_die(&size, 4);
  355. return;
  356. }
  357. size = get_size(path);
  358. write_or_die(&size, 4);
  359. check_size = copy_file(path);
  360. if (size != check_size)
  361. die("error in size of file '%s'", path);
  362. }
  363. static void read_ftrace_printk(void)
  364. {
  365. unsigned int size, check_size;
  366. char *path;
  367. struct stat st;
  368. int ret;
  369. path = get_tracing_file("printk_formats");
  370. ret = stat(path, &st);
  371. if (ret < 0) {
  372. /* not found */
  373. size = 0;
  374. write_or_die(&size, 4);
  375. goto out;
  376. }
  377. size = get_size(path);
  378. write_or_die(&size, 4);
  379. check_size = copy_file(path);
  380. if (size != check_size)
  381. die("error in size of file '%s'", path);
  382. out:
  383. put_tracing_file(path);
  384. }
  385. static struct tracepoint_path *
  386. get_tracepoints_path(struct perf_event_attr *pattrs, int nb_events)
  387. {
  388. struct tracepoint_path path, *ppath = &path;
  389. int i, nr_tracepoints = 0;
  390. for (i = 0; i < nb_events; i++) {
  391. if (pattrs[i].type != PERF_TYPE_TRACEPOINT)
  392. continue;
  393. ++nr_tracepoints;
  394. ppath->next = tracepoint_id_to_path(pattrs[i].config);
  395. if (!ppath->next)
  396. die("%s\n", "No memory to alloc tracepoints list");
  397. ppath = ppath->next;
  398. }
  399. return nr_tracepoints > 0 ? path.next : NULL;
  400. }
  401. bool have_tracepoints(struct perf_event_attr *pattrs, int nb_events)
  402. {
  403. int i;
  404. for (i = 0; i < nb_events; i++)
  405. if (pattrs[i].type == PERF_TYPE_TRACEPOINT)
  406. return true;
  407. return false;
  408. }
  409. int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events)
  410. {
  411. char buf[BUFSIZ];
  412. struct tracepoint_path *tps = get_tracepoints_path(pattrs, nb_events);
  413. /*
  414. * What? No tracepoints? No sense writing anything here, bail out.
  415. */
  416. if (tps == NULL)
  417. return -1;
  418. output_fd = fd;
  419. buf[0] = 23;
  420. buf[1] = 8;
  421. buf[2] = 68;
  422. memcpy(buf + 3, "tracing", 7);
  423. write_or_die(buf, 10);
  424. write_or_die(VERSION, strlen(VERSION) + 1);
  425. /* save endian */
  426. if (bigendian())
  427. buf[0] = 1;
  428. else
  429. buf[0] = 0;
  430. write_or_die(buf, 1);
  431. /* save size of long */
  432. buf[0] = sizeof(long);
  433. write_or_die(buf, 1);
  434. /* save page_size */
  435. page_size = sysconf(_SC_PAGESIZE);
  436. write_or_die(&page_size, 4);
  437. read_header_files();
  438. read_ftrace_files(tps);
  439. read_event_files(tps);
  440. read_proc_kallsyms();
  441. read_ftrace_printk();
  442. return 0;
  443. }
  444. ssize_t read_tracing_data_size(int fd, struct perf_event_attr *pattrs,
  445. int nb_events)
  446. {
  447. ssize_t size;
  448. int err = 0;
  449. calc_data_size = 1;
  450. err = read_tracing_data(fd, pattrs, nb_events);
  451. size = calc_data_size - 1;
  452. calc_data_size = 0;
  453. if (err < 0)
  454. return err;
  455. return size;
  456. }