trace-event-info.c 10 KB

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