blob: 87acf9c77948e7289c63c2236a46c9cf6f5b87a7 [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 {
46 if (!stack->stack)
47 stack->stack = malloc_or_die(sizeof(char *) * STK_BLK);
48 else
49 stack->stack = realloc(stack->stack, sizeof(char *) *
50 (stack->size + STK_BLK));
51 for (i = stack->size; i < stack->size + STK_BLK; i++)
52 stack->stack[i] = NULL;
53 stack->size += STK_BLK;
54 }
55
56 stack->stack[pos] = strdup(child);
57}
58
Jiri Olsa39956e72013-12-03 14:09:39 +010059static int add_and_get_index(const char *parent, const char *child, int cpu)
Jiri Olsa07a180a2013-12-03 14:09:32 +010060{
61 int i;
62
63 if (cpu < 0)
64 return 0;
65
66 if (cpu > cpus) {
67 if (fstack)
68 fstack = realloc(fstack, sizeof(*fstack) * (cpu + 1));
69 else
70 fstack = malloc_or_die(sizeof(*fstack) * (cpu + 1));
71
72 /* Account for holes in the cpu count */
73 for (i = cpus + 1; i <= cpu; i++)
74 memset(&fstack[i], 0, sizeof(fstack[i]));
75 cpus = cpu;
76 }
77
78 for (i = 0; i < fstack[cpu].size && fstack[cpu].stack[i]; i++) {
79 if (strcmp(parent, fstack[cpu].stack[i]) == 0) {
80 add_child(&fstack[cpu], child, i+1);
81 return i;
82 }
83 }
84
85 /* Not found */
86 add_child(&fstack[cpu], parent, 0);
87 add_child(&fstack[cpu], child, 1);
88 return 0;
89}
90
91static int function_handler(struct trace_seq *s, struct pevent_record *record,
92 struct event_format *event, void *context)
93{
94 struct pevent *pevent = event->pevent;
95 unsigned long long function;
96 unsigned long long pfunction;
97 const char *func;
98 const char *parent;
Jiri Olsa39956e72013-12-03 14:09:39 +010099 int index;
Jiri Olsa07a180a2013-12-03 14:09:32 +0100100
101 if (pevent_get_field_val(s, event, "ip", record, &function, 1))
102 return trace_seq_putc(s, '!');
103
104 func = pevent_find_function(pevent, function);
105
106 if (pevent_get_field_val(s, event, "parent_ip", record, &pfunction, 1))
107 return trace_seq_putc(s, '!');
108
109 parent = pevent_find_function(pevent, pfunction);
110
Jiri Olsa39956e72013-12-03 14:09:39 +0100111 index = add_and_get_index(parent, func, record->cpu);
Jiri Olsa07a180a2013-12-03 14:09:32 +0100112
Jiri Olsa39956e72013-12-03 14:09:39 +0100113 trace_seq_printf(s, "%*s", index*3, "");
Jiri Olsa07a180a2013-12-03 14:09:32 +0100114
115 if (func)
116 trace_seq_printf(s, "%s", func);
117 else
118 trace_seq_printf(s, "0x%llx", function);
119
120 trace_seq_printf(s, " <-- ");
121 if (parent)
122 trace_seq_printf(s, "%s", parent);
123 else
124 trace_seq_printf(s, "0x%llx", pfunction);
125
126 return 0;
127}
128
129int PEVENT_PLUGIN_LOADER(struct pevent *pevent)
130{
131 pevent_register_event_handler(pevent, -1, "ftrace", "function",
132 function_handler, NULL);
133 return 0;
134}
135
136void PEVENT_PLUGIN_UNLOADER(void)
137{
138 int i, x;
139
140 for (i = 0; i <= cpus; i++) {
141 for (x = 0; x < fstack[i].size && fstack[i].stack[x]; x++)
142 free(fstack[i].stack[x]);
143 free(fstack[i].stack);
144 }
145
146 free(fstack);
147 fstack = NULL;
148 cpus = -1;
149}