tools lib traceevent: Add offset option for function plugin
[linux-2.6-microblaze.git] / tools / lib / traceevent / plugins / plugin_function.c
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
24 #include "event-parse.h"
25 #include "event-utils.h"
26 #include "trace-seq.h"
27
28 static struct func_stack {
29         int size;
30         char **stack;
31 } *fstack;
32
33 static int cpus = -1;
34
35 #define STK_BLK 10
36
37 struct tep_plugin_option plugin_options[] =
38 {
39         {
40                 .name = "parent",
41                 .plugin_alias = "ftrace",
42                 .description =
43                 "Print parent of functions for function events",
44         },
45         {
46                 .name = "indent",
47                 .plugin_alias = "ftrace",
48                 .description =
49                 "Try to show function call indents, based on parents",
50                 .set = 1,
51         },
52         {
53                 .name = "offset",
54                 .plugin_alias = "ftrace",
55                 .description =
56                 "Show function names as well as their offsets",
57                 .set = 0,
58         },
59         {
60                 .name = NULL,
61         }
62 };
63
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];
67
68 static void add_child(struct func_stack *stack, const char *child, int pos)
69 {
70         int i;
71
72         if (!child)
73                 return;
74
75         if (pos < stack->size)
76                 free(stack->stack[pos]);
77         else {
78                 char **ptr;
79
80                 ptr = realloc(stack->stack, sizeof(char *) *
81                               (stack->size + STK_BLK));
82                 if (!ptr) {
83                         warning("could not allocate plugin memory\n");
84                         return;
85                 }
86
87                 stack->stack = ptr;
88
89                 for (i = stack->size; i < stack->size + STK_BLK; i++)
90                         stack->stack[i] = NULL;
91                 stack->size += STK_BLK;
92         }
93
94         stack->stack[pos] = strdup(child);
95 }
96
97 static int add_and_get_index(const char *parent, const char *child, int cpu)
98 {
99         int i;
100
101         if (cpu < 0)
102                 return 0;
103
104         if (cpu > cpus) {
105                 struct func_stack *ptr;
106
107                 ptr = realloc(fstack, sizeof(*fstack) * (cpu + 1));
108                 if (!ptr) {
109                         warning("could not allocate plugin memory\n");
110                         return 0;
111                 }
112
113                 fstack = ptr;
114
115                 /* Account for holes in the cpu count */
116                 for (i = cpus + 1; i <= cpu; i++)
117                         memset(&fstack[i], 0, sizeof(fstack[i]));
118                 cpus = cpu;
119         }
120
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);
124                         return i;
125                 }
126         }
127
128         /* Not found */
129         add_child(&fstack[cpu], parent, 0);
130         add_child(&fstack[cpu], child, 1);
131         return 0;
132 }
133
134 static void show_function(struct trace_seq *s, struct tep_handle *tep,
135                           const char *func, unsigned long long function)
136 {
137         unsigned long long offset;
138
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));
143         }
144 }
145
146 static int function_handler(struct trace_seq *s, struct tep_record *record,
147                             struct tep_event *event, void *context)
148 {
149         struct tep_handle *tep = event->tep;
150         unsigned long long function;
151         unsigned long long pfunction;
152         const char *func;
153         const char *parent;
154         int index = 0;
155
156         if (tep_get_field_val(s, event, "ip", record, &function, 1))
157                 return trace_seq_putc(s, '!');
158
159         func = tep_find_function(tep, function);
160
161         if (tep_get_field_val(s, event, "parent_ip", record, &pfunction, 1))
162                 return trace_seq_putc(s, '!');
163
164         parent = tep_find_function(tep, pfunction);
165
166         if (parent && ftrace_indent->set)
167                 index = add_and_get_index(parent, func, record->cpu);
168
169         trace_seq_printf(s, "%*s", index*3, "");
170
171         if (func)
172                 show_function(s, tep, func, function);
173         else
174                 trace_seq_printf(s, "0x%llx", function);
175
176         if (ftrace_parent->set) {
177                 trace_seq_printf(s, " <-- ");
178                 if (parent)
179                         show_function(s, tep, parent, pfunction);
180                 else
181                         trace_seq_printf(s, "0x%llx", pfunction);
182         }
183
184         return 0;
185 }
186
187 int TEP_PLUGIN_LOADER(struct tep_handle *tep)
188 {
189         tep_register_event_handler(tep, -1, "ftrace", "function",
190                                    function_handler, NULL);
191
192         tep_plugin_add_options("ftrace", plugin_options);
193
194         return 0;
195 }
196
197 void TEP_PLUGIN_UNLOADER(struct tep_handle *tep)
198 {
199         int i, x;
200
201         tep_unregister_event_handler(tep, -1, "ftrace", "function",
202                                      function_handler, NULL);
203
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);
208         }
209
210         tep_plugin_remove_options(plugin_options);
211
212         free(fstack);
213         fstack = NULL;
214         cpus = -1;
215 }