trace-event-read.c 10 KB

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