trace-event-parse.c 6.7 KB

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