plugin_function.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <[email protected]>
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "event-parse.h"
  9. #include "event-utils.h"
  10. #include "trace-seq.h"
  11. static struct func_stack {
  12. int size;
  13. char **stack;
  14. } *fstack;
  15. static int cpus = -1;
  16. #define STK_BLK 10
  17. struct tep_plugin_option plugin_options[] =
  18. {
  19. {
  20. .name = "parent",
  21. .plugin_alias = "ftrace",
  22. .description =
  23. "Print parent of functions for function events",
  24. },
  25. {
  26. .name = "indent",
  27. .plugin_alias = "ftrace",
  28. .description =
  29. "Try to show function call indents, based on parents",
  30. .set = 1,
  31. },
  32. {
  33. .name = "offset",
  34. .plugin_alias = "ftrace",
  35. .description =
  36. "Show function names as well as their offsets",
  37. .set = 0,
  38. },
  39. {
  40. .name = NULL,
  41. }
  42. };
  43. static struct tep_plugin_option *ftrace_parent = &plugin_options[0];
  44. static struct tep_plugin_option *ftrace_indent = &plugin_options[1];
  45. static struct tep_plugin_option *ftrace_offset = &plugin_options[2];
  46. static void add_child(struct func_stack *stack, const char *child, int pos)
  47. {
  48. int i;
  49. if (!child)
  50. return;
  51. if (pos < stack->size)
  52. free(stack->stack[pos]);
  53. else {
  54. char **ptr;
  55. ptr = realloc(stack->stack, sizeof(char *) *
  56. (stack->size + STK_BLK));
  57. if (!ptr) {
  58. warning("could not allocate plugin memory\n");
  59. return;
  60. }
  61. stack->stack = ptr;
  62. for (i = stack->size; i < stack->size + STK_BLK; i++)
  63. stack->stack[i] = NULL;
  64. stack->size += STK_BLK;
  65. }
  66. stack->stack[pos] = strdup(child);
  67. }
  68. static int add_and_get_index(const char *parent, const char *child, int cpu)
  69. {
  70. int i;
  71. if (cpu < 0)
  72. return 0;
  73. if (cpu > cpus) {
  74. struct func_stack *ptr;
  75. ptr = realloc(fstack, sizeof(*fstack) * (cpu + 1));
  76. if (!ptr) {
  77. warning("could not allocate plugin memory\n");
  78. return 0;
  79. }
  80. fstack = ptr;
  81. /* Account for holes in the cpu count */
  82. for (i = cpus + 1; i <= cpu; i++)
  83. memset(&fstack[i], 0, sizeof(fstack[i]));
  84. cpus = cpu;
  85. }
  86. for (i = 0; i < fstack[cpu].size && fstack[cpu].stack[i]; i++) {
  87. if (strcmp(parent, fstack[cpu].stack[i]) == 0) {
  88. add_child(&fstack[cpu], child, i+1);
  89. return i;
  90. }
  91. }
  92. /* Not found */
  93. add_child(&fstack[cpu], parent, 0);
  94. add_child(&fstack[cpu], child, 1);
  95. return 0;
  96. }
  97. static void show_function(struct trace_seq *s, struct tep_handle *tep,
  98. const char *func, unsigned long long function)
  99. {
  100. unsigned long long offset;
  101. trace_seq_printf(s, "%s", func);
  102. if (ftrace_offset->set) {
  103. offset = tep_find_function_address(tep, function);
  104. trace_seq_printf(s, "+0x%x ", (int)(function - offset));
  105. }
  106. }
  107. static int function_handler(struct trace_seq *s, struct tep_record *record,
  108. struct tep_event *event, void *context)
  109. {
  110. struct tep_handle *tep = event->tep;
  111. unsigned long long function;
  112. unsigned long long pfunction;
  113. const char *func;
  114. const char *parent;
  115. int index = 0;
  116. if (tep_get_field_val(s, event, "ip", record, &function, 1))
  117. return trace_seq_putc(s, '!');
  118. func = tep_find_function(tep, function);
  119. if (tep_get_field_val(s, event, "parent_ip", record, &pfunction, 1))
  120. return trace_seq_putc(s, '!');
  121. parent = tep_find_function(tep, pfunction);
  122. if (parent && ftrace_indent->set)
  123. index = add_and_get_index(parent, func, record->cpu);
  124. trace_seq_printf(s, "%*s", index*3, "");
  125. if (func)
  126. show_function(s, tep, func, function);
  127. else
  128. trace_seq_printf(s, "0x%llx", function);
  129. if (ftrace_parent->set) {
  130. trace_seq_printf(s, " <-- ");
  131. if (parent)
  132. show_function(s, tep, parent, pfunction);
  133. else
  134. trace_seq_printf(s, "0x%llx", pfunction);
  135. }
  136. return 0;
  137. }
  138. static int
  139. trace_stack_handler(struct trace_seq *s, struct tep_record *record,
  140. struct tep_event *event, void *context)
  141. {
  142. struct tep_format_field *field;
  143. unsigned long long addr;
  144. const char *func;
  145. int long_size;
  146. void *data = record->data;
  147. field = tep_find_any_field(event, "caller");
  148. if (!field) {
  149. trace_seq_printf(s, "<CANT FIND FIELD %s>", "caller");
  150. return 0;
  151. }
  152. trace_seq_puts(s, "<stack trace >\n");
  153. long_size = tep_get_long_size(event->tep);
  154. for (data += field->offset; data < record->data + record->size;
  155. data += long_size) {
  156. addr = tep_read_number(event->tep, data, long_size);
  157. if ((long_size == 8 && addr == (unsigned long long)-1) ||
  158. ((int)addr == -1))
  159. break;
  160. func = tep_find_function(event->tep, addr);
  161. if (func)
  162. trace_seq_printf(s, "=> %s (%llx)\n", func, addr);
  163. else
  164. trace_seq_printf(s, "=> %llx\n", addr);
  165. }
  166. return 0;
  167. }
  168. static int
  169. trace_raw_data_handler(struct trace_seq *s, struct tep_record *record,
  170. struct tep_event *event, void *context)
  171. {
  172. struct tep_format_field *field;
  173. unsigned long long id;
  174. int long_size;
  175. void *data = record->data;
  176. if (tep_get_field_val(s, event, "id", record, &id, 1))
  177. return trace_seq_putc(s, '!');
  178. trace_seq_printf(s, "# %llx", id);
  179. field = tep_find_any_field(event, "buf");
  180. if (!field) {
  181. trace_seq_printf(s, "<CANT FIND FIELD %s>", "buf");
  182. return 0;
  183. }
  184. long_size = tep_get_long_size(event->tep);
  185. for (data += field->offset; data < record->data + record->size;
  186. data += long_size) {
  187. int size = sizeof(long);
  188. int left = (record->data + record->size) - data;
  189. int i;
  190. if (size > left)
  191. size = left;
  192. for (i = 0; i < size; i++)
  193. trace_seq_printf(s, " %02x", *(unsigned char *)(data + i));
  194. }
  195. return 0;
  196. }
  197. int TEP_PLUGIN_LOADER(struct tep_handle *tep)
  198. {
  199. tep_register_event_handler(tep, -1, "ftrace", "function",
  200. function_handler, NULL);
  201. tep_register_event_handler(tep, -1, "ftrace", "kernel_stack",
  202. trace_stack_handler, NULL);
  203. tep_register_event_handler(tep, -1, "ftrace", "raw_data",
  204. trace_raw_data_handler, NULL);
  205. tep_plugin_add_options("ftrace", plugin_options);
  206. return 0;
  207. }
  208. void TEP_PLUGIN_UNLOADER(struct tep_handle *tep)
  209. {
  210. int i, x;
  211. tep_unregister_event_handler(tep, -1, "ftrace", "function",
  212. function_handler, NULL);
  213. for (i = 0; i <= cpus; i++) {
  214. for (x = 0; x < fstack[i].size && fstack[i].stack[x]; x++)
  215. free(fstack[i].stack[x]);
  216. free(fstack[i].stack);
  217. }
  218. tep_plugin_remove_options(plugin_options);
  219. free(fstack);
  220. fstack = NULL;
  221. cpus = -1;
  222. }