trace-event-read.c 6.8 KB

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