trace-event-read.c 6.4 KB

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