2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
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!)
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.
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>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 #include "event-parse.h"
25 #include "event-utils.h"
26 #include "trace-seq.h"
28 static struct func_stack {
37 struct tep_plugin_option plugin_options[] =
41 .plugin_alias = "ftrace",
43 "Print parent of functions for function events",
47 .plugin_alias = "ftrace",
49 "Try to show function call indents, based on parents",
54 .plugin_alias = "ftrace",
56 "Show function names as well as their offsets",
64 static struct tep_plugin_option *ftrace_parent = &plugin_options[0];
65 static struct tep_plugin_option *ftrace_indent = &plugin_options[1];
66 static struct tep_plugin_option *ftrace_offset = &plugin_options[2];
68 static void add_child(struct func_stack *stack, const char *child, int pos)
75 if (pos < stack->size)
76 free(stack->stack[pos]);
80 ptr = realloc(stack->stack, sizeof(char *) *
81 (stack->size + STK_BLK));
83 warning("could not allocate plugin memory\n");
89 for (i = stack->size; i < stack->size + STK_BLK; i++)
90 stack->stack[i] = NULL;
91 stack->size += STK_BLK;
94 stack->stack[pos] = strdup(child);
97 static int add_and_get_index(const char *parent, const char *child, int cpu)
105 struct func_stack *ptr;
107 ptr = realloc(fstack, sizeof(*fstack) * (cpu + 1));
109 warning("could not allocate plugin memory\n");
115 /* Account for holes in the cpu count */
116 for (i = cpus + 1; i <= cpu; i++)
117 memset(&fstack[i], 0, sizeof(fstack[i]));
121 for (i = 0; i < fstack[cpu].size && fstack[cpu].stack[i]; i++) {
122 if (strcmp(parent, fstack[cpu].stack[i]) == 0) {
123 add_child(&fstack[cpu], child, i+1);
129 add_child(&fstack[cpu], parent, 0);
130 add_child(&fstack[cpu], child, 1);
134 static void show_function(struct trace_seq *s, struct tep_handle *tep,
135 const char *func, unsigned long long function)
137 unsigned long long offset;
139 trace_seq_printf(s, "%s", func);
140 if (ftrace_offset->set) {
141 offset = tep_find_function_address(tep, function);
142 trace_seq_printf(s, "+0x%x ", (int)(function - offset));
146 static int function_handler(struct trace_seq *s, struct tep_record *record,
147 struct tep_event *event, void *context)
149 struct tep_handle *tep = event->tep;
150 unsigned long long function;
151 unsigned long long pfunction;
156 if (tep_get_field_val(s, event, "ip", record, &function, 1))
157 return trace_seq_putc(s, '!');
159 func = tep_find_function(tep, function);
161 if (tep_get_field_val(s, event, "parent_ip", record, &pfunction, 1))
162 return trace_seq_putc(s, '!');
164 parent = tep_find_function(tep, pfunction);
166 if (parent && ftrace_indent->set)
167 index = add_and_get_index(parent, func, record->cpu);
169 trace_seq_printf(s, "%*s", index*3, "");
172 show_function(s, tep, func, function);
174 trace_seq_printf(s, "0x%llx", function);
176 if (ftrace_parent->set) {
177 trace_seq_printf(s, " <-- ");
179 show_function(s, tep, parent, pfunction);
181 trace_seq_printf(s, "0x%llx", pfunction);
187 int TEP_PLUGIN_LOADER(struct tep_handle *tep)
189 tep_register_event_handler(tep, -1, "ftrace", "function",
190 function_handler, NULL);
192 tep_plugin_add_options("ftrace", plugin_options);
197 void TEP_PLUGIN_UNLOADER(struct tep_handle *tep)
201 tep_unregister_event_handler(tep, -1, "ftrace", "function",
202 function_handler, NULL);
204 for (i = 0; i <= cpus; i++) {
205 for (x = 0; x < fstack[i].size && fstack[i].stack[x]; x++)
206 free(fstack[i].stack[x]);
207 free(fstack[i].stack);
210 tep_plugin_remove_options(plugin_options);