trace-event-read.c 7.6 KB

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