trace-event-info.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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("perf");
  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. if (hdr_sz)
  160. write_or_die(&size, hdr_sz);
  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. close(fd);
  169. /* ugh, handle big-endian hdr_size == 4 */
  170. sizep = (char*)&size;
  171. if (bigendian())
  172. sizep += sizeof(u64) - hdr_sz;
  173. if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
  174. die("writing to %s", output_file);
  175. }
  176. static void read_header_files(void)
  177. {
  178. char *path;
  179. struct stat st;
  180. path = get_tracing_file("events/header_page");
  181. if (stat(path, &st) < 0)
  182. die("can't read '%s'", path);
  183. write_or_die("header_page", 12);
  184. record_file(path, 8);
  185. put_tracing_file(path);
  186. path = get_tracing_file("events/header_event");
  187. if (stat(path, &st) < 0)
  188. die("can't read '%s'", path);
  189. write_or_die("header_event", 13);
  190. record_file(path, 8);
  191. put_tracing_file(path);
  192. }
  193. static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
  194. {
  195. while (tps) {
  196. if (!strcmp(sys, tps->name))
  197. return true;
  198. tps = tps->next;
  199. }
  200. return false;
  201. }
  202. static void copy_event_system(const char *sys, struct tracepoint_path *tps)
  203. {
  204. struct dirent *dent;
  205. struct stat st;
  206. char *format;
  207. DIR *dir;
  208. int count = 0;
  209. int ret;
  210. dir = opendir(sys);
  211. if (!dir)
  212. die("can't read directory '%s'", sys);
  213. while ((dent = readdir(dir))) {
  214. if (dent->d_type != DT_DIR ||
  215. strcmp(dent->d_name, ".") == 0 ||
  216. strcmp(dent->d_name, "..") == 0 ||
  217. !name_in_tp_list(dent->d_name, tps))
  218. continue;
  219. format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
  220. sprintf(format, "%s/%s/format", sys, dent->d_name);
  221. ret = stat(format, &st);
  222. free(format);
  223. if (ret < 0)
  224. continue;
  225. count++;
  226. }
  227. write_or_die(&count, 4);
  228. rewinddir(dir);
  229. while ((dent = readdir(dir))) {
  230. if (dent->d_type != DT_DIR ||
  231. strcmp(dent->d_name, ".") == 0 ||
  232. strcmp(dent->d_name, "..") == 0 ||
  233. !name_in_tp_list(dent->d_name, tps))
  234. continue;
  235. format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
  236. sprintf(format, "%s/%s/format", sys, dent->d_name);
  237. ret = stat(format, &st);
  238. if (ret >= 0)
  239. record_file(format, 8);
  240. free(format);
  241. }
  242. closedir(dir);
  243. }
  244. static void read_ftrace_files(struct tracepoint_path *tps)
  245. {
  246. char *path;
  247. path = get_tracing_file("events/ftrace");
  248. copy_event_system(path, tps);
  249. put_tracing_file(path);
  250. }
  251. static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
  252. {
  253. while (tps) {
  254. if (!strcmp(sys, tps->system))
  255. return true;
  256. tps = tps->next;
  257. }
  258. return false;
  259. }
  260. static void read_event_files(struct tracepoint_path *tps)
  261. {
  262. struct dirent *dent;
  263. struct stat st;
  264. char *path;
  265. char *sys;
  266. DIR *dir;
  267. int count = 0;
  268. int ret;
  269. path = get_tracing_file("events");
  270. dir = opendir(path);
  271. if (!dir)
  272. die("can't read directory '%s'", path);
  273. while ((dent = readdir(dir))) {
  274. if (dent->d_type != DT_DIR ||
  275. strcmp(dent->d_name, ".") == 0 ||
  276. strcmp(dent->d_name, "..") == 0 ||
  277. strcmp(dent->d_name, "ftrace") == 0 ||
  278. !system_in_tp_list(dent->d_name, tps))
  279. continue;
  280. count++;
  281. }
  282. write_or_die(&count, 4);
  283. rewinddir(dir);
  284. while ((dent = readdir(dir))) {
  285. if (dent->d_type != DT_DIR ||
  286. strcmp(dent->d_name, ".") == 0 ||
  287. strcmp(dent->d_name, "..") == 0 ||
  288. strcmp(dent->d_name, "ftrace") == 0 ||
  289. !system_in_tp_list(dent->d_name, tps))
  290. continue;
  291. sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
  292. sprintf(sys, "%s/%s", path, dent->d_name);
  293. ret = stat(sys, &st);
  294. if (ret >= 0) {
  295. write_or_die(dent->d_name, strlen(dent->d_name) + 1);
  296. copy_event_system(sys, tps);
  297. }
  298. free(sys);
  299. }
  300. closedir(dir);
  301. put_tracing_file(path);
  302. }
  303. static void read_proc_kallsyms(void)
  304. {
  305. unsigned int size;
  306. const char *path = "/proc/kallsyms";
  307. struct stat st;
  308. int ret;
  309. ret = stat(path, &st);
  310. if (ret < 0) {
  311. /* not found */
  312. size = 0;
  313. write_or_die(&size, 4);
  314. return;
  315. }
  316. record_file(path, 4);
  317. }
  318. static void read_ftrace_printk(void)
  319. {
  320. unsigned int size;
  321. char *path;
  322. struct stat st;
  323. int ret;
  324. path = get_tracing_file("printk_formats");
  325. ret = stat(path, &st);
  326. if (ret < 0) {
  327. /* not found */
  328. size = 0;
  329. write_or_die(&size, 4);
  330. goto out;
  331. }
  332. record_file(path, 4);
  333. out:
  334. put_tracing_file(path);
  335. }
  336. static struct tracepoint_path *
  337. get_tracepoints_path(struct list_head *pattrs)
  338. {
  339. struct tracepoint_path path, *ppath = &path;
  340. struct perf_evsel *pos;
  341. int nr_tracepoints = 0;
  342. list_for_each_entry(pos, pattrs, node) {
  343. if (pos->attr.type != PERF_TYPE_TRACEPOINT)
  344. continue;
  345. ++nr_tracepoints;
  346. ppath->next = tracepoint_id_to_path(pos->attr.config);
  347. if (!ppath->next)
  348. die("%s\n", "No memory to alloc tracepoints list");
  349. ppath = ppath->next;
  350. }
  351. return nr_tracepoints > 0 ? path.next : NULL;
  352. }
  353. static void
  354. put_tracepoints_path(struct tracepoint_path *tps)
  355. {
  356. while (tps) {
  357. struct tracepoint_path *t = tps;
  358. tps = tps->next;
  359. free(t->name);
  360. free(t->system);
  361. free(t);
  362. }
  363. }
  364. bool have_tracepoints(struct list_head *pattrs)
  365. {
  366. struct perf_evsel *pos;
  367. list_for_each_entry(pos, pattrs, node)
  368. if (pos->attr.type == PERF_TYPE_TRACEPOINT)
  369. return true;
  370. return false;
  371. }
  372. static void tracing_data_header(void)
  373. {
  374. char buf[20];
  375. /* just guessing this is someone's birthday.. ;) */
  376. buf[0] = 23;
  377. buf[1] = 8;
  378. buf[2] = 68;
  379. memcpy(buf + 3, "tracing", 7);
  380. write_or_die(buf, 10);
  381. write_or_die(VERSION, strlen(VERSION) + 1);
  382. /* save endian */
  383. if (bigendian())
  384. buf[0] = 1;
  385. else
  386. buf[0] = 0;
  387. write_or_die(buf, 1);
  388. /* save size of long */
  389. buf[0] = sizeof(long);
  390. write_or_die(buf, 1);
  391. /* save page_size */
  392. page_size = sysconf(_SC_PAGESIZE);
  393. write_or_die(&page_size, 4);
  394. }
  395. struct tracing_data *tracing_data_get(struct list_head *pattrs,
  396. int fd, bool temp)
  397. {
  398. struct tracepoint_path *tps;
  399. struct tracing_data *tdata;
  400. output_fd = fd;
  401. tps = get_tracepoints_path(pattrs);
  402. if (!tps)
  403. return NULL;
  404. tdata = malloc_or_die(sizeof(*tdata));
  405. tdata->temp = temp;
  406. tdata->size = 0;
  407. if (temp) {
  408. int temp_fd;
  409. snprintf(tdata->temp_file, sizeof(tdata->temp_file),
  410. "/tmp/perf-XXXXXX");
  411. if (!mkstemp(tdata->temp_file))
  412. die("Can't make temp file");
  413. temp_fd = open(tdata->temp_file, O_RDWR);
  414. if (temp_fd < 0)
  415. die("Can't read '%s'", tdata->temp_file);
  416. /*
  417. * Set the temp file the default output, so all the
  418. * tracing data are stored into it.
  419. */
  420. output_fd = temp_fd;
  421. }
  422. tracing_data_header();
  423. read_header_files();
  424. read_ftrace_files(tps);
  425. read_event_files(tps);
  426. read_proc_kallsyms();
  427. read_ftrace_printk();
  428. /*
  429. * All tracing data are stored by now, we can restore
  430. * the default output file in case we used temp file.
  431. */
  432. if (temp) {
  433. tdata->size = lseek(output_fd, 0, SEEK_CUR);
  434. close(output_fd);
  435. output_fd = fd;
  436. }
  437. put_tracepoints_path(tps);
  438. return tdata;
  439. }
  440. void tracing_data_put(struct tracing_data *tdata)
  441. {
  442. if (tdata->temp) {
  443. record_file(tdata->temp_file, 0);
  444. unlink(tdata->temp_file);
  445. }
  446. free(tdata);
  447. }
  448. int read_tracing_data(int fd, struct list_head *pattrs)
  449. {
  450. struct tracing_data *tdata;
  451. /*
  452. * We work over the real file, so we can write data
  453. * directly, no temp file is needed.
  454. */
  455. tdata = tracing_data_get(pattrs, fd, false);
  456. if (!tdata)
  457. return -ENOMEM;
  458. tracing_data_put(tdata);
  459. return 0;
  460. }