trace-event-read.c 9.3 KB

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