Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1 | /* |
| 2 | * uprobes-based tracing events |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | * |
| 17 | * Copyright (C) IBM Corporation, 2010-2012 |
| 18 | * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com> |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/uaccess.h> |
| 23 | #include <linux/uprobes.h> |
| 24 | #include <linux/namei.h> |
Andy Shevchenko | b2e902f | 2012-12-17 16:01:27 -0800 | [diff] [blame] | 25 | #include <linux/string.h> |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 26 | |
| 27 | #include "trace_probe.h" |
| 28 | |
| 29 | #define UPROBE_EVENT_SYSTEM "uprobes" |
| 30 | |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 31 | struct uprobe_trace_entry_head { |
| 32 | struct trace_entry ent; |
| 33 | unsigned long vaddr[]; |
| 34 | }; |
| 35 | |
| 36 | #define SIZEOF_TRACE_ENTRY(is_return) \ |
| 37 | (sizeof(struct uprobe_trace_entry_head) + \ |
| 38 | sizeof(unsigned long) * (is_return ? 2 : 1)) |
| 39 | |
| 40 | #define DATAOF_TRACE_ENTRY(entry, is_return) \ |
| 41 | ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return)) |
| 42 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 43 | struct trace_uprobe_filter { |
| 44 | rwlock_t rwlock; |
| 45 | int nr_systemwide; |
| 46 | struct list_head perf_events; |
| 47 | }; |
| 48 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 49 | /* |
| 50 | * uprobe event core functions |
| 51 | */ |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 52 | struct trace_uprobe { |
| 53 | struct list_head list; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 54 | struct trace_uprobe_filter filter; |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 55 | struct uprobe_consumer consumer; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 56 | struct inode *inode; |
| 57 | char *filename; |
| 58 | unsigned long offset; |
| 59 | unsigned long nhit; |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 60 | struct trace_probe tp; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 61 | }; |
| 62 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 63 | #define SIZEOF_TRACE_UPROBE(n) \ |
| 64 | (offsetof(struct trace_uprobe, tp.args) + \ |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 65 | (sizeof(struct probe_arg) * (n))) |
| 66 | |
| 67 | static int register_uprobe_event(struct trace_uprobe *tu); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 68 | static int unregister_uprobe_event(struct trace_uprobe *tu); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 69 | |
| 70 | static DEFINE_MUTEX(uprobe_lock); |
| 71 | static LIST_HEAD(uprobe_list); |
| 72 | |
| 73 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs); |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 74 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
| 75 | unsigned long func, struct pt_regs *regs); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 76 | |
Namhyung Kim | 3fd996a | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 77 | #ifdef CONFIG_STACK_GROWSUP |
| 78 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) |
| 79 | { |
| 80 | return addr - (n * sizeof(long)); |
| 81 | } |
| 82 | #else |
| 83 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) |
| 84 | { |
| 85 | return addr + (n * sizeof(long)); |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n) |
| 90 | { |
| 91 | unsigned long ret; |
| 92 | unsigned long addr = user_stack_pointer(regs); |
| 93 | |
| 94 | addr = adjust_stack_addr(addr, n); |
| 95 | |
| 96 | if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret))) |
| 97 | return 0; |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Uprobes-specific fetch functions |
| 104 | */ |
| 105 | #define DEFINE_FETCH_stack(type) \ |
| 106 | static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\ |
| 107 | void *offset, void *dest) \ |
| 108 | { \ |
| 109 | *(type *)dest = (type)get_user_stack_nth(regs, \ |
| 110 | ((unsigned long)offset)); \ |
| 111 | } |
| 112 | DEFINE_BASIC_FETCH_FUNCS(stack) |
| 113 | /* No string on the stack entry */ |
| 114 | #define fetch_stack_string NULL |
| 115 | #define fetch_stack_string_size NULL |
| 116 | |
Namhyung Kim | 5baaa59 | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 117 | #define DEFINE_FETCH_memory(type) \ |
| 118 | static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\ |
| 119 | void *addr, void *dest) \ |
| 120 | { \ |
| 121 | type retval; \ |
| 122 | void __user *vaddr = (void __force __user *) addr; \ |
| 123 | \ |
| 124 | if (copy_from_user(&retval, vaddr, sizeof(type))) \ |
| 125 | *(type *)dest = 0; \ |
| 126 | else \ |
| 127 | *(type *) dest = retval; \ |
| 128 | } |
| 129 | DEFINE_BASIC_FETCH_FUNCS(memory) |
| 130 | /* |
| 131 | * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max |
| 132 | * length and relative data location. |
| 133 | */ |
| 134 | static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs, |
| 135 | void *addr, void *dest) |
| 136 | { |
| 137 | long ret; |
| 138 | u32 rloc = *(u32 *)dest; |
| 139 | int maxlen = get_rloc_len(rloc); |
| 140 | u8 *dst = get_rloc_data(dest); |
| 141 | void __user *src = (void __force __user *) addr; |
| 142 | |
| 143 | if (!maxlen) |
| 144 | return; |
| 145 | |
| 146 | ret = strncpy_from_user(dst, src, maxlen); |
| 147 | |
| 148 | if (ret < 0) { /* Failed to fetch string */ |
| 149 | ((u8 *)get_rloc_data(dest))[0] = '\0'; |
| 150 | *(u32 *)dest = make_data_rloc(0, get_rloc_offs(rloc)); |
| 151 | } else { |
| 152 | *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(rloc)); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs, |
| 157 | void *addr, void *dest) |
| 158 | { |
| 159 | int len; |
| 160 | void __user *vaddr = (void __force __user *) addr; |
| 161 | |
| 162 | len = strnlen_user(vaddr, MAX_STRING_SIZE); |
| 163 | |
| 164 | if (len == 0 || len > MAX_STRING_SIZE) /* Failed to check length */ |
| 165 | *(u32 *)dest = 0; |
| 166 | else |
| 167 | *(u32 *)dest = len; |
| 168 | } |
Namhyung Kim | 3fd996a | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 169 | |
Namhyung Kim | 1301a44 | 2013-11-26 15:21:04 +0900 | [diff] [blame] | 170 | /* uprobes do not support symbol fetch methods */ |
| 171 | #define fetch_symbol_u8 NULL |
| 172 | #define fetch_symbol_u16 NULL |
| 173 | #define fetch_symbol_u32 NULL |
| 174 | #define fetch_symbol_u64 NULL |
| 175 | #define fetch_symbol_string NULL |
| 176 | #define fetch_symbol_string_size NULL |
| 177 | |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 178 | /* Fetch type information table */ |
| 179 | const struct fetch_type uprobes_fetch_type_table[] = { |
| 180 | /* Special types */ |
| 181 | [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string, |
| 182 | sizeof(u32), 1, "__data_loc char[]"), |
| 183 | [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32, |
| 184 | string_size, sizeof(u32), 0, "u32"), |
| 185 | /* Basic types */ |
| 186 | ASSIGN_FETCH_TYPE(u8, u8, 0), |
| 187 | ASSIGN_FETCH_TYPE(u16, u16, 0), |
| 188 | ASSIGN_FETCH_TYPE(u32, u32, 0), |
| 189 | ASSIGN_FETCH_TYPE(u64, u64, 0), |
| 190 | ASSIGN_FETCH_TYPE(s8, u8, 1), |
| 191 | ASSIGN_FETCH_TYPE(s16, u16, 1), |
| 192 | ASSIGN_FETCH_TYPE(s32, u32, 1), |
| 193 | ASSIGN_FETCH_TYPE(s64, u64, 1), |
| 194 | |
| 195 | ASSIGN_FETCH_TYPE_END |
| 196 | }; |
| 197 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 198 | static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter) |
| 199 | { |
| 200 | rwlock_init(&filter->rwlock); |
| 201 | filter->nr_systemwide = 0; |
| 202 | INIT_LIST_HEAD(&filter->perf_events); |
| 203 | } |
| 204 | |
| 205 | static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter) |
| 206 | { |
| 207 | return !filter->nr_systemwide && list_empty(&filter->perf_events); |
| 208 | } |
| 209 | |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 210 | static inline bool is_ret_probe(struct trace_uprobe *tu) |
| 211 | { |
| 212 | return tu->consumer.ret_handler != NULL; |
| 213 | } |
| 214 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 215 | /* |
| 216 | * Allocate new trace_uprobe and initialize it (including uprobes). |
| 217 | */ |
| 218 | static struct trace_uprobe * |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 219 | alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 220 | { |
| 221 | struct trace_uprobe *tu; |
| 222 | |
| 223 | if (!event || !is_good_name(event)) |
| 224 | return ERR_PTR(-EINVAL); |
| 225 | |
| 226 | if (!group || !is_good_name(group)) |
| 227 | return ERR_PTR(-EINVAL); |
| 228 | |
| 229 | tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL); |
| 230 | if (!tu) |
| 231 | return ERR_PTR(-ENOMEM); |
| 232 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 233 | tu->tp.call.class = &tu->tp.class; |
| 234 | tu->tp.call.name = kstrdup(event, GFP_KERNEL); |
| 235 | if (!tu->tp.call.name) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 236 | goto error; |
| 237 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 238 | tu->tp.class.system = kstrdup(group, GFP_KERNEL); |
| 239 | if (!tu->tp.class.system) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 240 | goto error; |
| 241 | |
| 242 | INIT_LIST_HEAD(&tu->list); |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 243 | tu->consumer.handler = uprobe_dispatcher; |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 244 | if (is_ret) |
| 245 | tu->consumer.ret_handler = uretprobe_dispatcher; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 246 | init_trace_uprobe_filter(&tu->filter); |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 247 | tu->tp.call.flags |= TRACE_EVENT_FL_USE_CALL_FILTER; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 248 | return tu; |
| 249 | |
| 250 | error: |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 251 | kfree(tu->tp.call.name); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 252 | kfree(tu); |
| 253 | |
| 254 | return ERR_PTR(-ENOMEM); |
| 255 | } |
| 256 | |
| 257 | static void free_trace_uprobe(struct trace_uprobe *tu) |
| 258 | { |
| 259 | int i; |
| 260 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 261 | for (i = 0; i < tu->tp.nr_args; i++) |
| 262 | traceprobe_free_probe_arg(&tu->tp.args[i]); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 263 | |
| 264 | iput(tu->inode); |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 265 | kfree(tu->tp.call.class->system); |
| 266 | kfree(tu->tp.call.name); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 267 | kfree(tu->filename); |
| 268 | kfree(tu); |
| 269 | } |
| 270 | |
| 271 | static struct trace_uprobe *find_probe_event(const char *event, const char *group) |
| 272 | { |
| 273 | struct trace_uprobe *tu; |
| 274 | |
| 275 | list_for_each_entry(tu, &uprobe_list, list) |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 276 | if (strcmp(tu->tp.call.name, event) == 0 && |
| 277 | strcmp(tu->tp.call.class->system, group) == 0) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 278 | return tu; |
| 279 | |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */ |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 284 | static int unregister_trace_uprobe(struct trace_uprobe *tu) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 285 | { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 286 | int ret; |
| 287 | |
| 288 | ret = unregister_uprobe_event(tu); |
| 289 | if (ret) |
| 290 | return ret; |
| 291 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 292 | list_del(&tu->list); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 293 | free_trace_uprobe(tu); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 294 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* Register a trace_uprobe and probe_event */ |
| 298 | static int register_trace_uprobe(struct trace_uprobe *tu) |
| 299 | { |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 300 | struct trace_uprobe *old_tu; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 301 | int ret; |
| 302 | |
| 303 | mutex_lock(&uprobe_lock); |
| 304 | |
| 305 | /* register as an event */ |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 306 | old_tu = find_probe_event(tu->tp.call.name, tu->tp.call.class->system); |
| 307 | if (old_tu) { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 308 | /* delete old event */ |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 309 | ret = unregister_trace_uprobe(old_tu); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 310 | if (ret) |
| 311 | goto end; |
| 312 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 313 | |
| 314 | ret = register_uprobe_event(tu); |
| 315 | if (ret) { |
| 316 | pr_warning("Failed to register probe event(%d)\n", ret); |
| 317 | goto end; |
| 318 | } |
| 319 | |
| 320 | list_add_tail(&tu->list, &uprobe_list); |
| 321 | |
| 322 | end: |
| 323 | mutex_unlock(&uprobe_lock); |
| 324 | |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * Argument syntax: |
Namhyung Kim | 306cfe2 | 2013-07-03 16:44:46 +0900 | [diff] [blame] | 330 | * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS] |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 331 | * |
| 332 | * - Remove uprobe: -:[GRP/]EVENT |
| 333 | */ |
| 334 | static int create_trace_uprobe(int argc, char **argv) |
| 335 | { |
| 336 | struct trace_uprobe *tu; |
| 337 | struct inode *inode; |
| 338 | char *arg, *event, *group, *filename; |
| 339 | char buf[MAX_EVENT_NAME_LEN]; |
| 340 | struct path path; |
| 341 | unsigned long offset; |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 342 | bool is_delete, is_return; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 343 | int i, ret; |
| 344 | |
| 345 | inode = NULL; |
| 346 | ret = 0; |
| 347 | is_delete = false; |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 348 | is_return = false; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 349 | event = NULL; |
| 350 | group = NULL; |
| 351 | |
| 352 | /* argc must be >= 1 */ |
| 353 | if (argv[0][0] == '-') |
| 354 | is_delete = true; |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 355 | else if (argv[0][0] == 'r') |
| 356 | is_return = true; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 357 | else if (argv[0][0] != 'p') { |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 358 | pr_info("Probe definition must be started with 'p', 'r' or '-'.\n"); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 359 | return -EINVAL; |
| 360 | } |
| 361 | |
| 362 | if (argv[0][1] == ':') { |
| 363 | event = &argv[0][2]; |
| 364 | arg = strchr(event, '/'); |
| 365 | |
| 366 | if (arg) { |
| 367 | group = event; |
| 368 | event = arg + 1; |
| 369 | event[-1] = '\0'; |
| 370 | |
| 371 | if (strlen(group) == 0) { |
| 372 | pr_info("Group name is not specified\n"); |
| 373 | return -EINVAL; |
| 374 | } |
| 375 | } |
| 376 | if (strlen(event) == 0) { |
| 377 | pr_info("Event name is not specified\n"); |
| 378 | return -EINVAL; |
| 379 | } |
| 380 | } |
| 381 | if (!group) |
| 382 | group = UPROBE_EVENT_SYSTEM; |
| 383 | |
| 384 | if (is_delete) { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 385 | int ret; |
| 386 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 387 | if (!event) { |
| 388 | pr_info("Delete command needs an event name.\n"); |
| 389 | return -EINVAL; |
| 390 | } |
| 391 | mutex_lock(&uprobe_lock); |
| 392 | tu = find_probe_event(event, group); |
| 393 | |
| 394 | if (!tu) { |
| 395 | mutex_unlock(&uprobe_lock); |
| 396 | pr_info("Event %s/%s doesn't exist.\n", group, event); |
| 397 | return -ENOENT; |
| 398 | } |
| 399 | /* delete an event */ |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 400 | ret = unregister_trace_uprobe(tu); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 401 | mutex_unlock(&uprobe_lock); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 402 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | if (argc < 2) { |
| 406 | pr_info("Probe point is not specified.\n"); |
| 407 | return -EINVAL; |
| 408 | } |
| 409 | if (isdigit(argv[1][0])) { |
| 410 | pr_info("probe point must be have a filename.\n"); |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | arg = strchr(argv[1], ':'); |
zhangwei(Jovi) | fa44063 | 2013-06-13 14:21:51 +0800 | [diff] [blame] | 414 | if (!arg) { |
| 415 | ret = -EINVAL; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 416 | goto fail_address_parse; |
zhangwei(Jovi) | fa44063 | 2013-06-13 14:21:51 +0800 | [diff] [blame] | 417 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 418 | |
| 419 | *arg++ = '\0'; |
| 420 | filename = argv[1]; |
| 421 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); |
| 422 | if (ret) |
| 423 | goto fail_address_parse; |
| 424 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 425 | inode = igrab(path.dentry->d_inode); |
Oleg Nesterov | 84d7ed7 | 2013-01-27 18:20:45 +0100 | [diff] [blame] | 426 | path_put(&path); |
| 427 | |
Oleg Nesterov | 7e4e28c | 2013-01-28 17:08:47 +0100 | [diff] [blame] | 428 | if (!inode || !S_ISREG(inode->i_mode)) { |
Jovi Zhang | d24d7db | 2012-07-18 18:16:44 +0800 | [diff] [blame] | 429 | ret = -EINVAL; |
| 430 | goto fail_address_parse; |
| 431 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 432 | |
Oleg Nesterov | 84d7ed7 | 2013-01-27 18:20:45 +0100 | [diff] [blame] | 433 | ret = kstrtoul(arg, 0, &offset); |
| 434 | if (ret) |
| 435 | goto fail_address_parse; |
| 436 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 437 | argc -= 2; |
| 438 | argv += 2; |
| 439 | |
| 440 | /* setup a probe */ |
| 441 | if (!event) { |
Andy Shevchenko | b2e902f | 2012-12-17 16:01:27 -0800 | [diff] [blame] | 442 | char *tail; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 443 | char *ptr; |
| 444 | |
Andy Shevchenko | b2e902f | 2012-12-17 16:01:27 -0800 | [diff] [blame] | 445 | tail = kstrdup(kbasename(filename), GFP_KERNEL); |
| 446 | if (!tail) { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 447 | ret = -ENOMEM; |
| 448 | goto fail_address_parse; |
| 449 | } |
| 450 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 451 | ptr = strpbrk(tail, ".-_"); |
| 452 | if (ptr) |
| 453 | *ptr = '\0'; |
| 454 | |
| 455 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset); |
| 456 | event = buf; |
| 457 | kfree(tail); |
| 458 | } |
| 459 | |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 460 | tu = alloc_trace_uprobe(group, event, argc, is_return); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 461 | if (IS_ERR(tu)) { |
| 462 | pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu)); |
| 463 | ret = PTR_ERR(tu); |
| 464 | goto fail_address_parse; |
| 465 | } |
| 466 | tu->offset = offset; |
| 467 | tu->inode = inode; |
| 468 | tu->filename = kstrdup(filename, GFP_KERNEL); |
| 469 | |
| 470 | if (!tu->filename) { |
| 471 | pr_info("Failed to allocate filename.\n"); |
| 472 | ret = -ENOMEM; |
| 473 | goto error; |
| 474 | } |
| 475 | |
| 476 | /* parse arguments */ |
| 477 | ret = 0; |
| 478 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 479 | struct probe_arg *parg = &tu->tp.args[i]; |
| 480 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 481 | /* Increment count for freeing args in error case */ |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 482 | tu->tp.nr_args++; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 483 | |
| 484 | /* Parse argument name */ |
| 485 | arg = strchr(argv[i], '='); |
| 486 | if (arg) { |
| 487 | *arg++ = '\0'; |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 488 | parg->name = kstrdup(argv[i], GFP_KERNEL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 489 | } else { |
| 490 | arg = argv[i]; |
| 491 | /* If argument name is omitted, set "argN" */ |
| 492 | snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1); |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 493 | parg->name = kstrdup(buf, GFP_KERNEL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 494 | } |
| 495 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 496 | if (!parg->name) { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 497 | pr_info("Failed to allocate argument[%d] name.\n", i); |
| 498 | ret = -ENOMEM; |
| 499 | goto error; |
| 500 | } |
| 501 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 502 | if (!is_good_name(parg->name)) { |
| 503 | pr_info("Invalid argument[%d] name: %s\n", i, parg->name); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 504 | ret = -EINVAL; |
| 505 | goto error; |
| 506 | } |
| 507 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 508 | if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 509 | pr_info("Argument[%d] name '%s' conflicts with " |
| 510 | "another field.\n", i, argv[i]); |
| 511 | ret = -EINVAL; |
| 512 | goto error; |
| 513 | } |
| 514 | |
| 515 | /* Parse fetch argument */ |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 516 | ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg, |
Namhyung Kim | a473414 | 2013-11-27 11:36:47 +0900 | [diff] [blame] | 517 | is_return, false); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 518 | if (ret) { |
| 519 | pr_info("Parse error at argument[%d]. (%d)\n", i, ret); |
| 520 | goto error; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | ret = register_trace_uprobe(tu); |
| 525 | if (ret) |
| 526 | goto error; |
| 527 | return 0; |
| 528 | |
| 529 | error: |
| 530 | free_trace_uprobe(tu); |
| 531 | return ret; |
| 532 | |
| 533 | fail_address_parse: |
| 534 | if (inode) |
| 535 | iput(inode); |
| 536 | |
Jovi Zhang | d24d7db | 2012-07-18 18:16:44 +0800 | [diff] [blame] | 537 | pr_info("Failed to parse address or file.\n"); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 538 | |
| 539 | return ret; |
| 540 | } |
| 541 | |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 542 | static int cleanup_all_probes(void) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 543 | { |
| 544 | struct trace_uprobe *tu; |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 545 | int ret = 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 546 | |
| 547 | mutex_lock(&uprobe_lock); |
| 548 | while (!list_empty(&uprobe_list)) { |
| 549 | tu = list_entry(uprobe_list.next, struct trace_uprobe, list); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 550 | ret = unregister_trace_uprobe(tu); |
| 551 | if (ret) |
| 552 | break; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 553 | } |
| 554 | mutex_unlock(&uprobe_lock); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 555 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* Probes listing interfaces */ |
| 559 | static void *probes_seq_start(struct seq_file *m, loff_t *pos) |
| 560 | { |
| 561 | mutex_lock(&uprobe_lock); |
| 562 | return seq_list_start(&uprobe_list, *pos); |
| 563 | } |
| 564 | |
| 565 | static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 566 | { |
| 567 | return seq_list_next(v, &uprobe_list, pos); |
| 568 | } |
| 569 | |
| 570 | static void probes_seq_stop(struct seq_file *m, void *v) |
| 571 | { |
| 572 | mutex_unlock(&uprobe_lock); |
| 573 | } |
| 574 | |
| 575 | static int probes_seq_show(struct seq_file *m, void *v) |
| 576 | { |
| 577 | struct trace_uprobe *tu = v; |
Oleg Nesterov | 3ede82d | 2013-03-30 19:48:09 +0100 | [diff] [blame] | 578 | char c = is_ret_probe(tu) ? 'r' : 'p'; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 579 | int i; |
| 580 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 581 | seq_printf(m, "%c:%s/%s", c, tu->tp.call.class->system, tu->tp.call.name); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 582 | seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset); |
| 583 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 584 | for (i = 0; i < tu->tp.nr_args; i++) |
| 585 | seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 586 | |
| 587 | seq_printf(m, "\n"); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static const struct seq_operations probes_seq_op = { |
| 592 | .start = probes_seq_start, |
| 593 | .next = probes_seq_next, |
| 594 | .stop = probes_seq_stop, |
| 595 | .show = probes_seq_show |
| 596 | }; |
| 597 | |
| 598 | static int probes_open(struct inode *inode, struct file *file) |
| 599 | { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 600 | int ret; |
| 601 | |
| 602 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { |
| 603 | ret = cleanup_all_probes(); |
| 604 | if (ret) |
| 605 | return ret; |
| 606 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 607 | |
| 608 | return seq_open(file, &probes_seq_op); |
| 609 | } |
| 610 | |
| 611 | static ssize_t probes_write(struct file *file, const char __user *buffer, |
| 612 | size_t count, loff_t *ppos) |
| 613 | { |
| 614 | return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe); |
| 615 | } |
| 616 | |
| 617 | static const struct file_operations uprobe_events_ops = { |
| 618 | .owner = THIS_MODULE, |
| 619 | .open = probes_open, |
| 620 | .read = seq_read, |
| 621 | .llseek = seq_lseek, |
| 622 | .release = seq_release, |
| 623 | .write = probes_write, |
| 624 | }; |
| 625 | |
| 626 | /* Probes profiling interfaces */ |
| 627 | static int probes_profile_seq_show(struct seq_file *m, void *v) |
| 628 | { |
| 629 | struct trace_uprobe *tu = v; |
| 630 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 631 | seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->tp.call.name, tu->nhit); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static const struct seq_operations profile_seq_op = { |
| 636 | .start = probes_seq_start, |
| 637 | .next = probes_seq_next, |
| 638 | .stop = probes_seq_stop, |
| 639 | .show = probes_profile_seq_show |
| 640 | }; |
| 641 | |
| 642 | static int profile_open(struct inode *inode, struct file *file) |
| 643 | { |
| 644 | return seq_open(file, &profile_seq_op); |
| 645 | } |
| 646 | |
| 647 | static const struct file_operations uprobe_profile_ops = { |
| 648 | .owner = THIS_MODULE, |
| 649 | .open = profile_open, |
| 650 | .read = seq_read, |
| 651 | .llseek = seq_lseek, |
| 652 | .release = seq_release, |
| 653 | }; |
| 654 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 655 | struct uprobe_cpu_buffer { |
| 656 | struct mutex mutex; |
| 657 | void *buf; |
| 658 | }; |
| 659 | static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer; |
| 660 | static int uprobe_buffer_refcnt; |
| 661 | |
| 662 | static int uprobe_buffer_init(void) |
| 663 | { |
| 664 | int cpu, err_cpu; |
| 665 | |
| 666 | uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer); |
| 667 | if (uprobe_cpu_buffer == NULL) |
| 668 | return -ENOMEM; |
| 669 | |
| 670 | for_each_possible_cpu(cpu) { |
| 671 | struct page *p = alloc_pages_node(cpu_to_node(cpu), |
| 672 | GFP_KERNEL, 0); |
| 673 | if (p == NULL) { |
| 674 | err_cpu = cpu; |
| 675 | goto err; |
| 676 | } |
| 677 | per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p); |
| 678 | mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex); |
| 679 | } |
| 680 | |
| 681 | return 0; |
| 682 | |
| 683 | err: |
| 684 | for_each_possible_cpu(cpu) { |
| 685 | if (cpu == err_cpu) |
| 686 | break; |
| 687 | free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf); |
| 688 | } |
| 689 | |
| 690 | free_percpu(uprobe_cpu_buffer); |
| 691 | return -ENOMEM; |
| 692 | } |
| 693 | |
| 694 | static int uprobe_buffer_enable(void) |
| 695 | { |
| 696 | int ret = 0; |
| 697 | |
| 698 | BUG_ON(!mutex_is_locked(&event_mutex)); |
| 699 | |
| 700 | if (uprobe_buffer_refcnt++ == 0) { |
| 701 | ret = uprobe_buffer_init(); |
| 702 | if (ret < 0) |
| 703 | uprobe_buffer_refcnt--; |
| 704 | } |
| 705 | |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | static void uprobe_buffer_disable(void) |
| 710 | { |
| 711 | BUG_ON(!mutex_is_locked(&event_mutex)); |
| 712 | |
| 713 | if (--uprobe_buffer_refcnt == 0) { |
| 714 | free_percpu(uprobe_cpu_buffer); |
| 715 | uprobe_cpu_buffer = NULL; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | static struct uprobe_cpu_buffer *uprobe_buffer_get(void) |
| 720 | { |
| 721 | struct uprobe_cpu_buffer *ucb; |
| 722 | int cpu; |
| 723 | |
| 724 | cpu = raw_smp_processor_id(); |
| 725 | ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu); |
| 726 | |
| 727 | /* |
| 728 | * Use per-cpu buffers for fastest access, but we might migrate |
| 729 | * so the mutex makes sure we have sole access to it. |
| 730 | */ |
| 731 | mutex_lock(&ucb->mutex); |
| 732 | |
| 733 | return ucb; |
| 734 | } |
| 735 | |
| 736 | static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb) |
| 737 | { |
| 738 | mutex_unlock(&ucb->mutex); |
| 739 | } |
| 740 | |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 741 | static void uprobe_trace_print(struct trace_uprobe *tu, |
| 742 | unsigned long func, struct pt_regs *regs) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 743 | { |
| 744 | struct uprobe_trace_entry_head *entry; |
| 745 | struct ring_buffer_event *event; |
| 746 | struct ring_buffer *buffer; |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 747 | struct uprobe_cpu_buffer *ucb; |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 748 | void *data; |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 749 | int size, dsize, esize; |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 750 | struct ftrace_event_call *call = &tu->tp.call; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 751 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 752 | dsize = __get_data_size(&tu->tp, regs); |
| 753 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
| 754 | |
| 755 | if (WARN_ON_ONCE(!uprobe_cpu_buffer || tu->tp.size + dsize > PAGE_SIZE)) |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 756 | return; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 757 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 758 | ucb = uprobe_buffer_get(); |
| 759 | store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize); |
| 760 | |
| 761 | size = esize + tu->tp.size + dsize; |
| 762 | event = trace_current_buffer_lock_reserve(&buffer, call->event.type, |
| 763 | size, 0, 0); |
| 764 | if (!event) |
| 765 | goto out; |
| 766 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 767 | entry = ring_buffer_event_data(event); |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 768 | if (is_ret_probe(tu)) { |
| 769 | entry->vaddr[0] = func; |
| 770 | entry->vaddr[1] = instruction_pointer(regs); |
| 771 | data = DATAOF_TRACE_ENTRY(entry, true); |
| 772 | } else { |
| 773 | entry->vaddr[0] = instruction_pointer(regs); |
| 774 | data = DATAOF_TRACE_ENTRY(entry, false); |
| 775 | } |
| 776 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 777 | memcpy(data, ucb->buf, tu->tp.size + dsize); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 778 | |
Tom Zanussi | f306cc8 | 2013-10-24 08:34:17 -0500 | [diff] [blame] | 779 | if (!call_filter_check_discard(call, entry, buffer, event)) |
Oleg Nesterov | 0e3853d | 2013-03-28 19:19:11 +0100 | [diff] [blame] | 780 | trace_buffer_unlock_commit(buffer, event, 0, 0); |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 781 | |
| 782 | out: |
| 783 | uprobe_buffer_put(ucb); |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 784 | } |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 785 | |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 786 | /* uprobe handler */ |
| 787 | static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs) |
| 788 | { |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 789 | if (!is_ret_probe(tu)) |
| 790 | uprobe_trace_print(tu, 0, regs); |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 791 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 792 | } |
| 793 | |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 794 | static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func, |
| 795 | struct pt_regs *regs) |
| 796 | { |
| 797 | uprobe_trace_print(tu, func, regs); |
| 798 | } |
| 799 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 800 | /* Event entry printers */ |
| 801 | static enum print_line_t |
| 802 | print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event) |
| 803 | { |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 804 | struct uprobe_trace_entry_head *entry; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 805 | struct trace_seq *s = &iter->seq; |
| 806 | struct trace_uprobe *tu; |
| 807 | u8 *data; |
| 808 | int i; |
| 809 | |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 810 | entry = (struct uprobe_trace_entry_head *)iter->ent; |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 811 | tu = container_of(event, struct trace_uprobe, tp.call.event); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 812 | |
Oleg Nesterov | 3ede82d | 2013-03-30 19:48:09 +0100 | [diff] [blame] | 813 | if (is_ret_probe(tu)) { |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 814 | if (!trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", tu->tp.call.name, |
Oleg Nesterov | 3ede82d | 2013-03-30 19:48:09 +0100 | [diff] [blame] | 815 | entry->vaddr[1], entry->vaddr[0])) |
| 816 | goto partial; |
| 817 | data = DATAOF_TRACE_ENTRY(entry, true); |
| 818 | } else { |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 819 | if (!trace_seq_printf(s, "%s: (0x%lx)", tu->tp.call.name, |
Oleg Nesterov | 3ede82d | 2013-03-30 19:48:09 +0100 | [diff] [blame] | 820 | entry->vaddr[0])) |
| 821 | goto partial; |
| 822 | data = DATAOF_TRACE_ENTRY(entry, false); |
| 823 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 824 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 825 | for (i = 0; i < tu->tp.nr_args; i++) { |
| 826 | struct probe_arg *parg = &tu->tp.args[i]; |
| 827 | |
| 828 | if (!parg->type->print(s, parg->name, data + parg->offset, entry)) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 829 | goto partial; |
| 830 | } |
| 831 | |
| 832 | if (trace_seq_puts(s, "\n")) |
| 833 | return TRACE_TYPE_HANDLED; |
| 834 | |
| 835 | partial: |
| 836 | return TRACE_TYPE_PARTIAL_LINE; |
| 837 | } |
| 838 | |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 839 | typedef bool (*filter_func_t)(struct uprobe_consumer *self, |
| 840 | enum uprobe_filter_ctx ctx, |
| 841 | struct mm_struct *mm); |
| 842 | |
| 843 | static int |
| 844 | probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 845 | { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 846 | int ret = 0; |
| 847 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 848 | if (trace_probe_is_enabled(&tu->tp)) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 849 | return -EINTR; |
| 850 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 851 | ret = uprobe_buffer_enable(); |
| 852 | if (ret < 0) |
| 853 | return ret; |
| 854 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 855 | WARN_ON(!uprobe_filter_is_empty(&tu->filter)); |
| 856 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 857 | tu->tp.flags |= flag; |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 858 | tu->consumer.filter = filter; |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 859 | ret = uprobe_register(tu->inode, tu->offset, &tu->consumer); |
| 860 | if (ret) |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 861 | tu->tp.flags &= ~flag; |
Oleg Nesterov | 4161824 | 2013-01-27 18:36:24 +0100 | [diff] [blame] | 862 | |
| 863 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static void probe_event_disable(struct trace_uprobe *tu, int flag) |
| 867 | { |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 868 | if (!trace_probe_is_enabled(&tu->tp)) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 869 | return; |
| 870 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 871 | WARN_ON(!uprobe_filter_is_empty(&tu->filter)); |
| 872 | |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 873 | uprobe_unregister(tu->inode, tu->offset, &tu->consumer); |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 874 | tu->tp.flags &= ~flag; |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 875 | |
| 876 | uprobe_buffer_disable(); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | static int uprobe_event_define_fields(struct ftrace_event_call *event_call) |
| 880 | { |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 881 | int ret, i, size; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 882 | struct uprobe_trace_entry_head field; |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 883 | struct trace_uprobe *tu = event_call->data; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 884 | |
Oleg Nesterov | 4d1298e | 2013-03-30 19:23:15 +0100 | [diff] [blame] | 885 | if (is_ret_probe(tu)) { |
| 886 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0); |
| 887 | DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0); |
| 888 | size = SIZEOF_TRACE_ENTRY(true); |
| 889 | } else { |
| 890 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0); |
| 891 | size = SIZEOF_TRACE_ENTRY(false); |
| 892 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 893 | /* Set argument names as fields */ |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 894 | for (i = 0; i < tu->tp.nr_args; i++) { |
| 895 | struct probe_arg *parg = &tu->tp.args[i]; |
| 896 | |
| 897 | ret = trace_define_field(event_call, parg->type->fmttype, |
| 898 | parg->name, size + parg->offset, |
| 899 | parg->type->size, parg->type->is_signed, |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 900 | FILTER_OTHER); |
| 901 | |
| 902 | if (ret) |
| 903 | return ret; |
| 904 | } |
| 905 | return 0; |
| 906 | } |
| 907 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 908 | #ifdef CONFIG_PERF_EVENTS |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 909 | static bool |
| 910 | __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm) |
| 911 | { |
| 912 | struct perf_event *event; |
| 913 | |
| 914 | if (filter->nr_systemwide) |
| 915 | return true; |
| 916 | |
| 917 | list_for_each_entry(event, &filter->perf_events, hw.tp_list) { |
| 918 | if (event->hw.tp_target->mm == mm) |
| 919 | return true; |
| 920 | } |
| 921 | |
| 922 | return false; |
| 923 | } |
| 924 | |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 925 | static inline bool |
| 926 | uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event) |
| 927 | { |
| 928 | return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm); |
| 929 | } |
| 930 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 931 | static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event) |
| 932 | { |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 933 | bool done; |
| 934 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 935 | write_lock(&tu->filter.rwlock); |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 936 | if (event->hw.tp_target) { |
| 937 | /* |
| 938 | * event->parent != NULL means copy_process(), we can avoid |
| 939 | * uprobe_apply(). current->mm must be probed and we can rely |
| 940 | * on dup_mmap() which preserves the already installed bp's. |
| 941 | * |
| 942 | * attr.enable_on_exec means that exec/mmap will install the |
| 943 | * breakpoints we need. |
| 944 | */ |
| 945 | done = tu->filter.nr_systemwide || |
| 946 | event->parent || event->attr.enable_on_exec || |
| 947 | uprobe_filter_event(tu, event); |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 948 | list_add(&event->hw.tp_list, &tu->filter.perf_events); |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 949 | } else { |
| 950 | done = tu->filter.nr_systemwide; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 951 | tu->filter.nr_systemwide++; |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 952 | } |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 953 | write_unlock(&tu->filter.rwlock); |
| 954 | |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 955 | if (!done) |
| 956 | uprobe_apply(tu->inode, tu->offset, &tu->consumer, true); |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 957 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event) |
| 962 | { |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 963 | bool done; |
| 964 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 965 | write_lock(&tu->filter.rwlock); |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 966 | if (event->hw.tp_target) { |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 967 | list_del(&event->hw.tp_list); |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 968 | done = tu->filter.nr_systemwide || |
| 969 | (event->hw.tp_target->flags & PF_EXITING) || |
| 970 | uprobe_filter_event(tu, event); |
| 971 | } else { |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 972 | tu->filter.nr_systemwide--; |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 973 | done = tu->filter.nr_systemwide; |
| 974 | } |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 975 | write_unlock(&tu->filter.rwlock); |
| 976 | |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 977 | if (!done) |
| 978 | uprobe_apply(tu->inode, tu->offset, &tu->consumer, false); |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 979 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 980 | return 0; |
| 981 | } |
| 982 | |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 983 | static bool uprobe_perf_filter(struct uprobe_consumer *uc, |
| 984 | enum uprobe_filter_ctx ctx, struct mm_struct *mm) |
| 985 | { |
| 986 | struct trace_uprobe *tu; |
| 987 | int ret; |
| 988 | |
| 989 | tu = container_of(uc, struct trace_uprobe, consumer); |
| 990 | read_lock(&tu->filter.rwlock); |
| 991 | ret = __uprobe_perf_filter(&tu->filter, mm); |
| 992 | read_unlock(&tu->filter.rwlock); |
| 993 | |
| 994 | return ret; |
| 995 | } |
| 996 | |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 997 | static void uprobe_perf_print(struct trace_uprobe *tu, |
| 998 | unsigned long func, struct pt_regs *regs) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 999 | { |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1000 | struct ftrace_event_call *call = &tu->tp.call; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1001 | struct uprobe_trace_entry_head *entry; |
| 1002 | struct hlist_head *head; |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 1003 | struct uprobe_cpu_buffer *ucb; |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 1004 | void *data; |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 1005 | int size, dsize, esize; |
| 1006 | int rctx; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1007 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 1008 | dsize = __get_data_size(&tu->tp, regs); |
| 1009 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
| 1010 | |
| 1011 | if (WARN_ON_ONCE(!uprobe_cpu_buffer)) |
| 1012 | return; |
| 1013 | |
| 1014 | size = esize + tu->tp.size + dsize; |
| 1015 | size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32); |
| 1016 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough")) |
| 1017 | return; |
| 1018 | |
| 1019 | ucb = uprobe_buffer_get(); |
| 1020 | store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1021 | |
| 1022 | preempt_disable(); |
Oleg Nesterov | 515619f | 2013-04-13 15:36:49 +0200 | [diff] [blame] | 1023 | head = this_cpu_ptr(call->perf_events); |
| 1024 | if (hlist_empty(head)) |
| 1025 | goto out; |
| 1026 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1027 | entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx); |
| 1028 | if (!entry) |
| 1029 | goto out; |
| 1030 | |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 1031 | if (is_ret_probe(tu)) { |
| 1032 | entry->vaddr[0] = func; |
Oleg Nesterov | 32520b2 | 2013-04-10 16:25:49 +0200 | [diff] [blame] | 1033 | entry->vaddr[1] = instruction_pointer(regs); |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 1034 | data = DATAOF_TRACE_ENTRY(entry, true); |
| 1035 | } else { |
Oleg Nesterov | 32520b2 | 2013-04-10 16:25:49 +0200 | [diff] [blame] | 1036 | entry->vaddr[0] = instruction_pointer(regs); |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 1037 | data = DATAOF_TRACE_ENTRY(entry, false); |
| 1038 | } |
| 1039 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 1040 | memcpy(data, ucb->buf, tu->tp.size + dsize); |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1041 | |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 1042 | if (size - esize > tu->tp.size + dsize) { |
| 1043 | int len = tu->tp.size + dsize; |
| 1044 | |
| 1045 | memset(data + len, 0, size - esize - len); |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1046 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1047 | |
Oleg Nesterov | 32520b2 | 2013-04-10 16:25:49 +0200 | [diff] [blame] | 1048 | perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1049 | out: |
| 1050 | preempt_enable(); |
Namhyung Kim | dcad1a2 | 2013-07-03 16:40:28 +0900 | [diff] [blame^] | 1051 | uprobe_buffer_put(ucb); |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* uprobe profile handler */ |
| 1055 | static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs) |
| 1056 | { |
| 1057 | if (!uprobe_perf_filter(&tu->consumer, 0, current->mm)) |
| 1058 | return UPROBE_HANDLER_REMOVE; |
| 1059 | |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 1060 | if (!is_ret_probe(tu)) |
| 1061 | uprobe_perf_print(tu, 0, regs); |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 1062 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1063 | } |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1064 | |
| 1065 | static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func, |
| 1066 | struct pt_regs *regs) |
| 1067 | { |
| 1068 | uprobe_perf_print(tu, func, regs); |
| 1069 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1070 | #endif /* CONFIG_PERF_EVENTS */ |
| 1071 | |
| 1072 | static |
| 1073 | int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data) |
| 1074 | { |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 1075 | struct trace_uprobe *tu = event->data; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1076 | |
| 1077 | switch (type) { |
| 1078 | case TRACE_REG_REGISTER: |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 1079 | return probe_event_enable(tu, TP_FLAG_TRACE, NULL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1080 | |
| 1081 | case TRACE_REG_UNREGISTER: |
| 1082 | probe_event_disable(tu, TP_FLAG_TRACE); |
| 1083 | return 0; |
| 1084 | |
| 1085 | #ifdef CONFIG_PERF_EVENTS |
| 1086 | case TRACE_REG_PERF_REGISTER: |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 1087 | return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1088 | |
| 1089 | case TRACE_REG_PERF_UNREGISTER: |
| 1090 | probe_event_disable(tu, TP_FLAG_PROFILE); |
| 1091 | return 0; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 1092 | |
| 1093 | case TRACE_REG_PERF_OPEN: |
| 1094 | return uprobe_perf_open(tu, data); |
| 1095 | |
| 1096 | case TRACE_REG_PERF_CLOSE: |
| 1097 | return uprobe_perf_close(tu, data); |
| 1098 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1099 | #endif |
| 1100 | default: |
| 1101 | return 0; |
| 1102 | } |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs) |
| 1107 | { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1108 | struct trace_uprobe *tu; |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 1109 | int ret = 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1110 | |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 1111 | tu = container_of(con, struct trace_uprobe, consumer); |
Oleg Nesterov | 1b47aef | 2013-01-31 19:55:27 +0100 | [diff] [blame] | 1112 | tu->nhit++; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1113 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1114 | if (tu->tp.flags & TP_FLAG_TRACE) |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 1115 | ret |= uprobe_trace_func(tu, regs); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1116 | |
| 1117 | #ifdef CONFIG_PERF_EVENTS |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1118 | if (tu->tp.flags & TP_FLAG_PROFILE) |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 1119 | ret |= uprobe_perf_func(tu, regs); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1120 | #endif |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 1121 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1122 | } |
| 1123 | |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1124 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
| 1125 | unsigned long func, struct pt_regs *regs) |
| 1126 | { |
| 1127 | struct trace_uprobe *tu; |
| 1128 | |
| 1129 | tu = container_of(con, struct trace_uprobe, consumer); |
| 1130 | |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1131 | if (tu->tp.flags & TP_FLAG_TRACE) |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1132 | uretprobe_trace_func(tu, func, regs); |
| 1133 | |
| 1134 | #ifdef CONFIG_PERF_EVENTS |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1135 | if (tu->tp.flags & TP_FLAG_PROFILE) |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 1136 | uretprobe_perf_func(tu, func, regs); |
| 1137 | #endif |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1141 | static struct trace_event_functions uprobe_funcs = { |
| 1142 | .trace = print_uprobe_event |
| 1143 | }; |
| 1144 | |
| 1145 | static int register_uprobe_event(struct trace_uprobe *tu) |
| 1146 | { |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1147 | struct ftrace_event_call *call = &tu->tp.call; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1148 | int ret; |
| 1149 | |
| 1150 | /* Initialize ftrace_event_call */ |
| 1151 | INIT_LIST_HEAD(&call->class->fields); |
| 1152 | call->event.funcs = &uprobe_funcs; |
| 1153 | call->class->define_fields = uprobe_event_define_fields; |
| 1154 | |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 1155 | if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1156 | return -ENOMEM; |
| 1157 | |
| 1158 | ret = register_ftrace_event(&call->event); |
| 1159 | if (!ret) { |
| 1160 | kfree(call->print_fmt); |
| 1161 | return -ENODEV; |
| 1162 | } |
| 1163 | call->flags = 0; |
| 1164 | call->class->reg = trace_uprobe_register; |
| 1165 | call->data = tu; |
| 1166 | ret = trace_add_event_call(call); |
| 1167 | |
| 1168 | if (ret) { |
| 1169 | pr_info("Failed to register uprobe event: %s\n", call->name); |
| 1170 | kfree(call->print_fmt); |
| 1171 | unregister_ftrace_event(&call->event); |
| 1172 | } |
| 1173 | |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 1177 | static int unregister_uprobe_event(struct trace_uprobe *tu) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1178 | { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 1179 | int ret; |
| 1180 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1181 | /* tu->event is unregistered in trace_remove_event_call() */ |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1182 | ret = trace_remove_event_call(&tu->tp.call); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 1183 | if (ret) |
| 1184 | return ret; |
Namhyung Kim | 14577c3 | 2013-07-03 15:42:53 +0900 | [diff] [blame] | 1185 | kfree(tu->tp.call.print_fmt); |
| 1186 | tu->tp.call.print_fmt = NULL; |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame] | 1187 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | /* Make a trace interface for controling probe points */ |
| 1191 | static __init int init_uprobe_trace(void) |
| 1192 | { |
| 1193 | struct dentry *d_tracer; |
| 1194 | |
| 1195 | d_tracer = tracing_init_dentry(); |
| 1196 | if (!d_tracer) |
| 1197 | return 0; |
| 1198 | |
| 1199 | trace_create_file("uprobe_events", 0644, d_tracer, |
| 1200 | NULL, &uprobe_events_ops); |
| 1201 | /* Profile interface */ |
| 1202 | trace_create_file("uprobe_profile", 0444, d_tracer, |
| 1203 | NULL, &uprobe_profile_ops); |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | fs_initcall(init_uprobe_trace); |