1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
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 <wrapper/ringbuffer/frontend_types.h>
15 #include <wrapper/vmalloc.h>
16 #include <wrapper/irqflags.h>
17 #include <lttng-tracer.h>
18 #include <blacklist/kprobes.h>
21 int lttng_kprobes_handler_pre(struct kprobe
*p
, struct pt_regs
*regs
)
23 struct lttng_event
*event
=
24 container_of(p
, struct lttng_event
, u
.kprobe
.kp
);
25 struct lttng_probe_ctx lttng_probe_ctx
= {
27 .interruptible
= !lttng_regs_irqs_disabled(regs
),
29 struct lttng_channel
*chan
= event
->chan
;
30 struct lib_ring_buffer_ctx ctx
;
32 unsigned long data
= (unsigned long) p
->addr
;
34 if (unlikely(!READ_ONCE(chan
->session
->active
)))
36 if (unlikely(!READ_ONCE(chan
->enabled
)))
38 if (unlikely(!READ_ONCE(event
->enabled
)))
41 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
, sizeof(data
),
42 lttng_alignof(data
), -1);
43 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
46 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(data
));
47 chan
->ops
->event_write(&ctx
, &data
, sizeof(data
));
48 chan
->ops
->event_commit(&ctx
);
53 * Create event description
56 int lttng_create_kprobe_event(const char *name
, struct lttng_event
*event
)
58 struct lttng_event_field
*field
;
59 struct lttng_event_desc
*desc
;
62 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
65 desc
->name
= kstrdup(name
, GFP_KERNEL
);
71 desc
->fields
= field
=
72 kzalloc(1 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
78 field
->type
.atype
= atype_integer
;
79 field
->type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
80 field
->type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
81 field
->type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
82 field
->type
.u
.basic
.integer
.reverse_byte_order
= 0;
83 field
->type
.u
.basic
.integer
.base
= 16;
84 field
->type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
85 desc
->owner
= THIS_MODULE
;
97 int lttng_kprobes_register(const char *name
,
98 const char *symbol_name
,
101 struct lttng_event
*event
)
105 /* Kprobes expects a NULL symbol name if unused */
106 if (symbol_name
[0] == '\0')
109 ret
= lttng_create_kprobe_event(name
, event
);
112 memset(&event
->u
.kprobe
.kp
, 0, sizeof(event
->u
.kprobe
.kp
));
113 event
->u
.kprobe
.kp
.pre_handler
= lttng_kprobes_handler_pre
;
115 event
->u
.kprobe
.symbol_name
=
116 kzalloc(LTTNG_KERNEL_SYM_NAME_LEN
* sizeof(char),
118 if (!event
->u
.kprobe
.symbol_name
) {
122 memcpy(event
->u
.kprobe
.symbol_name
, symbol_name
,
123 LTTNG_KERNEL_SYM_NAME_LEN
* sizeof(char));
124 event
->u
.kprobe
.kp
.symbol_name
=
125 event
->u
.kprobe
.symbol_name
;
127 event
->u
.kprobe
.kp
.offset
= offset
;
128 event
->u
.kprobe
.kp
.addr
= (void *) (unsigned long) addr
;
131 * Ensure the memory we just allocated don't trigger page faults.
132 * Well.. kprobes itself puts the page fault handler on the blacklist,
133 * but we can never be too careful.
135 wrapper_vmalloc_sync_all();
137 ret
= register_kprobe(&event
->u
.kprobe
.kp
);
143 kfree(event
->u
.kprobe
.symbol_name
);
145 kfree(event
->desc
->fields
);
146 kfree(event
->desc
->name
);
151 EXPORT_SYMBOL_GPL(lttng_kprobes_register
);
153 void lttng_kprobes_unregister(struct lttng_event
*event
)
155 unregister_kprobe(&event
->u
.kprobe
.kp
);
157 EXPORT_SYMBOL_GPL(lttng_kprobes_unregister
);
159 void lttng_kprobes_destroy_private(struct lttng_event
*event
)
161 kfree(event
->u
.kprobe
.symbol_name
);
162 kfree(event
->desc
->fields
);
163 kfree(event
->desc
->name
);
166 EXPORT_SYMBOL_GPL(lttng_kprobes_destroy_private
);
168 MODULE_LICENSE("GPL and additional rights");
169 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
170 MODULE_DESCRIPTION("LTTng kprobes probes");
171 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
172 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
173 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
174 LTTNG_MODULES_EXTRAVERSION
);