trace-event-read.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. int file_bigendian;
  41. int host_bigendian;
  42. static int long_size;
  43. static ssize_t calc_data_size;
  44. static bool repipe;
  45. static void *malloc_or_die(int size)
  46. {
  47. void *ret;
  48. ret = malloc(size);
  49. if (!ret)
  50. die("malloc");
  51. return ret;
  52. }
  53. static int do_read(int fd, void *buf, int size)
  54. {
  55. int rsize = size;
  56. while (size) {
  57. int ret = read(fd, buf, size);
  58. if (ret <= 0)
  59. return -1;
  60. if (repipe) {
  61. int retw = write(STDOUT_FILENO, buf, ret);
  62. if (retw <= 0 || retw != ret)
  63. die("repiping input file");
  64. }
  65. size -= ret;
  66. buf += ret;
  67. }
  68. return rsize;
  69. }
  70. static int read_or_die(void *data, int size)
  71. {
  72. int r;
  73. r = do_read(input_fd, data, size);
  74. if (r <= 0)
  75. die("reading input file (size expected=%d received=%d)",
  76. size, r);
  77. if (calc_data_size)
  78. calc_data_size += r;
  79. return r;
  80. }
  81. /* If it fails, the next read will report it */
  82. static void skip(int size)
  83. {
  84. char buf[BUFSIZ];
  85. int r;
  86. while (size) {
  87. r = size > BUFSIZ ? BUFSIZ : size;
  88. read_or_die(buf, r);
  89. size -= r;
  90. };
  91. }
  92. static unsigned int read4(struct pevent *pevent)
  93. {
  94. unsigned int data;
  95. read_or_die(&data, 4);
  96. return __data2host4(pevent, data);
  97. }
  98. static unsigned long long read8(struct pevent *pevent)
  99. {
  100. unsigned long long data;
  101. read_or_die(&data, 8);
  102. return __data2host8(pevent, data);
  103. }
  104. static char *read_string(void)
  105. {
  106. char buf[BUFSIZ];
  107. char *str = NULL;
  108. int size = 0;
  109. off_t r;
  110. char c;
  111. for (;;) {
  112. r = read(input_fd, &c, 1);
  113. if (r < 0)
  114. die("reading input file");
  115. if (!r)
  116. die("no data");
  117. if (repipe) {
  118. int retw = write(STDOUT_FILENO, &c, 1);
  119. if (retw <= 0 || retw != r)
  120. die("repiping input file string");
  121. }
  122. buf[size++] = c;
  123. if (!c)
  124. break;
  125. }
  126. if (calc_data_size)
  127. calc_data_size += size;
  128. str = malloc_or_die(size);
  129. memcpy(str, buf, size);
  130. return str;
  131. }
  132. static void read_proc_kallsyms(struct pevent *pevent)
  133. {
  134. unsigned int size;
  135. char *buf;
  136. size = read4(pevent);
  137. if (!size)
  138. return;
  139. buf = malloc_or_die(size + 1);
  140. read_or_die(buf, size);
  141. buf[size] = '\0';
  142. parse_proc_kallsyms(pevent, buf, size);
  143. free(buf);
  144. }
  145. static void read_ftrace_printk(struct pevent *pevent)
  146. {
  147. unsigned int size;
  148. char *buf;
  149. size = read4(pevent);
  150. if (!size)
  151. return;
  152. buf = malloc_or_die(size);
  153. read_or_die(buf, size);
  154. parse_ftrace_printk(pevent, buf, size);
  155. free(buf);
  156. }
  157. static void read_header_files(struct pevent *pevent)
  158. {
  159. unsigned long long size;
  160. char *header_event;
  161. char buf[BUFSIZ];
  162. read_or_die(buf, 12);
  163. if (memcmp(buf, "header_page", 12) != 0)
  164. die("did not read header page");
  165. size = read8(pevent);
  166. skip(size);
  167. /*
  168. * The size field in the page is of type long,
  169. * use that instead, since it represents the kernel.
  170. */
  171. long_size = header_page_size_size;
  172. read_or_die(buf, 13);
  173. if (memcmp(buf, "header_event", 13) != 0)
  174. die("did not read header event");
  175. size = read8(pevent);
  176. header_event = malloc_or_die(size);
  177. read_or_die(header_event, size);
  178. free(header_event);
  179. }
  180. static void read_ftrace_file(struct pevent *pevent, unsigned long long size)
  181. {
  182. char *buf;
  183. buf = malloc_or_die(size);
  184. read_or_die(buf, size);
  185. parse_ftrace_file(pevent, buf, size);
  186. free(buf);
  187. }
  188. static void read_event_file(struct pevent *pevent, char *sys,
  189. unsigned long long size)
  190. {
  191. char *buf;
  192. buf = malloc_or_die(size);
  193. read_or_die(buf, size);
  194. parse_event_file(pevent, buf, size, sys);
  195. free(buf);
  196. }
  197. static void read_ftrace_files(struct pevent *pevent)
  198. {
  199. unsigned long long size;
  200. int count;
  201. int i;
  202. count = read4(pevent);
  203. for (i = 0; i < count; i++) {
  204. size = read8(pevent);
  205. read_ftrace_file(pevent, size);
  206. }
  207. }
  208. static void read_event_files(struct pevent *pevent)
  209. {
  210. unsigned long long size;
  211. char *sys;
  212. int systems;
  213. int count;
  214. int i,x;
  215. systems = read4(pevent);
  216. for (i = 0; i < systems; i++) {
  217. sys = read_string();
  218. count = read4(pevent);
  219. for (x=0; x < count; x++) {
  220. size = read8(pevent);
  221. read_event_file(pevent, sys, size);
  222. }
  223. }
  224. }
  225. ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe)
  226. {
  227. char buf[BUFSIZ];
  228. char test[] = { 23, 8, 68 };
  229. char *version;
  230. int show_version = 0;
  231. int show_funcs = 0;
  232. int show_printk = 0;
  233. ssize_t size;
  234. calc_data_size = 1;
  235. repipe = __repipe;
  236. input_fd = fd;
  237. read_or_die(buf, 3);
  238. if (memcmp(buf, test, 3) != 0)
  239. die("no trace data in the file");
  240. read_or_die(buf, 7);
  241. if (memcmp(buf, "tracing", 7) != 0)
  242. die("not a trace file (missing 'tracing' tag)");
  243. version = read_string();
  244. if (show_version)
  245. printf("version = %s\n", version);
  246. free(version);
  247. read_or_die(buf, 1);
  248. file_bigendian = buf[0];
  249. host_bigendian = bigendian();
  250. *ppevent = read_trace_init(file_bigendian, host_bigendian);
  251. if (*ppevent == NULL)
  252. die("read_trace_init failed");
  253. read_or_die(buf, 1);
  254. long_size = buf[0];
  255. page_size = read4(*ppevent);
  256. read_header_files(*ppevent);
  257. read_ftrace_files(*ppevent);
  258. read_event_files(*ppevent);
  259. read_proc_kallsyms(*ppevent);
  260. read_ftrace_printk(*ppevent);
  261. size = calc_data_size - 1;
  262. calc_data_size = 0;
  263. repipe = false;
  264. if (show_funcs) {
  265. pevent_print_funcs(*ppevent);
  266. return size;
  267. }
  268. if (show_printk) {
  269. pevent_print_printk(*ppevent);
  270. return size;
  271. }
  272. return size;
  273. }