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 27static struct func_stack { 28 int size; 29 char **stack; 30} *fstack; 31 32static int cpus = -1; 33 34#define STK_BLK 10 35 36struct 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 59static 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 { 69 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 80 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 88static int add_and_get_index(const char *parent, const char *child, int cpu) 89{ 90 int i; 91 92 if (cpu < 0) 93 return 0; 94 95 if (cpu > cpus) { 96 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; 105 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; 133 int index; 134 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 145 if (parent && ftrace_indent->set) 146 index = add_and_get_index(parent, func, record->cpu); 147 148 trace_seq_printf(s, "%*s", index*3, ""); 149 150 if (func) 151 trace_seq_printf(s, "%s", func); 152 else 153 trace_seq_printf(s, "0x%llx", function); 154 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); 161 } 162 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); 170 171 traceevent_plugin_add_options("ftrace", plugin_options); 172 173 return 0; 174} 175 176void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent) 177{ 178 int i, x; 179 180 pevent_unregister_event_handler(pevent, -1, "ftrace", "function", 181 function_handler, NULL); 182 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); 187 } 188 189 traceevent_plugin_remove_options(plugin_options); 190 191 free(fstack); 192 fstack = NULL; 193 cpus = -1; 194} 195