2 * probes/lttng-kprobes.c
4 * LTTng kprobes integration module.
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
23 #include <linux/module.h>
24 #include <linux/kprobes.h>
25 #include <linux/slab.h>
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>
31 #include <blacklist/kprobes.h>
34 int lttng_kprobes_handler_pre(struct kprobe
*p
, struct pt_regs
*regs
)
36 struct lttng_event
*event
=
37 container_of(p
, struct lttng_event
, u
.kprobe
.kp
);
38 struct lttng_probe_ctx lttng_probe_ctx
= {
40 .interruptible
= !lttng_regs_irqs_disabled(regs
),
42 struct lttng_channel
*chan
= event
->chan
;
43 struct lib_ring_buffer_ctx ctx
;
45 unsigned long data
= (unsigned long) p
->addr
;
47 if (unlikely(!READ_ONCE(chan
->session
->active
)))
49 if (unlikely(!READ_ONCE(chan
->enabled
)))
51 if (unlikely(!READ_ONCE(event
->enabled
)))
54 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
, sizeof(data
),
55 lttng_alignof(data
), -1);
56 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
59 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(data
));
60 chan
->ops
->event_write(&ctx
, &data
, sizeof(data
));
61 chan
->ops
->event_commit(&ctx
);
66 * Create event description
69 int lttng_create_kprobe_event(const char *name
, struct lttng_event
*event
)
71 struct lttng_event_field
*field
;
72 struct lttng_event_desc
*desc
;
75 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
78 desc
->name
= kstrdup(name
, GFP_KERNEL
);
84 desc
->fields
= field
=
85 kzalloc(1 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
91 field
->type
.atype
= atype_integer
;
92 field
->type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
93 field
->type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
94 field
->type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
95 field
->type
.u
.basic
.integer
.reverse_byte_order
= 0;
96 field
->type
.u
.basic
.integer
.base
= 16;
97 field
->type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
98 desc
->owner
= THIS_MODULE
;
110 int lttng_kprobes_register(const char *name
,
111 const char *symbol_name
,
114 struct lttng_event
*event
)
118 /* Kprobes expects a NULL symbol name if unused */
119 if (symbol_name
[0] == '\0')
122 ret
= lttng_create_kprobe_event(name
, event
);
125 memset(&event
->u
.kprobe
.kp
, 0, sizeof(event
->u
.kprobe
.kp
));
126 event
->u
.kprobe
.kp
.pre_handler
= lttng_kprobes_handler_pre
;
128 event
->u
.kprobe
.symbol_name
=
129 kzalloc(LTTNG_KERNEL_SYM_NAME_LEN
* sizeof(char),
131 if (!event
->u
.kprobe
.symbol_name
) {
135 memcpy(event
->u
.kprobe
.symbol_name
, symbol_name
,
136 LTTNG_KERNEL_SYM_NAME_LEN
* sizeof(char));
137 event
->u
.kprobe
.kp
.symbol_name
=
138 event
->u
.kprobe
.symbol_name
;
140 event
->u
.kprobe
.kp
.offset
= offset
;
141 event
->u
.kprobe
.kp
.addr
= (void *) (unsigned long) addr
;
144 * Ensure the memory we just allocated don't trigger page faults.
145 * Well.. kprobes itself puts the page fault handler on the blacklist,
146 * but we can never be too careful.
148 wrapper_vmalloc_sync_all();
150 ret
= register_kprobe(&event
->u
.kprobe
.kp
);
156 kfree(event
->u
.kprobe
.symbol_name
);
158 kfree(event
->desc
->fields
);
159 kfree(event
->desc
->name
);
164 EXPORT_SYMBOL_GPL(lttng_kprobes_register
);
166 void lttng_kprobes_unregister(struct lttng_event
*event
)
168 unregister_kprobe(&event
->u
.kprobe
.kp
);
170 EXPORT_SYMBOL_GPL(lttng_kprobes_unregister
);
172 void lttng_kprobes_destroy_private(struct lttng_event
*event
)
174 kfree(event
->u
.kprobe
.symbol_name
);
175 kfree(event
->desc
->fields
);
176 kfree(event
->desc
->name
);
179 EXPORT_SYMBOL_GPL(lttng_kprobes_destroy_private
);
181 MODULE_LICENSE("GPL and additional rights");
182 MODULE_AUTHOR("Mathieu Desnoyers");
183 MODULE_DESCRIPTION("Linux Trace Toolkit Kprobes Support");
184 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
185 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
186 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
187 LTTNG_MODULES_EXTRAVERSION
);