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