trace-event-parse.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include "../perf.h"
  27. #include "util.h"
  28. #include "trace-event.h"
  29. int header_page_size_size;
  30. int header_page_ts_size;
  31. int header_page_data_offset;
  32. bool latency_format;
  33. struct pevent *read_trace_init(int file_bigendian, int host_bigendian)
  34. {
  35. struct pevent *pevent = pevent_alloc();
  36. if (pevent != NULL) {
  37. pevent_set_flag(pevent, PEVENT_NSEC_OUTPUT);
  38. pevent_set_file_bigendian(pevent, file_bigendian);
  39. pevent_set_host_bigendian(pevent, host_bigendian);
  40. }
  41. return pevent;
  42. }
  43. static int get_common_field(struct scripting_context *context,
  44. int *offset, int *size, const char *type)
  45. {
  46. struct pevent *pevent = context->pevent;
  47. struct event_format *event;
  48. struct format_field *field;
  49. if (!*size) {
  50. if (!pevent->events)
  51. return 0;
  52. event = pevent->events[0];
  53. field = pevent_find_common_field(event, type);
  54. if (!field)
  55. return 0;
  56. *offset = field->offset;
  57. *size = field->size;
  58. }
  59. return pevent_read_number(pevent, context->event_data + *offset, *size);
  60. }
  61. int common_lock_depth(struct scripting_context *context)
  62. {
  63. static int offset;
  64. static int size;
  65. int ret;
  66. ret = get_common_field(context, &size, &offset,
  67. "common_lock_depth");
  68. if (ret < 0)
  69. return -1;
  70. return ret;
  71. }
  72. int common_flags(struct scripting_context *context)
  73. {
  74. static int offset;
  75. static int size;
  76. int ret;
  77. ret = get_common_field(context, &size, &offset,
  78. "common_flags");
  79. if (ret < 0)
  80. return -1;
  81. return ret;
  82. }
  83. int common_pc(struct scripting_context *context)
  84. {
  85. static int offset;
  86. static int size;
  87. int ret;
  88. ret = get_common_field(context, &size, &offset,
  89. "common_preempt_count");
  90. if (ret < 0)
  91. return -1;
  92. return ret;
  93. }
  94. unsigned long long
  95. raw_field_value(struct event_format *event, const char *name, void *data)
  96. {
  97. struct format_field *field;
  98. unsigned long long val;
  99. field = pevent_find_any_field(event, name);
  100. if (!field)
  101. return 0ULL;
  102. pevent_read_number_field(field, data, &val);
  103. return val;
  104. }
  105. void *raw_field_ptr(struct event_format *event, const char *name, void *data)
  106. {
  107. struct format_field *field;
  108. field = pevent_find_any_field(event, name);
  109. if (!field)
  110. return NULL;
  111. if (field->flags & FIELD_IS_DYNAMIC) {
  112. int offset;
  113. offset = *(int *)(data + field->offset);
  114. offset &= 0xffff;
  115. return data + offset;
  116. }
  117. return data + field->offset;
  118. }
  119. int trace_parse_common_type(struct pevent *pevent, void *data)
  120. {
  121. struct pevent_record record;
  122. record.data = data;
  123. return pevent_data_type(pevent, &record);
  124. }
  125. int trace_parse_common_pid(struct pevent *pevent, void *data)
  126. {
  127. struct pevent_record record;
  128. record.data = data;
  129. return pevent_data_pid(pevent, &record);
  130. }
  131. unsigned long long read_size(struct event_format *event, void *ptr, int size)
  132. {
  133. return pevent_read_number(event->pevent, ptr, size);
  134. }
  135. void event_format__print(struct event_format *event,
  136. int cpu, void *data, int size)
  137. {
  138. struct pevent_record record;
  139. struct trace_seq s;
  140. memset(&record, 0, sizeof(record));
  141. record.cpu = cpu;
  142. record.size = size;
  143. record.data = data;
  144. trace_seq_init(&s);
  145. pevent_event_info(&s, event, &record);
  146. trace_seq_do_printf(&s);
  147. }
  148. void parse_proc_kallsyms(struct pevent *pevent,
  149. char *file, unsigned int size __maybe_unused)
  150. {
  151. unsigned long long addr;
  152. char *func;
  153. char *line;
  154. char *next = NULL;
  155. char *addr_str;
  156. char *mod;
  157. char *fmt;
  158. line = strtok_r(file, "\n", &next);
  159. while (line) {
  160. mod = NULL;
  161. addr_str = strtok_r(line, " ", &fmt);
  162. addr = strtoull(addr_str, NULL, 16);
  163. /* skip character */
  164. strtok_r(NULL, " ", &fmt);
  165. func = strtok_r(NULL, "\t", &fmt);
  166. mod = strtok_r(NULL, "]", &fmt);
  167. /* truncate the extra '[' */
  168. if (mod)
  169. mod = mod + 1;
  170. pevent_register_function(pevent, func, addr, mod);
  171. line = strtok_r(NULL, "\n", &next);
  172. }
  173. }
  174. void parse_ftrace_printk(struct pevent *pevent,
  175. char *file, unsigned int size __maybe_unused)
  176. {
  177. unsigned long long addr;
  178. char *printk;
  179. char *line;
  180. char *next = NULL;
  181. char *addr_str;
  182. char *fmt;
  183. line = strtok_r(file, "\n", &next);
  184. while (line) {
  185. addr_str = strtok_r(line, ":", &fmt);
  186. if (!addr_str) {
  187. warning("printk format with empty entry");
  188. break;
  189. }
  190. addr = strtoull(addr_str, NULL, 16);
  191. /* fmt still has a space, skip it */
  192. printk = strdup(fmt+1);
  193. line = strtok_r(NULL, "\n", &next);
  194. pevent_register_print_string(pevent, printk, addr);
  195. }
  196. }
  197. int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size)
  198. {
  199. return pevent_parse_event(pevent, buf, size, "ftrace");
  200. }
  201. int parse_event_file(struct pevent *pevent,
  202. char *buf, unsigned long size, char *sys)
  203. {
  204. return pevent_parse_event(pevent, buf, size, sys);
  205. }
  206. struct event_format *trace_find_next_event(struct pevent *pevent,
  207. struct event_format *event)
  208. {
  209. static int idx;
  210. if (!pevent || !pevent->events)
  211. return NULL;
  212. if (!event) {
  213. idx = 0;
  214. return pevent->events[0];
  215. }
  216. if (idx < pevent->nr_events && event == pevent->events[idx]) {
  217. idx++;
  218. if (idx == pevent->nr_events)
  219. return NULL;
  220. return pevent->events[idx];
  221. }
  222. for (idx = 1; idx < pevent->nr_events; idx++) {
  223. if (event == pevent->events[idx - 1])
  224. return pevent->events[idx];
  225. }
  226. return NULL;
  227. }
  228. struct flag {
  229. const char *name;
  230. unsigned long long value;
  231. };
  232. static const struct flag flags[] = {
  233. { "HI_SOFTIRQ", 0 },
  234. { "TIMER_SOFTIRQ", 1 },
  235. { "NET_TX_SOFTIRQ", 2 },
  236. { "NET_RX_SOFTIRQ", 3 },
  237. { "BLOCK_SOFTIRQ", 4 },
  238. { "BLOCK_IOPOLL_SOFTIRQ", 5 },
  239. { "TASKLET_SOFTIRQ", 6 },
  240. { "SCHED_SOFTIRQ", 7 },
  241. { "HRTIMER_SOFTIRQ", 8 },
  242. { "RCU_SOFTIRQ", 9 },
  243. { "HRTIMER_NORESTART", 0 },
  244. { "HRTIMER_RESTART", 1 },
  245. };
  246. unsigned long long eval_flag(const char *flag)
  247. {
  248. int i;
  249. /*
  250. * Some flags in the format files do not get converted.
  251. * If the flag is not numeric, see if it is something that
  252. * we already know about.
  253. */
  254. if (isdigit(flag[0]))
  255. return strtoull(flag, NULL, 0);
  256. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  257. if (strcmp(flags[i].name, flag) == 0)
  258. return flags[i].value;
  259. return 0;
  260. }