trace-event-read.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 <stdarg.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <sys/wait.h>
  29. #include <sys/mman.h>
  30. #include <pthread.h>
  31. #include <fcntl.h>
  32. #include <unistd.h>
  33. #include <errno.h>
  34. #include "../perf.h"
  35. #include "util.h"
  36. #include "trace-event.h"
  37. #include "debug.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. size = read4(pevent);
  136. if (!size)
  137. return 0;
  138. /*
  139. * Just skip it, now that we configure libtraceevent to use the
  140. * tools/perf/ symbol resolver.
  141. *
  142. * We need to skip it so that we can continue parsing old perf.data
  143. * files, that contains this /proc/kallsyms payload.
  144. *
  145. * Newer perf.data files will have just the 4-bytes zeros "kallsyms
  146. * payload", so that older tools can continue reading it and interpret
  147. * it as "no kallsyms payload is present".
  148. */
  149. lseek(input_fd, size, SEEK_CUR);
  150. trace_data_size += size;
  151. return 0;
  152. }
  153. static int read_ftrace_printk(struct pevent *pevent)
  154. {
  155. unsigned int size;
  156. char *buf;
  157. /* it can have 0 size */
  158. size = read4(pevent);
  159. if (!size)
  160. return 0;
  161. buf = malloc(size);
  162. if (buf == NULL)
  163. return -1;
  164. if (do_read(buf, size) < 0) {
  165. free(buf);
  166. return -1;
  167. }
  168. parse_ftrace_printk(pevent, buf, size);
  169. free(buf);
  170. return 0;
  171. }
  172. static int read_header_files(struct pevent *pevent)
  173. {
  174. unsigned long long size;
  175. char *header_page;
  176. char buf[BUFSIZ];
  177. int ret = 0;
  178. if (do_read(buf, 12) < 0)
  179. return -1;
  180. if (memcmp(buf, "header_page", 12) != 0) {
  181. pr_debug("did not read header page");
  182. return -1;
  183. }
  184. size = read8(pevent);
  185. header_page = malloc(size);
  186. if (header_page == NULL)
  187. return -1;
  188. if (do_read(header_page, size) < 0) {
  189. pr_debug("did not read header page");
  190. free(header_page);
  191. return -1;
  192. }
  193. if (!pevent_parse_header_page(pevent, header_page, size,
  194. pevent_get_long_size(pevent))) {
  195. /*
  196. * The commit field in the page is of type long,
  197. * use that instead, since it represents the kernel.
  198. */
  199. pevent_set_long_size(pevent, pevent->header_page_size_size);
  200. }
  201. free(header_page);
  202. if (do_read(buf, 13) < 0)
  203. return -1;
  204. if (memcmp(buf, "header_event", 13) != 0) {
  205. pr_debug("did not read header event");
  206. return -1;
  207. }
  208. size = read8(pevent);
  209. skip(size);
  210. return ret;
  211. }
  212. static int read_ftrace_file(struct pevent *pevent, unsigned long long size)
  213. {
  214. char *buf;
  215. buf = malloc(size);
  216. if (buf == NULL)
  217. return -1;
  218. if (do_read(buf, size) < 0) {
  219. free(buf);
  220. return -1;
  221. }
  222. parse_ftrace_file(pevent, buf, size);
  223. free(buf);
  224. return 0;
  225. }
  226. static int read_event_file(struct pevent *pevent, char *sys,
  227. unsigned long long size)
  228. {
  229. char *buf;
  230. buf = malloc(size);
  231. if (buf == NULL)
  232. return -1;
  233. if (do_read(buf, size) < 0) {
  234. free(buf);
  235. return -1;
  236. }
  237. parse_event_file(pevent, buf, size, sys);
  238. free(buf);
  239. return 0;
  240. }
  241. static int read_ftrace_files(struct pevent *pevent)
  242. {
  243. unsigned long long size;
  244. int count;
  245. int i;
  246. int ret;
  247. count = read4(pevent);
  248. for (i = 0; i < count; i++) {
  249. size = read8(pevent);
  250. ret = read_ftrace_file(pevent, size);
  251. if (ret)
  252. return ret;
  253. }
  254. return 0;
  255. }
  256. static int read_event_files(struct pevent *pevent)
  257. {
  258. unsigned long long size;
  259. char *sys;
  260. int systems;
  261. int count;
  262. int i,x;
  263. int ret;
  264. systems = read4(pevent);
  265. for (i = 0; i < systems; i++) {
  266. sys = read_string();
  267. if (sys == NULL)
  268. return -1;
  269. count = read4(pevent);
  270. for (x=0; x < count; x++) {
  271. size = read8(pevent);
  272. ret = read_event_file(pevent, sys, size);
  273. if (ret) {
  274. free(sys);
  275. return ret;
  276. }
  277. }
  278. free(sys);
  279. }
  280. return 0;
  281. }
  282. ssize_t trace_report(int fd, struct trace_event *tevent, bool __repipe)
  283. {
  284. char buf[BUFSIZ];
  285. char test[] = { 23, 8, 68 };
  286. char *version;
  287. int show_version = 0;
  288. int show_funcs = 0;
  289. int show_printk = 0;
  290. ssize_t size = -1;
  291. int file_bigendian;
  292. int host_bigendian;
  293. int file_long_size;
  294. int file_page_size;
  295. struct pevent *pevent = NULL;
  296. int err;
  297. repipe = __repipe;
  298. input_fd = fd;
  299. if (do_read(buf, 3) < 0)
  300. return -1;
  301. if (memcmp(buf, test, 3) != 0) {
  302. pr_debug("no trace data in the file");
  303. return -1;
  304. }
  305. if (do_read(buf, 7) < 0)
  306. return -1;
  307. if (memcmp(buf, "tracing", 7) != 0) {
  308. pr_debug("not a trace file (missing 'tracing' tag)");
  309. return -1;
  310. }
  311. version = read_string();
  312. if (version == NULL)
  313. return -1;
  314. if (show_version)
  315. printf("version = %s\n", version);
  316. free(version);
  317. if (do_read(buf, 1) < 0)
  318. return -1;
  319. file_bigendian = buf[0];
  320. host_bigendian = bigendian();
  321. if (trace_event__init(tevent)) {
  322. pr_debug("trace_event__init failed");
  323. goto out;
  324. }
  325. pevent = tevent->pevent;
  326. pevent_set_flag(pevent, PEVENT_NSEC_OUTPUT);
  327. pevent_set_file_bigendian(pevent, file_bigendian);
  328. pevent_set_host_bigendian(pevent, host_bigendian);
  329. if (do_read(buf, 1) < 0)
  330. goto out;
  331. file_long_size = buf[0];
  332. file_page_size = read4(pevent);
  333. if (!file_page_size)
  334. goto out;
  335. pevent_set_long_size(pevent, file_long_size);
  336. pevent_set_page_size(pevent, file_page_size);
  337. err = read_header_files(pevent);
  338. if (err)
  339. goto out;
  340. err = read_ftrace_files(pevent);
  341. if (err)
  342. goto out;
  343. err = read_event_files(pevent);
  344. if (err)
  345. goto out;
  346. err = read_proc_kallsyms(pevent);
  347. if (err)
  348. goto out;
  349. err = read_ftrace_printk(pevent);
  350. if (err)
  351. goto out;
  352. size = trace_data_size;
  353. repipe = false;
  354. if (show_funcs) {
  355. pevent_print_funcs(pevent);
  356. } else if (show_printk) {
  357. pevent_print_printk(pevent);
  358. }
  359. pevent = NULL;
  360. out:
  361. if (pevent)
  362. trace_event__cleanup(tevent);
  363. return size;
  364. }