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