trace-event-read.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 <fcntl.h>
  31. #include <unistd.h>
  32. #include <errno.h>
  33. #include "../perf.h"
  34. #include "util.h"
  35. #include "trace-event.h"
  36. #include "debug.h"
  37. static int input_fd;
  38. static ssize_t trace_data_size;
  39. static bool repipe;
  40. static int __do_read(int fd, void *buf, int size)
  41. {
  42. int rsize = size;
  43. while (size) {
  44. int ret = read(fd, buf, size);
  45. if (ret <= 0)
  46. return -1;
  47. if (repipe) {
  48. int retw = write(STDOUT_FILENO, buf, ret);
  49. if (retw <= 0 || retw != ret) {
  50. pr_debug("repiping input file");
  51. return -1;
  52. }
  53. }
  54. size -= ret;
  55. buf += ret;
  56. }
  57. return rsize;
  58. }
  59. static int do_read(void *data, int size)
  60. {
  61. int r;
  62. r = __do_read(input_fd, data, size);
  63. if (r <= 0) {
  64. pr_debug("reading input file (size expected=%d received=%d)",
  65. size, r);
  66. return -1;
  67. }
  68. trace_data_size += r;
  69. return r;
  70. }
  71. /* If it fails, the next read will report it */
  72. static void skip(int size)
  73. {
  74. char buf[BUFSIZ];
  75. int r;
  76. while (size) {
  77. r = size > BUFSIZ ? BUFSIZ : size;
  78. do_read(buf, r);
  79. size -= r;
  80. };
  81. }
  82. static unsigned int read4(struct tep_handle *pevent)
  83. {
  84. unsigned int data;
  85. if (do_read(&data, 4) < 0)
  86. return 0;
  87. return __data2host4(pevent, data);
  88. }
  89. static unsigned long long read8(struct tep_handle *pevent)
  90. {
  91. unsigned long long data;
  92. if (do_read(&data, 8) < 0)
  93. return 0;
  94. return __data2host8(pevent, data);
  95. }
  96. static char *read_string(void)
  97. {
  98. char buf[BUFSIZ];
  99. char *str = NULL;
  100. int size = 0;
  101. off_t r;
  102. char c;
  103. for (;;) {
  104. r = read(input_fd, &c, 1);
  105. if (r < 0) {
  106. pr_debug("reading input file");
  107. goto out;
  108. }
  109. if (!r) {
  110. pr_debug("no data");
  111. goto out;
  112. }
  113. if (repipe) {
  114. int retw = write(STDOUT_FILENO, &c, 1);
  115. if (retw <= 0 || retw != r) {
  116. pr_debug("repiping input file string");
  117. goto out;
  118. }
  119. }
  120. buf[size++] = c;
  121. if (!c)
  122. break;
  123. }
  124. trace_data_size += size;
  125. str = malloc(size);
  126. if (str)
  127. memcpy(str, buf, size);
  128. out:
  129. return str;
  130. }
  131. static int read_proc_kallsyms(struct tep_handle *pevent)
  132. {
  133. unsigned int size;
  134. size = read4(pevent);
  135. if (!size)
  136. return 0;
  137. /*
  138. * Just skip it, now that we configure libtraceevent to use the
  139. * tools/perf/ symbol resolver.
  140. *
  141. * We need to skip it so that we can continue parsing old perf.data
  142. * files, that contains this /proc/kallsyms payload.
  143. *
  144. * Newer perf.data files will have just the 4-bytes zeros "kallsyms
  145. * payload", so that older tools can continue reading it and interpret
  146. * it as "no kallsyms payload is present".
  147. */
  148. lseek(input_fd, size, SEEK_CUR);
  149. trace_data_size += size;
  150. return 0;
  151. }
  152. static int read_ftrace_printk(struct tep_handle *pevent)
  153. {
  154. unsigned int size;
  155. char *buf;
  156. /* it can have 0 size */
  157. size = read4(pevent);
  158. if (!size)
  159. return 0;
  160. buf = malloc(size + 1);
  161. if (buf == NULL)
  162. return -1;
  163. if (do_read(buf, size) < 0) {
  164. free(buf);
  165. return -1;
  166. }
  167. buf[size] = '\0';
  168. parse_ftrace_printk(pevent, buf, size);
  169. free(buf);
  170. return 0;
  171. }
  172. static int read_header_files(struct tep_handle *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 (!tep_parse_header_page(pevent, header_page, size,
  194. tep_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. tep_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 tep_handle *pevent, unsigned long long size)
  213. {
  214. int ret;
  215. char *buf;
  216. buf = malloc(size);
  217. if (buf == NULL) {
  218. pr_debug("memory allocation failure\n");
  219. return -1;
  220. }
  221. ret = do_read(buf, size);
  222. if (ret < 0) {
  223. pr_debug("error reading ftrace file.\n");
  224. goto out;
  225. }
  226. ret = parse_ftrace_file(pevent, buf, size);
  227. if (ret < 0)
  228. pr_debug("error parsing ftrace file.\n");
  229. out:
  230. free(buf);
  231. return ret;
  232. }
  233. static int read_event_file(struct tep_handle *pevent, char *sys,
  234. unsigned long long size)
  235. {
  236. int ret;
  237. char *buf;
  238. buf = malloc(size);
  239. if (buf == NULL) {
  240. pr_debug("memory allocation failure\n");
  241. return -1;
  242. }
  243. ret = do_read(buf, size);
  244. if (ret < 0) {
  245. free(buf);
  246. goto out;
  247. }
  248. ret = parse_event_file(pevent, buf, size, sys);
  249. if (ret < 0)
  250. pr_debug("error parsing event file.\n");
  251. out:
  252. free(buf);
  253. return ret;
  254. }
  255. static int read_ftrace_files(struct tep_handle *pevent)
  256. {
  257. unsigned long long size;
  258. int count;
  259. int i;
  260. int ret;
  261. count = read4(pevent);
  262. for (i = 0; i < count; i++) {
  263. size = read8(pevent);
  264. ret = read_ftrace_file(pevent, size);
  265. if (ret)
  266. return ret;
  267. }
  268. return 0;
  269. }
  270. static int read_event_files(struct tep_handle *pevent)
  271. {
  272. unsigned long long size;
  273. char *sys;
  274. int systems;
  275. int count;
  276. int i,x;
  277. int ret;
  278. systems = read4(pevent);
  279. for (i = 0; i < systems; i++) {
  280. sys = read_string();
  281. if (sys == NULL)
  282. return -1;
  283. count = read4(pevent);
  284. for (x=0; x < count; x++) {
  285. size = read8(pevent);
  286. ret = read_event_file(pevent, sys, size);
  287. if (ret) {
  288. free(sys);
  289. return ret;
  290. }
  291. }
  292. free(sys);
  293. }
  294. return 0;
  295. }
  296. static int read_saved_cmdline(struct tep_handle *pevent)
  297. {
  298. unsigned long long size;
  299. char *buf;
  300. int ret;
  301. /* it can have 0 size */
  302. size = read8(pevent);
  303. if (!size)
  304. return 0;
  305. buf = malloc(size + 1);
  306. if (buf == NULL) {
  307. pr_debug("memory allocation failure\n");
  308. return -1;
  309. }
  310. ret = do_read(buf, size);
  311. if (ret < 0) {
  312. pr_debug("error reading saved cmdlines\n");
  313. goto out;
  314. }
  315. parse_saved_cmdline(pevent, buf, size);
  316. ret = 0;
  317. out:
  318. free(buf);
  319. return ret;
  320. }
  321. ssize_t trace_report(int fd, struct trace_event *tevent, bool __repipe)
  322. {
  323. char buf[BUFSIZ];
  324. char test[] = { 23, 8, 68 };
  325. char *version;
  326. int show_version = 0;
  327. int show_funcs = 0;
  328. int show_printk = 0;
  329. ssize_t size = -1;
  330. int file_bigendian;
  331. int host_bigendian;
  332. int file_long_size;
  333. int file_page_size;
  334. struct tep_handle *pevent = NULL;
  335. int err;
  336. repipe = __repipe;
  337. input_fd = fd;
  338. if (do_read(buf, 3) < 0)
  339. return -1;
  340. if (memcmp(buf, test, 3) != 0) {
  341. pr_debug("no trace data in the file");
  342. return -1;
  343. }
  344. if (do_read(buf, 7) < 0)
  345. return -1;
  346. if (memcmp(buf, "tracing", 7) != 0) {
  347. pr_debug("not a trace file (missing 'tracing' tag)");
  348. return -1;
  349. }
  350. version = read_string();
  351. if (version == NULL)
  352. return -1;
  353. if (show_version)
  354. printf("version = %s\n", version);
  355. if (do_read(buf, 1) < 0) {
  356. free(version);
  357. return -1;
  358. }
  359. file_bigendian = buf[0];
  360. host_bigendian = bigendian();
  361. if (trace_event__init(tevent)) {
  362. pr_debug("trace_event__init failed");
  363. goto out;
  364. }
  365. pevent = tevent->pevent;
  366. tep_set_flag(pevent, TEP_NSEC_OUTPUT);
  367. tep_set_file_bigendian(pevent, file_bigendian);
  368. tep_set_host_bigendian(pevent, host_bigendian);
  369. if (do_read(buf, 1) < 0)
  370. goto out;
  371. file_long_size = buf[0];
  372. file_page_size = read4(pevent);
  373. if (!file_page_size)
  374. goto out;
  375. tep_set_long_size(pevent, file_long_size);
  376. tep_set_page_size(pevent, file_page_size);
  377. err = read_header_files(pevent);
  378. if (err)
  379. goto out;
  380. err = read_ftrace_files(pevent);
  381. if (err)
  382. goto out;
  383. err = read_event_files(pevent);
  384. if (err)
  385. goto out;
  386. err = read_proc_kallsyms(pevent);
  387. if (err)
  388. goto out;
  389. err = read_ftrace_printk(pevent);
  390. if (err)
  391. goto out;
  392. if (atof(version) >= 0.6) {
  393. err = read_saved_cmdline(pevent);
  394. if (err)
  395. goto out;
  396. }
  397. size = trace_data_size;
  398. repipe = false;
  399. if (show_funcs) {
  400. tep_print_funcs(pevent);
  401. } else if (show_printk) {
  402. tep_print_printk(pevent);
  403. }
  404. pevent = NULL;
  405. out:
  406. if (pevent)
  407. trace_event__cleanup(tevent);
  408. free(version);
  409. return size;
  410. }