plugin_function.c 3.6 KB

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