blob: a00ec190821aa352234ddf11f5d6ab3bb911dc5f [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
Steven Rostedt (Red Hat)49440822014-06-02 23:20:15 -040036struct pevent_plugin_option plugin_options[] =
37{
38 {
39 .name = "parent",
40 .plugin_alias = "ftrace",
41 .description =
42 "Print parent of functions for function events",
43 },
44 {
45 .name = "indent",
46 .plugin_alias = "ftrace",
47 .description =
48 "Try to show function call indents, based on parents",
49 .set = 1,
50 },
51 {
52 .name = NULL,
53 }
54};
55
56static struct pevent_plugin_option *ftrace_parent = &plugin_options[0];
57static struct pevent_plugin_option *ftrace_indent = &plugin_options[1];
58
Jiri Olsa07a180a2013-12-03 14:09:32 +010059static void add_child(struct func_stack *stack, const char *child, int pos)
60{
61 int i;
62
63 if (!child)
64 return;
65
66 if (pos < stack->size)
67 free(stack->stack[pos]);
68 else {
Jiri Olsad8e56c92013-12-03 14:09:40 +010069 char **ptr;
70
71 ptr = realloc(stack->stack, sizeof(char *) *
72 (stack->size + STK_BLK));
73 if (!ptr) {
74 warning("could not allocate plugin memory\n");
75 return;
76 }
77
78 stack->stack = ptr;
79
Jiri Olsa07a180a2013-12-03 14:09:32 +010080 for (i = stack->size; i < stack->size + STK_BLK; i++)
81 stack->stack[i] = NULL;
82 stack->size += STK_BLK;
83 }
84
85 stack->stack[pos] = strdup(child);
86}
87
Jiri Olsa39956e72013-12-03 14:09:39 +010088static int add_and_get_index(const char *parent, const char *child, int cpu)
Jiri Olsa07a180a2013-12-03 14:09:32 +010089{
90 int i;
91
92 if (cpu < 0)
93 return 0;
94
95 if (cpu > cpus) {
Jiri Olsad8e56c92013-12-03 14:09:40 +010096 struct func_stack *ptr;
97
98 ptr = realloc(fstack, sizeof(*fstack) * (cpu + 1));
99 if (!ptr) {
100 warning("could not allocate plugin memory\n");
101 return 0;
102 }
103
104 fstack = ptr;
Jiri Olsa07a180a2013-12-03 14:09:32 +0100105
106 /* Account for holes in the cpu count */
107 for (i = cpus + 1; i <= cpu; i++)
108 memset(&fstack[i], 0, sizeof(fstack[i]));
109 cpus = cpu;
110 }
111
112 for (i = 0; i < fstack[cpu].size && fstack[cpu].stack[i]; i++) {
113 if (strcmp(parent, fstack[cpu].stack[i]) == 0) {
114 add_child(&fstack[cpu], child, i+1);
115 return i;
116 }
117 }
118
119 /* Not found */
120 add_child(&fstack[cpu], parent, 0);
121 add_child(&fstack[cpu], child, 1);
122 return 0;
123}
124
125static int function_handler(struct trace_seq *s, struct pevent_record *record,
126 struct event_format *event, void *context)
127{
128 struct pevent *pevent = event->pevent;
129 unsigned long long function;
130 unsigned long long pfunction;
131 const char *func;
132 const char *parent;
Jiri Olsa39956e72013-12-03 14:09:39 +0100133 int index;
Jiri Olsa07a180a2013-12-03 14:09:32 +0100134
135 if (pevent_get_field_val(s, event, "ip", record, &function, 1))
136 return trace_seq_putc(s, '!');
137
138 func = pevent_find_function(pevent, function);
139
140 if (pevent_get_field_val(s, event, "parent_ip", record, &pfunction, 1))
141 return trace_seq_putc(s, '!');
142
143 parent = pevent_find_function(pevent, pfunction);
144
Steven Rostedt (Red Hat)49440822014-06-02 23:20:15 -0400145 if (parent && ftrace_indent->set)
146 index = add_and_get_index(parent, func, record->cpu);
Jiri Olsa07a180a2013-12-03 14:09:32 +0100147
Jiri Olsa39956e72013-12-03 14:09:39 +0100148 trace_seq_printf(s, "%*s", index*3, "");
Jiri Olsa07a180a2013-12-03 14:09:32 +0100149
150 if (func)
151 trace_seq_printf(s, "%s", func);
152 else
153 trace_seq_printf(s, "0x%llx", function);
154
Steven Rostedt (Red Hat)49440822014-06-02 23:20:15 -0400155 if (ftrace_parent->set) {
156 trace_seq_printf(s, " <-- ");
157 if (parent)
158 trace_seq_printf(s, "%s", parent);
159 else
160 trace_seq_printf(s, "0x%llx", pfunction);
161 }
Jiri Olsa07a180a2013-12-03 14:09:32 +0100162
163 return 0;
164}
165
166int PEVENT_PLUGIN_LOADER(struct pevent *pevent)
167{
168 pevent_register_event_handler(pevent, -1, "ftrace", "function",
169 function_handler, NULL);
Steven Rostedt (Red Hat)49440822014-06-02 23:20:15 -0400170
171 traceevent_plugin_add_options("ftrace", plugin_options);
172
Jiri Olsa07a180a2013-12-03 14:09:32 +0100173 return 0;
174}
175
Namhyung Kim8d0c2222014-01-15 10:45:28 +0900176void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent)
Jiri Olsa07a180a2013-12-03 14:09:32 +0100177{
178 int i, x;
179
Namhyung Kimac668c72014-01-16 11:31:09 +0900180 pevent_unregister_event_handler(pevent, -1, "ftrace", "function",
181 function_handler, NULL);
182
Jiri Olsa07a180a2013-12-03 14:09:32 +0100183 for (i = 0; i <= cpus; i++) {
184 for (x = 0; x < fstack[i].size && fstack[i].stack[x]; x++)
185 free(fstack[i].stack[x]);
186 free(fstack[i].stack);
187 }
188
Steven Rostedt (Red Hat)49440822014-06-02 23:20:15 -0400189 traceevent_plugin_remove_options(plugin_options);
190
Jiri Olsa07a180a2013-12-03 14:09:32 +0100191 free(fstack);
192 fstack = NULL;
193 cpus = -1;
194}