0906992022897fca346e9ab937e51b8b2163c87a
[lttng-modules.git] / probes / lttng-uprobes.c
1 /*
2 * probes/lttng-uprobes.c
3 *
4 * LTTng uprobes integration module.
5 *
6 * Copyright (C) 2013 Yannick Brosseau <yannick.brosseau@gmail.com>
7 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <linux/fdtable.h>
25 #include <linux/module.h>
26 #include <linux/namei.h>
27 #include <linux/slab.h>
28 #include <lttng-events.h>
29 #include <lttng-tracer.h>
30 #include <wrapper/irqflags.h>
31 #include <wrapper/ringbuffer/frontend_types.h>
32 #include <wrapper/uprobes.h>
33 #include <wrapper/vmalloc.h>
34
35 static
36 int lttng_uprobes_handler_pre(struct uprobe_consumer *uc, struct pt_regs *regs)
37 {
38 struct lttng_event *event =
39 container_of(uc, struct lttng_event, u.uprobe.up_consumer);
40 struct lttng_probe_ctx lttng_probe_ctx = {
41 .event = event,
42 .interruptible = !lttng_regs_irqs_disabled(regs),
43 };
44 struct lttng_channel *chan = event->chan;
45 struct lib_ring_buffer_ctx ctx;
46 int ret;
47
48 struct {
49 unsigned long ip;
50 } payload;
51
52 if (unlikely(!ACCESS_ONCE(chan->session->active)))
53 return 0;
54 if (unlikely(!ACCESS_ONCE(chan->enabled)))
55 return 0;
56 if (unlikely(!ACCESS_ONCE(event->enabled)))
57 return 0;
58
59 lib_ring_buffer_ctx_init(&ctx, chan->chan, &lttng_probe_ctx,
60 sizeof(payload), lttng_alignof(payload), -1);
61
62 ret = chan->ops->event_reserve(&ctx, event->id);
63 if (ret < 0)
64 return 0;
65
66 /* Event payload. */
67 payload.ip = regs->ip;
68 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(payload));
69 chan->ops->event_write(&ctx, &payload, sizeof(payload));
70 chan->ops->event_commit(&ctx);
71 return 0;
72 }
73
74 /*
75 * Create event description.
76 */
77 static
78 int lttng_create_uprobe_event(const char *name, struct lttng_event *event)
79 {
80 struct lttng_event_desc *desc;
81 struct lttng_event_field *fields;
82 int ret;
83
84 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
85 if (!desc)
86 return -ENOMEM;
87 desc->name = kstrdup(name, GFP_KERNEL);
88 if (!desc->name) {
89 ret = -ENOMEM;
90 goto error_str;
91 }
92
93 desc->nr_fields = 1;
94 desc->fields = fields =
95 kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL);
96
97 if (!desc->fields) {
98 ret = -ENOMEM;
99 goto error_fields;
100 }
101 fields[0].name = "ip";
102 fields[0].type.atype = atype_integer;
103 fields[0].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
104 fields[0].type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
105 fields[0].type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long);
106 fields[0].type.u.basic.integer.reverse_byte_order = 0;
107 fields[0].type.u.basic.integer.base = 16;
108 fields[0].type.u.basic.integer.encoding = lttng_encode_none;
109
110 desc->owner = THIS_MODULE;
111 event->desc = desc;
112
113 return 0;
114
115 error_fields:
116 kfree(desc->name);
117 error_str:
118 kfree(desc);
119 return ret;
120 }
121
122 int lttng_uprobes_register(const char *name,
123 const char *path_name,
124 uint64_t offset,
125 struct lttng_event *event)
126 {
127 int ret;
128
129 /* Shoudl we fail if the path is empty, it should be checked before */
130 if (path_name[0] == '\0')
131 path_name = NULL;
132
133 ret = lttng_create_uprobe_event(name, event);
134 if (ret)
135 goto error;
136
137 memset(&event->u.uprobe.up_consumer, 0,
138 sizeof(event->u.uprobe.up_consumer));
139
140 event->u.uprobe.up_consumer.handler = lttng_uprobes_handler_pre;
141 if (path_name) {
142 struct path path;
143 ret = kern_path(path_name, LOOKUP_FOLLOW, &path);
144 if (ret)
145 goto path_error;
146
147 event->u.uprobe.inode = igrab(path.dentry->d_inode);
148 }
149 event->u.uprobe.offset = offset;
150
151 /* Ensure the memory we just allocated don't trigger page faults. */
152 wrapper_vmalloc_sync_all();
153 printk(KERN_WARNING "Registering probe on inode %lu and offset %llu\n", event->u.uprobe.inode->i_ino, event->u.uprobe.offset);
154 ret = wrapper_uprobe_register(event->u.uprobe.inode,
155 event->u.uprobe.offset,
156 &event->u.uprobe.up_consumer);
157 if (ret) {
158 printk(KERN_WARNING "Error registering probe on inode %lu "
159 "and offset %llu\n", event->u.uprobe.inode->i_ino,
160 event->u.uprobe.offset);
161 goto register_error;
162 }
163 return 0;
164
165 register_error:
166 iput(event->u.uprobe.inode);
167 path_error:
168 kfree(event->desc->name);
169 kfree(event->desc);
170 error:
171 return ret;
172 }
173 EXPORT_SYMBOL_GPL(lttng_uprobes_register);
174
175 void lttng_uprobes_unregister(struct lttng_event *event)
176 {
177 wrapper_uprobe_unregister(event->u.uprobe.inode,
178 event->u.uprobe.offset,
179 &event->u.uprobe.up_consumer);
180 }
181 EXPORT_SYMBOL_GPL(lttng_uprobes_unregister);
182
183 void lttng_uprobes_destroy_private(struct lttng_event *event)
184 {
185 iput(event->u.uprobe.inode);
186 kfree(event->desc->name);
187 kfree(event->desc);
188 }
189 EXPORT_SYMBOL_GPL(lttng_uprobes_destroy_private);
190
191 MODULE_LICENSE("GPL and additional rights");
192 MODULE_AUTHOR("Yannick Brosseau");
193 MODULE_DESCRIPTION("Linux Trace Toolkit Uprobes Support");
This page took 0.035373 seconds and 3 git commands to generate.