diff options
Diffstat (limited to 'tools/lib/traceevent/plugin_function.c')
-rw-r--r-- | tools/lib/traceevent/plugin_function.c | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/tools/lib/traceevent/plugin_function.c b/tools/lib/traceevent/plugin_function.c new file mode 100644 index 000000000000..8deb22e69361 --- /dev/null +++ b/tools/lib/traceevent/plugin_function.c | |||
@@ -0,0 +1,151 @@ | |||
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 | |||
27 | static struct func_stack { | ||
28 | int index; | ||
29 | int size; | ||
30 | char **stack; | ||
31 | } *fstack; | ||
32 | |||
33 | static int cpus = -1; | ||
34 | |||
35 | #define STK_BLK 10 | ||
36 | |||
37 | static void add_child(struct func_stack *stack, const char *child, int pos) | ||
38 | { | ||
39 | int i; | ||
40 | |||
41 | if (!child) | ||
42 | return; | ||
43 | |||
44 | if (pos < stack->size) | ||
45 | free(stack->stack[pos]); | ||
46 | else { | ||
47 | if (!stack->stack) | ||
48 | stack->stack = malloc_or_die(sizeof(char *) * STK_BLK); | ||
49 | else | ||
50 | stack->stack = realloc(stack->stack, sizeof(char *) * | ||
51 | (stack->size + STK_BLK)); | ||
52 | for (i = stack->size; i < stack->size + STK_BLK; i++) | ||
53 | stack->stack[i] = NULL; | ||
54 | stack->size += STK_BLK; | ||
55 | } | ||
56 | |||
57 | stack->stack[pos] = strdup(child); | ||
58 | } | ||
59 | |||
60 | static int get_index(const char *parent, const char *child, int cpu) | ||
61 | { | ||
62 | int i; | ||
63 | |||
64 | if (cpu < 0) | ||
65 | return 0; | ||
66 | |||
67 | if (cpu > cpus) { | ||
68 | if (fstack) | ||
69 | fstack = realloc(fstack, sizeof(*fstack) * (cpu + 1)); | ||
70 | else | ||
71 | fstack = malloc_or_die(sizeof(*fstack) * (cpu + 1)); | ||
72 | |||
73 | /* Account for holes in the cpu count */ | ||
74 | for (i = cpus + 1; i <= cpu; i++) | ||
75 | memset(&fstack[i], 0, sizeof(fstack[i])); | ||
76 | cpus = cpu; | ||
77 | } | ||
78 | |||
79 | for (i = 0; i < fstack[cpu].size && fstack[cpu].stack[i]; i++) { | ||
80 | if (strcmp(parent, fstack[cpu].stack[i]) == 0) { | ||
81 | add_child(&fstack[cpu], child, i+1); | ||
82 | return i; | ||
83 | } | ||
84 | } | ||
85 | |||
86 | /* Not found */ | ||
87 | add_child(&fstack[cpu], parent, 0); | ||
88 | add_child(&fstack[cpu], child, 1); | ||
89 | return 0; | ||
90 | } | ||
91 | |||
92 | static int function_handler(struct trace_seq *s, struct pevent_record *record, | ||
93 | struct event_format *event, void *context) | ||
94 | { | ||
95 | struct pevent *pevent = event->pevent; | ||
96 | unsigned long long function; | ||
97 | unsigned long long pfunction; | ||
98 | const char *func; | ||
99 | const char *parent; | ||
100 | int i, index; | ||
101 | |||
102 | if (pevent_get_field_val(s, event, "ip", record, &function, 1)) | ||
103 | return trace_seq_putc(s, '!'); | ||
104 | |||
105 | func = pevent_find_function(pevent, function); | ||
106 | |||
107 | if (pevent_get_field_val(s, event, "parent_ip", record, &pfunction, 1)) | ||
108 | return trace_seq_putc(s, '!'); | ||
109 | |||
110 | parent = pevent_find_function(pevent, pfunction); | ||
111 | |||
112 | index = get_index(parent, func, record->cpu); | ||
113 | |||
114 | for (i = 0; i < index; i++) | ||
115 | trace_seq_printf(s, " "); | ||
116 | |||
117 | if (func) | ||
118 | trace_seq_printf(s, "%s", func); | ||
119 | else | ||
120 | trace_seq_printf(s, "0x%llx", function); | ||
121 | |||
122 | trace_seq_printf(s, " <-- "); | ||
123 | if (parent) | ||
124 | trace_seq_printf(s, "%s", parent); | ||
125 | else | ||
126 | trace_seq_printf(s, "0x%llx", pfunction); | ||
127 | |||
128 | return 0; | ||
129 | } | ||
130 | |||
131 | int PEVENT_PLUGIN_LOADER(struct pevent *pevent) | ||
132 | { | ||
133 | pevent_register_event_handler(pevent, -1, "ftrace", "function", | ||
134 | function_handler, NULL); | ||
135 | return 0; | ||
136 | } | ||
137 | |||
138 | void PEVENT_PLUGIN_UNLOADER(void) | ||
139 | { | ||
140 | int i, x; | ||
141 | |||
142 | for (i = 0; i <= cpus; i++) { | ||
143 | for (x = 0; x < fstack[i].size && fstack[i].stack[x]; x++) | ||
144 | free(fstack[i].stack[x]); | ||
145 | free(fstack[i].stack); | ||
146 | } | ||
147 | |||
148 | free(fstack); | ||
149 | fstack = NULL; | ||
150 | cpus = -1; | ||
151 | } | ||