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