trace-event-read.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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(void)
  95. {
  96. unsigned int data;
  97. read_or_die(&data, 4);
  98. return __data2host4(perf_pevent, data);
  99. }
  100. static unsigned long long read8(void)
  101. {
  102. unsigned long long data;
  103. read_or_die(&data, 8);
  104. return __data2host8(perf_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(void)
  135. {
  136. unsigned int size;
  137. char *buf;
  138. size = read4();
  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(buf, size);
  145. free(buf);
  146. }
  147. static void read_ftrace_printk(void)
  148. {
  149. unsigned int size;
  150. char *buf;
  151. size = read4();
  152. if (!size)
  153. return;
  154. buf = malloc_or_die(size);
  155. read_or_die(buf, size);
  156. parse_ftrace_printk(buf, size);
  157. free(buf);
  158. }
  159. static void read_header_files(void)
  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();
  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();
  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(unsigned long long size)
  183. {
  184. char *buf;
  185. buf = malloc_or_die(size);
  186. read_or_die(buf, size);
  187. parse_ftrace_file(buf, size);
  188. free(buf);
  189. }
  190. static void read_event_file(char *sys, unsigned long long size)
  191. {
  192. char *buf;
  193. buf = malloc_or_die(size);
  194. read_or_die(buf, size);
  195. parse_event_file(buf, size, sys);
  196. free(buf);
  197. }
  198. static void read_ftrace_files(void)
  199. {
  200. unsigned long long size;
  201. int count;
  202. int i;
  203. count = read4();
  204. for (i = 0; i < count; i++) {
  205. size = read8();
  206. read_ftrace_file(size);
  207. }
  208. }
  209. static void read_event_files(void)
  210. {
  211. unsigned long long size;
  212. char *sys;
  213. int systems;
  214. int count;
  215. int i,x;
  216. systems = read4();
  217. for (i = 0; i < systems; i++) {
  218. sys = read_string();
  219. count = read4();
  220. for (x=0; x < count; x++) {
  221. size = read8();
  222. read_event_file(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(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(perf_pevent, ptr);
  317. ptr += 8;
  318. switch (header_page_size_size) {
  319. case 4:
  320. cpu_data[cpu].page_size = data2host4(perf_pevent, ptr);
  321. ptr += 4;
  322. break;
  323. case 8:
  324. cpu_data[cpu].page_size = data2host8(perf_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(cpu);
  337. }
  338. type_len_ts = data2host4(perf_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(perf_pevent, ptr);
  347. ptr += 4;
  348. length *= 4;
  349. ptr += length;
  350. goto read_again;
  351. case RINGBUF_TYPE_TIME_EXTEND:
  352. extend = data2host4(perf_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(perf_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(int cpu)
  382. {
  383. struct pevent_record *data;
  384. data = trace_peek_data(cpu);
  385. cpu_data[cpu].next = NULL;
  386. return data;
  387. }
  388. ssize_t trace_report(int fd, 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. read_trace_init(file_bigendian, host_bigendian);
  414. read_or_die(buf, 1);
  415. long_size = buf[0];
  416. page_size = read4();
  417. read_header_files();
  418. read_ftrace_files();
  419. read_event_files();
  420. read_proc_kallsyms();
  421. read_ftrace_printk();
  422. size = calc_data_size - 1;
  423. calc_data_size = 0;
  424. repipe = false;
  425. if (show_funcs) {
  426. pevent_print_funcs(perf_pevent);
  427. return size;
  428. }
  429. if (show_printk) {
  430. pevent_print_printk(perf_pevent);
  431. return size;
  432. }
  433. return size;
  434. }