blob: 80ba4ff1fe8411c56ef5fa405595dfe62e26a60d [file] [log] [blame]
Jiri Olsa07a180a2013-12-03 14:09:32 +01001/*
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
24#include "event-parse.h"
25#include "event-utils.h"
26
27static struct func_stack {
Jiri Olsa07a180a2013-12-03 14:09:32 +010028 int size;
29 char **stack;
30} *fstack;
31
32static int cpus = -1;
33
34#define STK_BLK 10
35
36static void add_child(struct func_stack *stack, const char *child, int pos)
37{
38 int i;
39
40 if (!child)
41 return;
42
43 if (pos < stack->size)
44 free(stack->stack[pos]);
45 else {
Jiri Olsad8e56c92013-12-03 14:09:40 +010046 char **ptr;
47
48 ptr = realloc(stack->stack, sizeof(char *) *
49 (stack->size + STK_BLK));
50 if (!ptr) {
51 warning("could not allocate plugin memory\n");
52 return;
53 }
54
55 stack->stack = ptr;
56
Jiri Olsa07a180a2013-12-03 14:09:32 +010057 for (i = stack->size; i < stack->size + STK_BLK; i++)
58 stack->stack[i] = NULL;
59 stack->size += STK_BLK;
60 }
61
62 stack->stack[pos] = strdup(child);
63}
64
Jiri Olsa39956e72013-12-03 14:09:39 +010065static int add_and_get_index(const char *parent, const char *child, int cpu)
Jiri Olsa07a180a2013-12-03 14:09:32 +010066{
67 int i;
68
69 if (cpu < 0)
70 return 0;
71
72 if (cpu > cpus) {
Jiri Olsad8e56c92013-12-03 14:09:40 +010073 struct func_stack *ptr;
74
75 ptr = realloc(fstack, sizeof(*fstack) * (cpu + 1));
76 if (!ptr) {
77 warning("could not allocate plugin memory\n");
78 return 0;
79 }
80
81 fstack = ptr;
Jiri Olsa07a180a2013-12-03 14:09:32 +010082
83 /* Account for holes in the cpu count */
84 for (i = cpus + 1; i <= cpu; i++)
85 memset(&fstack[i], 0, sizeof(fstack[i]));
86 cpus = cpu;
87 }
88
89 for (i = 0; i < fstack[cpu].size && fstack[cpu].stack[i]; i++) {
90 if (strcmp(parent, fstack[cpu].stack[i]) == 0) {
91 add_child(&fstack[cpu], child, i+1);
92 return i;
93 }
94 }
95
96 /* Not found */
97 add_child(&fstack[cpu], parent, 0);
98 add_child(&fstack[cpu], child, 1);
99 return 0;
100}
101
102static int function_handler(struct trace_seq *s, struct pevent_record *record,
103 struct event_format *event, void *context)
104{
105 struct pevent *pevent = event->pevent;
106 unsigned long long function;
107 unsigned long long pfunction;
108 const char *func;
109 const char *parent;
Jiri Olsa39956e72013-12-03 14:09:39 +0100110 int index;
Jiri Olsa07a180a2013-12-03 14:09:32 +0100111
112 if (pevent_get_field_val(s, event, "ip", record, &function, 1))
113 return trace_seq_putc(s, '!');
114
115 func = pevent_find_function(pevent, function);
116
117 if (pevent_get_field_val(s, event, "parent_ip", record, &pfunction, 1))
118 return trace_seq_putc(s, '!');
119
120 parent = pevent_find_function(pevent, pfunction);
121
Jiri Olsa39956e72013-12-03 14:09:39 +0100122 index = add_and_get_index(parent, func, record->cpu);
Jiri Olsa07a180a2013-12-03 14:09:32 +0100123
Jiri Olsa39956e72013-12-03 14:09:39 +0100124 trace_seq_printf(s, "%*s", index*3, "");
Jiri Olsa07a180a2013-12-03 14:09:32 +0100125
126 if (func)
127 trace_seq_printf(s, "%s", func);
128 else
129 trace_seq_printf(s, "0x%llx", function);
130
131 trace_seq_printf(s, " <-- ");
132 if (parent)
133 trace_seq_printf(s, "%s", parent);
134 else
135 trace_seq_printf(s, "0x%llx", pfunction);
136
137 return 0;
138}
139
140int PEVENT_PLUGIN_LOADER(struct pevent *pevent)
141{
142 pevent_register_event_handler(pevent, -1, "ftrace", "function",
143 function_handler, NULL);
144 return 0;
145}
146
Namhyung Kim8d0c2222014-01-15 10:45:28 +0900147void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent)
Jiri Olsa07a180a2013-12-03 14:09:32 +0100148{
149 int i, x;
150
Namhyung Kimac668c72014-01-16 11:31:09 +0900151 pevent_unregister_event_handler(pevent, -1, "ftrace", "function",
152 function_handler, NULL);
153
Jiri Olsa07a180a2013-12-03 14:09:32 +0100154 for (i = 0; i <= cpus; i++) {
155 for (x = 0; x < fstack[i].size && fstack[i].stack[x]; x++)
156 free(fstack[i].stack[x]);
157 free(fstack[i].stack);
158 }
159
160 free(fstack);
161 fstack = NULL;
162 cpus = -1;
163}