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; |
| 54 | struct ftrace_event_class class; |
| 55 | struct ftrace_event_call call; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 56 | struct trace_uprobe_filter filter; |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 57 | struct uprobe_consumer consumer; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 58 | struct inode *inode; |
| 59 | char *filename; |
| 60 | unsigned long offset; |
| 61 | unsigned long nhit; |
| 62 | unsigned int flags; /* For TP_FLAG_* */ |
| 63 | ssize_t size; /* trace entry size */ |
| 64 | unsigned int nr_args; |
| 65 | struct probe_arg args[]; |
| 66 | }; |
| 67 | |
| 68 | #define SIZEOF_TRACE_UPROBE(n) \ |
| 69 | (offsetof(struct trace_uprobe, args) + \ |
| 70 | (sizeof(struct probe_arg) * (n))) |
| 71 | |
| 72 | static int register_uprobe_event(struct trace_uprobe *tu); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 73 | static int unregister_uprobe_event(struct trace_uprobe *tu); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 74 | |
| 75 | static DEFINE_MUTEX(uprobe_lock); |
| 76 | static LIST_HEAD(uprobe_list); |
| 77 | |
| 78 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs); |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 79 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
| 80 | unsigned long func, struct pt_regs *regs); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 81 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 82 | static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter) |
| 83 | { |
| 84 | rwlock_init(&filter->rwlock); |
| 85 | filter->nr_systemwide = 0; |
| 86 | INIT_LIST_HEAD(&filter->perf_events); |
| 87 | } |
| 88 | |
| 89 | static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter) |
| 90 | { |
| 91 | return !filter->nr_systemwide && list_empty(&filter->perf_events); |
| 92 | } |
| 93 | |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 94 | static inline bool is_ret_probe(struct trace_uprobe *tu) |
| 95 | { |
| 96 | return tu->consumer.ret_handler != NULL; |
| 97 | } |
| 98 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 99 | /* |
| 100 | * Allocate new trace_uprobe and initialize it (including uprobes). |
| 101 | */ |
| 102 | static struct trace_uprobe * |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 103 | 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] | 104 | { |
| 105 | struct trace_uprobe *tu; |
| 106 | |
| 107 | if (!event || !is_good_name(event)) |
| 108 | return ERR_PTR(-EINVAL); |
| 109 | |
| 110 | if (!group || !is_good_name(group)) |
| 111 | return ERR_PTR(-EINVAL); |
| 112 | |
| 113 | tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL); |
| 114 | if (!tu) |
| 115 | return ERR_PTR(-ENOMEM); |
| 116 | |
| 117 | tu->call.class = &tu->class; |
| 118 | tu->call.name = kstrdup(event, GFP_KERNEL); |
| 119 | if (!tu->call.name) |
| 120 | goto error; |
| 121 | |
| 122 | tu->class.system = kstrdup(group, GFP_KERNEL); |
| 123 | if (!tu->class.system) |
| 124 | goto error; |
| 125 | |
| 126 | INIT_LIST_HEAD(&tu->list); |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 127 | tu->consumer.handler = uprobe_dispatcher; |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 128 | if (is_ret) |
| 129 | tu->consumer.ret_handler = uretprobe_dispatcher; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 130 | init_trace_uprobe_filter(&tu->filter); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 131 | return tu; |
| 132 | |
| 133 | error: |
| 134 | kfree(tu->call.name); |
| 135 | kfree(tu); |
| 136 | |
| 137 | return ERR_PTR(-ENOMEM); |
| 138 | } |
| 139 | |
| 140 | static void free_trace_uprobe(struct trace_uprobe *tu) |
| 141 | { |
| 142 | int i; |
| 143 | |
| 144 | for (i = 0; i < tu->nr_args; i++) |
| 145 | traceprobe_free_probe_arg(&tu->args[i]); |
| 146 | |
| 147 | iput(tu->inode); |
| 148 | kfree(tu->call.class->system); |
| 149 | kfree(tu->call.name); |
| 150 | kfree(tu->filename); |
| 151 | kfree(tu); |
| 152 | } |
| 153 | |
| 154 | static struct trace_uprobe *find_probe_event(const char *event, const char *group) |
| 155 | { |
| 156 | struct trace_uprobe *tu; |
| 157 | |
| 158 | list_for_each_entry(tu, &uprobe_list, list) |
| 159 | if (strcmp(tu->call.name, event) == 0 && |
| 160 | strcmp(tu->call.class->system, group) == 0) |
| 161 | return tu; |
| 162 | |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | /* 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^] | 167 | static int unregister_trace_uprobe(struct trace_uprobe *tu) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 168 | { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 169 | int ret; |
| 170 | |
| 171 | ret = unregister_uprobe_event(tu); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 175 | list_del(&tu->list); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 176 | free_trace_uprobe(tu); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 177 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* Register a trace_uprobe and probe_event */ |
| 181 | static int register_trace_uprobe(struct trace_uprobe *tu) |
| 182 | { |
| 183 | struct trace_uprobe *old_tp; |
| 184 | int ret; |
| 185 | |
| 186 | mutex_lock(&uprobe_lock); |
| 187 | |
| 188 | /* register as an event */ |
| 189 | old_tp = find_probe_event(tu->call.name, tu->call.class->system); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 190 | if (old_tp) { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 191 | /* delete old event */ |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 192 | ret = unregister_trace_uprobe(old_tp); |
| 193 | if (ret) |
| 194 | goto end; |
| 195 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 196 | |
| 197 | ret = register_uprobe_event(tu); |
| 198 | if (ret) { |
| 199 | pr_warning("Failed to register probe event(%d)\n", ret); |
| 200 | goto end; |
| 201 | } |
| 202 | |
| 203 | list_add_tail(&tu->list, &uprobe_list); |
| 204 | |
| 205 | end: |
| 206 | mutex_unlock(&uprobe_lock); |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Argument syntax: |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 213 | * - Add uprobe: p|r[:[GRP/]EVENT] PATH:SYMBOL [FETCHARGS] |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 214 | * |
| 215 | * - Remove uprobe: -:[GRP/]EVENT |
| 216 | */ |
| 217 | static int create_trace_uprobe(int argc, char **argv) |
| 218 | { |
| 219 | struct trace_uprobe *tu; |
| 220 | struct inode *inode; |
| 221 | char *arg, *event, *group, *filename; |
| 222 | char buf[MAX_EVENT_NAME_LEN]; |
| 223 | struct path path; |
| 224 | unsigned long offset; |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 225 | bool is_delete, is_return; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 226 | int i, ret; |
| 227 | |
| 228 | inode = NULL; |
| 229 | ret = 0; |
| 230 | is_delete = false; |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 231 | is_return = false; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 232 | event = NULL; |
| 233 | group = NULL; |
| 234 | |
| 235 | /* argc must be >= 1 */ |
| 236 | if (argv[0][0] == '-') |
| 237 | is_delete = true; |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 238 | else if (argv[0][0] == 'r') |
| 239 | is_return = true; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 240 | else if (argv[0][0] != 'p') { |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 241 | pr_info("Probe definition must be started with 'p', 'r' or '-'.\n"); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | if (argv[0][1] == ':') { |
| 246 | event = &argv[0][2]; |
| 247 | arg = strchr(event, '/'); |
| 248 | |
| 249 | if (arg) { |
| 250 | group = event; |
| 251 | event = arg + 1; |
| 252 | event[-1] = '\0'; |
| 253 | |
| 254 | if (strlen(group) == 0) { |
| 255 | pr_info("Group name is not specified\n"); |
| 256 | return -EINVAL; |
| 257 | } |
| 258 | } |
| 259 | if (strlen(event) == 0) { |
| 260 | pr_info("Event name is not specified\n"); |
| 261 | return -EINVAL; |
| 262 | } |
| 263 | } |
| 264 | if (!group) |
| 265 | group = UPROBE_EVENT_SYSTEM; |
| 266 | |
| 267 | if (is_delete) { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 268 | int ret; |
| 269 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 270 | if (!event) { |
| 271 | pr_info("Delete command needs an event name.\n"); |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | mutex_lock(&uprobe_lock); |
| 275 | tu = find_probe_event(event, group); |
| 276 | |
| 277 | if (!tu) { |
| 278 | mutex_unlock(&uprobe_lock); |
| 279 | pr_info("Event %s/%s doesn't exist.\n", group, event); |
| 280 | return -ENOENT; |
| 281 | } |
| 282 | /* delete an event */ |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 283 | ret = unregister_trace_uprobe(tu); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 284 | mutex_unlock(&uprobe_lock); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 285 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | if (argc < 2) { |
| 289 | pr_info("Probe point is not specified.\n"); |
| 290 | return -EINVAL; |
| 291 | } |
| 292 | if (isdigit(argv[1][0])) { |
| 293 | pr_info("probe point must be have a filename.\n"); |
| 294 | return -EINVAL; |
| 295 | } |
| 296 | arg = strchr(argv[1], ':'); |
zhangwei(Jovi) | fa44063 | 2013-06-13 14:21:51 +0800 | [diff] [blame] | 297 | if (!arg) { |
| 298 | ret = -EINVAL; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 299 | goto fail_address_parse; |
zhangwei(Jovi) | fa44063 | 2013-06-13 14:21:51 +0800 | [diff] [blame] | 300 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 301 | |
| 302 | *arg++ = '\0'; |
| 303 | filename = argv[1]; |
| 304 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); |
| 305 | if (ret) |
| 306 | goto fail_address_parse; |
| 307 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 308 | inode = igrab(path.dentry->d_inode); |
Oleg Nesterov | 84d7ed7 | 2013-01-27 18:20:45 +0100 | [diff] [blame] | 309 | path_put(&path); |
| 310 | |
Oleg Nesterov | 7e4e28c | 2013-01-28 17:08:47 +0100 | [diff] [blame] | 311 | if (!inode || !S_ISREG(inode->i_mode)) { |
Jovi Zhang | d24d7db | 2012-07-18 18:16:44 +0800 | [diff] [blame] | 312 | ret = -EINVAL; |
| 313 | goto fail_address_parse; |
| 314 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 315 | |
Oleg Nesterov | 84d7ed7 | 2013-01-27 18:20:45 +0100 | [diff] [blame] | 316 | ret = kstrtoul(arg, 0, &offset); |
| 317 | if (ret) |
| 318 | goto fail_address_parse; |
| 319 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 320 | argc -= 2; |
| 321 | argv += 2; |
| 322 | |
| 323 | /* setup a probe */ |
| 324 | if (!event) { |
Andy Shevchenko | b2e902f | 2012-12-17 16:01:27 -0800 | [diff] [blame] | 325 | char *tail; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 326 | char *ptr; |
| 327 | |
Andy Shevchenko | b2e902f | 2012-12-17 16:01:27 -0800 | [diff] [blame] | 328 | tail = kstrdup(kbasename(filename), GFP_KERNEL); |
| 329 | if (!tail) { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 330 | ret = -ENOMEM; |
| 331 | goto fail_address_parse; |
| 332 | } |
| 333 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 334 | ptr = strpbrk(tail, ".-_"); |
| 335 | if (ptr) |
| 336 | *ptr = '\0'; |
| 337 | |
| 338 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset); |
| 339 | event = buf; |
| 340 | kfree(tail); |
| 341 | } |
| 342 | |
Oleg Nesterov | 4ee5a52 | 2013-03-30 20:28:15 +0100 | [diff] [blame] | 343 | tu = alloc_trace_uprobe(group, event, argc, is_return); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 344 | if (IS_ERR(tu)) { |
| 345 | pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu)); |
| 346 | ret = PTR_ERR(tu); |
| 347 | goto fail_address_parse; |
| 348 | } |
| 349 | tu->offset = offset; |
| 350 | tu->inode = inode; |
| 351 | tu->filename = kstrdup(filename, GFP_KERNEL); |
| 352 | |
| 353 | if (!tu->filename) { |
| 354 | pr_info("Failed to allocate filename.\n"); |
| 355 | ret = -ENOMEM; |
| 356 | goto error; |
| 357 | } |
| 358 | |
| 359 | /* parse arguments */ |
| 360 | ret = 0; |
| 361 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { |
| 362 | /* Increment count for freeing args in error case */ |
| 363 | tu->nr_args++; |
| 364 | |
| 365 | /* Parse argument name */ |
| 366 | arg = strchr(argv[i], '='); |
| 367 | if (arg) { |
| 368 | *arg++ = '\0'; |
| 369 | tu->args[i].name = kstrdup(argv[i], GFP_KERNEL); |
| 370 | } else { |
| 371 | arg = argv[i]; |
| 372 | /* If argument name is omitted, set "argN" */ |
| 373 | snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1); |
| 374 | tu->args[i].name = kstrdup(buf, GFP_KERNEL); |
| 375 | } |
| 376 | |
| 377 | if (!tu->args[i].name) { |
| 378 | pr_info("Failed to allocate argument[%d] name.\n", i); |
| 379 | ret = -ENOMEM; |
| 380 | goto error; |
| 381 | } |
| 382 | |
| 383 | if (!is_good_name(tu->args[i].name)) { |
| 384 | pr_info("Invalid argument[%d] name: %s\n", i, tu->args[i].name); |
| 385 | ret = -EINVAL; |
| 386 | goto error; |
| 387 | } |
| 388 | |
| 389 | if (traceprobe_conflict_field_name(tu->args[i].name, tu->args, i)) { |
| 390 | pr_info("Argument[%d] name '%s' conflicts with " |
| 391 | "another field.\n", i, argv[i]); |
| 392 | ret = -EINVAL; |
| 393 | goto error; |
| 394 | } |
| 395 | |
| 396 | /* Parse fetch argument */ |
| 397 | ret = traceprobe_parse_probe_arg(arg, &tu->size, &tu->args[i], false, false); |
| 398 | if (ret) { |
| 399 | pr_info("Parse error at argument[%d]. (%d)\n", i, ret); |
| 400 | goto error; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | ret = register_trace_uprobe(tu); |
| 405 | if (ret) |
| 406 | goto error; |
| 407 | return 0; |
| 408 | |
| 409 | error: |
| 410 | free_trace_uprobe(tu); |
| 411 | return ret; |
| 412 | |
| 413 | fail_address_parse: |
| 414 | if (inode) |
| 415 | iput(inode); |
| 416 | |
Jovi Zhang | d24d7db | 2012-07-18 18:16:44 +0800 | [diff] [blame] | 417 | pr_info("Failed to parse address or file.\n"); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 418 | |
| 419 | return ret; |
| 420 | } |
| 421 | |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 422 | static int cleanup_all_probes(void) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 423 | { |
| 424 | struct trace_uprobe *tu; |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 425 | int ret = 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 426 | |
| 427 | mutex_lock(&uprobe_lock); |
| 428 | while (!list_empty(&uprobe_list)) { |
| 429 | tu = list_entry(uprobe_list.next, struct trace_uprobe, list); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 430 | ret = unregister_trace_uprobe(tu); |
| 431 | if (ret) |
| 432 | break; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 433 | } |
| 434 | mutex_unlock(&uprobe_lock); |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 435 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* Probes listing interfaces */ |
| 439 | static void *probes_seq_start(struct seq_file *m, loff_t *pos) |
| 440 | { |
| 441 | mutex_lock(&uprobe_lock); |
| 442 | return seq_list_start(&uprobe_list, *pos); |
| 443 | } |
| 444 | |
| 445 | static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos) |
| 446 | { |
| 447 | return seq_list_next(v, &uprobe_list, pos); |
| 448 | } |
| 449 | |
| 450 | static void probes_seq_stop(struct seq_file *m, void *v) |
| 451 | { |
| 452 | mutex_unlock(&uprobe_lock); |
| 453 | } |
| 454 | |
| 455 | static int probes_seq_show(struct seq_file *m, void *v) |
| 456 | { |
| 457 | struct trace_uprobe *tu = v; |
Oleg Nesterov | 3ede82d | 2013-03-30 19:48:09 +0100 | [diff] [blame] | 458 | char c = is_ret_probe(tu) ? 'r' : 'p'; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 459 | int i; |
| 460 | |
Oleg Nesterov | 3ede82d | 2013-03-30 19:48:09 +0100 | [diff] [blame] | 461 | seq_printf(m, "%c:%s/%s", c, tu->call.class->system, tu->call.name); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 462 | seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset); |
| 463 | |
| 464 | for (i = 0; i < tu->nr_args; i++) |
| 465 | seq_printf(m, " %s=%s", tu->args[i].name, tu->args[i].comm); |
| 466 | |
| 467 | seq_printf(m, "\n"); |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static const struct seq_operations probes_seq_op = { |
| 472 | .start = probes_seq_start, |
| 473 | .next = probes_seq_next, |
| 474 | .stop = probes_seq_stop, |
| 475 | .show = probes_seq_show |
| 476 | }; |
| 477 | |
| 478 | static int probes_open(struct inode *inode, struct file *file) |
| 479 | { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 480 | int ret; |
| 481 | |
| 482 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { |
| 483 | ret = cleanup_all_probes(); |
| 484 | if (ret) |
| 485 | return ret; |
| 486 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 487 | |
| 488 | return seq_open(file, &probes_seq_op); |
| 489 | } |
| 490 | |
| 491 | static ssize_t probes_write(struct file *file, const char __user *buffer, |
| 492 | size_t count, loff_t *ppos) |
| 493 | { |
| 494 | return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe); |
| 495 | } |
| 496 | |
| 497 | static const struct file_operations uprobe_events_ops = { |
| 498 | .owner = THIS_MODULE, |
| 499 | .open = probes_open, |
| 500 | .read = seq_read, |
| 501 | .llseek = seq_lseek, |
| 502 | .release = seq_release, |
| 503 | .write = probes_write, |
| 504 | }; |
| 505 | |
| 506 | /* Probes profiling interfaces */ |
| 507 | static int probes_profile_seq_show(struct seq_file *m, void *v) |
| 508 | { |
| 509 | struct trace_uprobe *tu = v; |
| 510 | |
| 511 | seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->call.name, tu->nhit); |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static const struct seq_operations profile_seq_op = { |
| 516 | .start = probes_seq_start, |
| 517 | .next = probes_seq_next, |
| 518 | .stop = probes_seq_stop, |
| 519 | .show = probes_profile_seq_show |
| 520 | }; |
| 521 | |
| 522 | static int profile_open(struct inode *inode, struct file *file) |
| 523 | { |
| 524 | return seq_open(file, &profile_seq_op); |
| 525 | } |
| 526 | |
| 527 | static const struct file_operations uprobe_profile_ops = { |
| 528 | .owner = THIS_MODULE, |
| 529 | .open = profile_open, |
| 530 | .read = seq_read, |
| 531 | .llseek = seq_lseek, |
| 532 | .release = seq_release, |
| 533 | }; |
| 534 | |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 535 | static void uprobe_trace_print(struct trace_uprobe *tu, |
| 536 | unsigned long func, struct pt_regs *regs) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 537 | { |
| 538 | struct uprobe_trace_entry_head *entry; |
| 539 | struct ring_buffer_event *event; |
| 540 | struct ring_buffer *buffer; |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 541 | void *data; |
Oleg Nesterov | 0e3853d | 2013-03-28 19:19:11 +0100 | [diff] [blame] | 542 | int size, i; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 543 | struct ftrace_event_call *call = &tu->call; |
| 544 | |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 545 | size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 546 | event = trace_current_buffer_lock_reserve(&buffer, call->event.type, |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 547 | size + tu->size, 0, 0); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 548 | if (!event) |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 549 | return; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 550 | |
| 551 | entry = ring_buffer_event_data(event); |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 552 | if (is_ret_probe(tu)) { |
| 553 | entry->vaddr[0] = func; |
| 554 | entry->vaddr[1] = instruction_pointer(regs); |
| 555 | data = DATAOF_TRACE_ENTRY(entry, true); |
| 556 | } else { |
| 557 | entry->vaddr[0] = instruction_pointer(regs); |
| 558 | data = DATAOF_TRACE_ENTRY(entry, false); |
| 559 | } |
| 560 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 561 | for (i = 0; i < tu->nr_args; i++) |
| 562 | call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset); |
| 563 | |
| 564 | if (!filter_current_check_discard(buffer, call, entry, event)) |
Oleg Nesterov | 0e3853d | 2013-03-28 19:19:11 +0100 | [diff] [blame] | 565 | trace_buffer_unlock_commit(buffer, event, 0, 0); |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 566 | } |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 567 | |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 568 | /* uprobe handler */ |
| 569 | static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs) |
| 570 | { |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 571 | if (!is_ret_probe(tu)) |
| 572 | uprobe_trace_print(tu, 0, regs); |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 573 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 574 | } |
| 575 | |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 576 | static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func, |
| 577 | struct pt_regs *regs) |
| 578 | { |
| 579 | uprobe_trace_print(tu, func, regs); |
| 580 | } |
| 581 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 582 | /* Event entry printers */ |
| 583 | static enum print_line_t |
| 584 | print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event) |
| 585 | { |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 586 | struct uprobe_trace_entry_head *entry; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 587 | struct trace_seq *s = &iter->seq; |
| 588 | struct trace_uprobe *tu; |
| 589 | u8 *data; |
| 590 | int i; |
| 591 | |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 592 | entry = (struct uprobe_trace_entry_head *)iter->ent; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 593 | tu = container_of(event, struct trace_uprobe, call.event); |
| 594 | |
Oleg Nesterov | 3ede82d | 2013-03-30 19:48:09 +0100 | [diff] [blame] | 595 | if (is_ret_probe(tu)) { |
| 596 | if (!trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", tu->call.name, |
| 597 | entry->vaddr[1], entry->vaddr[0])) |
| 598 | goto partial; |
| 599 | data = DATAOF_TRACE_ENTRY(entry, true); |
| 600 | } else { |
| 601 | if (!trace_seq_printf(s, "%s: (0x%lx)", tu->call.name, |
| 602 | entry->vaddr[0])) |
| 603 | goto partial; |
| 604 | data = DATAOF_TRACE_ENTRY(entry, false); |
| 605 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 606 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 607 | for (i = 0; i < tu->nr_args; i++) { |
| 608 | if (!tu->args[i].type->print(s, tu->args[i].name, |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 609 | data + tu->args[i].offset, entry)) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 610 | goto partial; |
| 611 | } |
| 612 | |
| 613 | if (trace_seq_puts(s, "\n")) |
| 614 | return TRACE_TYPE_HANDLED; |
| 615 | |
| 616 | partial: |
| 617 | return TRACE_TYPE_PARTIAL_LINE; |
| 618 | } |
| 619 | |
Oleg Nesterov | b64b007 | 2013-01-31 19:15:30 +0100 | [diff] [blame] | 620 | static inline bool is_trace_uprobe_enabled(struct trace_uprobe *tu) |
| 621 | { |
| 622 | return tu->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE); |
| 623 | } |
| 624 | |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 625 | typedef bool (*filter_func_t)(struct uprobe_consumer *self, |
| 626 | enum uprobe_filter_ctx ctx, |
| 627 | struct mm_struct *mm); |
| 628 | |
| 629 | static int |
| 630 | 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] | 631 | { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 632 | int ret = 0; |
| 633 | |
Oleg Nesterov | b64b007 | 2013-01-31 19:15:30 +0100 | [diff] [blame] | 634 | if (is_trace_uprobe_enabled(tu)) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 635 | return -EINTR; |
| 636 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 637 | WARN_ON(!uprobe_filter_is_empty(&tu->filter)); |
| 638 | |
Oleg Nesterov | 4161824 | 2013-01-27 18:36:24 +0100 | [diff] [blame] | 639 | tu->flags |= flag; |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 640 | tu->consumer.filter = filter; |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 641 | ret = uprobe_register(tu->inode, tu->offset, &tu->consumer); |
| 642 | if (ret) |
Oleg Nesterov | 4161824 | 2013-01-27 18:36:24 +0100 | [diff] [blame] | 643 | tu->flags &= ~flag; |
Oleg Nesterov | 4161824 | 2013-01-27 18:36:24 +0100 | [diff] [blame] | 644 | |
| 645 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static void probe_event_disable(struct trace_uprobe *tu, int flag) |
| 649 | { |
Oleg Nesterov | b64b007 | 2013-01-31 19:15:30 +0100 | [diff] [blame] | 650 | if (!is_trace_uprobe_enabled(tu)) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 651 | return; |
| 652 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 653 | WARN_ON(!uprobe_filter_is_empty(&tu->filter)); |
| 654 | |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 655 | uprobe_unregister(tu->inode, tu->offset, &tu->consumer); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 656 | tu->flags &= ~flag; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | static int uprobe_event_define_fields(struct ftrace_event_call *event_call) |
| 660 | { |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 661 | int ret, i, size; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 662 | struct uprobe_trace_entry_head field; |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 663 | struct trace_uprobe *tu = event_call->data; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 664 | |
Oleg Nesterov | 4d1298e | 2013-03-30 19:23:15 +0100 | [diff] [blame] | 665 | if (is_ret_probe(tu)) { |
| 666 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0); |
| 667 | DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0); |
| 668 | size = SIZEOF_TRACE_ENTRY(true); |
| 669 | } else { |
| 670 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0); |
| 671 | size = SIZEOF_TRACE_ENTRY(false); |
| 672 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 673 | /* Set argument names as fields */ |
| 674 | for (i = 0; i < tu->nr_args; i++) { |
| 675 | ret = trace_define_field(event_call, tu->args[i].type->fmttype, |
| 676 | tu->args[i].name, |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 677 | size + tu->args[i].offset, |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 678 | tu->args[i].type->size, |
| 679 | tu->args[i].type->is_signed, |
| 680 | FILTER_OTHER); |
| 681 | |
| 682 | if (ret) |
| 683 | return ret; |
| 684 | } |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | #define LEN_OR_ZERO (len ? len - pos : 0) |
| 689 | static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len) |
| 690 | { |
| 691 | const char *fmt, *arg; |
| 692 | int i; |
| 693 | int pos = 0; |
| 694 | |
Oleg Nesterov | 4d1298e | 2013-03-30 19:23:15 +0100 | [diff] [blame] | 695 | if (is_ret_probe(tu)) { |
| 696 | fmt = "(%lx <- %lx)"; |
| 697 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; |
| 698 | } else { |
| 699 | fmt = "(%lx)"; |
| 700 | arg = "REC->" FIELD_STRING_IP; |
| 701 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 702 | |
| 703 | /* When len=0, we just calculate the needed length */ |
| 704 | |
| 705 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); |
| 706 | |
| 707 | for (i = 0; i < tu->nr_args; i++) { |
| 708 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s", |
| 709 | tu->args[i].name, tu->args[i].type->fmt); |
| 710 | } |
| 711 | |
| 712 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); |
| 713 | |
| 714 | for (i = 0; i < tu->nr_args; i++) { |
| 715 | pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s", |
| 716 | tu->args[i].name); |
| 717 | } |
| 718 | |
| 719 | return pos; /* return the length of print_fmt */ |
| 720 | } |
| 721 | #undef LEN_OR_ZERO |
| 722 | |
| 723 | static int set_print_fmt(struct trace_uprobe *tu) |
| 724 | { |
| 725 | char *print_fmt; |
| 726 | int len; |
| 727 | |
| 728 | /* First: called with 0 length to calculate the needed length */ |
| 729 | len = __set_print_fmt(tu, NULL, 0); |
| 730 | print_fmt = kmalloc(len + 1, GFP_KERNEL); |
| 731 | if (!print_fmt) |
| 732 | return -ENOMEM; |
| 733 | |
| 734 | /* Second: actually write the @print_fmt */ |
| 735 | __set_print_fmt(tu, print_fmt, len + 1); |
| 736 | tu->call.print_fmt = print_fmt; |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | #ifdef CONFIG_PERF_EVENTS |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 742 | static bool |
| 743 | __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm) |
| 744 | { |
| 745 | struct perf_event *event; |
| 746 | |
| 747 | if (filter->nr_systemwide) |
| 748 | return true; |
| 749 | |
| 750 | list_for_each_entry(event, &filter->perf_events, hw.tp_list) { |
| 751 | if (event->hw.tp_target->mm == mm) |
| 752 | return true; |
| 753 | } |
| 754 | |
| 755 | return false; |
| 756 | } |
| 757 | |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 758 | static inline bool |
| 759 | uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event) |
| 760 | { |
| 761 | return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm); |
| 762 | } |
| 763 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 764 | static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event) |
| 765 | { |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 766 | bool done; |
| 767 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 768 | write_lock(&tu->filter.rwlock); |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 769 | if (event->hw.tp_target) { |
| 770 | /* |
| 771 | * event->parent != NULL means copy_process(), we can avoid |
| 772 | * uprobe_apply(). current->mm must be probed and we can rely |
| 773 | * on dup_mmap() which preserves the already installed bp's. |
| 774 | * |
| 775 | * attr.enable_on_exec means that exec/mmap will install the |
| 776 | * breakpoints we need. |
| 777 | */ |
| 778 | done = tu->filter.nr_systemwide || |
| 779 | event->parent || event->attr.enable_on_exec || |
| 780 | uprobe_filter_event(tu, event); |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 781 | list_add(&event->hw.tp_list, &tu->filter.perf_events); |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 782 | } else { |
| 783 | done = tu->filter.nr_systemwide; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 784 | tu->filter.nr_systemwide++; |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 785 | } |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 786 | write_unlock(&tu->filter.rwlock); |
| 787 | |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 788 | if (!done) |
| 789 | uprobe_apply(tu->inode, tu->offset, &tu->consumer, true); |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 790 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event) |
| 795 | { |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 796 | bool done; |
| 797 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 798 | write_lock(&tu->filter.rwlock); |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 799 | if (event->hw.tp_target) { |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 800 | list_del(&event->hw.tp_list); |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 801 | done = tu->filter.nr_systemwide || |
| 802 | (event->hw.tp_target->flags & PF_EXITING) || |
| 803 | uprobe_filter_event(tu, event); |
| 804 | } else { |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 805 | tu->filter.nr_systemwide--; |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 806 | done = tu->filter.nr_systemwide; |
| 807 | } |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 808 | write_unlock(&tu->filter.rwlock); |
| 809 | |
Oleg Nesterov | b2fe8ba | 2013-02-04 19:05:43 +0100 | [diff] [blame] | 810 | if (!done) |
| 811 | uprobe_apply(tu->inode, tu->offset, &tu->consumer, false); |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 812 | |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 813 | return 0; |
| 814 | } |
| 815 | |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 816 | static bool uprobe_perf_filter(struct uprobe_consumer *uc, |
| 817 | enum uprobe_filter_ctx ctx, struct mm_struct *mm) |
| 818 | { |
| 819 | struct trace_uprobe *tu; |
| 820 | int ret; |
| 821 | |
| 822 | tu = container_of(uc, struct trace_uprobe, consumer); |
| 823 | read_lock(&tu->filter.rwlock); |
| 824 | ret = __uprobe_perf_filter(&tu->filter, mm); |
| 825 | read_unlock(&tu->filter.rwlock); |
| 826 | |
| 827 | return ret; |
| 828 | } |
| 829 | |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 830 | static void uprobe_perf_print(struct trace_uprobe *tu, |
| 831 | unsigned long func, struct pt_regs *regs) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 832 | { |
| 833 | struct ftrace_event_call *call = &tu->call; |
| 834 | struct uprobe_trace_entry_head *entry; |
| 835 | struct hlist_head *head; |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 836 | void *data; |
| 837 | int size, rctx, i; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 838 | |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 839 | size = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 840 | size = ALIGN(size + tu->size + sizeof(u32), sizeof(u64)) - sizeof(u32); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 841 | |
| 842 | preempt_disable(); |
Oleg Nesterov | 515619f | 2013-04-13 15:36:49 +0200 | [diff] [blame] | 843 | head = this_cpu_ptr(call->perf_events); |
| 844 | if (hlist_empty(head)) |
| 845 | goto out; |
| 846 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 847 | entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx); |
| 848 | if (!entry) |
| 849 | goto out; |
| 850 | |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 851 | if (is_ret_probe(tu)) { |
| 852 | entry->vaddr[0] = func; |
Oleg Nesterov | 32520b2 | 2013-04-10 16:25:49 +0200 | [diff] [blame] | 853 | entry->vaddr[1] = instruction_pointer(regs); |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 854 | data = DATAOF_TRACE_ENTRY(entry, true); |
| 855 | } else { |
Oleg Nesterov | 32520b2 | 2013-04-10 16:25:49 +0200 | [diff] [blame] | 856 | entry->vaddr[0] = instruction_pointer(regs); |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 857 | data = DATAOF_TRACE_ENTRY(entry, false); |
| 858 | } |
| 859 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 860 | for (i = 0; i < tu->nr_args; i++) |
| 861 | call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset); |
| 862 | |
Oleg Nesterov | 32520b2 | 2013-04-10 16:25:49 +0200 | [diff] [blame] | 863 | perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 864 | out: |
| 865 | preempt_enable(); |
Oleg Nesterov | a51cc60 | 2013-03-30 18:02:12 +0100 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /* uprobe profile handler */ |
| 869 | static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs) |
| 870 | { |
| 871 | if (!uprobe_perf_filter(&tu->consumer, 0, current->mm)) |
| 872 | return UPROBE_HANDLER_REMOVE; |
| 873 | |
Oleg Nesterov | 393a736 | 2013-03-30 18:46:22 +0100 | [diff] [blame] | 874 | if (!is_ret_probe(tu)) |
| 875 | uprobe_perf_print(tu, 0, regs); |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 876 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 877 | } |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 878 | |
| 879 | static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func, |
| 880 | struct pt_regs *regs) |
| 881 | { |
| 882 | uprobe_perf_print(tu, func, regs); |
| 883 | } |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 884 | #endif /* CONFIG_PERF_EVENTS */ |
| 885 | |
| 886 | static |
| 887 | int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data) |
| 888 | { |
Oleg Nesterov | 457d177 | 2013-03-29 18:26:51 +0100 | [diff] [blame] | 889 | struct trace_uprobe *tu = event->data; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 890 | |
| 891 | switch (type) { |
| 892 | case TRACE_REG_REGISTER: |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 893 | return probe_event_enable(tu, TP_FLAG_TRACE, NULL); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 894 | |
| 895 | case TRACE_REG_UNREGISTER: |
| 896 | probe_event_disable(tu, TP_FLAG_TRACE); |
| 897 | return 0; |
| 898 | |
| 899 | #ifdef CONFIG_PERF_EVENTS |
| 900 | case TRACE_REG_PERF_REGISTER: |
Oleg Nesterov | 31ba334 | 2013-02-04 17:11:58 +0100 | [diff] [blame] | 901 | return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 902 | |
| 903 | case TRACE_REG_PERF_UNREGISTER: |
| 904 | probe_event_disable(tu, TP_FLAG_PROFILE); |
| 905 | return 0; |
Oleg Nesterov | 736288b | 2013-02-03 20:58:35 +0100 | [diff] [blame] | 906 | |
| 907 | case TRACE_REG_PERF_OPEN: |
| 908 | return uprobe_perf_open(tu, data); |
| 909 | |
| 910 | case TRACE_REG_PERF_CLOSE: |
| 911 | return uprobe_perf_close(tu, data); |
| 912 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 913 | #endif |
| 914 | default: |
| 915 | return 0; |
| 916 | } |
| 917 | return 0; |
| 918 | } |
| 919 | |
| 920 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs) |
| 921 | { |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 922 | struct trace_uprobe *tu; |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 923 | int ret = 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 924 | |
Oleg Nesterov | a932b73 | 2013-01-31 19:47:23 +0100 | [diff] [blame] | 925 | tu = container_of(con, struct trace_uprobe, consumer); |
Oleg Nesterov | 1b47aef | 2013-01-31 19:55:27 +0100 | [diff] [blame] | 926 | tu->nhit++; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 927 | |
| 928 | if (tu->flags & TP_FLAG_TRACE) |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 929 | ret |= uprobe_trace_func(tu, regs); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 930 | |
| 931 | #ifdef CONFIG_PERF_EVENTS |
| 932 | if (tu->flags & TP_FLAG_PROFILE) |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 933 | ret |= uprobe_perf_func(tu, regs); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 934 | #endif |
Oleg Nesterov | f42d24a | 2013-02-04 17:48:34 +0100 | [diff] [blame] | 935 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 936 | } |
| 937 | |
Oleg Nesterov | c1ae5c7 | 2013-03-30 18:25:23 +0100 | [diff] [blame] | 938 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
| 939 | unsigned long func, struct pt_regs *regs) |
| 940 | { |
| 941 | struct trace_uprobe *tu; |
| 942 | |
| 943 | tu = container_of(con, struct trace_uprobe, consumer); |
| 944 | |
| 945 | if (tu->flags & TP_FLAG_TRACE) |
| 946 | uretprobe_trace_func(tu, func, regs); |
| 947 | |
| 948 | #ifdef CONFIG_PERF_EVENTS |
| 949 | if (tu->flags & TP_FLAG_PROFILE) |
| 950 | uretprobe_perf_func(tu, func, regs); |
| 951 | #endif |
| 952 | return 0; |
| 953 | } |
| 954 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 955 | static struct trace_event_functions uprobe_funcs = { |
| 956 | .trace = print_uprobe_event |
| 957 | }; |
| 958 | |
| 959 | static int register_uprobe_event(struct trace_uprobe *tu) |
| 960 | { |
| 961 | struct ftrace_event_call *call = &tu->call; |
| 962 | int ret; |
| 963 | |
| 964 | /* Initialize ftrace_event_call */ |
| 965 | INIT_LIST_HEAD(&call->class->fields); |
| 966 | call->event.funcs = &uprobe_funcs; |
| 967 | call->class->define_fields = uprobe_event_define_fields; |
| 968 | |
| 969 | if (set_print_fmt(tu) < 0) |
| 970 | return -ENOMEM; |
| 971 | |
| 972 | ret = register_ftrace_event(&call->event); |
| 973 | if (!ret) { |
| 974 | kfree(call->print_fmt); |
| 975 | return -ENODEV; |
| 976 | } |
| 977 | call->flags = 0; |
| 978 | call->class->reg = trace_uprobe_register; |
| 979 | call->data = tu; |
| 980 | ret = trace_add_event_call(call); |
| 981 | |
| 982 | if (ret) { |
| 983 | pr_info("Failed to register uprobe event: %s\n", call->name); |
| 984 | kfree(call->print_fmt); |
| 985 | unregister_ftrace_event(&call->event); |
| 986 | } |
| 987 | |
| 988 | return ret; |
| 989 | } |
| 990 | |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 991 | static int unregister_uprobe_event(struct trace_uprobe *tu) |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 992 | { |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 993 | int ret; |
| 994 | |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 995 | /* tu->event is unregistered in trace_remove_event_call() */ |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 996 | ret = trace_remove_event_call(&tu->call); |
| 997 | if (ret) |
| 998 | return ret; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 999 | kfree(tu->call.print_fmt); |
| 1000 | tu->call.print_fmt = NULL; |
Steven Rostedt (Red Hat) | c6c2401 | 2013-07-03 23:33:51 -0400 | [diff] [blame^] | 1001 | return 0; |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* Make a trace interface for controling probe points */ |
| 1005 | static __init int init_uprobe_trace(void) |
| 1006 | { |
| 1007 | struct dentry *d_tracer; |
| 1008 | |
| 1009 | d_tracer = tracing_init_dentry(); |
| 1010 | if (!d_tracer) |
| 1011 | return 0; |
| 1012 | |
| 1013 | trace_create_file("uprobe_events", 0644, d_tracer, |
| 1014 | NULL, &uprobe_events_ops); |
| 1015 | /* Profile interface */ |
| 1016 | trace_create_file("uprobe_profile", 0444, d_tracer, |
| 1017 | NULL, &uprobe_profile_ops); |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | fs_initcall(init_uprobe_trace); |