1f49c072c7daae9a3f25a1ddccda4b9965816b5e
[lttng-modules.git] / src / probes / lttng-kprobes.c
1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
2 *
3 * probes/lttng-kprobes.c
4 *
5 * LTTng kprobes integration module.
6 *
7 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
10 #include <linux/module.h>
11 #include <linux/kprobes.h>
12 #include <linux/slab.h>
13 #include <lttng/events.h>
14 #include <lttng/events-internal.h>
15 #include <ringbuffer/frontend_types.h>
16 #include <wrapper/vmalloc.h>
17 #include <wrapper/irqflags.h>
18 #include <lttng/tracer.h>
19 #include <blacklist/kprobes.h>
20
21 static
22 int lttng_kprobes_event_handler_pre(struct kprobe *p, struct pt_regs *regs)
23 {
24 struct lttng_kernel_event_common_private *event_priv =
25 container_of(p, struct lttng_kernel_event_common_private, u.kprobe.kp);
26 struct lttng_kernel_event_common *event = event_priv->pub;
27 struct lttng_kernel_probe_ctx lttng_probe_ctx = {
28 .event = event,
29 .interruptible = !lttng_regs_irqs_disabled(regs),
30 };
31 unsigned long data = (unsigned long) p->addr;
32
33 switch (event->type) {
34 case LTTNG_KERNEL_EVENT_TYPE_RECORDER:
35 {
36 struct lttng_kernel_event_recorder_private *event_recorder_priv =
37 container_of(event_priv, struct lttng_kernel_event_recorder_private, parent);
38 struct lttng_kernel_event_recorder *event_recorder =
39 event_recorder_priv->pub;
40 struct lttng_kernel_channel_buffer *chan = event_recorder->chan;
41
42 if (unlikely(!LTTNG_READ_ONCE(chan->parent.session->active)))
43 return 0;
44 if (unlikely(!LTTNG_READ_ONCE(chan->parent.enabled)))
45 return 0;
46 break;
47 }
48 case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER:
49 break;
50 default:
51 WARN_ON_ONCE(1);
52 }
53
54 if (unlikely(!LTTNG_READ_ONCE(event->enabled)))
55 return 0;
56
57 switch (event->type) {
58 case LTTNG_KERNEL_EVENT_TYPE_RECORDER:
59 {
60 struct lttng_kernel_event_recorder *event_recorder =
61 container_of(event, struct lttng_kernel_event_recorder, parent);
62 struct lttng_kernel_channel_buffer *chan = event_recorder->chan;
63 struct lttng_kernel_ring_buffer_ctx ctx;
64 int ret;
65
66 lib_ring_buffer_ctx_init(&ctx, event_recorder, sizeof(data),
67 lttng_alignof(data), &lttng_probe_ctx);
68 ret = chan->ops->event_reserve(&ctx);
69 if (ret < 0)
70 return 0;
71 chan->ops->event_write(&ctx, &data, sizeof(data), lttng_alignof(data));
72 chan->ops->event_commit(&ctx);
73 break;
74 }
75 case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER:
76 {
77 struct lttng_kernel_event_notifier *event_notifier =
78 container_of(event, struct lttng_kernel_event_notifier, parent);
79 struct lttng_kernel_notification_ctx notif_ctx;
80
81 notif_ctx.eval_capture = LTTNG_READ_ONCE(event_notifier->eval_capture);
82 event_notifier->notification_send(event_notifier, NULL, NULL, &notif_ctx);
83 break;
84 }
85 default:
86 WARN_ON_ONCE(1);
87 }
88 return 0;
89 }
90
91 static const struct lttng_kernel_event_field *event_fields[] = {
92 lttng_kernel_static_event_field("ip",
93 lttng_kernel_static_type_integer_from_type(unsigned long, __BYTE_ORDER, 16),
94 false, false, false),
95 };
96
97 static const struct lttng_kernel_tracepoint_class tp_class = {
98 .nr_fields = ARRAY_SIZE(event_fields),
99 .fields = event_fields,
100 };
101
102 /*
103 * Create event description
104 */
105 static
106 int lttng_create_kprobe_event(const char *name, struct lttng_kernel_event_common *event)
107 {
108 struct lttng_kernel_event_desc *desc;
109 int ret;
110
111 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
112 if (!desc)
113 return -ENOMEM;
114 desc->tp_class = &tp_class;
115 desc->event_name = kstrdup(name, GFP_KERNEL);
116 if (!desc->event_name) {
117 ret = -ENOMEM;
118 goto error_str;
119 }
120 desc->owner = THIS_MODULE;
121 event->priv->desc = desc;
122
123 return 0;
124
125 error_str:
126 kfree(desc);
127 return ret;
128 }
129
130 static
131 int _lttng_kprobes_register(const char *symbol_name,
132 uint64_t offset,
133 uint64_t addr,
134 struct lttng_kprobe *lttng_kp,
135 kprobe_pre_handler_t pre_handler)
136 {
137 int ret;
138
139 /* Kprobes expects a NULL symbol name if unused */
140 if (symbol_name[0] == '\0')
141 symbol_name = NULL;
142
143 memset(&lttng_kp->kp, 0, sizeof(lttng_kp->kp));
144 lttng_kp->kp.pre_handler = pre_handler;
145
146 if (symbol_name) {
147 lttng_kp->symbol_name =
148 kzalloc(LTTNG_KERNEL_ABI_SYM_NAME_LEN * sizeof(char),
149 GFP_KERNEL);
150 if (!lttng_kp->symbol_name) {
151 ret = -ENOMEM;
152 goto name_error;
153 }
154 memcpy(lttng_kp->symbol_name, symbol_name,
155 LTTNG_KERNEL_ABI_SYM_NAME_LEN * sizeof(char));
156 lttng_kp->kp.symbol_name = lttng_kp->symbol_name;
157 }
158
159 lttng_kp->kp.offset = offset;
160 lttng_kp->kp.addr = (void *) (unsigned long) addr;
161
162 /*
163 * Ensure the memory we just allocated don't notify page faults.
164 * Well.. kprobes itself puts the page fault handler on the blacklist,
165 * but we can never be too careful.
166 */
167 wrapper_vmalloc_sync_mappings();
168
169 ret = register_kprobe(&lttng_kp->kp);
170 if (ret)
171 goto register_error;
172
173 return 0;
174
175 register_error:
176 kfree(lttng_kp->symbol_name);
177 name_error:
178 return ret;
179 }
180
181 int lttng_kprobes_register_event(const char *name,
182 const char *symbol_name,
183 uint64_t offset,
184 uint64_t addr,
185 struct lttng_kernel_event_common *event)
186 {
187 int ret;
188
189 ret = lttng_create_kprobe_event(name, event);
190 if (ret)
191 goto error;
192
193 ret = _lttng_kprobes_register(symbol_name, offset, addr,
194 &event->priv->u.kprobe, lttng_kprobes_event_handler_pre);
195 if (ret)
196 goto register_error;
197
198 return 0;
199
200 register_error:
201 kfree(event->priv->desc->event_name);
202 kfree(event->priv->desc);
203 error:
204 return ret;
205 }
206 EXPORT_SYMBOL_GPL(lttng_kprobes_register_event);
207
208 void lttng_kprobes_unregister_event(struct lttng_kernel_event_common *event)
209 {
210 unregister_kprobe(&event->priv->u.kprobe.kp);
211 }
212 EXPORT_SYMBOL_GPL(lttng_kprobes_unregister_event);
213
214 void lttng_kprobes_destroy_event_private(struct lttng_kernel_event_common *event)
215 {
216 kfree(event->priv->u.kprobe.symbol_name);
217 kfree(event->priv->desc->event_name);
218 kfree(event->priv->desc);
219 }
220 EXPORT_SYMBOL_GPL(lttng_kprobes_destroy_event_private);
221
222 MODULE_LICENSE("GPL and additional rights");
223 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
224 MODULE_DESCRIPTION("LTTng kprobes probes");
225 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
226 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
227 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
228 LTTNG_MODULES_EXTRAVERSION);
This page took 0.050032 seconds and 4 git commands to generate.