trace-event-info.c 11 KB

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