trace-event-parse.c 7.3 KB

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