trace-event-parse.c 6.7 KB

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