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> | |
d6d808f3 | 31 | |
f17701fb MD |
32 | static |
33 | int lttng_kprobes_handler_pre(struct kprobe *p, struct pt_regs *regs) | |
d6d808f3 | 34 | { |
a90917c3 MD |
35 | struct lttng_event *event = |
36 | container_of(p, struct lttng_event, u.kprobe.kp); | |
79150a49 JD |
37 | struct lttng_probe_ctx lttng_probe_ctx = { |
38 | .event = event, | |
ccecf3fb | 39 | .interruptible = !lttng_regs_irqs_disabled(regs), |
79150a49 | 40 | }; |
a90917c3 | 41 | struct lttng_channel *chan = event->chan; |
d6d808f3 MD |
42 | struct lib_ring_buffer_ctx ctx; |
43 | int ret; | |
44 | unsigned long data = (unsigned long) p->addr; | |
45 | ||
e64957da | 46 | if (unlikely(!ACCESS_ONCE(chan->session->active))) |
f17701fb | 47 | return 0; |
e64957da MD |
48 | if (unlikely(!ACCESS_ONCE(chan->enabled))) |
49 | return 0; | |
50 | if (unlikely(!ACCESS_ONCE(event->enabled))) | |
51 | return 0; | |
52 | ||
79150a49 | 53 | lib_ring_buffer_ctx_init(&ctx, chan->chan, <tng_probe_ctx, sizeof(data), |
a90917c3 | 54 | lttng_alignof(data), -1); |
4e1f08f4 | 55 | ret = chan->ops->event_reserve(&ctx, event->id); |
d6d808f3 | 56 | if (ret < 0) |
f17701fb | 57 | return 0; |
a90917c3 | 58 | lib_ring_buffer_align_ctx(&ctx, lttng_alignof(data)); |
d6d808f3 MD |
59 | chan->ops->event_write(&ctx, &data, sizeof(data)); |
60 | chan->ops->event_commit(&ctx); | |
f17701fb | 61 | return 0; |
d6d808f3 | 62 | } |
f17701fb MD |
63 | |
64 | /* | |
65 | * Create event description | |
66 | */ | |
67 | static | |
a90917c3 | 68 | int lttng_create_kprobe_event(const char *name, struct lttng_event *event) |
f17701fb MD |
69 | { |
70 | struct lttng_event_field *field; | |
d3dbe23c | 71 | struct lttng_event_desc *desc; |
f17701fb MD |
72 | int ret; |
73 | ||
d3dbe23c MD |
74 | desc = kzalloc(sizeof(*event->desc), GFP_KERNEL); |
75 | if (!desc) | |
f17701fb | 76 | return -ENOMEM; |
d3dbe23c MD |
77 | desc->name = kstrdup(name, GFP_KERNEL); |
78 | if (!desc->name) { | |
f17701fb MD |
79 | ret = -ENOMEM; |
80 | goto error_str; | |
81 | } | |
d3dbe23c MD |
82 | desc->nr_fields = 1; |
83 | desc->fields = field = | |
f17701fb | 84 | kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL); |
0d1a681e MD |
85 | if (!field) { |
86 | ret = -ENOMEM; | |
87 | goto error_field; | |
88 | } | |
f17701fb MD |
89 | field->name = "ip"; |
90 | field->type.atype = atype_integer; | |
ba1f5986 | 91 | field->type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT; |
a90917c3 | 92 | field->type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT; |
06254b0f | 93 | field->type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long); |
f17701fb | 94 | field->type.u.basic.integer.reverse_byte_order = 0; |
e0a7a7c4 MD |
95 | field->type.u.basic.integer.base = 16; |
96 | field->type.u.basic.integer.encoding = lttng_encode_none; | |
dc7f600a | 97 | desc->owner = THIS_MODULE; |
d3dbe23c | 98 | event->desc = desc; |
f17701fb MD |
99 | |
100 | return 0; | |
101 | ||
0d1a681e MD |
102 | error_field: |
103 | kfree(desc->name); | |
f17701fb | 104 | error_str: |
d3dbe23c | 105 | kfree(desc); |
f17701fb MD |
106 | return ret; |
107 | } | |
108 | ||
109 | int lttng_kprobes_register(const char *name, | |
110 | const char *symbol_name, | |
111 | uint64_t offset, | |
112 | uint64_t addr, | |
a90917c3 | 113 | struct lttng_event *event) |
f17701fb MD |
114 | { |
115 | int ret; | |
116 | ||
c37f2a9b MD |
117 | /* Kprobes expects a NULL symbol name if unused */ |
118 | if (symbol_name[0] == '\0') | |
119 | symbol_name = NULL; | |
120 | ||
f17701fb MD |
121 | ret = lttng_create_kprobe_event(name, event); |
122 | if (ret) | |
123 | goto error; | |
124 | memset(&event->u.kprobe.kp, 0, sizeof(event->u.kprobe.kp)); | |
125 | event->u.kprobe.kp.pre_handler = lttng_kprobes_handler_pre; | |
c37f2a9b MD |
126 | if (symbol_name) { |
127 | event->u.kprobe.symbol_name = | |
f8695253 | 128 | kzalloc(LTTNG_KERNEL_SYM_NAME_LEN * sizeof(char), |
c37f2a9b MD |
129 | GFP_KERNEL); |
130 | if (!event->u.kprobe.symbol_name) { | |
131 | ret = -ENOMEM; | |
132 | goto name_error; | |
133 | } | |
134 | memcpy(event->u.kprobe.symbol_name, symbol_name, | |
f8695253 | 135 | LTTNG_KERNEL_SYM_NAME_LEN * sizeof(char)); |
c37f2a9b MD |
136 | event->u.kprobe.kp.symbol_name = |
137 | event->u.kprobe.symbol_name; | |
f17701fb | 138 | } |
f17701fb | 139 | event->u.kprobe.kp.offset = offset; |
b2c4e8fb | 140 | event->u.kprobe.kp.addr = (void *) (unsigned long) addr; |
16a9a591 MD |
141 | |
142 | /* | |
143 | * Ensure the memory we just allocated don't trigger page faults. | |
144 | * Well.. kprobes itself puts the page fault handler on the blacklist, | |
145 | * but we can never be too careful. | |
146 | */ | |
147 | wrapper_vmalloc_sync_all(); | |
148 | ||
f17701fb MD |
149 | ret = register_kprobe(&event->u.kprobe.kp); |
150 | if (ret) | |
151 | goto register_error; | |
152 | return 0; | |
153 | ||
154 | register_error: | |
155 | kfree(event->u.kprobe.symbol_name); | |
156 | name_error: | |
0d1a681e | 157 | kfree(event->desc->fields); |
f17701fb MD |
158 | kfree(event->desc->name); |
159 | kfree(event->desc); | |
160 | error: | |
161 | return ret; | |
162 | } | |
163 | EXPORT_SYMBOL_GPL(lttng_kprobes_register); | |
164 | ||
a90917c3 | 165 | void lttng_kprobes_unregister(struct lttng_event *event) |
f17701fb MD |
166 | { |
167 | unregister_kprobe(&event->u.kprobe.kp); | |
edeb3137 MD |
168 | } |
169 | EXPORT_SYMBOL_GPL(lttng_kprobes_unregister); | |
170 | ||
a90917c3 | 171 | void lttng_kprobes_destroy_private(struct lttng_event *event) |
edeb3137 | 172 | { |
f17701fb | 173 | kfree(event->u.kprobe.symbol_name); |
0d1a681e | 174 | kfree(event->desc->fields); |
f17701fb MD |
175 | kfree(event->desc->name); |
176 | kfree(event->desc); | |
177 | } | |
edeb3137 | 178 | EXPORT_SYMBOL_GPL(lttng_kprobes_destroy_private); |
d6d808f3 MD |
179 | |
180 | MODULE_LICENSE("GPL and additional rights"); | |
181 | MODULE_AUTHOR("Mathieu Desnoyers"); | |
182 | MODULE_DESCRIPTION("Linux Trace Toolkit Kprobes Support"); | |
13ab8b0a MD |
183 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
184 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." | |
185 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) | |
186 | LTTNG_MODULES_EXTRAVERSION); |