Commit | Line | Data |
---|---|---|
d6d808f3 | 1 | /* |
886d51a3 | 2 | * probes/lttng-kprobes.c |
d6d808f3 MD |
3 | * |
4 | * LTTng kprobes integration module. | |
5 | * | |
886d51a3 MD |
6 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; only | |
11 | * version 2.1 of the License. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
d6d808f3 MD |
21 | */ |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/kprobes.h> | |
f17701fb | 25 | #include <linux/slab.h> |
156a3977 MD |
26 | #include <lttng-events.h> |
27 | #include <wrapper/ringbuffer/frontend_types.h> | |
28 | #include <wrapper/vmalloc.h> | |
29 | #include <wrapper/irqflags.h> | |
30 | #include <lttng-tracer.h> | |
2d16de12 | 31 | #include <blacklist/kprobes.h> |
d6d808f3 | 32 | |
f17701fb MD |
33 | static |
34 | int lttng_kprobes_handler_pre(struct kprobe *p, struct pt_regs *regs) | |
d6d808f3 | 35 | { |
a90917c3 MD |
36 | struct lttng_event *event = |
37 | container_of(p, struct lttng_event, u.kprobe.kp); | |
79150a49 JD |
38 | struct lttng_probe_ctx lttng_probe_ctx = { |
39 | .event = event, | |
ccecf3fb | 40 | .interruptible = !lttng_regs_irqs_disabled(regs), |
79150a49 | 41 | }; |
a90917c3 | 42 | struct lttng_channel *chan = event->chan; |
d6d808f3 MD |
43 | struct lib_ring_buffer_ctx ctx; |
44 | int ret; | |
45 | unsigned long data = (unsigned long) p->addr; | |
46 | ||
acb98454 | 47 | if (unlikely(!READ_ONCE(chan->session->active))) |
f17701fb | 48 | return 0; |
acb98454 | 49 | if (unlikely(!READ_ONCE(chan->enabled))) |
e64957da | 50 | return 0; |
acb98454 | 51 | if (unlikely(!READ_ONCE(event->enabled))) |
e64957da MD |
52 | return 0; |
53 | ||
79150a49 | 54 | lib_ring_buffer_ctx_init(&ctx, chan->chan, <tng_probe_ctx, sizeof(data), |
a90917c3 | 55 | lttng_alignof(data), -1); |
4e1f08f4 | 56 | ret = chan->ops->event_reserve(&ctx, event->id); |
d6d808f3 | 57 | if (ret < 0) |
f17701fb | 58 | return 0; |
a90917c3 | 59 | lib_ring_buffer_align_ctx(&ctx, lttng_alignof(data)); |
d6d808f3 MD |
60 | chan->ops->event_write(&ctx, &data, sizeof(data)); |
61 | chan->ops->event_commit(&ctx); | |
f17701fb | 62 | return 0; |
d6d808f3 | 63 | } |
f17701fb MD |
64 | |
65 | /* | |
66 | * Create event description | |
67 | */ | |
68 | static | |
a90917c3 | 69 | int lttng_create_kprobe_event(const char *name, struct lttng_event *event) |
f17701fb MD |
70 | { |
71 | struct lttng_event_field *field; | |
d3dbe23c | 72 | struct lttng_event_desc *desc; |
f17701fb MD |
73 | int ret; |
74 | ||
d3dbe23c MD |
75 | desc = kzalloc(sizeof(*event->desc), GFP_KERNEL); |
76 | if (!desc) | |
f17701fb | 77 | return -ENOMEM; |
d3dbe23c MD |
78 | desc->name = kstrdup(name, GFP_KERNEL); |
79 | if (!desc->name) { | |
f17701fb MD |
80 | ret = -ENOMEM; |
81 | goto error_str; | |
82 | } | |
d3dbe23c MD |
83 | desc->nr_fields = 1; |
84 | desc->fields = field = | |
f17701fb | 85 | kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL); |
0d1a681e MD |
86 | if (!field) { |
87 | ret = -ENOMEM; | |
88 | goto error_field; | |
89 | } | |
f17701fb MD |
90 | field->name = "ip"; |
91 | field->type.atype = atype_integer; | |
ba1f5986 | 92 | field->type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT; |
a90917c3 | 93 | field->type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT; |
06254b0f | 94 | field->type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long); |
f17701fb | 95 | field->type.u.basic.integer.reverse_byte_order = 0; |
e0a7a7c4 MD |
96 | field->type.u.basic.integer.base = 16; |
97 | field->type.u.basic.integer.encoding = lttng_encode_none; | |
dc7f600a | 98 | desc->owner = THIS_MODULE; |
d3dbe23c | 99 | event->desc = desc; |
f17701fb MD |
100 | |
101 | return 0; | |
102 | ||
0d1a681e MD |
103 | error_field: |
104 | kfree(desc->name); | |
f17701fb | 105 | error_str: |
d3dbe23c | 106 | kfree(desc); |
f17701fb MD |
107 | return ret; |
108 | } | |
109 | ||
110 | int lttng_kprobes_register(const char *name, | |
111 | const char *symbol_name, | |
112 | uint64_t offset, | |
113 | uint64_t addr, | |
a90917c3 | 114 | struct lttng_event *event) |
f17701fb MD |
115 | { |
116 | int ret; | |
117 | ||
c37f2a9b MD |
118 | /* Kprobes expects a NULL symbol name if unused */ |
119 | if (symbol_name[0] == '\0') | |
120 | symbol_name = NULL; | |
121 | ||
f17701fb MD |
122 | ret = lttng_create_kprobe_event(name, event); |
123 | if (ret) | |
124 | goto error; | |
125 | memset(&event->u.kprobe.kp, 0, sizeof(event->u.kprobe.kp)); | |
126 | event->u.kprobe.kp.pre_handler = lttng_kprobes_handler_pre; | |
c37f2a9b MD |
127 | if (symbol_name) { |
128 | event->u.kprobe.symbol_name = | |
f8695253 | 129 | kzalloc(LTTNG_KERNEL_SYM_NAME_LEN * sizeof(char), |
c37f2a9b MD |
130 | GFP_KERNEL); |
131 | if (!event->u.kprobe.symbol_name) { | |
132 | ret = -ENOMEM; | |
133 | goto name_error; | |
134 | } | |
135 | memcpy(event->u.kprobe.symbol_name, symbol_name, | |
f8695253 | 136 | LTTNG_KERNEL_SYM_NAME_LEN * sizeof(char)); |
c37f2a9b MD |
137 | event->u.kprobe.kp.symbol_name = |
138 | event->u.kprobe.symbol_name; | |
f17701fb | 139 | } |
f17701fb | 140 | event->u.kprobe.kp.offset = offset; |
b2c4e8fb | 141 | event->u.kprobe.kp.addr = (void *) (unsigned long) addr; |
16a9a591 MD |
142 | |
143 | /* | |
144 | * Ensure the memory we just allocated don't trigger page faults. | |
145 | * Well.. kprobes itself puts the page fault handler on the blacklist, | |
146 | * but we can never be too careful. | |
147 | */ | |
148 | wrapper_vmalloc_sync_all(); | |
149 | ||
f17701fb MD |
150 | ret = register_kprobe(&event->u.kprobe.kp); |
151 | if (ret) | |
152 | goto register_error; | |
153 | return 0; | |
154 | ||
155 | register_error: | |
156 | kfree(event->u.kprobe.symbol_name); | |
157 | name_error: | |
0d1a681e | 158 | kfree(event->desc->fields); |
f17701fb MD |
159 | kfree(event->desc->name); |
160 | kfree(event->desc); | |
161 | error: | |
162 | return ret; | |
163 | } | |
164 | EXPORT_SYMBOL_GPL(lttng_kprobes_register); | |
165 | ||
a90917c3 | 166 | void lttng_kprobes_unregister(struct lttng_event *event) |
f17701fb MD |
167 | { |
168 | unregister_kprobe(&event->u.kprobe.kp); | |
edeb3137 MD |
169 | } |
170 | EXPORT_SYMBOL_GPL(lttng_kprobes_unregister); | |
171 | ||
a90917c3 | 172 | void lttng_kprobes_destroy_private(struct lttng_event *event) |
edeb3137 | 173 | { |
f17701fb | 174 | kfree(event->u.kprobe.symbol_name); |
0d1a681e | 175 | kfree(event->desc->fields); |
f17701fb MD |
176 | kfree(event->desc->name); |
177 | kfree(event->desc); | |
178 | } | |
edeb3137 | 179 | EXPORT_SYMBOL_GPL(lttng_kprobes_destroy_private); |
d6d808f3 MD |
180 | |
181 | MODULE_LICENSE("GPL and additional rights"); | |
182 | MODULE_AUTHOR("Mathieu Desnoyers"); | |
183 | MODULE_DESCRIPTION("Linux Trace Toolkit Kprobes Support"); | |
13ab8b0a MD |
184 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
185 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." | |
186 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) | |
187 | LTTNG_MODULES_EXTRAVERSION); |