trace-event-info.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. #include <ctype.h>
  22. #include "util.h"
  23. #include <dirent.h>
  24. #include <mntent.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <sys/wait.h>
  32. #include <pthread.h>
  33. #include <fcntl.h>
  34. #include <unistd.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 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. void *malloc_or_die(unsigned int size)
  64. {
  65. void *data;
  66. data = malloc(size);
  67. if (!data)
  68. die("malloc");
  69. return data;
  70. }
  71. static const char *find_debugfs(void)
  72. {
  73. const char *path = debugfs_mount(NULL);
  74. if (!path)
  75. die("Your kernel not support debugfs filesystem");
  76. return path;
  77. }
  78. /*
  79. * Finds the path to the debugfs/tracing
  80. * Allocates the string and stores it.
  81. */
  82. static const char *find_tracing_dir(void)
  83. {
  84. static char *tracing;
  85. static int tracing_found;
  86. const char *debugfs;
  87. if (tracing_found)
  88. return tracing;
  89. debugfs = find_debugfs();
  90. tracing = malloc_or_die(strlen(debugfs) + 9);
  91. sprintf(tracing, "%s/tracing", debugfs);
  92. tracing_found = 1;
  93. return tracing;
  94. }
  95. static char *get_tracing_file(const char *name)
  96. {
  97. const char *tracing;
  98. char *file;
  99. tracing = find_tracing_dir();
  100. if (!tracing)
  101. return NULL;
  102. file = malloc_or_die(strlen(tracing) + strlen(name) + 2);
  103. sprintf(file, "%s/%s", tracing, name);
  104. return file;
  105. }
  106. static void put_tracing_file(char *file)
  107. {
  108. free(file);
  109. }
  110. static ssize_t calc_data_size;
  111. static ssize_t write_or_die(const void *buf, size_t len)
  112. {
  113. int ret;
  114. if (calc_data_size) {
  115. calc_data_size += len;
  116. return len;
  117. }
  118. ret = write(output_fd, buf, len);
  119. if (ret < 0)
  120. die("writing to '%s'", output_file);
  121. return ret;
  122. }
  123. int bigendian(void)
  124. {
  125. unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
  126. unsigned int *ptr;
  127. ptr = (unsigned int *)(void *)str;
  128. return *ptr == 0x01020304;
  129. }
  130. /* unfortunately, you can not stat debugfs or proc files for size */
  131. static void record_file(const char *file, size_t hdr_sz)
  132. {
  133. unsigned long long size = 0;
  134. char buf[BUFSIZ], *sizep;
  135. off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
  136. int r, fd;
  137. fd = open(file, O_RDONLY);
  138. if (fd < 0)
  139. die("Can't read '%s'", file);
  140. /* put in zeros for file size, then fill true size later */
  141. if (hdr_sz)
  142. write_or_die(&size, hdr_sz);
  143. do {
  144. r = read(fd, buf, BUFSIZ);
  145. if (r > 0) {
  146. size += r;
  147. write_or_die(buf, r);
  148. }
  149. } while (r > 0);
  150. close(fd);
  151. /* ugh, handle big-endian hdr_size == 4 */
  152. sizep = (char*)&size;
  153. if (bigendian())
  154. sizep += sizeof(u64) - hdr_sz;
  155. if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
  156. die("writing to %s", output_file);
  157. }
  158. static void read_header_files(void)
  159. {
  160. char *path;
  161. struct stat st;
  162. path = get_tracing_file("events/header_page");
  163. if (stat(path, &st) < 0)
  164. die("can't read '%s'", path);
  165. write_or_die("header_page", 12);
  166. record_file(path, 8);
  167. put_tracing_file(path);
  168. path = get_tracing_file("events/header_event");
  169. if (stat(path, &st) < 0)
  170. die("can't read '%s'", path);
  171. write_or_die("header_event", 13);
  172. record_file(path, 8);
  173. put_tracing_file(path);
  174. }
  175. static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
  176. {
  177. while (tps) {
  178. if (!strcmp(sys, tps->name))
  179. return true;
  180. tps = tps->next;
  181. }
  182. return false;
  183. }
  184. static void copy_event_system(const char *sys, struct tracepoint_path *tps)
  185. {
  186. struct dirent *dent;
  187. struct stat st;
  188. char *format;
  189. DIR *dir;
  190. int count = 0;
  191. int ret;
  192. dir = opendir(sys);
  193. if (!dir)
  194. die("can't read directory '%s'", sys);
  195. while ((dent = readdir(dir))) {
  196. if (dent->d_type != DT_DIR ||
  197. strcmp(dent->d_name, ".") == 0 ||
  198. strcmp(dent->d_name, "..") == 0 ||
  199. !name_in_tp_list(dent->d_name, tps))
  200. continue;
  201. format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
  202. sprintf(format, "%s/%s/format", sys, dent->d_name);
  203. ret = stat(format, &st);
  204. free(format);
  205. if (ret < 0)
  206. continue;
  207. count++;
  208. }
  209. write_or_die(&count, 4);
  210. rewinddir(dir);
  211. while ((dent = readdir(dir))) {
  212. if (dent->d_type != DT_DIR ||
  213. strcmp(dent->d_name, ".") == 0 ||
  214. strcmp(dent->d_name, "..") == 0 ||
  215. !name_in_tp_list(dent->d_name, tps))
  216. continue;
  217. format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
  218. sprintf(format, "%s/%s/format", sys, dent->d_name);
  219. ret = stat(format, &st);
  220. if (ret >= 0)
  221. record_file(format, 8);
  222. free(format);
  223. }
  224. closedir(dir);
  225. }
  226. static void read_ftrace_files(struct tracepoint_path *tps)
  227. {
  228. char *path;
  229. path = get_tracing_file("events/ftrace");
  230. copy_event_system(path, tps);
  231. put_tracing_file(path);
  232. }
  233. static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
  234. {
  235. while (tps) {
  236. if (!strcmp(sys, tps->system))
  237. return true;
  238. tps = tps->next;
  239. }
  240. return false;
  241. }
  242. static void read_event_files(struct tracepoint_path *tps)
  243. {
  244. struct dirent *dent;
  245. struct stat st;
  246. char *path;
  247. char *sys;
  248. DIR *dir;
  249. int count = 0;
  250. int ret;
  251. path = get_tracing_file("events");
  252. dir = opendir(path);
  253. if (!dir)
  254. die("can't read directory '%s'", path);
  255. while ((dent = readdir(dir))) {
  256. if (dent->d_type != DT_DIR ||
  257. strcmp(dent->d_name, ".") == 0 ||
  258. strcmp(dent->d_name, "..") == 0 ||
  259. strcmp(dent->d_name, "ftrace") == 0 ||
  260. !system_in_tp_list(dent->d_name, tps))
  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. strcmp(dent->d_name, "ftrace") == 0 ||
  271. !system_in_tp_list(dent->d_name, tps))
  272. continue;
  273. sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
  274. sprintf(sys, "%s/%s", path, dent->d_name);
  275. ret = stat(sys, &st);
  276. if (ret >= 0) {
  277. write_or_die(dent->d_name, strlen(dent->d_name) + 1);
  278. copy_event_system(sys, tps);
  279. }
  280. free(sys);
  281. }
  282. closedir(dir);
  283. put_tracing_file(path);
  284. }
  285. static void read_proc_kallsyms(void)
  286. {
  287. unsigned int size;
  288. const char *path = "/proc/kallsyms";
  289. struct stat st;
  290. int ret;
  291. ret = stat(path, &st);
  292. if (ret < 0) {
  293. /* not found */
  294. size = 0;
  295. write_or_die(&size, 4);
  296. return;
  297. }
  298. record_file(path, 4);
  299. }
  300. static void read_ftrace_printk(void)
  301. {
  302. unsigned int size;
  303. char *path;
  304. struct stat st;
  305. int ret;
  306. path = get_tracing_file("printk_formats");
  307. ret = stat(path, &st);
  308. if (ret < 0) {
  309. /* not found */
  310. size = 0;
  311. write_or_die(&size, 4);
  312. goto out;
  313. }
  314. record_file(path, 4);
  315. out:
  316. put_tracing_file(path);
  317. }
  318. static struct tracepoint_path *
  319. get_tracepoints_path(struct list_head *pattrs)
  320. {
  321. struct tracepoint_path path, *ppath = &path;
  322. struct perf_evsel *pos;
  323. int nr_tracepoints = 0;
  324. list_for_each_entry(pos, pattrs, node) {
  325. if (pos->attr.type != PERF_TYPE_TRACEPOINT)
  326. continue;
  327. ++nr_tracepoints;
  328. ppath->next = tracepoint_id_to_path(pos->attr.config);
  329. if (!ppath->next)
  330. die("%s\n", "No memory to alloc tracepoints list");
  331. ppath = ppath->next;
  332. }
  333. return nr_tracepoints > 0 ? path.next : NULL;
  334. }
  335. static void
  336. put_tracepoints_path(struct tracepoint_path *tps)
  337. {
  338. while (tps) {
  339. struct tracepoint_path *t = tps;
  340. tps = tps->next;
  341. free(t->name);
  342. free(t->system);
  343. free(t);
  344. }
  345. }
  346. bool have_tracepoints(struct list_head *pattrs)
  347. {
  348. struct perf_evsel *pos;
  349. list_for_each_entry(pos, pattrs, node)
  350. if (pos->attr.type == PERF_TYPE_TRACEPOINT)
  351. return true;
  352. return false;
  353. }
  354. static void tracing_data_header(void)
  355. {
  356. char buf[20];
  357. /* just guessing this is someone's birthday.. ;) */
  358. buf[0] = 23;
  359. buf[1] = 8;
  360. buf[2] = 68;
  361. memcpy(buf + 3, "tracing", 7);
  362. write_or_die(buf, 10);
  363. write_or_die(VERSION, strlen(VERSION) + 1);
  364. /* save endian */
  365. if (bigendian())
  366. buf[0] = 1;
  367. else
  368. buf[0] = 0;
  369. write_or_die(buf, 1);
  370. /* save size of long */
  371. buf[0] = sizeof(long);
  372. write_or_die(buf, 1);
  373. /* save page_size */
  374. page_size = sysconf(_SC_PAGESIZE);
  375. write_or_die(&page_size, 4);
  376. }
  377. struct tracing_data *tracing_data_get(struct list_head *pattrs,
  378. int fd, bool temp)
  379. {
  380. struct tracepoint_path *tps;
  381. struct tracing_data *tdata;
  382. output_fd = fd;
  383. tps = get_tracepoints_path(pattrs);
  384. if (!tps)
  385. return NULL;
  386. tdata = malloc_or_die(sizeof(*tdata));
  387. tdata->temp = temp;
  388. tdata->size = 0;
  389. if (temp) {
  390. int temp_fd;
  391. snprintf(tdata->temp_file, sizeof(tdata->temp_file),
  392. "/tmp/perf-XXXXXX");
  393. if (!mkstemp(tdata->temp_file))
  394. die("Can't make temp file");
  395. temp_fd = open(tdata->temp_file, O_RDWR);
  396. if (temp_fd < 0)
  397. die("Can't read '%s'", tdata->temp_file);
  398. /*
  399. * Set the temp file the default output, so all the
  400. * tracing data are stored into it.
  401. */
  402. output_fd = temp_fd;
  403. }
  404. tracing_data_header();
  405. read_header_files();
  406. read_ftrace_files(tps);
  407. read_event_files(tps);
  408. read_proc_kallsyms();
  409. read_ftrace_printk();
  410. /*
  411. * All tracing data are stored by now, we can restore
  412. * the default output file in case we used temp file.
  413. */
  414. if (temp) {
  415. tdata->size = lseek(output_fd, 0, SEEK_CUR);
  416. close(output_fd);
  417. output_fd = fd;
  418. }
  419. put_tracepoints_path(tps);
  420. return tdata;
  421. }
  422. void tracing_data_put(struct tracing_data *tdata)
  423. {
  424. if (tdata->temp) {
  425. record_file(tdata->temp_file, 0);
  426. unlink(tdata->temp_file);
  427. }
  428. free(tdata);
  429. }
  430. int read_tracing_data(int fd, struct list_head *pattrs)
  431. {
  432. struct tracing_data *tdata;
  433. /*
  434. * We work over the real file, so we can write data
  435. * directly, no temp file is needed.
  436. */
  437. tdata = tracing_data_get(pattrs, fd, false);
  438. if (!tdata)
  439. return -ENOMEM;
  440. tracing_data_put(tdata);
  441. return 0;
  442. }