drivers/net/ethernet/amd/pcnet32.c: neaten and remove unnecessary OOM messages
[linux/fpc-iii.git] / tools / lib / traceevent / plugin_function.c
bloba00ec190821aa352234ddf11f5d6ab3bb911dc5f
1 /*
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "event-parse.h"
25 #include "event-utils.h"
27 static struct func_stack {
28 int size;
29 char **stack;
30 } *fstack;
32 static int cpus = -1;
34 #define STK_BLK 10
36 struct pevent_plugin_option plugin_options[] =
39 .name = "parent",
40 .plugin_alias = "ftrace",
41 .description =
42 "Print parent of functions for function events",
45 .name = "indent",
46 .plugin_alias = "ftrace",
47 .description =
48 "Try to show function call indents, based on parents",
49 .set = 1,
52 .name = NULL,
56 static struct pevent_plugin_option *ftrace_parent = &plugin_options[0];
57 static struct pevent_plugin_option *ftrace_indent = &plugin_options[1];
59 static void add_child(struct func_stack *stack, const char *child, int pos)
61 int i;
63 if (!child)
64 return;
66 if (pos < stack->size)
67 free(stack->stack[pos]);
68 else {
69 char **ptr;
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;
78 stack->stack = ptr;
80 for (i = stack->size; i < stack->size + STK_BLK; i++)
81 stack->stack[i] = NULL;
82 stack->size += STK_BLK;
85 stack->stack[pos] = strdup(child);
88 static int add_and_get_index(const char *parent, const char *child, int cpu)
90 int i;
92 if (cpu < 0)
93 return 0;
95 if (cpu > cpus) {
96 struct func_stack *ptr;
98 ptr = realloc(fstack, sizeof(*fstack) * (cpu + 1));
99 if (!ptr) {
100 warning("could not allocate plugin memory\n");
101 return 0;
104 fstack = ptr;
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;
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;
119 /* Not found */
120 add_child(&fstack[cpu], parent, 0);
121 add_child(&fstack[cpu], child, 1);
122 return 0;
125 static int function_handler(struct trace_seq *s, struct pevent_record *record,
126 struct event_format *event, void *context)
128 struct pevent *pevent = event->pevent;
129 unsigned long long function;
130 unsigned long long pfunction;
131 const char *func;
132 const char *parent;
133 int index;
135 if (pevent_get_field_val(s, event, "ip", record, &function, 1))
136 return trace_seq_putc(s, '!');
138 func = pevent_find_function(pevent, function);
140 if (pevent_get_field_val(s, event, "parent_ip", record, &pfunction, 1))
141 return trace_seq_putc(s, '!');
143 parent = pevent_find_function(pevent, pfunction);
145 if (parent && ftrace_indent->set)
146 index = add_and_get_index(parent, func, record->cpu);
148 trace_seq_printf(s, "%*s", index*3, "");
150 if (func)
151 trace_seq_printf(s, "%s", func);
152 else
153 trace_seq_printf(s, "0x%llx", function);
155 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);
163 return 0;
166 int PEVENT_PLUGIN_LOADER(struct pevent *pevent)
168 pevent_register_event_handler(pevent, -1, "ftrace", "function",
169 function_handler, NULL);
171 traceevent_plugin_add_options("ftrace", plugin_options);
173 return 0;
176 void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent)
178 int i, x;
180 pevent_unregister_event_handler(pevent, -1, "ftrace", "function",
181 function_handler, NULL);
183 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);
189 traceevent_plugin_remove_options(plugin_options);
191 free(fstack);
192 fstack = NULL;
193 cpus = -1;