plugin_function.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. static struct func_stack {
  26. int size;
  27. char **stack;
  28. } *fstack;
  29. static int cpus = -1;
  30. #define STK_BLK 10
  31. struct pevent_plugin_option plugin_options[] =
  32. {
  33. {
  34. .name = "parent",
  35. .plugin_alias = "ftrace",
  36. .description =
  37. "Print parent of functions for function events",
  38. },
  39. {
  40. .name = "indent",
  41. .plugin_alias = "ftrace",
  42. .description =
  43. "Try to show function call indents, based on parents",
  44. .set = 1,
  45. },
  46. {
  47. .name = NULL,
  48. }
  49. };
  50. static struct pevent_plugin_option *ftrace_parent = &plugin_options[0];
  51. static struct pevent_plugin_option *ftrace_indent = &plugin_options[1];
  52. static void add_child(struct func_stack *stack, const char *child, int pos)
  53. {
  54. int i;
  55. if (!child)
  56. return;
  57. if (pos < stack->size)
  58. free(stack->stack[pos]);
  59. else {
  60. char **ptr;
  61. ptr = realloc(stack->stack, sizeof(char *) *
  62. (stack->size + STK_BLK));
  63. if (!ptr) {
  64. warning("could not allocate plugin memory\n");
  65. return;
  66. }
  67. stack->stack = ptr;
  68. for (i = stack->size; i < stack->size + STK_BLK; i++)
  69. stack->stack[i] = NULL;
  70. stack->size += STK_BLK;
  71. }
  72. stack->stack[pos] = strdup(child);
  73. }
  74. static int add_and_get_index(const char *parent, const char *child, int cpu)
  75. {
  76. int i;
  77. if (cpu < 0)
  78. return 0;
  79. if (cpu > cpus) {
  80. struct func_stack *ptr;
  81. ptr = realloc(fstack, sizeof(*fstack) * (cpu + 1));
  82. if (!ptr) {
  83. warning("could not allocate plugin memory\n");
  84. return 0;
  85. }
  86. fstack = ptr;
  87. /* Account for holes in the cpu count */
  88. for (i = cpus + 1; i <= cpu; i++)
  89. memset(&fstack[i], 0, sizeof(fstack[i]));
  90. cpus = cpu;
  91. }
  92. for (i = 0; i < fstack[cpu].size && fstack[cpu].stack[i]; i++) {
  93. if (strcmp(parent, fstack[cpu].stack[i]) == 0) {
  94. add_child(&fstack[cpu], child, i+1);
  95. return i;
  96. }
  97. }
  98. /* Not found */
  99. add_child(&fstack[cpu], parent, 0);
  100. add_child(&fstack[cpu], child, 1);
  101. return 0;
  102. }
  103. static int function_handler(struct trace_seq *s, struct pevent_record *record,
  104. struct event_format *event, void *context)
  105. {
  106. struct pevent *pevent = event->pevent;
  107. unsigned long long function;
  108. unsigned long long pfunction;
  109. const char *func;
  110. const char *parent;
  111. int index;
  112. if (pevent_get_field_val(s, event, "ip", record, &function, 1))
  113. return trace_seq_putc(s, '!');
  114. func = pevent_find_function(pevent, function);
  115. if (pevent_get_field_val(s, event, "parent_ip", record, &pfunction, 1))
  116. return trace_seq_putc(s, '!');
  117. parent = pevent_find_function(pevent, pfunction);
  118. if (parent && ftrace_indent->set)
  119. index = add_and_get_index(parent, func, record->cpu);
  120. trace_seq_printf(s, "%*s", index*3, "");
  121. if (func)
  122. trace_seq_printf(s, "%s", func);
  123. else
  124. trace_seq_printf(s, "0x%llx", function);
  125. if (ftrace_parent->set) {
  126. trace_seq_printf(s, " <-- ");
  127. if (parent)
  128. trace_seq_printf(s, "%s", parent);
  129. else
  130. trace_seq_printf(s, "0x%llx", pfunction);
  131. }
  132. return 0;
  133. }
  134. int PEVENT_PLUGIN_LOADER(struct pevent *pevent)
  135. {
  136. pevent_register_event_handler(pevent, -1, "ftrace", "function",
  137. function_handler, NULL);
  138. traceevent_plugin_add_options("ftrace", plugin_options);
  139. return 0;
  140. }
  141. void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent)
  142. {
  143. int i, x;
  144. pevent_unregister_event_handler(pevent, -1, "ftrace", "function",
  145. function_handler, NULL);
  146. for (i = 0; i <= cpus; i++) {
  147. for (x = 0; x < fstack[i].size && fstack[i].stack[x]; x++)
  148. free(fstack[i].stack[x]);
  149. free(fstack[i].stack);
  150. }
  151. traceevent_plugin_remove_options(plugin_options);
  152. free(fstack);
  153. fstack = NULL;
  154. cpus = -1;
  155. }