Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1 | /* |
| 2 | * kprobe based kernel tracer |
| 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> |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 31 | #include <linux/perf_counter.h> |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 32 | |
| 33 | #include "trace.h" |
| 34 | #include "trace_output.h" |
| 35 | |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 36 | #define MAX_TRACE_ARGS 128 |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 37 | #define MAX_ARGSTR_LEN 63 |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 38 | #define MAX_EVENT_NAME_LEN 64 |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 39 | #define KPROBE_EVENT_SYSTEM "kprobes" |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 40 | |
| 41 | /* currently, trace_kprobe only supports X86. */ |
| 42 | |
| 43 | struct fetch_func { |
| 44 | unsigned long (*func)(struct pt_regs *, void *); |
| 45 | void *data; |
| 46 | }; |
| 47 | |
| 48 | static __kprobes unsigned long call_fetch(struct fetch_func *f, |
| 49 | struct pt_regs *regs) |
| 50 | { |
| 51 | return f->func(regs, f->data); |
| 52 | } |
| 53 | |
| 54 | /* fetch handlers */ |
| 55 | static __kprobes unsigned long fetch_register(struct pt_regs *regs, |
| 56 | void *offset) |
| 57 | { |
| 58 | return regs_get_register(regs, (unsigned int)((unsigned long)offset)); |
| 59 | } |
| 60 | |
| 61 | static __kprobes unsigned long fetch_stack(struct pt_regs *regs, |
| 62 | void *num) |
| 63 | { |
| 64 | return regs_get_kernel_stack_nth(regs, |
| 65 | (unsigned int)((unsigned long)num)); |
| 66 | } |
| 67 | |
| 68 | static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr) |
| 69 | { |
| 70 | unsigned long retval; |
| 71 | |
| 72 | if (probe_kernel_address(addr, retval)) |
| 73 | return 0; |
| 74 | return retval; |
| 75 | } |
| 76 | |
| 77 | static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num) |
| 78 | { |
| 79 | return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num)); |
| 80 | } |
| 81 | |
| 82 | static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs, |
| 83 | void *dummy) |
| 84 | { |
| 85 | return regs_return_value(regs); |
| 86 | } |
| 87 | |
| 88 | static __kprobes unsigned long fetch_ip(struct pt_regs *regs, void *dummy) |
| 89 | { |
| 90 | return instruction_pointer(regs); |
| 91 | } |
| 92 | |
| 93 | static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs, |
| 94 | void *dummy) |
| 95 | { |
| 96 | return kernel_stack_pointer(regs); |
| 97 | } |
| 98 | |
| 99 | /* Memory fetching by symbol */ |
| 100 | struct symbol_cache { |
| 101 | char *symbol; |
| 102 | long offset; |
| 103 | unsigned long addr; |
| 104 | }; |
| 105 | |
| 106 | static unsigned long update_symbol_cache(struct symbol_cache *sc) |
| 107 | { |
| 108 | sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol); |
| 109 | if (sc->addr) |
| 110 | sc->addr += sc->offset; |
| 111 | return sc->addr; |
| 112 | } |
| 113 | |
| 114 | static void free_symbol_cache(struct symbol_cache *sc) |
| 115 | { |
| 116 | kfree(sc->symbol); |
| 117 | kfree(sc); |
| 118 | } |
| 119 | |
| 120 | static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset) |
| 121 | { |
| 122 | struct symbol_cache *sc; |
| 123 | |
| 124 | if (!sym || strlen(sym) == 0) |
| 125 | return NULL; |
| 126 | sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL); |
| 127 | if (!sc) |
| 128 | return NULL; |
| 129 | |
| 130 | sc->symbol = kstrdup(sym, GFP_KERNEL); |
| 131 | if (!sc->symbol) { |
| 132 | kfree(sc); |
| 133 | return NULL; |
| 134 | } |
| 135 | sc->offset = offset; |
| 136 | |
| 137 | update_symbol_cache(sc); |
| 138 | return sc; |
| 139 | } |
| 140 | |
| 141 | static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data) |
| 142 | { |
| 143 | struct symbol_cache *sc = data; |
| 144 | |
| 145 | if (sc->addr) |
| 146 | return fetch_memory(regs, (void *)sc->addr); |
| 147 | else |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* Special indirect memory access interface */ |
| 152 | struct indirect_fetch_data { |
| 153 | struct fetch_func orig; |
| 154 | long offset; |
| 155 | }; |
| 156 | |
| 157 | static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data) |
| 158 | { |
| 159 | struct indirect_fetch_data *ind = data; |
| 160 | unsigned long addr; |
| 161 | |
| 162 | addr = call_fetch(&ind->orig, regs); |
| 163 | if (addr) { |
| 164 | addr += ind->offset; |
| 165 | return fetch_memory(regs, (void *)addr); |
| 166 | } else |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data) |
| 171 | { |
| 172 | if (data->orig.func == fetch_indirect) |
| 173 | free_indirect_fetch_data(data->orig.data); |
| 174 | else if (data->orig.func == fetch_symbol) |
| 175 | free_symbol_cache(data->orig.data); |
| 176 | kfree(data); |
| 177 | } |
| 178 | |
| 179 | /** |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 180 | * Kprobe tracer core functions |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 181 | */ |
| 182 | |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 183 | struct probe_arg { |
| 184 | struct fetch_func fetch; |
| 185 | const char *name; |
| 186 | }; |
| 187 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 188 | struct trace_probe { |
| 189 | struct list_head list; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 190 | struct kretprobe rp; /* Use rp.kp for kprobe use */ |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 191 | unsigned long nhit; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 192 | const char *symbol; /* symbol name */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 193 | struct ftrace_event_call call; |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 194 | struct trace_event event; |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 195 | unsigned int nr_args; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 196 | struct probe_arg args[]; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 197 | }; |
| 198 | |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 199 | #define SIZEOF_TRACE_PROBE(n) \ |
| 200 | (offsetof(struct trace_probe, args) + \ |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 201 | (sizeof(struct probe_arg) * (n))) |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 202 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 203 | static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs); |
| 204 | static int kretprobe_trace_func(struct kretprobe_instance *ri, |
| 205 | struct pt_regs *regs); |
| 206 | |
| 207 | static __kprobes int probe_is_return(struct trace_probe *tp) |
| 208 | { |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 209 | return tp->rp.handler != NULL; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static __kprobes const char *probe_symbol(struct trace_probe *tp) |
| 213 | { |
| 214 | return tp->symbol ? tp->symbol : "unknown"; |
| 215 | } |
| 216 | |
Masami Hiramatsu | 30a7e07 | 2009-08-21 15:43:51 -0400 | [diff] [blame] | 217 | static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 218 | { |
| 219 | int ret = -EINVAL; |
| 220 | |
| 221 | if (ff->func == fetch_argument) |
| 222 | ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data); |
| 223 | else if (ff->func == fetch_register) { |
| 224 | const char *name; |
| 225 | name = regs_query_register_name((unsigned int)((long)ff->data)); |
| 226 | ret = snprintf(buf, n, "%%%s", name); |
| 227 | } else if (ff->func == fetch_stack) |
| 228 | ret = snprintf(buf, n, "s%lu", (unsigned long)ff->data); |
| 229 | else if (ff->func == fetch_memory) |
| 230 | ret = snprintf(buf, n, "@0x%p", ff->data); |
| 231 | else if (ff->func == fetch_symbol) { |
| 232 | struct symbol_cache *sc = ff->data; |
| 233 | ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset); |
| 234 | } else if (ff->func == fetch_retvalue) |
| 235 | ret = snprintf(buf, n, "rv"); |
| 236 | else if (ff->func == fetch_ip) |
| 237 | ret = snprintf(buf, n, "ra"); |
| 238 | else if (ff->func == fetch_stack_address) |
| 239 | ret = snprintf(buf, n, "sa"); |
| 240 | else if (ff->func == fetch_indirect) { |
| 241 | struct indirect_fetch_data *id = ff->data; |
| 242 | size_t l = 0; |
| 243 | ret = snprintf(buf, n, "%+ld(", id->offset); |
| 244 | if (ret >= n) |
| 245 | goto end; |
| 246 | l += ret; |
Masami Hiramatsu | 30a7e07 | 2009-08-21 15:43:51 -0400 | [diff] [blame] | 247 | ret = probe_arg_string(buf + l, n - l, &id->orig); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 248 | if (ret < 0) |
| 249 | goto end; |
| 250 | l += ret; |
| 251 | ret = snprintf(buf + l, n - l, ")"); |
| 252 | ret += l; |
| 253 | } |
| 254 | end: |
| 255 | if (ret >= n) |
| 256 | return -ENOSPC; |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | static int register_probe_event(struct trace_probe *tp); |
| 261 | static void unregister_probe_event(struct trace_probe *tp); |
| 262 | |
| 263 | static DEFINE_MUTEX(probe_lock); |
| 264 | static LIST_HEAD(probe_list); |
| 265 | |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 266 | /* |
| 267 | * Allocate new trace_probe and initialize it (including kprobes). |
| 268 | */ |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 269 | static struct trace_probe *alloc_trace_probe(const char *group, |
| 270 | const char *event, |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 271 | void *addr, |
| 272 | const char *symbol, |
| 273 | unsigned long offs, |
| 274 | int nargs, int is_return) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 275 | { |
| 276 | struct trace_probe *tp; |
| 277 | |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 278 | tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 279 | if (!tp) |
| 280 | return ERR_PTR(-ENOMEM); |
| 281 | |
| 282 | if (symbol) { |
| 283 | tp->symbol = kstrdup(symbol, GFP_KERNEL); |
| 284 | if (!tp->symbol) |
| 285 | goto error; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 286 | tp->rp.kp.symbol_name = tp->symbol; |
| 287 | tp->rp.kp.offset = offs; |
| 288 | } else |
| 289 | tp->rp.kp.addr = addr; |
| 290 | |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 291 | /* Set handler here for checking whether this probe is return or not. */ |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 292 | if (is_return) |
| 293 | tp->rp.handler = kretprobe_trace_func; |
| 294 | else |
| 295 | tp->rp.kp.pre_handler = kprobe_trace_func; |
| 296 | |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 297 | if (!event) |
| 298 | goto error; |
| 299 | tp->call.name = kstrdup(event, GFP_KERNEL); |
| 300 | if (!tp->call.name) |
| 301 | goto error; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 302 | |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 303 | if (!group) |
| 304 | goto error; |
| 305 | tp->call.system = kstrdup(group, GFP_KERNEL); |
| 306 | if (!tp->call.system) |
| 307 | goto error; |
| 308 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 309 | INIT_LIST_HEAD(&tp->list); |
| 310 | return tp; |
| 311 | error: |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 312 | kfree(tp->call.name); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 313 | kfree(tp->symbol); |
| 314 | kfree(tp); |
| 315 | return ERR_PTR(-ENOMEM); |
| 316 | } |
| 317 | |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 318 | static void free_probe_arg(struct probe_arg *arg) |
| 319 | { |
| 320 | if (arg->fetch.func == fetch_symbol) |
| 321 | free_symbol_cache(arg->fetch.data); |
| 322 | else if (arg->fetch.func == fetch_indirect) |
| 323 | free_indirect_fetch_data(arg->fetch.data); |
| 324 | kfree(arg->name); |
| 325 | } |
| 326 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 327 | static void free_trace_probe(struct trace_probe *tp) |
| 328 | { |
| 329 | int i; |
| 330 | |
| 331 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 332 | free_probe_arg(&tp->args[i]); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 333 | |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 334 | kfree(tp->call.system); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 335 | kfree(tp->call.name); |
| 336 | kfree(tp->symbol); |
| 337 | kfree(tp); |
| 338 | } |
| 339 | |
| 340 | static struct trace_probe *find_probe_event(const char *event) |
| 341 | { |
| 342 | struct trace_probe *tp; |
| 343 | |
| 344 | list_for_each_entry(tp, &probe_list, list) |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 345 | if (!strcmp(tp->call.name, event)) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 346 | return tp; |
| 347 | return NULL; |
| 348 | } |
| 349 | |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame^] | 350 | /* Unregister a trace_probe and probe_event: call with locking probe_lock */ |
| 351 | static void unregister_trace_probe(struct trace_probe *tp) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 352 | { |
| 353 | if (probe_is_return(tp)) |
| 354 | unregister_kretprobe(&tp->rp); |
| 355 | else |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 356 | unregister_kprobe(&tp->rp.kp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 357 | list_del(&tp->list); |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame^] | 358 | unregister_probe_event(tp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* Register a trace_probe and probe_event */ |
| 362 | static int register_trace_probe(struct trace_probe *tp) |
| 363 | { |
| 364 | struct trace_probe *old_tp; |
| 365 | int ret; |
| 366 | |
| 367 | mutex_lock(&probe_lock); |
| 368 | |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame^] | 369 | /* register as an event */ |
| 370 | old_tp = find_probe_event(tp->call.name); |
| 371 | if (old_tp) { |
| 372 | /* delete old event */ |
| 373 | unregister_trace_probe(old_tp); |
| 374 | free_trace_probe(old_tp); |
| 375 | } |
| 376 | ret = register_probe_event(tp); |
| 377 | if (ret) { |
| 378 | pr_warning("Faild to register probe event(%d)\n", ret); |
| 379 | goto end; |
| 380 | } |
| 381 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 382 | if (probe_is_return(tp)) |
| 383 | ret = register_kretprobe(&tp->rp); |
| 384 | else |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 385 | ret = register_kprobe(&tp->rp.kp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 386 | |
| 387 | if (ret) { |
| 388 | pr_warning("Could not insert probe(%d)\n", ret); |
| 389 | if (ret == -EILSEQ) { |
| 390 | pr_warning("Probing address(0x%p) is not an " |
| 391 | "instruction boundary.\n", |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 392 | tp->rp.kp.addr); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 393 | ret = -EINVAL; |
| 394 | } |
Masami Hiramatsu | 2d5e067 | 2009-09-14 16:48:56 -0400 | [diff] [blame^] | 395 | unregister_probe_event(tp); |
| 396 | } else |
| 397 | list_add_tail(&tp->list, &probe_list); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 398 | end: |
| 399 | mutex_unlock(&probe_lock); |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | /* Split symbol and offset. */ |
Masami Hiramatsu | 2fba0c8 | 2009-09-10 19:53:14 -0400 | [diff] [blame] | 404 | static int split_symbol_offset(char *symbol, unsigned long *offset) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 405 | { |
| 406 | char *tmp; |
| 407 | int ret; |
| 408 | |
| 409 | if (!offset) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | tmp = strchr(symbol, '+'); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 413 | if (tmp) { |
| 414 | /* skip sign because strict_strtol doesn't accept '+' */ |
Masami Hiramatsu | 2fba0c8 | 2009-09-10 19:53:14 -0400 | [diff] [blame] | 415 | ret = strict_strtoul(tmp + 1, 0, offset); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 416 | if (ret) |
| 417 | return ret; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 418 | *tmp = '\0'; |
| 419 | } else |
| 420 | *offset = 0; |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | #define PARAM_MAX_ARGS 16 |
| 425 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) |
| 426 | |
Masami Hiramatsu | 30a7e07 | 2009-08-21 15:43:51 -0400 | [diff] [blame] | 427 | static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 428 | { |
| 429 | int ret = 0; |
| 430 | unsigned long param; |
| 431 | long offset; |
| 432 | char *tmp; |
| 433 | |
| 434 | switch (arg[0]) { |
| 435 | case 'a': /* argument */ |
| 436 | ret = strict_strtoul(arg + 1, 10, ¶m); |
| 437 | if (ret || param > PARAM_MAX_ARGS) |
| 438 | ret = -EINVAL; |
| 439 | else { |
| 440 | ff->func = fetch_argument; |
| 441 | ff->data = (void *)param; |
| 442 | } |
| 443 | break; |
| 444 | case 'r': /* retval or retaddr */ |
| 445 | if (is_return && arg[1] == 'v') { |
| 446 | ff->func = fetch_retvalue; |
| 447 | ff->data = NULL; |
| 448 | } else if (is_return && arg[1] == 'a') { |
| 449 | ff->func = fetch_ip; |
| 450 | ff->data = NULL; |
| 451 | } else |
| 452 | ret = -EINVAL; |
| 453 | break; |
| 454 | case '%': /* named register */ |
| 455 | ret = regs_query_register_offset(arg + 1); |
| 456 | if (ret >= 0) { |
| 457 | ff->func = fetch_register; |
| 458 | ff->data = (void *)(unsigned long)ret; |
| 459 | ret = 0; |
| 460 | } |
| 461 | break; |
| 462 | case 's': /* stack */ |
| 463 | if (arg[1] == 'a') { |
| 464 | ff->func = fetch_stack_address; |
| 465 | ff->data = NULL; |
| 466 | } else { |
| 467 | ret = strict_strtoul(arg + 1, 10, ¶m); |
| 468 | if (ret || param > PARAM_MAX_STACK) |
| 469 | ret = -EINVAL; |
| 470 | else { |
| 471 | ff->func = fetch_stack; |
| 472 | ff->data = (void *)param; |
| 473 | } |
| 474 | } |
| 475 | break; |
| 476 | case '@': /* memory or symbol */ |
| 477 | if (isdigit(arg[1])) { |
| 478 | ret = strict_strtoul(arg + 1, 0, ¶m); |
| 479 | if (ret) |
| 480 | break; |
| 481 | ff->func = fetch_memory; |
| 482 | ff->data = (void *)param; |
| 483 | } else { |
| 484 | ret = split_symbol_offset(arg + 1, &offset); |
| 485 | if (ret) |
| 486 | break; |
| 487 | ff->data = alloc_symbol_cache(arg + 1, |
| 488 | offset); |
| 489 | if (ff->data) |
| 490 | ff->func = fetch_symbol; |
| 491 | else |
| 492 | ret = -EINVAL; |
| 493 | } |
| 494 | break; |
| 495 | case '+': /* indirect memory */ |
| 496 | case '-': |
| 497 | tmp = strchr(arg, '('); |
| 498 | if (!tmp) { |
| 499 | ret = -EINVAL; |
| 500 | break; |
| 501 | } |
| 502 | *tmp = '\0'; |
| 503 | ret = strict_strtol(arg + 1, 0, &offset); |
| 504 | if (ret) |
| 505 | break; |
| 506 | if (arg[0] == '-') |
| 507 | offset = -offset; |
| 508 | arg = tmp + 1; |
| 509 | tmp = strrchr(arg, ')'); |
| 510 | if (tmp) { |
| 511 | struct indirect_fetch_data *id; |
| 512 | *tmp = '\0'; |
| 513 | id = kzalloc(sizeof(struct indirect_fetch_data), |
| 514 | GFP_KERNEL); |
| 515 | if (!id) |
| 516 | return -ENOMEM; |
| 517 | id->offset = offset; |
Masami Hiramatsu | 30a7e07 | 2009-08-21 15:43:51 -0400 | [diff] [blame] | 518 | ret = parse_probe_arg(arg, &id->orig, is_return); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 519 | if (ret) |
| 520 | kfree(id); |
| 521 | else { |
| 522 | ff->func = fetch_indirect; |
| 523 | ff->data = (void *)id; |
| 524 | } |
| 525 | } else |
| 526 | ret = -EINVAL; |
| 527 | break; |
| 528 | default: |
| 529 | /* TODO: support custom handler */ |
| 530 | ret = -EINVAL; |
| 531 | } |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | static int create_trace_probe(int argc, char **argv) |
| 536 | { |
| 537 | /* |
| 538 | * Argument syntax: |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 539 | * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS] |
| 540 | * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS] |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 541 | * Fetch args: |
| 542 | * aN : fetch Nth of function argument. (N:0-) |
| 543 | * rv : fetch return value |
| 544 | * ra : fetch return address |
| 545 | * sa : fetch stack address |
| 546 | * sN : fetch Nth of stack (N:0-) |
| 547 | * @ADDR : fetch memory at ADDR (ADDR should be in kernel) |
| 548 | * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol) |
| 549 | * %REG : fetch register REG |
| 550 | * Indirect memory fetch: |
| 551 | * +|-offs(ARG) : fetch memory at ARG +|- offs address. |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 552 | * Alias name of args: |
| 553 | * NAME=FETCHARG : set NAME as alias of FETCHARG. |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 554 | */ |
| 555 | struct trace_probe *tp; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 556 | int i, ret = 0; |
| 557 | int is_return = 0; |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 558 | char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL; |
Masami Hiramatsu | 2fba0c8 | 2009-09-10 19:53:14 -0400 | [diff] [blame] | 559 | unsigned long offset = 0; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 560 | void *addr = NULL; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 561 | char buf[MAX_EVENT_NAME_LEN]; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 562 | |
| 563 | if (argc < 2) |
| 564 | return -EINVAL; |
| 565 | |
| 566 | if (argv[0][0] == 'p') |
| 567 | is_return = 0; |
| 568 | else if (argv[0][0] == 'r') |
| 569 | is_return = 1; |
| 570 | else |
| 571 | return -EINVAL; |
| 572 | |
| 573 | if (argv[0][1] == ':') { |
| 574 | event = &argv[0][2]; |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 575 | if (strchr(event, '/')) { |
| 576 | group = event; |
| 577 | event = strchr(group, '/') + 1; |
| 578 | event[-1] = '\0'; |
| 579 | if (strlen(group) == 0) { |
| 580 | pr_info("Group name is not specifiled\n"); |
| 581 | return -EINVAL; |
| 582 | } |
| 583 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 584 | if (strlen(event) == 0) { |
| 585 | pr_info("Event name is not specifiled\n"); |
| 586 | return -EINVAL; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | if (isdigit(argv[1][0])) { |
| 591 | if (is_return) |
| 592 | return -EINVAL; |
| 593 | /* an address specified */ |
| 594 | ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr); |
| 595 | if (ret) |
| 596 | return ret; |
| 597 | } else { |
| 598 | /* a symbol specified */ |
| 599 | symbol = argv[1]; |
| 600 | /* TODO: support .init module functions */ |
| 601 | ret = split_symbol_offset(symbol, &offset); |
| 602 | if (ret) |
| 603 | return ret; |
| 604 | if (offset && is_return) |
| 605 | return -EINVAL; |
| 606 | } |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 607 | argc -= 2; argv += 2; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 608 | |
| 609 | /* setup a probe */ |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 610 | if (!group) |
| 611 | group = KPROBE_EVENT_SYSTEM; |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 612 | if (!event) { |
| 613 | /* Make a new event name */ |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 614 | if (symbol) |
| 615 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld", |
| 616 | is_return ? 'r' : 'p', symbol, offset); |
| 617 | else |
| 618 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p", |
| 619 | is_return ? 'r' : 'p', addr); |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 620 | event = buf; |
| 621 | } |
Masami Hiramatsu | f52487e | 2009-09-10 19:53:53 -0400 | [diff] [blame] | 622 | tp = alloc_trace_probe(group, event, addr, symbol, offset, argc, |
| 623 | is_return); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 624 | if (IS_ERR(tp)) |
| 625 | return PTR_ERR(tp); |
| 626 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 627 | /* parse arguments */ |
Masami Hiramatsu | a82378d | 2009-08-13 16:35:18 -0400 | [diff] [blame] | 628 | ret = 0; |
| 629 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 630 | /* Parse argument name */ |
| 631 | arg = strchr(argv[i], '='); |
| 632 | if (arg) |
| 633 | *arg++ = '\0'; |
| 634 | else |
| 635 | arg = argv[i]; |
| 636 | tp->args[i].name = kstrdup(argv[i], GFP_KERNEL); |
| 637 | |
| 638 | /* Parse fetch argument */ |
| 639 | if (strlen(arg) > MAX_ARGSTR_LEN) { |
| 640 | pr_info("Argument%d(%s) is too long.\n", i, arg); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 641 | ret = -ENOSPC; |
| 642 | goto error; |
| 643 | } |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 644 | ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 645 | if (ret) |
| 646 | goto error; |
| 647 | } |
| 648 | tp->nr_args = i; |
| 649 | |
| 650 | ret = register_trace_probe(tp); |
| 651 | if (ret) |
| 652 | goto error; |
| 653 | return 0; |
| 654 | |
| 655 | error: |
| 656 | free_trace_probe(tp); |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | static void cleanup_all_probes(void) |
| 661 | { |
| 662 | struct trace_probe *tp; |
| 663 | |
| 664 | mutex_lock(&probe_lock); |
| 665 | /* TODO: Use batch unregistration */ |
| 666 | while (!list_empty(&probe_list)) { |
| 667 | tp = list_entry(probe_list.next, struct trace_probe, list); |
| 668 | unregister_trace_probe(tp); |
| 669 | free_trace_probe(tp); |
| 670 | } |
| 671 | mutex_unlock(&probe_lock); |
| 672 | } |
| 673 | |
| 674 | |
| 675 | /* Probes listing interfaces */ |
| 676 | static void *probes_seq_start(struct seq_file *m, loff_t *pos) |
| 677 | { |
| 678 | mutex_lock(&probe_lock); |
| 679 | return seq_list_start(&probe_list, *pos); |
| 680 | } |
| 681 | |
| 682 | static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 683 | { |
| 684 | return seq_list_next(v, &probe_list, pos); |
| 685 | } |
| 686 | |
| 687 | static void probes_seq_stop(struct seq_file *m, void *v) |
| 688 | { |
| 689 | mutex_unlock(&probe_lock); |
| 690 | } |
| 691 | |
| 692 | static int probes_seq_show(struct seq_file *m, void *v) |
| 693 | { |
| 694 | struct trace_probe *tp = v; |
| 695 | int i, ret; |
| 696 | char buf[MAX_ARGSTR_LEN + 1]; |
| 697 | |
| 698 | seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p'); |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 699 | seq_printf(m, ":%s", tp->call.name); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 700 | |
| 701 | if (tp->symbol) |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 702 | seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 703 | else |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 704 | seq_printf(m, " 0x%p", tp->rp.kp.addr); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 705 | |
| 706 | for (i = 0; i < tp->nr_args; i++) { |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 707 | ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 708 | if (ret < 0) { |
| 709 | pr_warning("Argument%d decoding error(%d).\n", i, ret); |
| 710 | return ret; |
| 711 | } |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 712 | seq_printf(m, " %s=%s", tp->args[i].name, buf); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 713 | } |
| 714 | seq_printf(m, "\n"); |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | static const struct seq_operations probes_seq_op = { |
| 719 | .start = probes_seq_start, |
| 720 | .next = probes_seq_next, |
| 721 | .stop = probes_seq_stop, |
| 722 | .show = probes_seq_show |
| 723 | }; |
| 724 | |
| 725 | static int probes_open(struct inode *inode, struct file *file) |
| 726 | { |
| 727 | if ((file->f_mode & FMODE_WRITE) && |
| 728 | (file->f_flags & O_TRUNC)) |
| 729 | cleanup_all_probes(); |
| 730 | |
| 731 | return seq_open(file, &probes_seq_op); |
| 732 | } |
| 733 | |
| 734 | static int command_trace_probe(const char *buf) |
| 735 | { |
| 736 | char **argv; |
| 737 | int argc = 0, ret = 0; |
| 738 | |
| 739 | argv = argv_split(GFP_KERNEL, buf, &argc); |
| 740 | if (!argv) |
| 741 | return -ENOMEM; |
| 742 | |
| 743 | if (argc) |
| 744 | ret = create_trace_probe(argc, argv); |
| 745 | |
| 746 | argv_free(argv); |
| 747 | return ret; |
| 748 | } |
| 749 | |
| 750 | #define WRITE_BUFSIZE 128 |
| 751 | |
| 752 | static ssize_t probes_write(struct file *file, const char __user *buffer, |
| 753 | size_t count, loff_t *ppos) |
| 754 | { |
| 755 | char *kbuf, *tmp; |
| 756 | int ret; |
| 757 | size_t done; |
| 758 | size_t size; |
| 759 | |
| 760 | kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL); |
| 761 | if (!kbuf) |
| 762 | return -ENOMEM; |
| 763 | |
| 764 | ret = done = 0; |
| 765 | while (done < count) { |
| 766 | size = count - done; |
| 767 | if (size >= WRITE_BUFSIZE) |
| 768 | size = WRITE_BUFSIZE - 1; |
| 769 | if (copy_from_user(kbuf, buffer + done, size)) { |
| 770 | ret = -EFAULT; |
| 771 | goto out; |
| 772 | } |
| 773 | kbuf[size] = '\0'; |
| 774 | tmp = strchr(kbuf, '\n'); |
| 775 | if (tmp) { |
| 776 | *tmp = '\0'; |
| 777 | size = tmp - kbuf + 1; |
| 778 | } else if (done + size < count) { |
| 779 | pr_warning("Line length is too long: " |
| 780 | "Should be less than %d.", WRITE_BUFSIZE); |
| 781 | ret = -EINVAL; |
| 782 | goto out; |
| 783 | } |
| 784 | done += size; |
| 785 | /* Remove comments */ |
| 786 | tmp = strchr(kbuf, '#'); |
| 787 | if (tmp) |
| 788 | *tmp = '\0'; |
| 789 | |
| 790 | ret = command_trace_probe(kbuf); |
| 791 | if (ret) |
| 792 | goto out; |
| 793 | } |
| 794 | ret = done; |
| 795 | out: |
| 796 | kfree(kbuf); |
| 797 | return ret; |
| 798 | } |
| 799 | |
| 800 | static const struct file_operations kprobe_events_ops = { |
| 801 | .owner = THIS_MODULE, |
| 802 | .open = probes_open, |
| 803 | .read = seq_read, |
| 804 | .llseek = seq_lseek, |
| 805 | .release = seq_release, |
| 806 | .write = probes_write, |
| 807 | }; |
| 808 | |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 809 | /* Probes profiling interfaces */ |
| 810 | static int probes_profile_seq_show(struct seq_file *m, void *v) |
| 811 | { |
| 812 | struct trace_probe *tp = v; |
| 813 | |
| 814 | seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit, |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 815 | tp->rp.kp.nmissed); |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 816 | |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | static const struct seq_operations profile_seq_op = { |
| 821 | .start = probes_seq_start, |
| 822 | .next = probes_seq_next, |
| 823 | .stop = probes_seq_stop, |
| 824 | .show = probes_profile_seq_show |
| 825 | }; |
| 826 | |
| 827 | static int profile_open(struct inode *inode, struct file *file) |
| 828 | { |
| 829 | return seq_open(file, &profile_seq_op); |
| 830 | } |
| 831 | |
| 832 | static const struct file_operations kprobe_profile_ops = { |
| 833 | .owner = THIS_MODULE, |
| 834 | .open = profile_open, |
| 835 | .read = seq_read, |
| 836 | .llseek = seq_lseek, |
| 837 | .release = seq_release, |
| 838 | }; |
| 839 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 840 | /* Kprobe handler */ |
| 841 | static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs) |
| 842 | { |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 843 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 844 | struct kprobe_trace_entry *entry; |
| 845 | struct ring_buffer_event *event; |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 846 | struct ring_buffer *buffer; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 847 | int size, i, pc; |
| 848 | unsigned long irq_flags; |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 849 | struct ftrace_event_call *call = &tp->call; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 850 | |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 851 | tp->nhit++; |
| 852 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 853 | local_save_flags(irq_flags); |
| 854 | pc = preempt_count(); |
| 855 | |
| 856 | size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args); |
| 857 | |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 858 | event = trace_current_buffer_lock_reserve(&buffer, call->id, size, |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 859 | irq_flags, pc); |
| 860 | if (!event) |
| 861 | return 0; |
| 862 | |
| 863 | entry = ring_buffer_event_data(event); |
| 864 | entry->nargs = tp->nr_args; |
| 865 | entry->ip = (unsigned long)kp->addr; |
| 866 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 867 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 868 | |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 869 | if (!filter_current_check_discard(buffer, call, entry, event)) |
| 870 | trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | /* Kretprobe handler */ |
| 875 | static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri, |
| 876 | struct pt_regs *regs) |
| 877 | { |
| 878 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); |
| 879 | struct kretprobe_trace_entry *entry; |
| 880 | struct ring_buffer_event *event; |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 881 | struct ring_buffer *buffer; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 882 | int size, i, pc; |
| 883 | unsigned long irq_flags; |
Masami Hiramatsu | 4263565 | 2009-08-13 16:35:26 -0400 | [diff] [blame] | 884 | struct ftrace_event_call *call = &tp->call; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 885 | |
| 886 | local_save_flags(irq_flags); |
| 887 | pc = preempt_count(); |
| 888 | |
| 889 | size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args); |
| 890 | |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 891 | event = trace_current_buffer_lock_reserve(&buffer, call->id, size, |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 892 | irq_flags, pc); |
| 893 | if (!event) |
| 894 | return 0; |
| 895 | |
| 896 | entry = ring_buffer_event_data(event); |
| 897 | entry->nargs = tp->nr_args; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 898 | entry->func = (unsigned long)tp->rp.kp.addr; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 899 | entry->ret_ip = (unsigned long)ri->ret_addr; |
| 900 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 901 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 902 | |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 903 | if (!filter_current_check_discard(buffer, call, entry, event)) |
| 904 | trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 905 | |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | /* Event entry printers */ |
| 910 | enum print_line_t |
| 911 | print_kprobe_event(struct trace_iterator *iter, int flags) |
| 912 | { |
| 913 | struct kprobe_trace_entry *field; |
| 914 | struct trace_seq *s = &iter->seq; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 915 | struct trace_event *event; |
| 916 | struct trace_probe *tp; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 917 | int i; |
| 918 | |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 919 | field = (struct kprobe_trace_entry *)iter->ent; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 920 | event = ftrace_find_event(field->ent.type); |
| 921 | tp = container_of(event, struct trace_probe, event); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 922 | |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 923 | if (!trace_seq_printf(s, "%s: (", tp->call.name)) |
| 924 | goto partial; |
| 925 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 926 | if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET)) |
| 927 | goto partial; |
| 928 | |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 929 | if (!trace_seq_puts(s, ")")) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 930 | goto partial; |
| 931 | |
| 932 | for (i = 0; i < field->nargs; i++) |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 933 | if (!trace_seq_printf(s, " %s=%lx", |
| 934 | tp->args[i].name, field->args[i])) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 935 | goto partial; |
| 936 | |
| 937 | if (!trace_seq_puts(s, "\n")) |
| 938 | goto partial; |
| 939 | |
| 940 | return TRACE_TYPE_HANDLED; |
| 941 | partial: |
| 942 | return TRACE_TYPE_PARTIAL_LINE; |
| 943 | } |
| 944 | |
| 945 | enum print_line_t |
| 946 | print_kretprobe_event(struct trace_iterator *iter, int flags) |
| 947 | { |
| 948 | struct kretprobe_trace_entry *field; |
| 949 | struct trace_seq *s = &iter->seq; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 950 | struct trace_event *event; |
| 951 | struct trace_probe *tp; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 952 | int i; |
| 953 | |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 954 | field = (struct kretprobe_trace_entry *)iter->ent; |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 955 | event = ftrace_find_event(field->ent.type); |
| 956 | tp = container_of(event, struct trace_probe, event); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 957 | |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 958 | if (!trace_seq_printf(s, "%s: (", tp->call.name)) |
| 959 | goto partial; |
| 960 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 961 | if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET)) |
| 962 | goto partial; |
| 963 | |
| 964 | if (!trace_seq_puts(s, " <- ")) |
| 965 | goto partial; |
| 966 | |
| 967 | if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET)) |
| 968 | goto partial; |
| 969 | |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 970 | if (!trace_seq_puts(s, ")")) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 971 | goto partial; |
| 972 | |
| 973 | for (i = 0; i < field->nargs; i++) |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 974 | if (!trace_seq_printf(s, " %s=%lx", |
| 975 | tp->args[i].name, field->args[i])) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 976 | goto partial; |
| 977 | |
| 978 | if (!trace_seq_puts(s, "\n")) |
| 979 | goto partial; |
| 980 | |
| 981 | return TRACE_TYPE_HANDLED; |
| 982 | partial: |
| 983 | return TRACE_TYPE_PARTIAL_LINE; |
| 984 | } |
| 985 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 986 | static int probe_event_enable(struct ftrace_event_call *call) |
| 987 | { |
| 988 | struct trace_probe *tp = (struct trace_probe *)call->data; |
| 989 | |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 990 | if (probe_is_return(tp)) { |
| 991 | tp->rp.handler = kretprobe_trace_func; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 992 | return enable_kretprobe(&tp->rp); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 993 | } else { |
| 994 | tp->rp.kp.pre_handler = kprobe_trace_func; |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 995 | return enable_kprobe(&tp->rp.kp); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 996 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | static void probe_event_disable(struct ftrace_event_call *call) |
| 1000 | { |
| 1001 | struct trace_probe *tp = (struct trace_probe *)call->data; |
| 1002 | |
| 1003 | if (probe_is_return(tp)) |
| 1004 | disable_kretprobe(&tp->rp); |
| 1005 | else |
Masami Hiramatsu | 4a846b4 | 2009-09-11 05:31:21 +0200 | [diff] [blame] | 1006 | disable_kprobe(&tp->rp.kp); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static int probe_event_raw_init(struct ftrace_event_call *event_call) |
| 1010 | { |
| 1011 | INIT_LIST_HEAD(&event_call->fields); |
Frederic Weisbecker | 8f8ffe2 | 2009-09-11 01:09:23 +0200 | [diff] [blame] | 1012 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | #undef DEFINE_FIELD |
| 1017 | #define DEFINE_FIELD(type, item, name, is_signed) \ |
| 1018 | do { \ |
| 1019 | ret = trace_define_field(event_call, #type, name, \ |
| 1020 | offsetof(typeof(field), item), \ |
| 1021 | sizeof(field.item), is_signed, \ |
| 1022 | FILTER_OTHER); \ |
| 1023 | if (ret) \ |
| 1024 | return ret; \ |
| 1025 | } while (0) |
| 1026 | |
| 1027 | static int kprobe_event_define_fields(struct ftrace_event_call *event_call) |
| 1028 | { |
| 1029 | int ret, i; |
| 1030 | struct kprobe_trace_entry field; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1031 | struct trace_probe *tp = (struct trace_probe *)event_call->data; |
| 1032 | |
| 1033 | ret = trace_define_common_fields(event_call); |
| 1034 | if (!ret) |
| 1035 | return ret; |
| 1036 | |
| 1037 | DEFINE_FIELD(unsigned long, ip, "ip", 0); |
| 1038 | DEFINE_FIELD(int, nargs, "nargs", 1); |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1039 | /* Set argument names as fields */ |
| 1040 | for (i = 0; i < tp->nr_args; i++) |
| 1041 | DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | static int kretprobe_event_define_fields(struct ftrace_event_call *event_call) |
| 1046 | { |
| 1047 | int ret, i; |
| 1048 | struct kretprobe_trace_entry field; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1049 | struct trace_probe *tp = (struct trace_probe *)event_call->data; |
| 1050 | |
| 1051 | ret = trace_define_common_fields(event_call); |
| 1052 | if (!ret) |
| 1053 | return ret; |
| 1054 | |
| 1055 | DEFINE_FIELD(unsigned long, func, "func", 0); |
| 1056 | DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0); |
| 1057 | DEFINE_FIELD(int, nargs, "nargs", 1); |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1058 | /* Set argument names as fields */ |
| 1059 | for (i = 0; i < tp->nr_args; i++) |
| 1060 | DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | static int __probe_event_show_format(struct trace_seq *s, |
| 1065 | struct trace_probe *tp, const char *fmt, |
| 1066 | const char *arg) |
| 1067 | { |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1068 | int i; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1069 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1070 | /* Show format */ |
| 1071 | if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt)) |
| 1072 | return 0; |
| 1073 | |
| 1074 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1075 | if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name)) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1076 | return 0; |
| 1077 | |
| 1078 | if (!trace_seq_printf(s, "\", %s", arg)) |
| 1079 | return 0; |
| 1080 | |
| 1081 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1082 | if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name)) |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1083 | return 0; |
| 1084 | |
| 1085 | return trace_seq_puts(s, "\n"); |
| 1086 | } |
| 1087 | |
| 1088 | #undef SHOW_FIELD |
| 1089 | #define SHOW_FIELD(type, item, name) \ |
| 1090 | do { \ |
| 1091 | ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \ |
Masami Hiramatsu | 38a4749 | 2009-08-21 15:43:43 -0400 | [diff] [blame] | 1092 | "offset:%u;\tsize:%u;\n", name, \ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1093 | (unsigned int)offsetof(typeof(field), item),\ |
| 1094 | (unsigned int)sizeof(type)); \ |
| 1095 | if (!ret) \ |
| 1096 | return 0; \ |
| 1097 | } while (0) |
| 1098 | |
| 1099 | static int kprobe_event_show_format(struct ftrace_event_call *call, |
| 1100 | struct trace_seq *s) |
| 1101 | { |
| 1102 | struct kprobe_trace_entry field __attribute__((unused)); |
| 1103 | int ret, i; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1104 | struct trace_probe *tp = (struct trace_probe *)call->data; |
| 1105 | |
| 1106 | SHOW_FIELD(unsigned long, ip, "ip"); |
| 1107 | SHOW_FIELD(int, nargs, "nargs"); |
| 1108 | |
| 1109 | /* Show fields */ |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1110 | for (i = 0; i < tp->nr_args; i++) |
| 1111 | SHOW_FIELD(unsigned long, args[i], tp->args[i].name); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1112 | trace_seq_puts(s, "\n"); |
| 1113 | |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 1114 | return __probe_event_show_format(s, tp, "(%lx)", "REC->ip"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | static int kretprobe_event_show_format(struct ftrace_event_call *call, |
| 1118 | struct trace_seq *s) |
| 1119 | { |
| 1120 | struct kretprobe_trace_entry field __attribute__((unused)); |
| 1121 | int ret, i; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1122 | struct trace_probe *tp = (struct trace_probe *)call->data; |
| 1123 | |
| 1124 | SHOW_FIELD(unsigned long, func, "func"); |
| 1125 | SHOW_FIELD(unsigned long, ret_ip, "ret_ip"); |
| 1126 | SHOW_FIELD(int, nargs, "nargs"); |
| 1127 | |
| 1128 | /* Show fields */ |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1129 | for (i = 0; i < tp->nr_args; i++) |
| 1130 | SHOW_FIELD(unsigned long, args[i], tp->args[i].name); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1131 | trace_seq_puts(s, "\n"); |
| 1132 | |
Masami Hiramatsu | 6e9f23d | 2009-09-10 19:53:45 -0400 | [diff] [blame] | 1133 | return __probe_event_show_format(s, tp, "(%lx <- %lx)", |
| 1134 | "REC->func, REC->ret_ip"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1135 | } |
| 1136 | |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1137 | #ifdef CONFIG_EVENT_PROFILE |
| 1138 | |
| 1139 | /* Kprobe profile handler */ |
| 1140 | static __kprobes int kprobe_profile_func(struct kprobe *kp, |
| 1141 | struct pt_regs *regs) |
| 1142 | { |
| 1143 | struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp); |
| 1144 | struct ftrace_event_call *call = &tp->call; |
| 1145 | struct kprobe_trace_entry *entry; |
| 1146 | int size, i, pc; |
| 1147 | unsigned long irq_flags; |
| 1148 | |
| 1149 | local_save_flags(irq_flags); |
| 1150 | pc = preempt_count(); |
| 1151 | |
| 1152 | size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args); |
| 1153 | |
| 1154 | do { |
| 1155 | char raw_data[size]; |
| 1156 | struct trace_entry *ent; |
| 1157 | |
| 1158 | *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL; |
| 1159 | entry = (struct kprobe_trace_entry *)raw_data; |
| 1160 | ent = &entry->ent; |
| 1161 | |
| 1162 | tracing_generic_entry_update(ent, irq_flags, pc); |
| 1163 | ent->type = call->id; |
| 1164 | entry->nargs = tp->nr_args; |
| 1165 | entry->ip = (unsigned long)kp->addr; |
| 1166 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1167 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1168 | perf_tpcounter_event(call->id, entry->ip, 1, entry, size); |
| 1169 | } while (0); |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
| 1173 | /* Kretprobe profile handler */ |
| 1174 | static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri, |
| 1175 | struct pt_regs *regs) |
| 1176 | { |
| 1177 | struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp); |
| 1178 | struct ftrace_event_call *call = &tp->call; |
| 1179 | struct kretprobe_trace_entry *entry; |
| 1180 | int size, i, pc; |
| 1181 | unsigned long irq_flags; |
| 1182 | |
| 1183 | local_save_flags(irq_flags); |
| 1184 | pc = preempt_count(); |
| 1185 | |
| 1186 | size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args); |
| 1187 | |
| 1188 | do { |
| 1189 | char raw_data[size]; |
| 1190 | struct trace_entry *ent; |
| 1191 | |
| 1192 | *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL; |
| 1193 | entry = (struct kretprobe_trace_entry *)raw_data; |
| 1194 | ent = &entry->ent; |
| 1195 | |
| 1196 | tracing_generic_entry_update(ent, irq_flags, pc); |
| 1197 | ent->type = call->id; |
| 1198 | entry->nargs = tp->nr_args; |
| 1199 | entry->func = (unsigned long)tp->rp.kp.addr; |
| 1200 | entry->ret_ip = (unsigned long)ri->ret_addr; |
| 1201 | for (i = 0; i < tp->nr_args; i++) |
Masami Hiramatsu | eca0d91 | 2009-09-10 19:53:38 -0400 | [diff] [blame] | 1202 | entry->args[i] = call_fetch(&tp->args[i].fetch, regs); |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1203 | perf_tpcounter_event(call->id, entry->ret_ip, 1, entry, size); |
| 1204 | } while (0); |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | static int probe_profile_enable(struct ftrace_event_call *call) |
| 1209 | { |
| 1210 | struct trace_probe *tp = (struct trace_probe *)call->data; |
| 1211 | |
| 1212 | if (atomic_inc_return(&call->profile_count)) |
| 1213 | return 0; |
| 1214 | |
| 1215 | if (probe_is_return(tp)) { |
| 1216 | tp->rp.handler = kretprobe_profile_func; |
| 1217 | return enable_kretprobe(&tp->rp); |
| 1218 | } else { |
| 1219 | tp->rp.kp.pre_handler = kprobe_profile_func; |
| 1220 | return enable_kprobe(&tp->rp.kp); |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | static void probe_profile_disable(struct ftrace_event_call *call) |
| 1225 | { |
| 1226 | if (atomic_add_negative(-1, &call->profile_count)) |
| 1227 | probe_event_disable(call); |
| 1228 | } |
| 1229 | |
| 1230 | #endif /* CONFIG_EVENT_PROFILE */ |
| 1231 | |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1232 | static int register_probe_event(struct trace_probe *tp) |
| 1233 | { |
| 1234 | struct ftrace_event_call *call = &tp->call; |
| 1235 | int ret; |
| 1236 | |
| 1237 | /* Initialize ftrace_event_call */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1238 | if (probe_is_return(tp)) { |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1239 | tp->event.trace = print_kretprobe_event; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1240 | call->raw_init = probe_event_raw_init; |
| 1241 | call->show_format = kretprobe_event_show_format; |
| 1242 | call->define_fields = kretprobe_event_define_fields; |
| 1243 | } else { |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1244 | tp->event.trace = print_kprobe_event; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1245 | call->raw_init = probe_event_raw_init; |
| 1246 | call->show_format = kprobe_event_show_format; |
| 1247 | call->define_fields = kprobe_event_define_fields; |
| 1248 | } |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1249 | call->event = &tp->event; |
| 1250 | call->id = register_ftrace_event(&tp->event); |
| 1251 | if (!call->id) |
| 1252 | return -ENODEV; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1253 | call->enabled = 1; |
| 1254 | call->regfunc = probe_event_enable; |
| 1255 | call->unregfunc = probe_event_disable; |
Masami Hiramatsu | e08d1c6 | 2009-09-10 19:53:30 -0400 | [diff] [blame] | 1256 | |
| 1257 | #ifdef CONFIG_EVENT_PROFILE |
| 1258 | atomic_set(&call->profile_count, -1); |
| 1259 | call->profile_enable = probe_profile_enable; |
| 1260 | call->profile_disable = probe_profile_disable; |
| 1261 | #endif |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1262 | call->data = tp; |
| 1263 | ret = trace_add_event_call(call); |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1264 | if (ret) { |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1265 | pr_info("Failed to register kprobe event: %s\n", call->name); |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1266 | unregister_ftrace_event(&tp->event); |
| 1267 | } |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1268 | return ret; |
| 1269 | } |
| 1270 | |
| 1271 | static void unregister_probe_event(struct trace_probe *tp) |
| 1272 | { |
Masami Hiramatsu | ff50d99 | 2009-08-13 16:35:34 -0400 | [diff] [blame] | 1273 | /* tp->event is unregistered in trace_remove_event_call() */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1274 | trace_remove_event_call(&tp->call); |
| 1275 | } |
| 1276 | |
| 1277 | /* Make a debugfs interface for controling probe points */ |
| 1278 | static __init int init_kprobe_trace(void) |
| 1279 | { |
| 1280 | struct dentry *d_tracer; |
| 1281 | struct dentry *entry; |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1282 | |
| 1283 | d_tracer = tracing_init_dentry(); |
| 1284 | if (!d_tracer) |
| 1285 | return 0; |
| 1286 | |
| 1287 | entry = debugfs_create_file("kprobe_events", 0644, d_tracer, |
| 1288 | NULL, &kprobe_events_ops); |
| 1289 | |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1290 | /* Event list interface */ |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1291 | if (!entry) |
| 1292 | pr_warning("Could not create debugfs " |
| 1293 | "'kprobe_events' entry\n"); |
Masami Hiramatsu | cd7e7bd | 2009-08-13 16:35:42 -0400 | [diff] [blame] | 1294 | |
| 1295 | /* Profile interface */ |
| 1296 | entry = debugfs_create_file("kprobe_profile", 0444, d_tracer, |
| 1297 | NULL, &kprobe_profile_ops); |
| 1298 | |
| 1299 | if (!entry) |
| 1300 | pr_warning("Could not create debugfs " |
| 1301 | "'kprobe_profile' entry\n"); |
Masami Hiramatsu | 413d37d | 2009-08-13 16:35:11 -0400 | [diff] [blame] | 1302 | return 0; |
| 1303 | } |
| 1304 | fs_initcall(init_kprobe_trace); |
| 1305 | |
| 1306 | |
| 1307 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
| 1308 | |
| 1309 | static int kprobe_trace_selftest_target(int a1, int a2, int a3, |
| 1310 | int a4, int a5, int a6) |
| 1311 | { |
| 1312 | return a1 + a2 + a3 + a4 + a5 + a6; |
| 1313 | } |
| 1314 | |
| 1315 | static __init int kprobe_trace_self_tests_init(void) |
| 1316 | { |
| 1317 | int ret; |
| 1318 | int (*target)(int, int, int, int, int, int); |
| 1319 | |
| 1320 | target = kprobe_trace_selftest_target; |
| 1321 | |
| 1322 | pr_info("Testing kprobe tracing: "); |
| 1323 | |
| 1324 | ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target " |
| 1325 | "a1 a2 a3 a4 a5 a6"); |
| 1326 | if (WARN_ON_ONCE(ret)) |
| 1327 | pr_warning("error enabling function entry\n"); |
| 1328 | |
| 1329 | ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target " |
| 1330 | "ra rv"); |
| 1331 | if (WARN_ON_ONCE(ret)) |
| 1332 | pr_warning("error enabling function return\n"); |
| 1333 | |
| 1334 | ret = target(1, 2, 3, 4, 5, 6); |
| 1335 | |
| 1336 | cleanup_all_probes(); |
| 1337 | |
| 1338 | pr_cont("OK\n"); |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | late_initcall(kprobe_trace_self_tests_init); |
| 1343 | |
| 1344 | #endif |