trace-event-read.c 9.4 KB

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