trace-event-info.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. /* unfortunately, you can not stat debugfs or proc files for size */
  149. static void record_file(const char *file, size_t hdr_sz)
  150. {
  151. unsigned long long size = 0;
  152. char buf[BUFSIZ], *sizep;
  153. off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
  154. int r, fd;
  155. fd = open(file, O_RDONLY);
  156. if (fd < 0)
  157. die("Can't read '%s'", file);
  158. /* put in zeros for file size, then fill true size later */
  159. write_or_die(&size, hdr_sz);
  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. close(fd);
  168. /* ugh, handle big-endian hdr_size == 4 */
  169. sizep = (char*)&size;
  170. if (bigendian())
  171. sizep += sizeof(u64) - hdr_sz;
  172. if (pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
  173. die("writing to %s", output_file);
  174. }
  175. static void read_header_files(void)
  176. {
  177. char *path;
  178. struct stat st;
  179. path = get_tracing_file("events/header_page");
  180. if (stat(path, &st) < 0)
  181. die("can't read '%s'", path);
  182. write_or_die("header_page", 12);
  183. record_file(path, 8);
  184. put_tracing_file(path);
  185. path = get_tracing_file("events/header_event");
  186. if (stat(path, &st) < 0)
  187. die("can't read '%s'", path);
  188. write_or_die("header_event", 13);
  189. record_file(path, 8);
  190. put_tracing_file(path);
  191. }
  192. static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
  193. {
  194. while (tps) {
  195. if (!strcmp(sys, tps->name))
  196. return true;
  197. tps = tps->next;
  198. }
  199. return false;
  200. }
  201. static void copy_event_system(const char *sys, struct tracepoint_path *tps)
  202. {
  203. struct dirent *dent;
  204. struct stat st;
  205. char *format;
  206. DIR *dir;
  207. int count = 0;
  208. int ret;
  209. dir = opendir(sys);
  210. if (!dir)
  211. die("can't read directory '%s'", sys);
  212. while ((dent = readdir(dir))) {
  213. if (dent->d_type != DT_DIR ||
  214. strcmp(dent->d_name, ".") == 0 ||
  215. strcmp(dent->d_name, "..") == 0 ||
  216. !name_in_tp_list(dent->d_name, tps))
  217. continue;
  218. format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
  219. sprintf(format, "%s/%s/format", sys, dent->d_name);
  220. ret = stat(format, &st);
  221. free(format);
  222. if (ret < 0)
  223. continue;
  224. count++;
  225. }
  226. write_or_die(&count, 4);
  227. rewinddir(dir);
  228. while ((dent = readdir(dir))) {
  229. if (dent->d_type != DT_DIR ||
  230. strcmp(dent->d_name, ".") == 0 ||
  231. strcmp(dent->d_name, "..") == 0 ||
  232. !name_in_tp_list(dent->d_name, tps))
  233. continue;
  234. format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
  235. sprintf(format, "%s/%s/format", sys, dent->d_name);
  236. ret = stat(format, &st);
  237. if (ret >= 0)
  238. record_file(format, 8);
  239. free(format);
  240. }
  241. closedir(dir);
  242. }
  243. static void read_ftrace_files(struct tracepoint_path *tps)
  244. {
  245. char *path;
  246. path = get_tracing_file("events/ftrace");
  247. copy_event_system(path, tps);
  248. put_tracing_file(path);
  249. }
  250. static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
  251. {
  252. while (tps) {
  253. if (!strcmp(sys, tps->system))
  254. return true;
  255. tps = tps->next;
  256. }
  257. return false;
  258. }
  259. static void read_event_files(struct tracepoint_path *tps)
  260. {
  261. struct dirent *dent;
  262. struct stat st;
  263. char *path;
  264. char *sys;
  265. DIR *dir;
  266. int count = 0;
  267. int ret;
  268. path = get_tracing_file("events");
  269. dir = opendir(path);
  270. if (!dir)
  271. die("can't read directory '%s'", path);
  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. strcmp(dent->d_name, "ftrace") == 0 ||
  277. !system_in_tp_list(dent->d_name, tps))
  278. continue;
  279. count++;
  280. }
  281. write_or_die(&count, 4);
  282. rewinddir(dir);
  283. while ((dent = readdir(dir))) {
  284. if (dent->d_type != DT_DIR ||
  285. strcmp(dent->d_name, ".") == 0 ||
  286. strcmp(dent->d_name, "..") == 0 ||
  287. strcmp(dent->d_name, "ftrace") == 0 ||
  288. !system_in_tp_list(dent->d_name, tps))
  289. continue;
  290. sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
  291. sprintf(sys, "%s/%s", path, dent->d_name);
  292. ret = stat(sys, &st);
  293. if (ret >= 0) {
  294. write_or_die(dent->d_name, strlen(dent->d_name) + 1);
  295. copy_event_system(sys, tps);
  296. }
  297. free(sys);
  298. }
  299. closedir(dir);
  300. put_tracing_file(path);
  301. }
  302. static void read_proc_kallsyms(void)
  303. {
  304. unsigned int size;
  305. const char *path = "/proc/kallsyms";
  306. struct stat st;
  307. int ret;
  308. ret = stat(path, &st);
  309. if (ret < 0) {
  310. /* not found */
  311. size = 0;
  312. write_or_die(&size, 4);
  313. return;
  314. }
  315. record_file(path, 4);
  316. }
  317. static void read_ftrace_printk(void)
  318. {
  319. unsigned int size;
  320. char *path;
  321. struct stat st;
  322. int ret;
  323. path = get_tracing_file("printk_formats");
  324. ret = stat(path, &st);
  325. if (ret < 0) {
  326. /* not found */
  327. size = 0;
  328. write_or_die(&size, 4);
  329. goto out;
  330. }
  331. record_file(path, 4);
  332. out:
  333. put_tracing_file(path);
  334. }
  335. static struct tracepoint_path *
  336. get_tracepoints_path(struct list_head *pattrs)
  337. {
  338. struct tracepoint_path path, *ppath = &path;
  339. struct perf_evsel *pos;
  340. int nr_tracepoints = 0;
  341. list_for_each_entry(pos, pattrs, node) {
  342. if (pos->attr.type != PERF_TYPE_TRACEPOINT)
  343. continue;
  344. ++nr_tracepoints;
  345. ppath->next = tracepoint_id_to_path(pos->attr.config);
  346. if (!ppath->next)
  347. die("%s\n", "No memory to alloc tracepoints list");
  348. ppath = ppath->next;
  349. }
  350. return nr_tracepoints > 0 ? path.next : NULL;
  351. }
  352. bool have_tracepoints(struct list_head *pattrs)
  353. {
  354. struct perf_evsel *pos;
  355. list_for_each_entry(pos, pattrs, node)
  356. if (pos->attr.type == PERF_TYPE_TRACEPOINT)
  357. return true;
  358. return false;
  359. }
  360. int read_tracing_data(int fd, struct list_head *pattrs)
  361. {
  362. char buf[BUFSIZ];
  363. struct tracepoint_path *tps = get_tracepoints_path(pattrs);
  364. /*
  365. * What? No tracepoints? No sense writing anything here, bail out.
  366. */
  367. if (tps == NULL)
  368. return -1;
  369. output_fd = fd;
  370. buf[0] = 23;
  371. buf[1] = 8;
  372. buf[2] = 68;
  373. memcpy(buf + 3, "tracing", 7);
  374. write_or_die(buf, 10);
  375. write_or_die(VERSION, strlen(VERSION) + 1);
  376. /* save endian */
  377. if (bigendian())
  378. buf[0] = 1;
  379. else
  380. buf[0] = 0;
  381. write_or_die(buf, 1);
  382. /* save size of long */
  383. buf[0] = sizeof(long);
  384. write_or_die(buf, 1);
  385. /* save page_size */
  386. page_size = sysconf(_SC_PAGESIZE);
  387. write_or_die(&page_size, 4);
  388. read_header_files();
  389. read_ftrace_files(tps);
  390. read_event_files(tps);
  391. read_proc_kallsyms();
  392. read_ftrace_printk();
  393. return 0;
  394. }
  395. ssize_t read_tracing_data_size(int fd, struct list_head *pattrs)
  396. {
  397. ssize_t size;
  398. int err = 0;
  399. calc_data_size = 1;
  400. err = read_tracing_data(fd, pattrs);
  401. size = calc_data_size - 1;
  402. calc_data_size = 0;
  403. if (err < 0)
  404. return err;
  405. return size;
  406. }