plugin_function.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. static void add_child(struct func_stack *stack, const char *child, int pos)
  32. {
  33. int i;
  34. if (!child)
  35. return;
  36. if (pos < stack->size)
  37. free(stack->stack[pos]);
  38. else {
  39. char **ptr;
  40. ptr = realloc(stack->stack, sizeof(char *) *
  41. (stack->size + STK_BLK));
  42. if (!ptr) {
  43. warning("could not allocate plugin memory\n");
  44. return;
  45. }
  46. stack->stack = ptr;
  47. for (i = stack->size; i < stack->size + STK_BLK; i++)
  48. stack->stack[i] = NULL;
  49. stack->size += STK_BLK;
  50. }
  51. stack->stack[pos] = strdup(child);
  52. }
  53. static int add_and_get_index(const char *parent, const char *child, int cpu)
  54. {
  55. int i;
  56. if (cpu < 0)
  57. return 0;
  58. if (cpu > cpus) {
  59. struct func_stack *ptr;
  60. ptr = realloc(fstack, sizeof(*fstack) * (cpu + 1));
  61. if (!ptr) {
  62. warning("could not allocate plugin memory\n");
  63. return 0;
  64. }
  65. fstack = ptr;
  66. /* Account for holes in the cpu count */
  67. for (i = cpus + 1; i <= cpu; i++)
  68. memset(&fstack[i], 0, sizeof(fstack[i]));
  69. cpus = cpu;
  70. }
  71. for (i = 0; i < fstack[cpu].size && fstack[cpu].stack[i]; i++) {
  72. if (strcmp(parent, fstack[cpu].stack[i]) == 0) {
  73. add_child(&fstack[cpu], child, i+1);
  74. return i;
  75. }
  76. }
  77. /* Not found */
  78. add_child(&fstack[cpu], parent, 0);
  79. add_child(&fstack[cpu], child, 1);
  80. return 0;
  81. }
  82. static int function_handler(struct trace_seq *s, struct pevent_record *record,
  83. struct event_format *event, void *context)
  84. {
  85. struct pevent *pevent = event->pevent;
  86. unsigned long long function;
  87. unsigned long long pfunction;
  88. const char *func;
  89. const char *parent;
  90. int index;
  91. if (pevent_get_field_val(s, event, "ip", record, &function, 1))
  92. return trace_seq_putc(s, '!');
  93. func = pevent_find_function(pevent, function);
  94. if (pevent_get_field_val(s, event, "parent_ip", record, &pfunction, 1))
  95. return trace_seq_putc(s, '!');
  96. parent = pevent_find_function(pevent, pfunction);
  97. index = add_and_get_index(parent, func, record->cpu);
  98. trace_seq_printf(s, "%*s", index*3, "");
  99. if (func)
  100. trace_seq_printf(s, "%s", func);
  101. else
  102. trace_seq_printf(s, "0x%llx", function);
  103. trace_seq_printf(s, " <-- ");
  104. if (parent)
  105. trace_seq_printf(s, "%s", parent);
  106. else
  107. trace_seq_printf(s, "0x%llx", pfunction);
  108. return 0;
  109. }
  110. int PEVENT_PLUGIN_LOADER(struct pevent *pevent)
  111. {
  112. pevent_register_event_handler(pevent, -1, "ftrace", "function",
  113. function_handler, NULL);
  114. return 0;
  115. }
  116. void PEVENT_PLUGIN_UNLOADER(void)
  117. {
  118. int i, x;
  119. for (i = 0; i <= cpus; i++) {
  120. for (x = 0; x < fstack[i].size && fstack[i].stack[x]; x++)
  121. free(fstack[i].stack[x]);
  122. free(fstack[i].stack);
  123. }
  124. free(fstack);
  125. fstack = NULL;
  126. cpus = -1;
  127. }