trace-event-read.c 9.4 KB

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