plugin_function.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  3. *
  4. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation;
  8. * version 2.1 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 Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this program; if not, see <http://www.gnu.org/licenses>
  17. *
  18. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "event-parse.h"
  24. #include "event-utils.h"
  25. #include "trace-seq.h"
  26. static struct func_stack {
  27. int size;
  28. char **stack;
  29. } *fstack;
  30. static int cpus = -1;
  31. #define STK_BLK 10
  32. struct tep_plugin_option plugin_options[] =
  33. {
  34. {
  35. .name = "parent",
  36. .plugin_alias = "ftrace",
  37. .description =
  38. "Print parent of functions for function events",
  39. },
  40. {
  41. .name = "indent",
  42. .plugin_alias = "ftrace",
  43. .description =
  44. "Try to show function call indents, based on parents",
  45. .set = 1,
  46. },
  47. {
  48. .name = NULL,
  49. }
  50. };
  51. static struct tep_plugin_option *ftrace_parent = &plugin_options[0];
  52. static struct tep_plugin_option *ftrace_indent = &plugin_options[1];
  53. static void add_child(struct func_stack *stack, const char *child, int pos)
  54. {
  55. int i;
  56. if (!child)
  57. return;
  58. if (pos < stack->size)
  59. free(stack->stack[pos]);
  60. else {
  61. char **ptr;
  62. ptr = realloc(stack->stack, sizeof(char *) *
  63. (stack->size + STK_BLK));
  64. if (!ptr) {
  65. warning("could not allocate plugin memory\n");
  66. return;
  67. }
  68. stack->stack = ptr;
  69. for (i = stack->size; i < stack->size + STK_BLK; i++)
  70. stack->stack[i] = NULL;
  71. stack->size += STK_BLK;
  72. }
  73. stack->stack[pos] = strdup(child);
  74. }
  75. static int add_and_get_index(const char *parent, const char *child, int cpu)
  76. {
  77. int i;
  78. if (cpu < 0)
  79. return 0;
  80. if (cpu > cpus) {
  81. struct func_stack *ptr;
  82. ptr = realloc(fstack, sizeof(*fstack) * (cpu + 1));
  83. if (!ptr) {
  84. warning("could not allocate plugin memory\n");
  85. return 0;
  86. }
  87. fstack = ptr;
  88. /* Account for holes in the cpu count */
  89. for (i = cpus + 1; i <= cpu; i++)
  90. memset(&fstack[i], 0, sizeof(fstack[i]));
  91. cpus = cpu;
  92. }
  93. for (i = 0; i < fstack[cpu].size && fstack[cpu].stack[i]; i++) {
  94. if (strcmp(parent, fstack[cpu].stack[i]) == 0) {
  95. add_child(&fstack[cpu], child, i+1);
  96. return i;
  97. }
  98. }
  99. /* Not found */
  100. add_child(&fstack[cpu], parent, 0);
  101. add_child(&fstack[cpu], child, 1);
  102. return 0;
  103. }
  104. static int function_handler(struct trace_seq *s, struct tep_record *record,
  105. struct tep_event_format *event, void *context)
  106. {
  107. struct tep_handle *pevent = event->pevent;
  108. unsigned long long function;
  109. unsigned long long pfunction;
  110. const char *func;
  111. const char *parent;
  112. int index = 0;
  113. if (tep_get_field_val(s, event, "ip", record, &function, 1))
  114. return trace_seq_putc(s, '!');
  115. func = tep_find_function(pevent, function);
  116. if (tep_get_field_val(s, event, "parent_ip", record, &pfunction, 1))
  117. return trace_seq_putc(s, '!');
  118. parent = tep_find_function(pevent, pfunction);
  119. if (parent && ftrace_indent->set)
  120. index = add_and_get_index(parent, func, record->cpu);
  121. trace_seq_printf(s, "%*s", index*3, "");
  122. if (func)
  123. trace_seq_printf(s, "%s", func);
  124. else
  125. trace_seq_printf(s, "0x%llx", function);
  126. if (ftrace_parent->set) {
  127. trace_seq_printf(s, " <-- ");
  128. if (parent)
  129. trace_seq_printf(s, "%s", parent);
  130. else
  131. trace_seq_printf(s, "0x%llx", pfunction);
  132. }
  133. return 0;
  134. }
  135. int TEP_PLUGIN_LOADER(struct tep_handle *pevent)
  136. {
  137. tep_register_event_handler(pevent, -1, "ftrace", "function",
  138. function_handler, NULL);
  139. tep_plugin_add_options("ftrace", plugin_options);
  140. return 0;
  141. }
  142. void TEP_PLUGIN_UNLOADER(struct tep_handle *pevent)
  143. {
  144. int i, x;
  145. tep_unregister_event_handler(pevent, -1, "ftrace", "function",
  146. function_handler, NULL);
  147. for (i = 0; i <= cpus; i++) {
  148. for (x = 0; x < fstack[i].size && fstack[i].stack[x]; x++)
  149. free(fstack[i].stack[x]);
  150. free(fstack[i].stack);
  151. }
  152. tep_plugin_remove_options(plugin_options);
  153. free(fstack);
  154. fstack = NULL;
  155. cpus = -1;
  156. }