trace-event-parse.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include "debug.h"
  10. #include "trace-event.h"
  11. #include <linux/ctype.h>
  12. #include <linux/kernel.h>
  13. #include <event-parse.h>
  14. static int get_common_field(struct scripting_context *context,
  15. int *offset, int *size, const char *type)
  16. {
  17. struct tep_handle *pevent = context->pevent;
  18. struct tep_event *event;
  19. struct tep_format_field *field;
  20. if (!*size) {
  21. event = tep_get_first_event(pevent);
  22. if (!event)
  23. return 0;
  24. field = tep_find_common_field(event, type);
  25. if (!field)
  26. return 0;
  27. *offset = field->offset;
  28. *size = field->size;
  29. }
  30. return tep_read_number(pevent, context->event_data + *offset, *size);
  31. }
  32. int common_lock_depth(struct scripting_context *context)
  33. {
  34. static int offset;
  35. static int size;
  36. int ret;
  37. ret = get_common_field(context, &size, &offset,
  38. "common_lock_depth");
  39. if (ret < 0)
  40. return -1;
  41. return ret;
  42. }
  43. int common_flags(struct scripting_context *context)
  44. {
  45. static int offset;
  46. static int size;
  47. int ret;
  48. ret = get_common_field(context, &size, &offset,
  49. "common_flags");
  50. if (ret < 0)
  51. return -1;
  52. return ret;
  53. }
  54. int common_pc(struct scripting_context *context)
  55. {
  56. static int offset;
  57. static int size;
  58. int ret;
  59. ret = get_common_field(context, &size, &offset,
  60. "common_preempt_count");
  61. if (ret < 0)
  62. return -1;
  63. return ret;
  64. }
  65. unsigned long long
  66. raw_field_value(struct tep_event *event, const char *name, void *data)
  67. {
  68. struct tep_format_field *field;
  69. unsigned long long val;
  70. field = tep_find_any_field(event, name);
  71. if (!field)
  72. return 0ULL;
  73. tep_read_number_field(field, data, &val);
  74. return val;
  75. }
  76. unsigned long long read_size(struct tep_event *event, void *ptr, int size)
  77. {
  78. return tep_read_number(event->tep, ptr, size);
  79. }
  80. void event_format__fprintf(const struct tep_event *event,
  81. int cpu, void *data, int size, FILE *fp)
  82. {
  83. struct tep_record record;
  84. struct trace_seq s;
  85. memset(&record, 0, sizeof(record));
  86. record.cpu = cpu;
  87. record.size = size;
  88. record.data = data;
  89. trace_seq_init(&s);
  90. tep_print_event(event->tep, &s, &record, "%s", TEP_PRINT_INFO);
  91. trace_seq_do_fprintf(&s, fp);
  92. trace_seq_destroy(&s);
  93. }
  94. /*
  95. * prev_state is of size long, which is 32 bits on 32 bit architectures.
  96. * As it needs to have the same bits for both 32 bit and 64 bit architectures
  97. * we can just assume that the flags we care about will all be within
  98. * the 32 bits.
  99. */
  100. #define MAX_STATE_BITS 32
  101. static const char *convert_sym(struct tep_print_flag_sym *sym)
  102. {
  103. static char save_states[MAX_STATE_BITS + 1];
  104. memset(save_states, 0, sizeof(save_states));
  105. /* This is the flags for the prev_state_field, now make them into a string */
  106. for (; sym; sym = sym->next) {
  107. long bitmask = strtoul(sym->value, NULL, 0);
  108. int i;
  109. for (i = 0; !(bitmask & 1); i++)
  110. bitmask >>= 1;
  111. if (i >= MAX_STATE_BITS)
  112. continue;
  113. save_states[i] = sym->str[0];
  114. }
  115. return save_states;
  116. }
  117. static struct tep_print_arg_field *
  118. find_arg_field(struct tep_format_field *prev_state_field, struct tep_print_arg *arg)
  119. {
  120. struct tep_print_arg_field *field;
  121. if (!arg)
  122. return NULL;
  123. if (arg->type == TEP_PRINT_FIELD)
  124. return &arg->field;
  125. if (arg->type == TEP_PRINT_OP) {
  126. field = find_arg_field(prev_state_field, arg->op.left);
  127. if (field && field->field == prev_state_field)
  128. return field;
  129. field = find_arg_field(prev_state_field, arg->op.right);
  130. if (field && field->field == prev_state_field)
  131. return field;
  132. }
  133. return NULL;
  134. }
  135. static struct tep_print_flag_sym *
  136. test_flags(struct tep_format_field *prev_state_field, struct tep_print_arg *arg)
  137. {
  138. struct tep_print_arg_field *field;
  139. field = find_arg_field(prev_state_field, arg->flags.field);
  140. if (!field)
  141. return NULL;
  142. return arg->flags.flags;
  143. }
  144. static struct tep_print_flag_sym *
  145. search_op(struct tep_format_field *prev_state_field, struct tep_print_arg *arg)
  146. {
  147. struct tep_print_flag_sym *sym = NULL;
  148. if (!arg)
  149. return NULL;
  150. if (arg->type == TEP_PRINT_OP) {
  151. sym = search_op(prev_state_field, arg->op.left);
  152. if (sym)
  153. return sym;
  154. sym = search_op(prev_state_field, arg->op.right);
  155. if (sym)
  156. return sym;
  157. } else if (arg->type == TEP_PRINT_FLAGS) {
  158. sym = test_flags(prev_state_field, arg);
  159. }
  160. return sym;
  161. }
  162. const char *parse_task_states(struct tep_format_field *state_field)
  163. {
  164. struct tep_print_flag_sym *sym;
  165. struct tep_print_arg *arg;
  166. struct tep_event *event;
  167. event = state_field->event;
  168. /*
  169. * Look at the event format fields, and search for where
  170. * the prev_state is parsed via the format flags.
  171. */
  172. for (arg = event->print_fmt.args; arg; arg = arg->next) {
  173. /*
  174. * Currently, the __print_flags() for the prev_state
  175. * is embedded in operations, so they too must be
  176. * searched.
  177. */
  178. sym = search_op(state_field, arg);
  179. if (sym)
  180. return convert_sym(sym);
  181. }
  182. return NULL;
  183. }
  184. void parse_ftrace_printk(struct tep_handle *pevent,
  185. char *file, unsigned int size __maybe_unused)
  186. {
  187. unsigned long long addr;
  188. char *printk;
  189. char *line;
  190. char *next = NULL;
  191. char *addr_str;
  192. char *fmt = NULL;
  193. line = strtok_r(file, "\n", &next);
  194. while (line) {
  195. addr_str = strtok_r(line, ":", &fmt);
  196. if (!addr_str) {
  197. pr_warning("printk format with empty entry");
  198. break;
  199. }
  200. addr = strtoull(addr_str, NULL, 16);
  201. /* fmt still has a space, skip it */
  202. printk = strdup(fmt+1);
  203. line = strtok_r(NULL, "\n", &next);
  204. tep_register_print_string(pevent, printk, addr);
  205. free(printk);
  206. }
  207. }
  208. void parse_saved_cmdline(struct tep_handle *pevent,
  209. char *file, unsigned int size __maybe_unused)
  210. {
  211. char comm[17]; /* Max comm length in the kernel is 16. */
  212. char *line;
  213. char *next = NULL;
  214. int pid;
  215. line = strtok_r(file, "\n", &next);
  216. while (line) {
  217. if (sscanf(line, "%d %16s", &pid, comm) == 2)
  218. tep_register_comm(pevent, comm, pid);
  219. line = strtok_r(NULL, "\n", &next);
  220. }
  221. }
  222. int parse_ftrace_file(struct tep_handle *pevent, char *buf, unsigned long size)
  223. {
  224. return tep_parse_event(pevent, buf, size, "ftrace");
  225. }
  226. int parse_event_file(struct tep_handle *pevent,
  227. char *buf, unsigned long size, char *sys)
  228. {
  229. return tep_parse_event(pevent, buf, size, sys);
  230. }
  231. struct flag {
  232. const char *name;
  233. unsigned long long value;
  234. };
  235. static const struct flag flags[] = {
  236. { "HI_SOFTIRQ", 0 },
  237. { "TIMER_SOFTIRQ", 1 },
  238. { "NET_TX_SOFTIRQ", 2 },
  239. { "NET_RX_SOFTIRQ", 3 },
  240. { "BLOCK_SOFTIRQ", 4 },
  241. { "IRQ_POLL_SOFTIRQ", 5 },
  242. { "TASKLET_SOFTIRQ", 6 },
  243. { "SCHED_SOFTIRQ", 7 },
  244. { "HRTIMER_SOFTIRQ", 8 },
  245. { "RCU_SOFTIRQ", 9 },
  246. { "HRTIMER_NORESTART", 0 },
  247. { "HRTIMER_RESTART", 1 },
  248. };
  249. unsigned long long eval_flag(const char *flag)
  250. {
  251. int i;
  252. /*
  253. * Some flags in the format files do not get converted.
  254. * If the flag is not numeric, see if it is something that
  255. * we already know about.
  256. */
  257. if (isdigit(flag[0]))
  258. return strtoull(flag, NULL, 0);
  259. for (i = 0; i < (int)(ARRAY_SIZE(flags)); i++)
  260. if (strcmp(flags[i].name, flag) == 0)
  261. return flags[i].value;
  262. return 0;
  263. }