trace-event-read.c 9.4 KB

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