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