trace-event-parse.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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__fprintf(struct event_format *event,
  96. int cpu, void *data, int size, FILE *fp)
  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_fprintf(&s, fp);
  107. trace_seq_destroy(&s);
  108. }
  109. void event_format__print(struct event_format *event,
  110. int cpu, void *data, int size)
  111. {
  112. return event_format__fprintf(event, cpu, data, size, stdout);
  113. }
  114. void parse_ftrace_printk(struct pevent *pevent,
  115. char *file, unsigned int size __maybe_unused)
  116. {
  117. unsigned long long addr;
  118. char *printk;
  119. char *line;
  120. char *next = NULL;
  121. char *addr_str;
  122. char *fmt = NULL;
  123. line = strtok_r(file, "\n", &next);
  124. while (line) {
  125. addr_str = strtok_r(line, ":", &fmt);
  126. if (!addr_str) {
  127. warning("printk format with empty entry");
  128. break;
  129. }
  130. addr = strtoull(addr_str, NULL, 16);
  131. /* fmt still has a space, skip it */
  132. printk = strdup(fmt+1);
  133. line = strtok_r(NULL, "\n", &next);
  134. pevent_register_print_string(pevent, printk, addr);
  135. }
  136. }
  137. int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size)
  138. {
  139. return pevent_parse_event(pevent, buf, size, "ftrace");
  140. }
  141. int parse_event_file(struct pevent *pevent,
  142. char *buf, unsigned long size, char *sys)
  143. {
  144. return pevent_parse_event(pevent, buf, size, sys);
  145. }
  146. struct event_format *trace_find_next_event(struct pevent *pevent,
  147. struct event_format *event)
  148. {
  149. static int idx;
  150. if (!pevent || !pevent->events)
  151. return NULL;
  152. if (!event) {
  153. idx = 0;
  154. return pevent->events[0];
  155. }
  156. if (idx < pevent->nr_events && event == pevent->events[idx]) {
  157. idx++;
  158. if (idx == pevent->nr_events)
  159. return NULL;
  160. return pevent->events[idx];
  161. }
  162. for (idx = 1; idx < pevent->nr_events; idx++) {
  163. if (event == pevent->events[idx - 1])
  164. return pevent->events[idx];
  165. }
  166. return NULL;
  167. }
  168. struct flag {
  169. const char *name;
  170. unsigned long long value;
  171. };
  172. static const struct flag flags[] = {
  173. { "HI_SOFTIRQ", 0 },
  174. { "TIMER_SOFTIRQ", 1 },
  175. { "NET_TX_SOFTIRQ", 2 },
  176. { "NET_RX_SOFTIRQ", 3 },
  177. { "BLOCK_SOFTIRQ", 4 },
  178. { "BLOCK_IOPOLL_SOFTIRQ", 5 },
  179. { "TASKLET_SOFTIRQ", 6 },
  180. { "SCHED_SOFTIRQ", 7 },
  181. { "HRTIMER_SOFTIRQ", 8 },
  182. { "RCU_SOFTIRQ", 9 },
  183. { "HRTIMER_NORESTART", 0 },
  184. { "HRTIMER_RESTART", 1 },
  185. };
  186. unsigned long long eval_flag(const char *flag)
  187. {
  188. int i;
  189. /*
  190. * Some flags in the format files do not get converted.
  191. * If the flag is not numeric, see if it is something that
  192. * we already know about.
  193. */
  194. if (isdigit(flag[0]))
  195. return strtoull(flag, NULL, 0);
  196. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  197. if (strcmp(flags[i].name, flag) == 0)
  198. return flags[i].value;
  199. return 0;
  200. }