trace-event-read.c 10 KB

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