Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1 | /* |
Masami Hiramatsu | 77b44d1 | 2009-11-03 19:12:47 -0500 | [diff] [blame] | 2 | * Kprobes-based tracing events |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 3 | * |
| 4 | * Created by Masami Hiramatsu <mhiramat@redhat.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 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 General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/kprobes.h> |
| 23 | #include <linux/seq_file.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/smp.h> |
| 26 | #include <linux/debugfs.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/string.h> |
| 29 | #include <linux/ctype.h> |
| 30 | #include <linux/ptrace.h> |
Frederic Weisbecker | d7a4b41 | 2009-09-23 23:08:43 +0200 | [diff] [blame] | 31 | #include <linux/perf_event.h> |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 32 | #include <linux/stringify.h> |
| 33 | #include <asm/bitsperlong.h> |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 34 | |
| 35 | #include "trace.h" |
| 36 | #include "trace_output.h" |
| 37 | |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 38 | #define MAX_TRACE_ARGS 128 |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 39 | #define MAX_ARGSTR_LEN 63 |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 40 | #define MAX_EVENT_NAME_LEN 64 |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 41 | #define KPROBE_EVENT_SYSTEM "kprobes" |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 42 | |
Masami Hiramatsu | a703d94 | 2009-10-07 18:28:07 -0400 | [diff] [blame] | 43 | /* Reserved field names */ |
Masami Hiramatsu | e93f4d8 | 2009-10-07 18:28:14 -0400 | [diff] [blame] | 44 | #define FIELD_STRING_IP "__probe_ip" |
Masami Hiramatsu | e93f4d8 | 2009-10-07 18:28:14 -0400 | [diff] [blame] | 45 | #define FIELD_STRING_RETIP "__probe_ret_ip" |
| 46 | #define FIELD_STRING_FUNC "__probe_func" |
Masami Hiramatsu | a703d94 | 2009-10-07 18:28:07 -0400 | [diff] [blame] | 47 | |
| 48 | const char *reserved_field_names[] = { |
| 49 | "common_type", |
| 50 | "common_flags", |
| 51 | "common_preempt_count", |
| 52 | "common_pid", |
| 53 | "common_tgid", |
| 54 | "common_lock_depth", |
| 55 | FIELD_STRING_IP, |
Masami Hiramatsu | a703d94 | 2009-10-07 18:28:07 -0400 | [diff] [blame] | 56 | FIELD_STRING_RETIP, |
| 57 | FIELD_STRING_FUNC, |
| 58 | }; |
| 59 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 60 | /* Printing function type */ |
| 61 | typedef int (*print_type_func_t)(struct trace_seq *, const char *, void *); |
| 62 | #define PRINT_TYPE_FUNC_NAME(type) print_type_##type |
| 63 | #define PRINT_TYPE_FMT_NAME(type) print_type_format_##type |
| 64 | |
| 65 | /* Printing in basic type function template */ |
| 66 | #define DEFINE_BASIC_PRINT_TYPE_FUNC(type, fmt, cast) \ |
| 67 | static __kprobes int PRINT_TYPE_FUNC_NAME(type)(struct trace_seq *s, \ |
| 68 | const char *name, void *data)\ |
| 69 | { \ |
| 70 | return trace_seq_printf(s, " %s=" fmt, name, (cast)*(type *)data);\ |
| 71 | } \ |
| 72 | static const char PRINT_TYPE_FMT_NAME(type)[] = fmt; |
| 73 | |
| 74 | DEFINE_BASIC_PRINT_TYPE_FUNC(u8, "%x", unsigned int) |
| 75 | DEFINE_BASIC_PRINT_TYPE_FUNC(u16, "%x", unsigned int) |
| 76 | DEFINE_BASIC_PRINT_TYPE_FUNC(u32, "%lx", unsigned long) |
| 77 | DEFINE_BASIC_PRINT_TYPE_FUNC(u64, "%llx", unsigned long long) |
| 78 | DEFINE_BASIC_PRINT_TYPE_FUNC(s8, "%d", int) |
| 79 | DEFINE_BASIC_PRINT_TYPE_FUNC(s16, "%d", int) |
| 80 | DEFINE_BASIC_PRINT_TYPE_FUNC(s32, "%ld", long) |
| 81 | DEFINE_BASIC_PRINT_TYPE_FUNC(s64, "%lld", long long) |
| 82 | |
| 83 | /* Data fetch function type */ |
| 84 | typedef void (*fetch_func_t)(struct pt_regs *, void *, void *); |
| 85 | |
| 86 | struct fetch_param { |
| 87 | fetch_func_t fn; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 88 | void *data; |
| 89 | }; |
| 90 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 91 | static __kprobes void call_fetch(struct fetch_param *fprm, |
| 92 | struct pt_regs *regs, void *dest) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 93 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 94 | return fprm->fn(regs, fprm->data, dest); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 97 | #define FETCH_FUNC_NAME(kind, type) fetch_##kind##_##type |
| 98 | /* |
| 99 | * Define macro for basic types - we don't need to define s* types, because |
| 100 | * we have to care only about bitwidth at recording time. |
| 101 | */ |
| 102 | #define DEFINE_BASIC_FETCH_FUNCS(kind) \ |
| 103 | DEFINE_FETCH_##kind(u8) \ |
| 104 | DEFINE_FETCH_##kind(u16) \ |
| 105 | DEFINE_FETCH_##kind(u32) \ |
| 106 | DEFINE_FETCH_##kind(u64) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 107 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 108 | #define CHECK_BASIC_FETCH_FUNCS(kind, fn) \ |
| 109 | ((FETCH_FUNC_NAME(kind, u8) == fn) || \ |
| 110 | (FETCH_FUNC_NAME(kind, u16) == fn) || \ |
| 111 | (FETCH_FUNC_NAME(kind, u32) == fn) || \ |
| 112 | (FETCH_FUNC_NAME(kind, u64) == fn)) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 113 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 114 | /* Data fetch function templates */ |
| 115 | #define DEFINE_FETCH_reg(type) \ |
| 116 | static __kprobes void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, \ |
| 117 | void *offset, void *dest) \ |
| 118 | { \ |
| 119 | *(type *)dest = (type)regs_get_register(regs, \ |
| 120 | (unsigned int)((unsigned long)offset)); \ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 121 | } |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 122 | DEFINE_BASIC_FETCH_FUNCS(reg) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 123 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 124 | #define DEFINE_FETCH_stack(type) \ |
| 125 | static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\ |
| 126 | void *offset, void *dest) \ |
| 127 | { \ |
| 128 | *(type *)dest = (type)regs_get_kernel_stack_nth(regs, \ |
| 129 | (unsigned int)((unsigned long)offset)); \ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 130 | } |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 131 | DEFINE_BASIC_FETCH_FUNCS(stack) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 132 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 133 | #define DEFINE_FETCH_retval(type) \ |
| 134 | static __kprobes void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,\ |
| 135 | void *dummy, void *dest) \ |
| 136 | { \ |
| 137 | *(type *)dest = (type)regs_return_value(regs); \ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 138 | } |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 139 | DEFINE_BASIC_FETCH_FUNCS(retval) |
| 140 | |
| 141 | #define DEFINE_FETCH_memory(type) \ |
| 142 | static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\ |
| 143 | void *addr, void *dest) \ |
| 144 | { \ |
| 145 | type retval; \ |
| 146 | if (probe_kernel_address(addr, retval)) \ |
| 147 | *(type *)dest = 0; \ |
| 148 | else \ |
| 149 | *(type *)dest = retval; \ |
| 150 | } |
| 151 | DEFINE_BASIC_FETCH_FUNCS(memory) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 152 | |
| 153 | /* Memory fetching by symbol */ |
| 154 | struct symbol_cache { |
| 155 | char *symbol; |
| 156 | long offset; |
| 157 | unsigned long addr; |
| 158 | }; |
| 159 | |
| 160 | static unsigned long update_symbol_cache(struct symbol_cache *sc) |
| 161 | { |
| 162 | sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol); |
| 163 | if (sc->addr) |
| 164 | sc->addr += sc->offset; |
| 165 | return sc->addr; |
| 166 | } |
| 167 | |
| 168 | static void free_symbol_cache(struct symbol_cache *sc) |
| 169 | { |
| 170 | kfree(sc->symbol); |
| 171 | kfree(sc); |
| 172 | } |
| 173 | |
| 174 | static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset) |
| 175 | { |
| 176 | struct symbol_cache *sc; |
| 177 | |
| 178 | if (!sym || strlen(sym) == 0) |
| 179 | return NULL; |
| 180 | sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL); |
| 181 | if (!sc) |
| 182 | return NULL; |
| 183 | |
| 184 | sc->symbol = kstrdup(sym, GFP_KERNEL); |
| 185 | if (!sc->symbol) { |
| 186 | kfree(sc); |
| 187 | return NULL; |
| 188 | } |
| 189 | sc->offset = offset; |
| 190 | |
| 191 | update_symbol_cache(sc); |
| 192 | return sc; |
| 193 | } |
| 194 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 195 | #define DEFINE_FETCH_symbol(type) \ |
| 196 | static __kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,\ |
| 197 | void *data, void *dest) \ |
| 198 | { \ |
| 199 | struct symbol_cache *sc = data; \ |
| 200 | if (sc->addr) \ |
| 201 | fetch_memory_##type(regs, (void *)sc->addr, dest); \ |
| 202 | else \ |
| 203 | *(type *)dest = 0; \ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 204 | } |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 205 | DEFINE_BASIC_FETCH_FUNCS(symbol) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 206 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 207 | /* Dereference memory access function */ |
| 208 | struct deref_fetch_param { |
| 209 | struct fetch_param orig; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 210 | long offset; |
| 211 | }; |
| 212 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 213 | #define DEFINE_FETCH_deref(type) \ |
| 214 | static __kprobes void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,\ |
| 215 | void *data, void *dest) \ |
| 216 | { \ |
| 217 | struct deref_fetch_param *dprm = data; \ |
| 218 | unsigned long addr; \ |
| 219 | call_fetch(&dprm->orig, regs, &addr); \ |
| 220 | if (addr) { \ |
| 221 | addr += dprm->offset; \ |
| 222 | fetch_memory_##type(regs, (void *)addr, dest); \ |
| 223 | } else \ |
| 224 | *(type *)dest = 0; \ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 225 | } |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 226 | DEFINE_BASIC_FETCH_FUNCS(deref) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 227 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 228 | static __kprobes void free_deref_fetch_param(struct deref_fetch_param *data) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 229 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 230 | if (CHECK_BASIC_FETCH_FUNCS(deref, data->orig.fn)) |
| 231 | free_deref_fetch_param(data->orig.data); |
| 232 | else if (CHECK_BASIC_FETCH_FUNCS(symbol, data->orig.fn)) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 233 | free_symbol_cache(data->orig.data); |
| 234 | kfree(data); |
| 235 | } |
| 236 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 237 | /* Default (unsigned long) fetch type */ |
| 238 | #define __DEFAULT_FETCH_TYPE(t) u##t |
| 239 | #define _DEFAULT_FETCH_TYPE(t) __DEFAULT_FETCH_TYPE(t) |
| 240 | #define DEFAULT_FETCH_TYPE _DEFAULT_FETCH_TYPE(BITS_PER_LONG) |
| 241 | #define DEFAULT_FETCH_TYPE_STR __stringify(DEFAULT_FETCH_TYPE) |
| 242 | |
| 243 | #define ASSIGN_FETCH_FUNC(kind, type) \ |
| 244 | .kind = FETCH_FUNC_NAME(kind, type) |
| 245 | |
| 246 | #define ASSIGN_FETCH_TYPE(ptype, ftype, sign) \ |
| 247 | {.name = #ptype, \ |
| 248 | .size = sizeof(ftype), \ |
| 249 | .is_signed = sign, \ |
| 250 | .print = PRINT_TYPE_FUNC_NAME(ptype), \ |
| 251 | .fmt = PRINT_TYPE_FMT_NAME(ptype), \ |
| 252 | ASSIGN_FETCH_FUNC(reg, ftype), \ |
| 253 | ASSIGN_FETCH_FUNC(stack, ftype), \ |
| 254 | ASSIGN_FETCH_FUNC(retval, ftype), \ |
| 255 | ASSIGN_FETCH_FUNC(memory, ftype), \ |
| 256 | ASSIGN_FETCH_FUNC(symbol, ftype), \ |
| 257 | ASSIGN_FETCH_FUNC(deref, ftype), \ |
| 258 | } |
| 259 | |
| 260 | /* Fetch type information table */ |
| 261 | static const struct fetch_type { |
| 262 | const char *name; /* Name of type */ |
| 263 | size_t size; /* Byte size of type */ |
| 264 | int is_signed; /* Signed flag */ |
| 265 | print_type_func_t print; /* Print functions */ |
| 266 | const char *fmt; /* Fromat string */ |
| 267 | /* Fetch functions */ |
| 268 | fetch_func_t reg; |
| 269 | fetch_func_t stack; |
| 270 | fetch_func_t retval; |
| 271 | fetch_func_t memory; |
| 272 | fetch_func_t symbol; |
| 273 | fetch_func_t deref; |
| 274 | } fetch_type_table[] = { |
| 275 | ASSIGN_FETCH_TYPE(u8, u8, 0), |
| 276 | ASSIGN_FETCH_TYPE(u16, u16, 0), |
| 277 | ASSIGN_FETCH_TYPE(u32, u32, 0), |
| 278 | ASSIGN_FETCH_TYPE(u64, u64, 0), |
| 279 | ASSIGN_FETCH_TYPE(s8, u8, 1), |
| 280 | ASSIGN_FETCH_TYPE(s16, u16, 1), |
| 281 | ASSIGN_FETCH_TYPE(s32, u32, 1), |
| 282 | ASSIGN_FETCH_TYPE(s64, u64, 1), |
| 283 | }; |
| 284 | |
| 285 | static const struct fetch_type *find_fetch_type(const char *type) |
| 286 | { |
| 287 | int i; |
| 288 | |
| 289 | if (!type) |
| 290 | type = DEFAULT_FETCH_TYPE_STR; |
| 291 | |
| 292 | for (i = 0; i < ARRAY_SIZE(fetch_type_table); i++) |
| 293 | if (strcmp(type, fetch_type_table[i].name) == 0) |
| 294 | return &fetch_type_table[i]; |
| 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | /* Special function : only accept unsigned long */ |
| 299 | static __kprobes void fetch_stack_address(struct pt_regs *regs, |
| 300 | void *dummy, void *dest) |
| 301 | { |
| 302 | *(unsigned long *)dest = kernel_stack_pointer(regs); |
| 303 | } |
| 304 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 305 | /** |
Masami Hiramatsu | 77b44d1 | 2009-11-03 19:12:47 -0500 | [diff] [blame] | 306 | * Kprobe event core functions |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 307 | */ |
| 308 | |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 309 | struct probe_arg { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 310 | struct fetch_param fetch; |
| 311 | unsigned int offset; /* Offset from argument entry */ |
| 312 | const char *name; /* Name of this argument */ |
| 313 | const char *comm; /* Command of this argument */ |
| 314 | const struct fetch_type *type; /* Type of this argument */ |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 315 | }; |
| 316 | |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 317 | /* Flags for trace_probe */ |
| 318 | #define TP_FLAG_TRACE 1 |
| 319 | #define TP_FLAG_PROFILE 2 |
| 320 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 321 | struct trace_probe { |
| 322 | struct list_head list; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 323 | struct kretprobe rp; /* Use rp.kp for kprobe use */ |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 324 | unsigned long nhit; |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 325 | unsigned int flags; /* For TP_FLAG_* */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 326 | const char *symbol; /* symbol name */ |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 327 | struct ftrace_event_class class; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 328 | struct ftrace_event_call call; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 329 | ssize_t size; /* trace entry size */ |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 330 | unsigned int nr_args; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 331 | struct probe_arg args[]; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 332 | }; |
| 333 | |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 334 | #define SIZEOF_TRACE_PROBE(n) \ |
| 335 | (offsetof(struct trace_probe, args) + \ |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 336 | (sizeof(struct probe_arg) * (n))) |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 337 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 338 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 339 | static __kprobes int probe_is_return(struct trace_probe *tp) |
| 340 | { |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 341 | return tp->rp.handler != NULL; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static __kprobes const char *probe_symbol(struct trace_probe *tp) |
| 345 | { |
| 346 | return tp->symbol ? tp->symbol : "unknown"; |
| 347 | } |
| 348 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 349 | static int register_probe_event(struct trace_probe *tp); |
| 350 | static void unregister_probe_event(struct trace_probe *tp); |
| 351 | |
| 352 | static DEFINE_MUTEX(probe_lock); |
| 353 | static LIST_HEAD(probe_list); |
| 354 | |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 355 | static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs); |
| 356 | static int kretprobe_dispatcher(struct kretprobe_instance *ri, |
| 357 | struct pt_regs *regs); |
| 358 | |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 359 | /* Check the name is good for event/group */ |
| 360 | static int check_event_name(const char *name) |
| 361 | { |
| 362 | if (!isalpha(*name) && *name != '_') |
| 363 | return 0; |
| 364 | while (*++name != '\0') { |
| 365 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') |
| 366 | return 0; |
| 367 | } |
| 368 | return 1; |
| 369 | } |
| 370 | |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 371 | /* |
| 372 | * Allocate new trace_probe and initialize it (including kprobes). |
| 373 | */ |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 374 | static struct trace_probe *alloc_trace_probe(const char *group, |
| 375 | const char *event, |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 376 | void *addr, |
| 377 | const char *symbol, |
| 378 | unsigned long offs, |
| 379 | int nargs, int is_return) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 380 | { |
| 381 | struct trace_probe *tp; |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 382 | int ret = -ENOMEM; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 383 | |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 384 | tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 385 | if (!tp) |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 386 | return ERR_PTR(ret); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 387 | |
| 388 | if (symbol) { |
| 389 | tp->symbol = kstrdup(symbol, GFP_KERNEL); |
| 390 | if (!tp->symbol) |
| 391 | goto error; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 392 | tp->rp.kp.symbol_name = tp->symbol; |
| 393 | tp->rp.kp.offset = offs; |
| 394 | } else |
| 395 | tp->rp.kp.addr = addr; |
| 396 | |
| 397 | if (is_return) |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 398 | tp->rp.handler = kretprobe_dispatcher; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 399 | else |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 400 | tp->rp.kp.pre_handler = kprobe_dispatcher; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 401 | |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 402 | if (!event || !check_event_name(event)) { |
| 403 | ret = -EINVAL; |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 404 | goto error; |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 405 | } |
| 406 | |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 407 | tp->call.class = &tp->class; |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 408 | tp->call.name = kstrdup(event, GFP_KERNEL); |
| 409 | if (!tp->call.name) |
| 410 | goto error; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 411 | |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 412 | if (!group || !check_event_name(group)) { |
| 413 | ret = -EINVAL; |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 414 | goto error; |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 415 | } |
| 416 | |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 417 | tp->class.system = kstrdup(group, GFP_KERNEL); |
| 418 | if (!tp->class.system) |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 419 | goto error; |
| 420 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 421 | INIT_LIST_HEAD(&tp->list); |
| 422 | return tp; |
| 423 | error: |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 424 | kfree(tp->call.name); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 425 | kfree(tp->symbol); |
| 426 | kfree(tp); |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 427 | return ERR_PTR(ret); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 428 | } |
| 429 | |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 430 | static void free_probe_arg(struct probe_arg *arg) |
| 431 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 432 | if (CHECK_BASIC_FETCH_FUNCS(deref, arg->fetch.fn)) |
| 433 | free_deref_fetch_param(arg->fetch.data); |
| 434 | else if (CHECK_BASIC_FETCH_FUNCS(symbol, arg->fetch.fn)) |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 435 | free_symbol_cache(arg->fetch.data); |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 436 | kfree(arg->name); |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 437 | kfree(arg->comm); |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 438 | } |
| 439 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 440 | static void free_trace_probe(struct trace_probe *tp) |
| 441 | { |
| 442 | int i; |
| 443 | |
| 444 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 445 | free_probe_arg(&tp->args[i]); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 446 | |
Steven Rostedt | 8f08201 | 2010-04-20 10:47:33 -0400 | [diff] [blame] | 447 | kfree(tp->call.class->system); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 448 | kfree(tp->call.name); |
| 449 | kfree(tp->symbol); |
| 450 | kfree(tp); |
| 451 | } |
| 452 | |
Masami Hiramatsu | dd004c4 | 2009-10-27 16:42:44 -0400 | [diff] [blame] | 453 | static struct trace_probe *find_probe_event(const char *event, |
| 454 | const char *group) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 455 | { |
| 456 | struct trace_probe *tp; |
| 457 | |
| 458 | list_for_each_entry(tp, &probe_list, list) |
Masami Hiramatsu | dd004c4 | 2009-10-27 16:42:44 -0400 | [diff] [blame] | 459 | if (strcmp(tp->call.name, event) == 0 && |
Steven Rostedt | 8f08201 | 2010-04-20 10:47:33 -0400 | [diff] [blame] | 460 | strcmp(tp->call.class->system, group) == 0) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 461 | return tp; |
| 462 | return NULL; |
| 463 | } |
| 464 | |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame] | 465 | /* Unregister a trace_probe and probe_event: call with locking probe_lock */ |
| 466 | static void unregister_trace_probe(struct trace_probe *tp) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 467 | { |
| 468 | if (probe_is_return(tp)) |
| 469 | unregister_kretprobe(&tp->rp); |
| 470 | else |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 471 | unregister_kprobe(&tp->rp.kp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 472 | list_del(&tp->list); |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame] | 473 | unregister_probe_event(tp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* Register a trace_probe and probe_event */ |
| 477 | static int register_trace_probe(struct trace_probe *tp) |
| 478 | { |
| 479 | struct trace_probe *old_tp; |
| 480 | int ret; |
| 481 | |
| 482 | mutex_lock(&probe_lock); |
| 483 | |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame] | 484 | /* register as an event */ |
Steven Rostedt | 8f08201 | 2010-04-20 10:47:33 -0400 | [diff] [blame] | 485 | old_tp = find_probe_event(tp->call.name, tp->call.class->system); |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame] | 486 | if (old_tp) { |
| 487 | /* delete old event */ |
| 488 | unregister_trace_probe(old_tp); |
| 489 | free_trace_probe(old_tp); |
| 490 | } |
| 491 | ret = register_probe_event(tp); |
| 492 | if (ret) { |
| 493 | pr_warning("Faild to register probe event(%d)\n", ret); |
| 494 | goto end; |
| 495 | } |
| 496 | |
Masami Hiramatsu | 5a0d905 | 2009-09-14 16:49:37 -0400 | [diff] [blame] | 497 | tp->rp.kp.flags |= KPROBE_FLAG_DISABLED; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 498 | if (probe_is_return(tp)) |
| 499 | ret = register_kretprobe(&tp->rp); |
| 500 | else |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 501 | ret = register_kprobe(&tp->rp.kp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 502 | |
| 503 | if (ret) { |
| 504 | pr_warning("Could not insert probe(%d)\n", ret); |
| 505 | if (ret == -EILSEQ) { |
| 506 | pr_warning("Probing address(0x%p) is not an " |
| 507 | "instruction boundary.\n", |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 508 | tp->rp.kp.addr); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 509 | ret = -EINVAL; |
| 510 | } |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame] | 511 | unregister_probe_event(tp); |
| 512 | } else |
| 513 | list_add_tail(&tp->list, &probe_list); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 514 | end: |
| 515 | mutex_unlock(&probe_lock); |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | /* Split symbol and offset. */ |
Masami Hiramatsu | 2fba0c8 | 2009-09-10 19:53:14 -0400 | [diff] [blame] | 520 | static int split_symbol_offset(char *symbol, unsigned long *offset) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 521 | { |
| 522 | char *tmp; |
| 523 | int ret; |
| 524 | |
| 525 | if (!offset) |
| 526 | return -EINVAL; |
| 527 | |
| 528 | tmp = strchr(symbol, '+'); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 529 | if (tmp) { |
| 530 | /* skip sign because strict_strtol doesn't accept '+' */ |
Masami Hiramatsu | 2fba0c8 | 2009-09-10 19:53:14 -0400 | [diff] [blame] | 531 | ret = strict_strtoul(tmp + 1, 0, offset); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 532 | if (ret) |
| 533 | return ret; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 534 | *tmp = '\0'; |
| 535 | } else |
| 536 | *offset = 0; |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | #define PARAM_MAX_ARGS 16 |
| 541 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) |
| 542 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 543 | static int parse_probe_vars(char *arg, const struct fetch_type *t, |
| 544 | struct fetch_param *f, int is_return) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 545 | { |
| 546 | int ret = 0; |
| 547 | unsigned long param; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 548 | |
Masami Hiramatsu | 2e06ff6 | 2009-10-07 18:27:59 -0400 | [diff] [blame] | 549 | if (strcmp(arg, "retval") == 0) { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 550 | if (is_return) |
| 551 | f->fn = t->retval; |
| 552 | else |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 553 | ret = -EINVAL; |
Masami Hiramatsu | 2e06ff6 | 2009-10-07 18:27:59 -0400 | [diff] [blame] | 554 | } else if (strncmp(arg, "stack", 5) == 0) { |
| 555 | if (arg[5] == '\0') { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 556 | if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR) == 0) |
| 557 | f->fn = fetch_stack_address; |
| 558 | else |
| 559 | ret = -EINVAL; |
Masami Hiramatsu | 2e06ff6 | 2009-10-07 18:27:59 -0400 | [diff] [blame] | 560 | } else if (isdigit(arg[5])) { |
| 561 | ret = strict_strtoul(arg + 5, 10, ¶m); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 562 | if (ret || param > PARAM_MAX_STACK) |
| 563 | ret = -EINVAL; |
| 564 | else { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 565 | f->fn = t->stack; |
| 566 | f->data = (void *)param; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 567 | } |
Masami Hiramatsu | 2e06ff6 | 2009-10-07 18:27:59 -0400 | [diff] [blame] | 568 | } else |
| 569 | ret = -EINVAL; |
Masami Hiramatsu | 2e06ff6 | 2009-10-07 18:27:59 -0400 | [diff] [blame] | 570 | } else |
Masami Hiramatsu | 405b265 | 2009-10-07 18:27:40 -0400 | [diff] [blame] | 571 | ret = -EINVAL; |
Masami Hiramatsu | 405b265 | 2009-10-07 18:27:40 -0400 | [diff] [blame] | 572 | return ret; |
| 573 | } |
| 574 | |
Masami Hiramatsu | ba8665d | 2009-11-30 19:19:20 -0500 | [diff] [blame] | 575 | /* Recursive argument parser */ |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 576 | static int __parse_probe_arg(char *arg, const struct fetch_type *t, |
| 577 | struct fetch_param *f, int is_return) |
Masami Hiramatsu | 405b265 | 2009-10-07 18:27:40 -0400 | [diff] [blame] | 578 | { |
| 579 | int ret = 0; |
| 580 | unsigned long param; |
| 581 | long offset; |
| 582 | char *tmp; |
| 583 | |
| 584 | switch (arg[0]) { |
| 585 | case '$': |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 586 | ret = parse_probe_vars(arg + 1, t, f, is_return); |
Masami Hiramatsu | 405b265 | 2009-10-07 18:27:40 -0400 | [diff] [blame] | 587 | break; |
| 588 | case '%': /* named register */ |
| 589 | ret = regs_query_register_offset(arg + 1); |
| 590 | if (ret >= 0) { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 591 | f->fn = t->reg; |
| 592 | f->data = (void *)(unsigned long)ret; |
Masami Hiramatsu | 405b265 | 2009-10-07 18:27:40 -0400 | [diff] [blame] | 593 | ret = 0; |
| 594 | } |
| 595 | break; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 596 | case '@': /* memory or symbol */ |
| 597 | if (isdigit(arg[1])) { |
| 598 | ret = strict_strtoul(arg + 1, 0, ¶m); |
| 599 | if (ret) |
| 600 | break; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 601 | f->fn = t->memory; |
| 602 | f->data = (void *)param; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 603 | } else { |
| 604 | ret = split_symbol_offset(arg + 1, &offset); |
| 605 | if (ret) |
| 606 | break; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 607 | f->data = alloc_symbol_cache(arg + 1, offset); |
| 608 | if (f->data) |
| 609 | f->fn = t->symbol; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 610 | } |
| 611 | break; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 612 | case '+': /* deref memory */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 613 | case '-': |
| 614 | tmp = strchr(arg, '('); |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 615 | if (!tmp) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 616 | break; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 617 | *tmp = '\0'; |
| 618 | ret = strict_strtol(arg + 1, 0, &offset); |
| 619 | if (ret) |
| 620 | break; |
| 621 | if (arg[0] == '-') |
| 622 | offset = -offset; |
| 623 | arg = tmp + 1; |
| 624 | tmp = strrchr(arg, ')'); |
| 625 | if (tmp) { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 626 | struct deref_fetch_param *dprm; |
| 627 | const struct fetch_type *t2 = find_fetch_type(NULL); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 628 | *tmp = '\0'; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 629 | dprm = kzalloc(sizeof(struct deref_fetch_param), |
| 630 | GFP_KERNEL); |
| 631 | if (!dprm) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 632 | return -ENOMEM; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 633 | dprm->offset = offset; |
| 634 | ret = __parse_probe_arg(arg, t2, &dprm->orig, |
| 635 | is_return); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 636 | if (ret) |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 637 | kfree(dprm); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 638 | else { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 639 | f->fn = t->deref; |
| 640 | f->data = (void *)dprm; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 641 | } |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 642 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 643 | break; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 644 | } |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 645 | if (!ret && !f->fn) |
| 646 | ret = -EINVAL; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 647 | return ret; |
| 648 | } |
| 649 | |
Masami Hiramatsu | ba8665d | 2009-11-30 19:19:20 -0500 | [diff] [blame] | 650 | /* String length checking wrapper */ |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 651 | static int parse_probe_arg(char *arg, struct trace_probe *tp, |
| 652 | struct probe_arg *parg, int is_return) |
Masami Hiramatsu | ba8665d | 2009-11-30 19:19:20 -0500 | [diff] [blame] | 653 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 654 | const char *t; |
| 655 | |
Masami Hiramatsu | ba8665d | 2009-11-30 19:19:20 -0500 | [diff] [blame] | 656 | if (strlen(arg) > MAX_ARGSTR_LEN) { |
| 657 | pr_info("Argument is too long.: %s\n", arg); |
| 658 | return -ENOSPC; |
| 659 | } |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 660 | parg->comm = kstrdup(arg, GFP_KERNEL); |
| 661 | if (!parg->comm) { |
| 662 | pr_info("Failed to allocate memory for command '%s'.\n", arg); |
| 663 | return -ENOMEM; |
| 664 | } |
| 665 | t = strchr(parg->comm, ':'); |
| 666 | if (t) { |
| 667 | arg[t - parg->comm] = '\0'; |
| 668 | t++; |
| 669 | } |
| 670 | parg->type = find_fetch_type(t); |
| 671 | if (!parg->type) { |
| 672 | pr_info("Unsupported type: %s\n", t); |
| 673 | return -EINVAL; |
| 674 | } |
| 675 | parg->offset = tp->size; |
| 676 | tp->size += parg->type->size; |
| 677 | return __parse_probe_arg(arg, parg->type, &parg->fetch, is_return); |
Masami Hiramatsu | ba8665d | 2009-11-30 19:19:20 -0500 | [diff] [blame] | 678 | } |
| 679 | |
Masami Hiramatsu | a703d94 | 2009-10-07 18:28:07 -0400 | [diff] [blame] | 680 | /* Return 1 if name is reserved or already used by another argument */ |
| 681 | static int conflict_field_name(const char *name, |
| 682 | struct probe_arg *args, int narg) |
| 683 | { |
| 684 | int i; |
| 685 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) |
| 686 | if (strcmp(reserved_field_names[i], name) == 0) |
| 687 | return 1; |
| 688 | for (i = 0; i < narg; i++) |
| 689 | if (strcmp(args[i].name, name) == 0) |
| 690 | return 1; |
| 691 | return 0; |
| 692 | } |
| 693 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 694 | static int create_trace_probe(int argc, char **argv) |
| 695 | { |
| 696 | /* |
| 697 | * Argument syntax: |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 698 | * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS] |
| 699 | * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS] |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 700 | * Fetch args: |
Masami Hiramatsu | 2e06ff6 | 2009-10-07 18:27:59 -0400 | [diff] [blame] | 701 | * $retval : fetch return value |
| 702 | * $stack : fetch stack address |
| 703 | * $stackN : fetch Nth of stack (N:0-) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 704 | * @ADDR : fetch memory at ADDR (ADDR should be in kernel) |
| 705 | * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol) |
| 706 | * %REG : fetch register REG |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 707 | * Dereferencing memory fetch: |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 708 | * +|-offs(ARG) : fetch memory at ARG +|- offs address. |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 709 | * Alias name of args: |
| 710 | * NAME=FETCHARG : set NAME as alias of FETCHARG. |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 711 | * Type of args: |
| 712 | * FETCHARG:TYPE : use TYPE instead of unsigned long. |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 713 | */ |
| 714 | struct trace_probe *tp; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 715 | int i, ret = 0; |
Masami Hiramatsu | a7c312b | 2009-12-08 17:03:16 -0500 | [diff] [blame] | 716 | int is_return = 0, is_delete = 0; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 717 | char *symbol = NULL, *event = NULL, *group = NULL; |
| 718 | char *arg, *tmp; |
Masami Hiramatsu | 2fba0c8 | 2009-09-10 19:53:14 -0400 | [diff] [blame] | 719 | unsigned long offset = 0; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 720 | void *addr = NULL; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 721 | char buf[MAX_EVENT_NAME_LEN]; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 722 | |
Masami Hiramatsu | a7c312b | 2009-12-08 17:03:16 -0500 | [diff] [blame] | 723 | /* argc must be >= 1 */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 724 | if (argv[0][0] == 'p') |
| 725 | is_return = 0; |
| 726 | else if (argv[0][0] == 'r') |
| 727 | is_return = 1; |
Masami Hiramatsu | a7c312b | 2009-12-08 17:03:16 -0500 | [diff] [blame] | 728 | else if (argv[0][0] == '-') |
| 729 | is_delete = 1; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 730 | else { |
Masami Hiramatsu | a7c312b | 2009-12-08 17:03:16 -0500 | [diff] [blame] | 731 | pr_info("Probe definition must be started with 'p', 'r' or" |
| 732 | " '-'.\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 733 | return -EINVAL; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 734 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 735 | |
| 736 | if (argv[0][1] == ':') { |
| 737 | event = &argv[0][2]; |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 738 | if (strchr(event, '/')) { |
| 739 | group = event; |
| 740 | event = strchr(group, '/') + 1; |
| 741 | event[-1] = '\0'; |
| 742 | if (strlen(group) == 0) { |
Wenji Huang | a5efd92 | 2010-02-24 15:40:23 +0800 | [diff] [blame] | 743 | pr_info("Group name is not specified\n"); |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 744 | return -EINVAL; |
| 745 | } |
| 746 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 747 | if (strlen(event) == 0) { |
Wenji Huang | a5efd92 | 2010-02-24 15:40:23 +0800 | [diff] [blame] | 748 | pr_info("Event name is not specified\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 749 | return -EINVAL; |
| 750 | } |
| 751 | } |
Masami Hiramatsu | a7c312b | 2009-12-08 17:03:16 -0500 | [diff] [blame] | 752 | if (!group) |
| 753 | group = KPROBE_EVENT_SYSTEM; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 754 | |
Masami Hiramatsu | a7c312b | 2009-12-08 17:03:16 -0500 | [diff] [blame] | 755 | if (is_delete) { |
| 756 | if (!event) { |
| 757 | pr_info("Delete command needs an event name.\n"); |
| 758 | return -EINVAL; |
| 759 | } |
| 760 | tp = find_probe_event(event, group); |
| 761 | if (!tp) { |
| 762 | pr_info("Event %s/%s doesn't exist.\n", group, event); |
| 763 | return -ENOENT; |
| 764 | } |
| 765 | /* delete an event */ |
| 766 | unregister_trace_probe(tp); |
| 767 | free_trace_probe(tp); |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | if (argc < 2) { |
| 772 | pr_info("Probe point is not specified.\n"); |
| 773 | return -EINVAL; |
| 774 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 775 | if (isdigit(argv[1][0])) { |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 776 | if (is_return) { |
| 777 | pr_info("Return probe point must be a symbol.\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 778 | return -EINVAL; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 779 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 780 | /* an address specified */ |
Heiko Carstens | a9bb18f | 2010-02-10 17:23:47 +0100 | [diff] [blame] | 781 | ret = strict_strtoul(&argv[1][0], 0, (unsigned long *)&addr); |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 782 | if (ret) { |
| 783 | pr_info("Failed to parse address.\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 784 | return ret; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 785 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 786 | } else { |
| 787 | /* a symbol specified */ |
| 788 | symbol = argv[1]; |
| 789 | /* TODO: support .init module functions */ |
| 790 | ret = split_symbol_offset(symbol, &offset); |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 791 | if (ret) { |
| 792 | pr_info("Failed to parse symbol.\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 793 | return ret; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 794 | } |
| 795 | if (offset && is_return) { |
| 796 | pr_info("Return probe must be used without offset.\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 797 | return -EINVAL; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 798 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 799 | } |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 800 | argc -= 2; argv += 2; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 801 | |
| 802 | /* setup a probe */ |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 803 | if (!event) { |
| 804 | /* Make a new event name */ |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 805 | if (symbol) |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 806 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld", |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 807 | is_return ? 'r' : 'p', symbol, offset); |
| 808 | else |
Masami Hiramatsu | 6f3cf44 | 2009-12-16 17:24:08 -0500 | [diff] [blame] | 809 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p", |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 810 | is_return ? 'r' : 'p', addr); |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 811 | event = buf; |
| 812 | } |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 813 | tp = alloc_trace_probe(group, event, addr, symbol, offset, argc, |
| 814 | is_return); |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 815 | if (IS_ERR(tp)) { |
| 816 | pr_info("Failed to allocate trace_probe.(%d)\n", |
| 817 | (int)PTR_ERR(tp)); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 818 | return PTR_ERR(tp); |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 819 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 820 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 821 | /* parse arguments */ |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 822 | ret = 0; |
| 823 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 824 | /* Parse argument name */ |
| 825 | arg = strchr(argv[i], '='); |
| 826 | if (arg) |
| 827 | *arg++ = '\0'; |
| 828 | else |
| 829 | arg = argv[i]; |
Masami Hiramatsu | a703d94 | 2009-10-07 18:28:07 -0400 | [diff] [blame] | 830 | |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 831 | tp->args[i].name = kstrdup(argv[i], GFP_KERNEL); |
Masami Hiramatsu | ba8665d | 2009-11-30 19:19:20 -0500 | [diff] [blame] | 832 | if (!tp->args[i].name) { |
| 833 | pr_info("Failed to allocate argument%d name '%s'.\n", |
| 834 | i, argv[i]); |
| 835 | ret = -ENOMEM; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 836 | goto error; |
| 837 | } |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 838 | tmp = strchr(tp->args[i].name, ':'); |
| 839 | if (tmp) |
| 840 | *tmp = '_'; /* convert : to _ */ |
| 841 | |
| 842 | if (conflict_field_name(tp->args[i].name, tp->args, i)) { |
| 843 | pr_info("Argument%d name '%s' conflicts with " |
| 844 | "another field.\n", i, argv[i]); |
| 845 | ret = -EINVAL; |
| 846 | goto error; |
| 847 | } |
Masami Hiramatsu | ba8665d | 2009-11-30 19:19:20 -0500 | [diff] [blame] | 848 | |
| 849 | /* Parse fetch argument */ |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 850 | ret = parse_probe_arg(arg, tp, &tp->args[i], is_return); |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 851 | if (ret) { |
| 852 | pr_info("Parse error at argument%d. (%d)\n", i, ret); |
Lai Jiangshan | abab9d3 | 2009-11-25 16:32:21 +0800 | [diff] [blame] | 853 | kfree(tp->args[i].name); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 854 | goto error; |
Masami Hiramatsu | e63cc23 | 2009-10-16 20:07:28 -0400 | [diff] [blame] | 855 | } |
Lai Jiangshan | abab9d3 | 2009-11-25 16:32:21 +0800 | [diff] [blame] | 856 | |
| 857 | tp->nr_args++; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 858 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 859 | |
| 860 | ret = register_trace_probe(tp); |
| 861 | if (ret) |
| 862 | goto error; |
| 863 | return 0; |
| 864 | |
| 865 | error: |
| 866 | free_trace_probe(tp); |
| 867 | return ret; |
| 868 | } |
| 869 | |
| 870 | static void cleanup_all_probes(void) |
| 871 | { |
| 872 | struct trace_probe *tp; |
| 873 | |
| 874 | mutex_lock(&probe_lock); |
| 875 | /* TODO: Use batch unregistration */ |
| 876 | while (!list_empty(&probe_list)) { |
| 877 | tp = list_entry(probe_list.next, struct trace_probe, list); |
| 878 | unregister_trace_probe(tp); |
| 879 | free_trace_probe(tp); |
| 880 | } |
| 881 | mutex_unlock(&probe_lock); |
| 882 | } |
| 883 | |
| 884 | |
| 885 | /* Probes listing interfaces */ |
| 886 | static void *probes_seq_start(struct seq_file *m, loff_t *pos) |
| 887 | { |
| 888 | mutex_lock(&probe_lock); |
| 889 | return seq_list_start(&probe_list, *pos); |
| 890 | } |
| 891 | |
| 892 | static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 893 | { |
| 894 | return seq_list_next(v, &probe_list, pos); |
| 895 | } |
| 896 | |
| 897 | static void probes_seq_stop(struct seq_file *m, void *v) |
| 898 | { |
| 899 | mutex_unlock(&probe_lock); |
| 900 | } |
| 901 | |
| 902 | static int probes_seq_show(struct seq_file *m, void *v) |
| 903 | { |
| 904 | struct trace_probe *tp = v; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 905 | int i; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 906 | |
| 907 | seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p'); |
Steven Rostedt | 8f08201 | 2010-04-20 10:47:33 -0400 | [diff] [blame] | 908 | seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 909 | |
Lai Jiangshan | 52a11f3 | 2009-11-25 16:33:15 +0800 | [diff] [blame] | 910 | if (!tp->symbol) |
| 911 | seq_printf(m, " 0x%p", tp->rp.kp.addr); |
| 912 | else if (tp->rp.kp.offset) |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 913 | seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 914 | else |
Lai Jiangshan | 52a11f3 | 2009-11-25 16:33:15 +0800 | [diff] [blame] | 915 | seq_printf(m, " %s", probe_symbol(tp)); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 916 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 917 | for (i = 0; i < tp->nr_args; i++) |
| 918 | seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 919 | seq_printf(m, "\n"); |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 920 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | static const struct seq_operations probes_seq_op = { |
| 925 | .start = probes_seq_start, |
| 926 | .next = probes_seq_next, |
| 927 | .stop = probes_seq_stop, |
| 928 | .show = probes_seq_show |
| 929 | }; |
| 930 | |
| 931 | static int probes_open(struct inode *inode, struct file *file) |
| 932 | { |
| 933 | if ((file->f_mode & FMODE_WRITE) && |
| 934 | (file->f_flags & O_TRUNC)) |
| 935 | cleanup_all_probes(); |
| 936 | |
| 937 | return seq_open(file, &probes_seq_op); |
| 938 | } |
| 939 | |
| 940 | static int command_trace_probe(const char *buf) |
| 941 | { |
| 942 | char **argv; |
| 943 | int argc = 0, ret = 0; |
| 944 | |
| 945 | argv = argv_split(GFP_KERNEL, buf, &argc); |
| 946 | if (!argv) |
| 947 | return -ENOMEM; |
| 948 | |
| 949 | if (argc) |
| 950 | ret = create_trace_probe(argc, argv); |
| 951 | |
| 952 | argv_free(argv); |
| 953 | return ret; |
| 954 | } |
| 955 | |
| 956 | #define WRITE_BUFSIZE 128 |
| 957 | |
| 958 | static ssize_t probes_write(struct file *file, const char __user *buffer, |
| 959 | size_t count, loff_t *ppos) |
| 960 | { |
| 961 | char *kbuf, *tmp; |
| 962 | int ret; |
| 963 | size_t done; |
| 964 | size_t size; |
| 965 | |
| 966 | kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL); |
| 967 | if (!kbuf) |
| 968 | return -ENOMEM; |
| 969 | |
| 970 | ret = done = 0; |
| 971 | while (done < count) { |
| 972 | size = count - done; |
| 973 | if (size >= WRITE_BUFSIZE) |
| 974 | size = WRITE_BUFSIZE - 1; |
| 975 | if (copy_from_user(kbuf, buffer + done, size)) { |
| 976 | ret = -EFAULT; |
| 977 | goto out; |
| 978 | } |
| 979 | kbuf[size] = '\0'; |
| 980 | tmp = strchr(kbuf, '\n'); |
| 981 | if (tmp) { |
| 982 | *tmp = '\0'; |
| 983 | size = tmp - kbuf + 1; |
| 984 | } else if (done + size < count) { |
| 985 | pr_warning("Line length is too long: " |
| 986 | "Should be less than %d.", WRITE_BUFSIZE); |
| 987 | ret = -EINVAL; |
| 988 | goto out; |
| 989 | } |
| 990 | done += size; |
| 991 | /* Remove comments */ |
| 992 | tmp = strchr(kbuf, '#'); |
| 993 | if (tmp) |
| 994 | *tmp = '\0'; |
| 995 | |
| 996 | ret = command_trace_probe(kbuf); |
| 997 | if (ret) |
| 998 | goto out; |
| 999 | } |
| 1000 | ret = done; |
| 1001 | out: |
| 1002 | kfree(kbuf); |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
| 1006 | static const struct file_operations kprobe_events_ops = { |
| 1007 | .owner = THIS_MODULE, |
| 1008 | .open = probes_open, |
| 1009 | .read = seq_read, |
| 1010 | .llseek = seq_lseek, |
| 1011 | .release = seq_release, |
| 1012 | .write = probes_write, |
| 1013 | }; |
| 1014 | |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1015 | /* Probes profiling interfaces */ |
| 1016 | static int probes_profile_seq_show(struct seq_file *m, void *v) |
| 1017 | { |
| 1018 | struct trace_probe *tp = v; |
| 1019 | |
| 1020 | seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit, |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 1021 | tp->rp.kp.nmissed); |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1022 | |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | static const struct seq_operations profile_seq_op = { |
| 1027 | .start = probes_seq_start, |
| 1028 | .next = probes_seq_next, |
| 1029 | .stop = probes_seq_stop, |
| 1030 | .show = probes_profile_seq_show |
| 1031 | }; |
| 1032 | |
| 1033 | static int profile_open(struct inode *inode, struct file *file) |
| 1034 | { |
| 1035 | return seq_open(file, &profile_seq_op); |
| 1036 | } |
| 1037 | |
| 1038 | static const struct file_operations kprobe_profile_ops = { |
| 1039 | .owner = THIS_MODULE, |
| 1040 | .open = profile_open, |
| 1041 | .read = seq_read, |
| 1042 | .llseek = seq_lseek, |
| 1043 | .release = seq_release, |
| 1044 | }; |
| 1045 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1046 | /* Kprobe handler */ |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1047 | static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1048 | { |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 1049 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1050 | struct kprobe_trace_entry_head *entry; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1051 | struct ring_buffer_event *event; |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 1052 | struct ring_buffer *buffer; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1053 | u8 *data; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1054 | int size, i, pc; |
| 1055 | unsigned long irq_flags; |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 1056 | struct ftrace_event_call *call = &tp->call; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1057 | |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1058 | tp->nhit++; |
| 1059 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1060 | local_save_flags(irq_flags); |
| 1061 | pc = preempt_count(); |
| 1062 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1063 | size = sizeof(*entry) + tp->size; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1064 | |
Steven Rostedt | 32c0eda | 2010-04-23 10:38:03 -0400 | [diff] [blame] | 1065 | event = trace_current_buffer_lock_reserve(&buffer, call->event.type, |
| 1066 | size, irq_flags, pc); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1067 | if (!event) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1068 | return; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1069 | |
| 1070 | entry = ring_buffer_event_data(event); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1071 | entry->ip = (unsigned long)kp->addr; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1072 | data = (u8 *)&entry[1]; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1073 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1074 | call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1075 | |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 1076 | if (!filter_current_check_discard(buffer, call, entry, event)) |
| 1077 | trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | /* Kretprobe handler */ |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1081 | static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri, |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1082 | struct pt_regs *regs) |
| 1083 | { |
| 1084 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1085 | struct kretprobe_trace_entry_head *entry; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1086 | struct ring_buffer_event *event; |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 1087 | struct ring_buffer *buffer; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1088 | u8 *data; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1089 | int size, i, pc; |
| 1090 | unsigned long irq_flags; |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 1091 | struct ftrace_event_call *call = &tp->call; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1092 | |
| 1093 | local_save_flags(irq_flags); |
| 1094 | pc = preempt_count(); |
| 1095 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1096 | size = sizeof(*entry) + tp->size; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1097 | |
Steven Rostedt | 32c0eda | 2010-04-23 10:38:03 -0400 | [diff] [blame] | 1098 | event = trace_current_buffer_lock_reserve(&buffer, call->event.type, |
| 1099 | size, irq_flags, pc); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1100 | if (!event) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1101 | return; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1102 | |
| 1103 | entry = ring_buffer_event_data(event); |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 1104 | entry->func = (unsigned long)tp->rp.kp.addr; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1105 | entry->ret_ip = (unsigned long)ri->ret_addr; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1106 | data = (u8 *)&entry[1]; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1107 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1108 | call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1109 | |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 1110 | if (!filter_current_check_discard(buffer, call, entry, event)) |
| 1111 | trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | /* Event entry printers */ |
| 1115 | enum print_line_t |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1116 | print_kprobe_event(struct trace_iterator *iter, int flags, |
| 1117 | struct trace_event *event) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1118 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1119 | struct kprobe_trace_entry_head *field; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1120 | struct trace_seq *s = &iter->seq; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1121 | struct trace_probe *tp; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1122 | u8 *data; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1123 | int i; |
| 1124 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1125 | field = (struct kprobe_trace_entry_head *)iter->ent; |
Steven Rostedt | 80decc7 | 2010-04-23 10:00:22 -0400 | [diff] [blame] | 1126 | tp = container_of(event, struct trace_probe, call.event); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1127 | |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 1128 | if (!trace_seq_printf(s, "%s: (", tp->call.name)) |
| 1129 | goto partial; |
| 1130 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1131 | if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET)) |
| 1132 | goto partial; |
| 1133 | |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 1134 | if (!trace_seq_puts(s, ")")) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1135 | goto partial; |
| 1136 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1137 | data = (u8 *)&field[1]; |
| 1138 | for (i = 0; i < tp->nr_args; i++) |
| 1139 | if (!tp->args[i].type->print(s, tp->args[i].name, |
| 1140 | data + tp->args[i].offset)) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1141 | goto partial; |
| 1142 | |
| 1143 | if (!trace_seq_puts(s, "\n")) |
| 1144 | goto partial; |
| 1145 | |
| 1146 | return TRACE_TYPE_HANDLED; |
| 1147 | partial: |
| 1148 | return TRACE_TYPE_PARTIAL_LINE; |
| 1149 | } |
| 1150 | |
| 1151 | enum print_line_t |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1152 | print_kretprobe_event(struct trace_iterator *iter, int flags, |
| 1153 | struct trace_event *event) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1154 | { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1155 | struct kretprobe_trace_entry_head *field; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1156 | struct trace_seq *s = &iter->seq; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1157 | struct trace_probe *tp; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1158 | u8 *data; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1159 | int i; |
| 1160 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1161 | field = (struct kretprobe_trace_entry_head *)iter->ent; |
Steven Rostedt | 80decc7 | 2010-04-23 10:00:22 -0400 | [diff] [blame] | 1162 | tp = container_of(event, struct trace_probe, call.event); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1163 | |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 1164 | if (!trace_seq_printf(s, "%s: (", tp->call.name)) |
| 1165 | goto partial; |
| 1166 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1167 | if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET)) |
| 1168 | goto partial; |
| 1169 | |
| 1170 | if (!trace_seq_puts(s, " <- ")) |
| 1171 | goto partial; |
| 1172 | |
| 1173 | if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET)) |
| 1174 | goto partial; |
| 1175 | |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 1176 | if (!trace_seq_puts(s, ")")) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1177 | goto partial; |
| 1178 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1179 | data = (u8 *)&field[1]; |
| 1180 | for (i = 0; i < tp->nr_args; i++) |
| 1181 | if (!tp->args[i].type->print(s, tp->args[i].name, |
| 1182 | data + tp->args[i].offset)) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1183 | goto partial; |
| 1184 | |
| 1185 | if (!trace_seq_puts(s, "\n")) |
| 1186 | goto partial; |
| 1187 | |
| 1188 | return TRACE_TYPE_HANDLED; |
| 1189 | partial: |
| 1190 | return TRACE_TYPE_PARTIAL_LINE; |
| 1191 | } |
| 1192 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1193 | static int probe_event_enable(struct ftrace_event_call *call) |
| 1194 | { |
| 1195 | struct trace_probe *tp = (struct trace_probe *)call->data; |
| 1196 | |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1197 | tp->flags |= TP_FLAG_TRACE; |
| 1198 | if (probe_is_return(tp)) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1199 | return enable_kretprobe(&tp->rp); |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1200 | else |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 1201 | return enable_kprobe(&tp->rp.kp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | static void probe_event_disable(struct ftrace_event_call *call) |
| 1205 | { |
| 1206 | struct trace_probe *tp = (struct trace_probe *)call->data; |
| 1207 | |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1208 | tp->flags &= ~TP_FLAG_TRACE; |
| 1209 | if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) { |
| 1210 | if (probe_is_return(tp)) |
| 1211 | disable_kretprobe(&tp->rp); |
| 1212 | else |
| 1213 | disable_kprobe(&tp->rp.kp); |
| 1214 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1215 | } |
| 1216 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1217 | #undef DEFINE_FIELD |
| 1218 | #define DEFINE_FIELD(type, item, name, is_signed) \ |
| 1219 | do { \ |
| 1220 | ret = trace_define_field(event_call, #type, name, \ |
| 1221 | offsetof(typeof(field), item), \ |
| 1222 | sizeof(field.item), is_signed, \ |
| 1223 | FILTER_OTHER); \ |
| 1224 | if (ret) \ |
| 1225 | return ret; \ |
| 1226 | } while (0) |
| 1227 | |
| 1228 | static int kprobe_event_define_fields(struct ftrace_event_call *event_call) |
| 1229 | { |
| 1230 | int ret, i; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1231 | struct kprobe_trace_entry_head field; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1232 | struct trace_probe *tp = (struct trace_probe *)event_call->data; |
| 1233 | |
Masami Hiramatsu | a703d94 | 2009-10-07 18:28:07 -0400 | [diff] [blame] | 1234 | DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0); |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1235 | /* Set argument names as fields */ |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1236 | for (i = 0; i < tp->nr_args; i++) { |
| 1237 | ret = trace_define_field(event_call, tp->args[i].type->name, |
| 1238 | tp->args[i].name, |
| 1239 | sizeof(field) + tp->args[i].offset, |
| 1240 | tp->args[i].type->size, |
| 1241 | tp->args[i].type->is_signed, |
| 1242 | FILTER_OTHER); |
| 1243 | if (ret) |
| 1244 | return ret; |
| 1245 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | static int kretprobe_event_define_fields(struct ftrace_event_call *event_call) |
| 1250 | { |
| 1251 | int ret, i; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1252 | struct kretprobe_trace_entry_head field; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1253 | struct trace_probe *tp = (struct trace_probe *)event_call->data; |
| 1254 | |
Masami Hiramatsu | a703d94 | 2009-10-07 18:28:07 -0400 | [diff] [blame] | 1255 | DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0); |
| 1256 | DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0); |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1257 | /* Set argument names as fields */ |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1258 | for (i = 0; i < tp->nr_args; i++) { |
| 1259 | ret = trace_define_field(event_call, tp->args[i].type->name, |
| 1260 | tp->args[i].name, |
| 1261 | sizeof(field) + tp->args[i].offset, |
| 1262 | tp->args[i].type->size, |
| 1263 | tp->args[i].type->is_signed, |
| 1264 | FILTER_OTHER); |
| 1265 | if (ret) |
| 1266 | return ret; |
| 1267 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1268 | return 0; |
| 1269 | } |
| 1270 | |
Lai Jiangshan | a342a028 | 2009-12-15 15:39:49 +0800 | [diff] [blame] | 1271 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len) |
| 1272 | { |
| 1273 | int i; |
| 1274 | int pos = 0; |
| 1275 | |
| 1276 | const char *fmt, *arg; |
| 1277 | |
| 1278 | if (!probe_is_return(tp)) { |
| 1279 | fmt = "(%lx)"; |
| 1280 | arg = "REC->" FIELD_STRING_IP; |
| 1281 | } else { |
| 1282 | fmt = "(%lx <- %lx)"; |
| 1283 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; |
| 1284 | } |
| 1285 | |
| 1286 | /* When len=0, we just calculate the needed length */ |
| 1287 | #define LEN_OR_ZERO (len ? len - pos : 0) |
| 1288 | |
| 1289 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); |
| 1290 | |
| 1291 | for (i = 0; i < tp->nr_args; i++) { |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1292 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s", |
| 1293 | tp->args[i].name, tp->args[i].type->fmt); |
Lai Jiangshan | a342a028 | 2009-12-15 15:39:49 +0800 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); |
| 1297 | |
| 1298 | for (i = 0; i < tp->nr_args; i++) { |
| 1299 | pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s", |
| 1300 | tp->args[i].name); |
| 1301 | } |
| 1302 | |
| 1303 | #undef LEN_OR_ZERO |
| 1304 | |
| 1305 | /* return the length of print_fmt */ |
| 1306 | return pos; |
| 1307 | } |
| 1308 | |
| 1309 | static int set_print_fmt(struct trace_probe *tp) |
| 1310 | { |
| 1311 | int len; |
| 1312 | char *print_fmt; |
| 1313 | |
| 1314 | /* First: called with 0 length to calculate the needed length */ |
| 1315 | len = __set_print_fmt(tp, NULL, 0); |
| 1316 | print_fmt = kmalloc(len + 1, GFP_KERNEL); |
| 1317 | if (!print_fmt) |
| 1318 | return -ENOMEM; |
| 1319 | |
| 1320 | /* Second: actually write the @print_fmt */ |
| 1321 | __set_print_fmt(tp, print_fmt, len + 1); |
| 1322 | tp->call.print_fmt = print_fmt; |
| 1323 | |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1327 | #ifdef CONFIG_PERF_EVENTS |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1328 | |
| 1329 | /* Kprobe profile handler */ |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 1330 | static __kprobes void kprobe_perf_func(struct kprobe *kp, |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1331 | struct pt_regs *regs) |
| 1332 | { |
| 1333 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); |
| 1334 | struct ftrace_event_call *call = &tp->call; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1335 | struct kprobe_trace_entry_head *entry; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 1336 | struct hlist_head *head; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1337 | u8 *data; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 1338 | int size, __size, i; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 1339 | int rctx; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1340 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1341 | __size = sizeof(*entry) + tp->size; |
Masami Hiramatsu | 74ebb63 | 2009-09-14 16:49:28 -0400 | [diff] [blame] | 1342 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); |
| 1343 | size -= sizeof(u32); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 1344 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, |
Masami Hiramatsu | a1a138d | 2009-09-25 11:20:12 -0700 | [diff] [blame] | 1345 | "profile buffer not large enough")) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1346 | return; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1347 | |
Steven Rostedt | ff5f149 | 2010-05-21 11:49:57 -0400 | [diff] [blame] | 1348 | entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 1349 | if (!entry) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1350 | return; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 1351 | |
Masami Hiramatsu | a1a138d | 2009-09-25 11:20:12 -0700 | [diff] [blame] | 1352 | entry->ip = (unsigned long)kp->addr; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1353 | data = (u8 *)&entry[1]; |
Masami Hiramatsu | a1a138d | 2009-09-25 11:20:12 -0700 | [diff] [blame] | 1354 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1355 | call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset); |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 1356 | |
Peter Zijlstra | 3771f07 | 2010-05-21 12:31:09 +0200 | [diff] [blame] | 1357 | head = this_cpu_ptr(call->perf_events); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 1358 | perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | /* Kretprobe profile handler */ |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 1362 | static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri, |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1363 | struct pt_regs *regs) |
| 1364 | { |
| 1365 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); |
| 1366 | struct ftrace_event_call *call = &tp->call; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1367 | struct kretprobe_trace_entry_head *entry; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 1368 | struct hlist_head *head; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1369 | u8 *data; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 1370 | int size, __size, i; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 1371 | int rctx; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1372 | |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1373 | __size = sizeof(*entry) + tp->size; |
Masami Hiramatsu | 74ebb63 | 2009-09-14 16:49:28 -0400 | [diff] [blame] | 1374 | size = ALIGN(__size + sizeof(u32), sizeof(u64)); |
| 1375 | size -= sizeof(u32); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 1376 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, |
Masami Hiramatsu | a1a138d | 2009-09-25 11:20:12 -0700 | [diff] [blame] | 1377 | "profile buffer not large enough")) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1378 | return; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1379 | |
Steven Rostedt | ff5f149 | 2010-05-21 11:49:57 -0400 | [diff] [blame] | 1380 | entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 1381 | if (!entry) |
Xiao Guangrong | 1e12a4a | 2010-01-28 09:34:27 +0800 | [diff] [blame] | 1382 | return; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 1383 | |
Masami Hiramatsu | a1a138d | 2009-09-25 11:20:12 -0700 | [diff] [blame] | 1384 | entry->func = (unsigned long)tp->rp.kp.addr; |
| 1385 | entry->ret_ip = (unsigned long)ri->ret_addr; |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1386 | data = (u8 *)&entry[1]; |
Masami Hiramatsu | a1a138d | 2009-09-25 11:20:12 -0700 | [diff] [blame] | 1387 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | 93ccae7 | 2010-04-12 13:17:08 -0400 | [diff] [blame] | 1388 | call_fetch(&tp->args[i].fetch, regs, data + tp->args[i].offset); |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 1389 | |
Peter Zijlstra | 3771f07 | 2010-05-21 12:31:09 +0200 | [diff] [blame] | 1390 | head = this_cpu_ptr(call->perf_events); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 1391 | perf_trace_buf_submit(entry, size, rctx, entry->ret_ip, 1, regs, head); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1392 | } |
| 1393 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 1394 | static int probe_perf_enable(struct ftrace_event_call *call) |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1395 | { |
| 1396 | struct trace_probe *tp = (struct trace_probe *)call->data; |
| 1397 | |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1398 | tp->flags |= TP_FLAG_PROFILE; |
Frederic Weisbecker | d7a4b41 | 2009-09-23 23:08:43 +0200 | [diff] [blame] | 1399 | |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1400 | if (probe_is_return(tp)) |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1401 | return enable_kretprobe(&tp->rp); |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1402 | else |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1403 | return enable_kprobe(&tp->rp.kp); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1404 | } |
| 1405 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 1406 | static void probe_perf_disable(struct ftrace_event_call *call) |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1407 | { |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1408 | struct trace_probe *tp = (struct trace_probe *)call->data; |
| 1409 | |
Frederic Weisbecker | d7a4b41 | 2009-09-23 23:08:43 +0200 | [diff] [blame] | 1410 | tp->flags &= ~TP_FLAG_PROFILE; |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1411 | |
Frederic Weisbecker | d7a4b41 | 2009-09-23 23:08:43 +0200 | [diff] [blame] | 1412 | if (!(tp->flags & TP_FLAG_TRACE)) { |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1413 | if (probe_is_return(tp)) |
| 1414 | disable_kretprobe(&tp->rp); |
| 1415 | else |
| 1416 | disable_kprobe(&tp->rp.kp); |
| 1417 | } |
| 1418 | } |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1419 | #endif /* CONFIG_PERF_EVENTS */ |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1420 | |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1421 | static __kprobes |
| 1422 | int kprobe_register(struct ftrace_event_call *event, enum trace_reg type) |
| 1423 | { |
| 1424 | switch (type) { |
| 1425 | case TRACE_REG_REGISTER: |
| 1426 | return probe_event_enable(event); |
| 1427 | case TRACE_REG_UNREGISTER: |
| 1428 | probe_event_disable(event); |
| 1429 | return 0; |
| 1430 | |
| 1431 | #ifdef CONFIG_PERF_EVENTS |
| 1432 | case TRACE_REG_PERF_REGISTER: |
| 1433 | return probe_perf_enable(event); |
| 1434 | case TRACE_REG_PERF_UNREGISTER: |
| 1435 | probe_perf_disable(event); |
| 1436 | return 0; |
| 1437 | #endif |
| 1438 | } |
| 1439 | return 0; |
| 1440 | } |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1441 | |
| 1442 | static __kprobes |
| 1443 | int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs) |
| 1444 | { |
| 1445 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); |
| 1446 | |
| 1447 | if (tp->flags & TP_FLAG_TRACE) |
| 1448 | kprobe_trace_func(kp, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1449 | #ifdef CONFIG_PERF_EVENTS |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1450 | if (tp->flags & TP_FLAG_PROFILE) |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 1451 | kprobe_perf_func(kp, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1452 | #endif |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1453 | return 0; /* We don't tweek kernel, so just return 0 */ |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1454 | } |
| 1455 | |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1456 | static __kprobes |
| 1457 | int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs) |
| 1458 | { |
| 1459 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); |
| 1460 | |
| 1461 | if (tp->flags & TP_FLAG_TRACE) |
| 1462 | kretprobe_trace_func(ri, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1463 | #ifdef CONFIG_PERF_EVENTS |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1464 | if (tp->flags & TP_FLAG_PROFILE) |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 1465 | kretprobe_perf_func(ri, regs); |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 1466 | #endif |
Masami Hiramatsu | 50d7805 | 2009-09-14 16:49:20 -0400 | [diff] [blame] | 1467 | return 0; /* We don't tweek kernel, so just return 0 */ |
| 1468 | } |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1469 | |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1470 | static struct trace_event_functions kretprobe_funcs = { |
| 1471 | .trace = print_kretprobe_event |
| 1472 | }; |
| 1473 | |
| 1474 | static struct trace_event_functions kprobe_funcs = { |
| 1475 | .trace = print_kprobe_event |
| 1476 | }; |
| 1477 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1478 | static int register_probe_event(struct trace_probe *tp) |
| 1479 | { |
| 1480 | struct ftrace_event_call *call = &tp->call; |
| 1481 | int ret; |
| 1482 | |
| 1483 | /* Initialize ftrace_event_call */ |
Li Zefan | ffb9f99 | 2010-05-24 16:24:52 +0800 | [diff] [blame^] | 1484 | INIT_LIST_HEAD(&call->class->fields); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1485 | if (probe_is_return(tp)) { |
Steven Rostedt | 80decc7 | 2010-04-23 10:00:22 -0400 | [diff] [blame] | 1486 | call->event.funcs = &kretprobe_funcs; |
Steven Rostedt | 2e33af0 | 2010-04-22 10:35:55 -0400 | [diff] [blame] | 1487 | call->class->define_fields = kretprobe_event_define_fields; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1488 | } else { |
Steven Rostedt | 80decc7 | 2010-04-23 10:00:22 -0400 | [diff] [blame] | 1489 | call->event.funcs = &kprobe_funcs; |
Steven Rostedt | 2e33af0 | 2010-04-22 10:35:55 -0400 | [diff] [blame] | 1490 | call->class->define_fields = kprobe_event_define_fields; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1491 | } |
Lai Jiangshan | a342a028 | 2009-12-15 15:39:49 +0800 | [diff] [blame] | 1492 | if (set_print_fmt(tp) < 0) |
| 1493 | return -ENOMEM; |
Steven Rostedt | 32c0eda | 2010-04-23 10:38:03 -0400 | [diff] [blame] | 1494 | ret = register_ftrace_event(&call->event); |
| 1495 | if (!ret) { |
Lai Jiangshan | a342a028 | 2009-12-15 15:39:49 +0800 | [diff] [blame] | 1496 | kfree(call->print_fmt); |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1497 | return -ENODEV; |
Lai Jiangshan | a342a028 | 2009-12-15 15:39:49 +0800 | [diff] [blame] | 1498 | } |
Steven Rostedt | 553552c | 2010-04-23 11:12:36 -0400 | [diff] [blame] | 1499 | call->flags = 0; |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 1500 | call->class->reg = kprobe_register; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1501 | call->data = tp; |
| 1502 | ret = trace_add_event_call(call); |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1503 | if (ret) { |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1504 | pr_info("Failed to register kprobe event: %s\n", call->name); |
Lai Jiangshan | a342a028 | 2009-12-15 15:39:49 +0800 | [diff] [blame] | 1505 | kfree(call->print_fmt); |
Steven Rostedt | 80decc7 | 2010-04-23 10:00:22 -0400 | [diff] [blame] | 1506 | unregister_ftrace_event(&call->event); |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1507 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1508 | return ret; |
| 1509 | } |
| 1510 | |
| 1511 | static void unregister_probe_event(struct trace_probe *tp) |
| 1512 | { |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1513 | /* tp->event is unregistered in trace_remove_event_call() */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1514 | trace_remove_event_call(&tp->call); |
Lai Jiangshan | a342a028 | 2009-12-15 15:39:49 +0800 | [diff] [blame] | 1515 | kfree(tp->call.print_fmt); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /* Make a debugfs interface for controling probe points */ |
| 1519 | static __init int init_kprobe_trace(void) |
| 1520 | { |
| 1521 | struct dentry *d_tracer; |
| 1522 | struct dentry *entry; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1523 | |
| 1524 | d_tracer = tracing_init_dentry(); |
| 1525 | if (!d_tracer) |
| 1526 | return 0; |
| 1527 | |
| 1528 | entry = debugfs_create_file("kprobe_events", 0644, d_tracer, |
| 1529 | NULL, &kprobe_events_ops); |
| 1530 | |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1531 | /* Event list interface */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1532 | if (!entry) |
| 1533 | pr_warning("Could not create debugfs " |
| 1534 | "'kprobe_events' entry\n"); |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1535 | |
| 1536 | /* Profile interface */ |
| 1537 | entry = debugfs_create_file("kprobe_profile", 0444, d_tracer, |
| 1538 | NULL, &kprobe_profile_ops); |
| 1539 | |
| 1540 | if (!entry) |
| 1541 | pr_warning("Could not create debugfs " |
| 1542 | "'kprobe_profile' entry\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1543 | return 0; |
| 1544 | } |
| 1545 | fs_initcall(init_kprobe_trace); |
| 1546 | |
| 1547 | |
| 1548 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
| 1549 | |
| 1550 | static int kprobe_trace_selftest_target(int a1, int a2, int a3, |
| 1551 | int a4, int a5, int a6) |
| 1552 | { |
| 1553 | return a1 + a2 + a3 + a4 + a5 + a6; |
| 1554 | } |
| 1555 | |
| 1556 | static __init int kprobe_trace_self_tests_init(void) |
| 1557 | { |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1558 | int ret, warn = 0; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1559 | int (*target)(int, int, int, int, int, int); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1560 | struct trace_probe *tp; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1561 | |
| 1562 | target = kprobe_trace_selftest_target; |
| 1563 | |
| 1564 | pr_info("Testing kprobe tracing: "); |
| 1565 | |
| 1566 | ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target " |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1567 | "$stack $stack0 +0($stack)"); |
| 1568 | if (WARN_ON_ONCE(ret)) { |
| 1569 | pr_warning("error on probing function entry.\n"); |
| 1570 | warn++; |
| 1571 | } else { |
| 1572 | /* Enable trace point */ |
| 1573 | tp = find_probe_event("testprobe", KPROBE_EVENT_SYSTEM); |
| 1574 | if (WARN_ON_ONCE(tp == NULL)) { |
| 1575 | pr_warning("error on getting new probe.\n"); |
| 1576 | warn++; |
| 1577 | } else |
| 1578 | probe_event_enable(&tp->call); |
| 1579 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1580 | |
| 1581 | ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target " |
Masami Hiramatsu | f397af0 | 2009-10-16 20:07:20 -0400 | [diff] [blame] | 1582 | "$retval"); |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1583 | if (WARN_ON_ONCE(ret)) { |
| 1584 | pr_warning("error on probing function return.\n"); |
| 1585 | warn++; |
| 1586 | } else { |
| 1587 | /* Enable trace point */ |
| 1588 | tp = find_probe_event("testprobe2", KPROBE_EVENT_SYSTEM); |
| 1589 | if (WARN_ON_ONCE(tp == NULL)) { |
| 1590 | pr_warning("error on getting new probe.\n"); |
| 1591 | warn++; |
| 1592 | } else |
| 1593 | probe_event_enable(&tp->call); |
| 1594 | } |
| 1595 | |
| 1596 | if (warn) |
| 1597 | goto end; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1598 | |
| 1599 | ret = target(1, 2, 3, 4, 5, 6); |
| 1600 | |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1601 | ret = command_trace_probe("-:testprobe"); |
| 1602 | if (WARN_ON_ONCE(ret)) { |
| 1603 | pr_warning("error on deleting a probe.\n"); |
| 1604 | warn++; |
| 1605 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1606 | |
Masami Hiramatsu | 231e36f | 2010-01-14 00:12:12 -0500 | [diff] [blame] | 1607 | ret = command_trace_probe("-:testprobe2"); |
| 1608 | if (WARN_ON_ONCE(ret)) { |
| 1609 | pr_warning("error on deleting a probe.\n"); |
| 1610 | warn++; |
| 1611 | } |
| 1612 | |
| 1613 | end: |
| 1614 | cleanup_all_probes(); |
| 1615 | if (warn) |
| 1616 | pr_cont("NG: Some tests are failed. Please check them.\n"); |
| 1617 | else |
| 1618 | pr_cont("OK\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1619 | return 0; |
| 1620 | } |
| 1621 | |
| 1622 | late_initcall(kprobe_trace_self_tests_init); |
| 1623 | |
| 1624 | #endif |