trace-event-read.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Copyright (C) 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 _FILE_OFFSET_BITS 64
  22. #include <dirent.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <getopt.h>
  27. #include <stdarg.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <sys/wait.h>
  31. #include <sys/mman.h>
  32. #include <pthread.h>
  33. #include <fcntl.h>
  34. #include <unistd.h>
  35. #include <ctype.h>
  36. #include <errno.h>
  37. #include "../perf.h"
  38. #include "util.h"
  39. #include "trace-event.h"
  40. static int input_fd;
  41. static int read_page;
  42. int file_bigendian;
  43. int host_bigendian;
  44. static int long_size;
  45. static unsigned long page_size;
  46. static ssize_t calc_data_size;
  47. /* If it fails, the next read will report it */
  48. static void skip(int size)
  49. {
  50. lseek(input_fd, size, SEEK_CUR);
  51. }
  52. static int do_read(int fd, void *buf, int size)
  53. {
  54. int rsize = size;
  55. while (size) {
  56. int ret = read(fd, buf, size);
  57. if (ret <= 0)
  58. return -1;
  59. size -= ret;
  60. buf += ret;
  61. }
  62. return rsize;
  63. }
  64. static int read_or_die(void *data, int size)
  65. {
  66. int r;
  67. r = do_read(input_fd, data, size);
  68. if (r <= 0)
  69. die("reading input file (size expected=%d received=%d)",
  70. size, r);
  71. if (calc_data_size)
  72. calc_data_size += r;
  73. return r;
  74. }
  75. static unsigned int read4(void)
  76. {
  77. unsigned int data;
  78. read_or_die(&data, 4);
  79. return __data2host4(data);
  80. }
  81. static unsigned long long read8(void)
  82. {
  83. unsigned long long data;
  84. read_or_die(&data, 8);
  85. return __data2host8(data);
  86. }
  87. static char *read_string(void)
  88. {
  89. char buf[BUFSIZ];
  90. char *str = NULL;
  91. int size = 0;
  92. off_t r;
  93. char c;
  94. for (;;) {
  95. r = read(input_fd, &c, 1);
  96. if (r < 0)
  97. die("reading input file");
  98. if (!r)
  99. die("no data");
  100. buf[size++] = c;
  101. if (!c)
  102. break;
  103. }
  104. if (calc_data_size)
  105. calc_data_size += size;
  106. str = malloc_or_die(size);
  107. memcpy(str, buf, size);
  108. return str;
  109. }
  110. static void read_proc_kallsyms(void)
  111. {
  112. unsigned int size;
  113. char *buf;
  114. size = read4();
  115. if (!size)
  116. return;
  117. buf = malloc_or_die(size + 1);
  118. read_or_die(buf, size);
  119. buf[size] = '\0';
  120. parse_proc_kallsyms(buf, size);
  121. free(buf);
  122. }
  123. static void read_ftrace_printk(void)
  124. {
  125. unsigned int size;
  126. char *buf;
  127. size = read4();
  128. if (!size)
  129. return;
  130. buf = malloc_or_die(size);
  131. read_or_die(buf, size);
  132. parse_ftrace_printk(buf, size);
  133. free(buf);
  134. }
  135. static void read_header_files(void)
  136. {
  137. unsigned long long size;
  138. char *header_event;
  139. char buf[BUFSIZ];
  140. read_or_die(buf, 12);
  141. if (memcmp(buf, "header_page", 12) != 0)
  142. die("did not read header page");
  143. size = read8();
  144. skip(size);
  145. /*
  146. * The size field in the page is of type long,
  147. * use that instead, since it represents the kernel.
  148. */
  149. long_size = header_page_size_size;
  150. read_or_die(buf, 13);
  151. if (memcmp(buf, "header_event", 13) != 0)
  152. die("did not read header event");
  153. size = read8();
  154. header_event = malloc_or_die(size);
  155. read_or_die(header_event, size);
  156. free(header_event);
  157. }
  158. static void read_ftrace_file(unsigned long long size)
  159. {
  160. char *buf;
  161. buf = malloc_or_die(size);
  162. read_or_die(buf, size);
  163. parse_ftrace_file(buf, size);
  164. free(buf);
  165. }
  166. static void read_event_file(char *sys, unsigned long long size)
  167. {
  168. char *buf;
  169. buf = malloc_or_die(size);
  170. read_or_die(buf, size);
  171. parse_event_file(buf, size, sys);
  172. free(buf);
  173. }
  174. static void read_ftrace_files(void)
  175. {
  176. unsigned long long size;
  177. int count;
  178. int i;
  179. count = read4();
  180. for (i = 0; i < count; i++) {
  181. size = read8();
  182. read_ftrace_file(size);
  183. }
  184. }
  185. static void read_event_files(void)
  186. {
  187. unsigned long long size;
  188. char *sys;
  189. int systems;
  190. int count;
  191. int i,x;
  192. systems = read4();
  193. for (i = 0; i < systems; i++) {
  194. sys = read_string();
  195. count = read4();
  196. for (x=0; x < count; x++) {
  197. size = read8();
  198. read_event_file(sys, size);
  199. }
  200. }
  201. }
  202. struct cpu_data {
  203. unsigned long long offset;
  204. unsigned long long size;
  205. unsigned long long timestamp;
  206. struct record *next;
  207. char *page;
  208. int cpu;
  209. int index;
  210. int page_size;
  211. };
  212. static struct cpu_data *cpu_data;
  213. static void update_cpu_data_index(int cpu)
  214. {
  215. cpu_data[cpu].offset += page_size;
  216. cpu_data[cpu].size -= page_size;
  217. cpu_data[cpu].index = 0;
  218. }
  219. static void get_next_page(int cpu)
  220. {
  221. off_t save_seek;
  222. off_t ret;
  223. if (!cpu_data[cpu].page)
  224. return;
  225. if (read_page) {
  226. if (cpu_data[cpu].size <= page_size) {
  227. free(cpu_data[cpu].page);
  228. cpu_data[cpu].page = NULL;
  229. return;
  230. }
  231. update_cpu_data_index(cpu);
  232. /* other parts of the code may expect the pointer to not move */
  233. save_seek = lseek(input_fd, 0, SEEK_CUR);
  234. ret = lseek(input_fd, cpu_data[cpu].offset, SEEK_SET);
  235. if (ret == (off_t)-1)
  236. die("failed to lseek");
  237. ret = read(input_fd, cpu_data[cpu].page, page_size);
  238. if (ret < 0)
  239. die("failed to read page");
  240. /* reset the file pointer back */
  241. lseek(input_fd, save_seek, SEEK_SET);
  242. return;
  243. }
  244. munmap(cpu_data[cpu].page, page_size);
  245. cpu_data[cpu].page = NULL;
  246. if (cpu_data[cpu].size <= page_size)
  247. return;
  248. update_cpu_data_index(cpu);
  249. cpu_data[cpu].page = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE,
  250. input_fd, cpu_data[cpu].offset);
  251. if (cpu_data[cpu].page == MAP_FAILED)
  252. die("failed to mmap cpu %d at offset 0x%llx",
  253. cpu, cpu_data[cpu].offset);
  254. }
  255. static unsigned int type_len4host(unsigned int type_len_ts)
  256. {
  257. if (file_bigendian)
  258. return (type_len_ts >> 27) & ((1 << 5) - 1);
  259. else
  260. return type_len_ts & ((1 << 5) - 1);
  261. }
  262. static unsigned int ts4host(unsigned int type_len_ts)
  263. {
  264. if (file_bigendian)
  265. return type_len_ts & ((1 << 27) - 1);
  266. else
  267. return type_len_ts >> 5;
  268. }
  269. static int calc_index(void *ptr, int cpu)
  270. {
  271. return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
  272. }
  273. struct record *trace_peek_data(int cpu)
  274. {
  275. struct record *data;
  276. void *page = cpu_data[cpu].page;
  277. int idx = cpu_data[cpu].index;
  278. void *ptr = page + idx;
  279. unsigned long long extend;
  280. unsigned int type_len_ts;
  281. unsigned int type_len;
  282. unsigned int delta;
  283. unsigned int length = 0;
  284. if (cpu_data[cpu].next)
  285. return cpu_data[cpu].next;
  286. if (!page)
  287. return NULL;
  288. if (!idx) {
  289. /* FIXME: handle header page */
  290. if (header_page_ts_size != 8)
  291. die("expected a long long type for timestamp");
  292. cpu_data[cpu].timestamp = data2host8(ptr);
  293. ptr += 8;
  294. switch (header_page_size_size) {
  295. case 4:
  296. cpu_data[cpu].page_size = data2host4(ptr);
  297. ptr += 4;
  298. break;
  299. case 8:
  300. cpu_data[cpu].page_size = data2host8(ptr);
  301. ptr += 8;
  302. break;
  303. default:
  304. die("bad long size");
  305. }
  306. ptr = cpu_data[cpu].page + header_page_data_offset;
  307. }
  308. read_again:
  309. idx = calc_index(ptr, cpu);
  310. if (idx >= cpu_data[cpu].page_size) {
  311. get_next_page(cpu);
  312. return trace_peek_data(cpu);
  313. }
  314. type_len_ts = data2host4(ptr);
  315. ptr += 4;
  316. type_len = type_len4host(type_len_ts);
  317. delta = ts4host(type_len_ts);
  318. switch (type_len) {
  319. case RINGBUF_TYPE_PADDING:
  320. if (!delta)
  321. die("error, hit unexpected end of page");
  322. length = data2host4(ptr);
  323. ptr += 4;
  324. length *= 4;
  325. ptr += length;
  326. goto read_again;
  327. case RINGBUF_TYPE_TIME_EXTEND:
  328. extend = data2host4(ptr);
  329. ptr += 4;
  330. extend <<= TS_SHIFT;
  331. extend += delta;
  332. cpu_data[cpu].timestamp += extend;
  333. goto read_again;
  334. case RINGBUF_TYPE_TIME_STAMP:
  335. ptr += 12;
  336. break;
  337. case 0:
  338. length = data2host4(ptr);
  339. ptr += 4;
  340. die("here! length=%d", length);
  341. break;
  342. default:
  343. length = type_len * 4;
  344. break;
  345. }
  346. cpu_data[cpu].timestamp += delta;
  347. data = malloc_or_die(sizeof(*data));
  348. memset(data, 0, sizeof(*data));
  349. data->ts = cpu_data[cpu].timestamp;
  350. data->size = length;
  351. data->data = ptr;
  352. ptr += length;
  353. cpu_data[cpu].index = calc_index(ptr, cpu);
  354. cpu_data[cpu].next = data;
  355. return data;
  356. }
  357. struct record *trace_read_data(int cpu)
  358. {
  359. struct record *data;
  360. data = trace_peek_data(cpu);
  361. cpu_data[cpu].next = NULL;
  362. return data;
  363. }
  364. ssize_t trace_report(int fd)
  365. {
  366. char buf[BUFSIZ];
  367. char test[] = { 23, 8, 68 };
  368. char *version;
  369. int show_version = 0;
  370. int show_funcs = 0;
  371. int show_printk = 0;
  372. ssize_t size;
  373. calc_data_size = 1;
  374. input_fd = fd;
  375. read_or_die(buf, 3);
  376. if (memcmp(buf, test, 3) != 0)
  377. die("no trace data in the file");
  378. read_or_die(buf, 7);
  379. if (memcmp(buf, "tracing", 7) != 0)
  380. die("not a trace file (missing 'tracing' tag)");
  381. version = read_string();
  382. if (show_version)
  383. printf("version = %s\n", version);
  384. free(version);
  385. read_or_die(buf, 1);
  386. file_bigendian = buf[0];
  387. host_bigendian = bigendian();
  388. read_or_die(buf, 1);
  389. long_size = buf[0];
  390. page_size = read4();
  391. read_header_files();
  392. read_ftrace_files();
  393. read_event_files();
  394. read_proc_kallsyms();
  395. read_ftrace_printk();
  396. size = calc_data_size - 1;
  397. calc_data_size = 0;
  398. if (show_funcs) {
  399. print_funcs();
  400. return size;
  401. }
  402. if (show_printk) {
  403. print_printk();
  404. return size;
  405. }
  406. return size;
  407. }