plugin_function.c 3.6 KB

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