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