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