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