trace-event-parse.c 7.6 KB

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