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