trace-event-parse.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 print_trace_event(struct pevent *pevent, int cpu, void *data, int size)
  149. {
  150. int type = trace_parse_common_type(pevent, data);
  151. struct event_format *event = pevent_find_event(pevent, type);
  152. if (!event) {
  153. warning("ug! no event found for type %d", type);
  154. return;
  155. }
  156. event_format__print(event, cpu, data, size);
  157. }
  158. void print_event(struct pevent *pevent, int cpu, void *data, int size,
  159. unsigned long long nsecs, char *comm)
  160. {
  161. struct pevent_record record;
  162. struct trace_seq s;
  163. int pid;
  164. pevent->latency_format = latency_format;
  165. record.ts = nsecs;
  166. record.cpu = cpu;
  167. record.size = size;
  168. record.data = data;
  169. pid = pevent_data_pid(pevent, &record);
  170. if (!pevent_pid_is_registered(pevent, pid))
  171. pevent_register_comm(pevent, comm, pid);
  172. trace_seq_init(&s);
  173. pevent_print_event(pevent, &s, &record);
  174. trace_seq_do_printf(&s);
  175. printf("\n");
  176. }
  177. void parse_proc_kallsyms(struct pevent *pevent,
  178. char *file, unsigned int size __maybe_unused)
  179. {
  180. unsigned long long addr;
  181. char *func;
  182. char *line;
  183. char *next = NULL;
  184. char *addr_str;
  185. char *mod;
  186. char ch;
  187. line = strtok_r(file, "\n", &next);
  188. while (line) {
  189. mod = NULL;
  190. sscanf(line, "%as %c %as\t[%as",
  191. (float *)(void *)&addr_str, /* workaround gcc warning */
  192. &ch, (float *)(void *)&func, (float *)(void *)&mod);
  193. addr = strtoull(addr_str, NULL, 16);
  194. free(addr_str);
  195. /* truncate the extra ']' */
  196. if (mod)
  197. mod[strlen(mod) - 1] = 0;
  198. pevent_register_function(pevent, func, addr, mod);
  199. free(func);
  200. free(mod);
  201. line = strtok_r(NULL, "\n", &next);
  202. }
  203. }
  204. void parse_ftrace_printk(struct pevent *pevent,
  205. char *file, unsigned int size __maybe_unused)
  206. {
  207. unsigned long long addr;
  208. char *printk;
  209. char *line;
  210. char *next = NULL;
  211. char *addr_str;
  212. char *fmt;
  213. line = strtok_r(file, "\n", &next);
  214. while (line) {
  215. addr_str = strtok_r(line, ":", &fmt);
  216. if (!addr_str) {
  217. warning("printk format with empty entry");
  218. break;
  219. }
  220. addr = strtoull(addr_str, NULL, 16);
  221. /* fmt still has a space, skip it */
  222. printk = strdup(fmt+1);
  223. line = strtok_r(NULL, "\n", &next);
  224. pevent_register_print_string(pevent, printk, addr);
  225. }
  226. }
  227. int parse_ftrace_file(struct pevent *pevent, char *buf, unsigned long size)
  228. {
  229. return pevent_parse_event(pevent, buf, size, "ftrace");
  230. }
  231. int parse_event_file(struct pevent *pevent,
  232. char *buf, unsigned long size, char *sys)
  233. {
  234. return pevent_parse_event(pevent, buf, size, sys);
  235. }
  236. struct event_format *trace_find_next_event(struct pevent *pevent,
  237. struct event_format *event)
  238. {
  239. static int idx;
  240. if (!pevent || !pevent->events)
  241. return NULL;
  242. if (!event) {
  243. idx = 0;
  244. return pevent->events[0];
  245. }
  246. if (idx < pevent->nr_events && event == pevent->events[idx]) {
  247. idx++;
  248. if (idx == pevent->nr_events)
  249. return NULL;
  250. return pevent->events[idx];
  251. }
  252. for (idx = 1; idx < pevent->nr_events; idx++) {
  253. if (event == pevent->events[idx - 1])
  254. return pevent->events[idx];
  255. }
  256. return NULL;
  257. }
  258. struct flag {
  259. const char *name;
  260. unsigned long long value;
  261. };
  262. static const struct flag flags[] = {
  263. { "HI_SOFTIRQ", 0 },
  264. { "TIMER_SOFTIRQ", 1 },
  265. { "NET_TX_SOFTIRQ", 2 },
  266. { "NET_RX_SOFTIRQ", 3 },
  267. { "BLOCK_SOFTIRQ", 4 },
  268. { "BLOCK_IOPOLL_SOFTIRQ", 5 },
  269. { "TASKLET_SOFTIRQ", 6 },
  270. { "SCHED_SOFTIRQ", 7 },
  271. { "HRTIMER_SOFTIRQ", 8 },
  272. { "RCU_SOFTIRQ", 9 },
  273. { "HRTIMER_NORESTART", 0 },
  274. { "HRTIMER_RESTART", 1 },
  275. };
  276. unsigned long long eval_flag(const char *flag)
  277. {
  278. int i;
  279. /*
  280. * Some flags in the format files do not get converted.
  281. * If the flag is not numeric, see if it is something that
  282. * we already know about.
  283. */
  284. if (isdigit(flag[0]))
  285. return strtoull(flag, NULL, 0);
  286. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  287. if (strcmp(flags[i].name, flag) == 0)
  288. return flags[i].value;
  289. return 0;
  290. }