1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
3 * probes/lttng-kprobes.c
5 * LTTng kprobes integration module.
7 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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>
22 int lttng_kprobes_event_handler_pre(struct kprobe
*p
, struct pt_regs
*regs
)
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
= {
29 .interruptible
= !lttng_regs_irqs_disabled(regs
),
31 unsigned long data
= (unsigned long) p
->addr
;
33 switch (event
->type
) {
34 case LTTNG_KERNEL_EVENT_TYPE_RECORDER
:
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
;
42 if (unlikely(!LTTNG_READ_ONCE(chan
->parent
.session
->active
)))
44 if (unlikely(!LTTNG_READ_ONCE(chan
->parent
.enabled
)))
48 case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER
:
54 if (unlikely(!LTTNG_READ_ONCE(event
->enabled
)))
57 switch (event
->type
) {
58 case LTTNG_KERNEL_EVENT_TYPE_RECORDER
:
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
;
66 lib_ring_buffer_ctx_init(&ctx
, event_recorder
, sizeof(data
),
67 lttng_alignof(data
), <tng_probe_ctx
);
68 ret
= chan
->ops
->event_reserve(&ctx
);
71 chan
->ops
->event_write(&ctx
, &data
, sizeof(data
), lttng_alignof(data
));
72 chan
->ops
->event_commit(&ctx
);
75 case LTTNG_KERNEL_EVENT_TYPE_NOTIFIER
:
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
;
81 notif_ctx
.eval_capture
= LTTNG_READ_ONCE(event_notifier
->eval_capture
);
82 event_notifier
->notification_send(event_notifier
, NULL
, NULL
, ¬if_ctx
);
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),
97 static const struct lttng_kernel_tracepoint_class tp_class
= {
98 .nr_fields
= ARRAY_SIZE(event_fields
),
99 .fields
= event_fields
,
103 * Create event description
106 int lttng_create_kprobe_event(const char *name
, struct lttng_kernel_event_common
*event
)
108 struct lttng_kernel_event_desc
*desc
;
111 desc
= kzalloc(sizeof(*desc
), GFP_KERNEL
);
114 desc
->tp_class
= &tp_class
;
115 desc
->event_name
= kstrdup(name
, GFP_KERNEL
);
116 if (!desc
->event_name
) {
120 desc
->owner
= THIS_MODULE
;
121 event
->priv
->desc
= desc
;
131 int _lttng_kprobes_register(const char *symbol_name
,
134 struct lttng_kprobe
*lttng_kp
,
135 kprobe_pre_handler_t pre_handler
)
139 /* Kprobes expects a NULL symbol name if unused */
140 if (symbol_name
[0] == '\0')
143 memset(<tng_kp
->kp
, 0, sizeof(lttng_kp
->kp
));
144 lttng_kp
->kp
.pre_handler
= pre_handler
;
147 lttng_kp
->symbol_name
=
148 kzalloc(LTTNG_KERNEL_ABI_SYM_NAME_LEN
* sizeof(char),
150 if (!lttng_kp
->symbol_name
) {
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
;
159 lttng_kp
->kp
.offset
= offset
;
160 lttng_kp
->kp
.addr
= (void *) (unsigned long) addr
;
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.
167 wrapper_vmalloc_sync_mappings();
169 ret
= register_kprobe(<tng_kp
->kp
);
176 kfree(lttng_kp
->symbol_name
);
181 int lttng_kprobes_register_event(const char *name
,
182 const char *symbol_name
,
185 struct lttng_kernel_event_common
*event
)
189 ret
= lttng_create_kprobe_event(name
, event
);
193 ret
= _lttng_kprobes_register(symbol_name
, offset
, addr
,
194 &event
->priv
->u
.kprobe
, lttng_kprobes_event_handler_pre
);
201 kfree(event
->priv
->desc
->event_name
);
202 kfree(event
->priv
->desc
);
206 EXPORT_SYMBOL_GPL(lttng_kprobes_register_event
);
208 void lttng_kprobes_unregister_event(struct lttng_kernel_event_common
*event
)
210 unregister_kprobe(&event
->priv
->u
.kprobe
.kp
);
212 EXPORT_SYMBOL_GPL(lttng_kprobes_unregister_event
);
214 void lttng_kprobes_destroy_event_private(struct lttng_kernel_event_common
*event
)
216 kfree(event
->priv
->u
.kprobe
.symbol_name
);
217 kfree(event
->priv
->desc
->event_name
);
218 kfree(event
->priv
->desc
);
220 EXPORT_SYMBOL_GPL(lttng_kprobes_destroy_event_private
);
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
);