trace-event-parse.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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_flag(pevent, PEVENT_NSEC_OUTPUT);
  42. pevent_set_file_bigendian(pevent, file_bigendian);
  43. pevent_set_host_bigendian(pevent, host_bigendian);
  44. return 0;
  45. }
  46. static int get_common_field(struct scripting_context *context,
  47. int *offset, int *size, const char *type)
  48. {
  49. struct event_format *event;
  50. struct format_field *field;
  51. if (!*size) {
  52. if (!pevent->events)
  53. return 0;
  54. event = pevent->events[0];
  55. field = pevent_find_common_field(event, type);
  56. if (!field)
  57. return 0;
  58. *offset = field->offset;
  59. *size = field->size;
  60. }
  61. return pevent_read_number(pevent, context->event_data + *offset, *size);
  62. }
  63. int common_lock_depth(struct scripting_context *context)
  64. {
  65. static int offset;
  66. static int size;
  67. int ret;
  68. ret = get_common_field(context, &size, &offset,
  69. "common_lock_depth");
  70. if (ret < 0)
  71. return -1;
  72. return ret;
  73. }
  74. int common_flags(struct scripting_context *context)
  75. {
  76. static int offset;
  77. static int size;
  78. int ret;
  79. ret = get_common_field(context, &size, &offset,
  80. "common_flags");
  81. if (ret < 0)
  82. return -1;
  83. return ret;
  84. }
  85. int common_pc(struct scripting_context *context)
  86. {
  87. static int offset;
  88. static int size;
  89. int ret;
  90. ret = get_common_field(context, &size, &offset,
  91. "common_preempt_count");
  92. if (ret < 0)
  93. return -1;
  94. return ret;
  95. }
  96. unsigned long long
  97. raw_field_value(struct event_format *event, const char *name, void *data)
  98. {
  99. struct format_field *field;
  100. unsigned long long val;
  101. field = pevent_find_any_field(event, name);
  102. if (!field)
  103. return 0ULL;
  104. pevent_read_number_field(field, data, &val);
  105. return val;
  106. }
  107. void *raw_field_ptr(struct event_format *event, const char *name, void *data)
  108. {
  109. struct format_field *field;
  110. field = pevent_find_any_field(event, name);
  111. if (!field)
  112. return NULL;
  113. if (field->flags & FIELD_IS_DYNAMIC) {
  114. int offset;
  115. offset = *(int *)(data + field->offset);
  116. offset &= 0xffff;
  117. return data + offset;
  118. }
  119. return data + field->offset;
  120. }
  121. int trace_parse_common_type(void *data)
  122. {
  123. struct pevent_record record;
  124. record.data = data;
  125. return pevent_data_type(pevent, &record);
  126. }
  127. int trace_parse_common_pid(void *data)
  128. {
  129. struct pevent_record record;
  130. record.data = data;
  131. return pevent_data_pid(pevent, &record);
  132. }
  133. unsigned long long read_size(void *ptr, int size)
  134. {
  135. return pevent_read_number(pevent, ptr, size);
  136. }
  137. struct event_format *trace_find_event(int type)
  138. {
  139. return pevent_find_event(pevent, type);
  140. }
  141. void print_trace_event(int cpu, void *data, int size)
  142. {
  143. struct event_format *event;
  144. struct pevent_record record;
  145. struct trace_seq s;
  146. int type;
  147. type = trace_parse_common_type(data);
  148. event = trace_find_event(type);
  149. if (!event) {
  150. warning("ug! no event found for type %d", type);
  151. return;
  152. }
  153. memset(&record, 0, sizeof(record));
  154. record.cpu = cpu;
  155. record.size = size;
  156. record.data = data;
  157. trace_seq_init(&s);
  158. pevent_print_event(pevent, &s, &record);
  159. trace_seq_do_printf(&s);
  160. printf("\n");
  161. }
  162. void print_event(int cpu, void *data, int size, unsigned long long nsecs,
  163. char *comm)
  164. {
  165. struct pevent_record record;
  166. struct trace_seq s;
  167. int pid;
  168. pevent->latency_format = latency_format;
  169. record.ts = nsecs;
  170. record.cpu = cpu;
  171. record.size = size;
  172. record.data = data;
  173. pid = pevent_data_pid(pevent, &record);
  174. if (!pevent_pid_is_registered(pevent, pid))
  175. pevent_register_comm(pevent, comm, pid);
  176. trace_seq_init(&s);
  177. pevent_print_event(pevent, &s, &record);
  178. trace_seq_do_printf(&s);
  179. printf("\n");
  180. }
  181. void parse_proc_kallsyms(char *file, unsigned int size __unused)
  182. {
  183. unsigned long long addr;
  184. char *func;
  185. char *line;
  186. char *next = NULL;
  187. char *addr_str;
  188. char *mod;
  189. char ch;
  190. line = strtok_r(file, "\n", &next);
  191. while (line) {
  192. mod = NULL;
  193. sscanf(line, "%as %c %as\t[%as",
  194. (float *)(void *)&addr_str, /* workaround gcc warning */
  195. &ch, (float *)(void *)&func, (float *)(void *)&mod);
  196. addr = strtoull(addr_str, NULL, 16);
  197. free(addr_str);
  198. /* truncate the extra ']' */
  199. if (mod)
  200. mod[strlen(mod) - 1] = 0;
  201. pevent_register_function(pevent, func, addr, mod);
  202. free(func);
  203. free(mod);
  204. line = strtok_r(NULL, "\n", &next);
  205. }
  206. }
  207. void parse_ftrace_printk(char *file, unsigned int size __unused)
  208. {
  209. unsigned long long addr;
  210. char *printk;
  211. char *line;
  212. char *next = NULL;
  213. char *addr_str;
  214. char *fmt;
  215. line = strtok_r(file, "\n", &next);
  216. while (line) {
  217. addr_str = strtok_r(line, ":", &fmt);
  218. if (!addr_str) {
  219. warning("printk format with empty entry");
  220. break;
  221. }
  222. addr = strtoull(addr_str, NULL, 16);
  223. /* fmt still has a space, skip it */
  224. printk = strdup(fmt+1);
  225. line = strtok_r(NULL, "\n", &next);
  226. pevent_register_print_string(pevent, printk, addr);
  227. }
  228. }
  229. int parse_ftrace_file(char *buf, unsigned long size)
  230. {
  231. return pevent_parse_event(pevent, buf, size, "ftrace");
  232. }
  233. int parse_event_file(char *buf, unsigned long size, char *sys)
  234. {
  235. return pevent_parse_event(pevent, buf, size, sys);
  236. }
  237. struct event_format *trace_find_next_event(struct event_format *event)
  238. {
  239. static int idx;
  240. if (!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. }