trace-event-read.c 9.6 KB

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