trace-event-parse.c 5.8 KB

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