2 * (C) Copyright 2009-2011 -
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * LTTng kprobes integration module.
7 * Dual LGPL v2.1/GPL v2 license.
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 "../lttng-tracer.h"
19 int lttng_kprobes_handler_pre(struct kprobe
*p
, struct pt_regs
*regs
)
21 struct lttng_event
*event
=
22 container_of(p
, struct lttng_event
, u
.kprobe
.kp
);
23 struct lttng_channel
*chan
= event
->chan
;
24 struct lib_ring_buffer_ctx ctx
;
26 unsigned long data
= (unsigned long) p
->addr
;
28 if (unlikely(!ACCESS_ONCE(chan
->session
->active
)))
30 if (unlikely(!ACCESS_ONCE(chan
->enabled
)))
32 if (unlikely(!ACCESS_ONCE(event
->enabled
)))
35 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, event
, sizeof(data
),
36 lttng_alignof(data
), -1);
37 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
40 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(data
));
41 chan
->ops
->event_write(&ctx
, &data
, sizeof(data
));
42 chan
->ops
->event_commit(&ctx
);
47 * Create event description
50 int lttng_create_kprobe_event(const char *name
, struct lttng_event
*event
)
52 struct lttng_event_field
*field
;
53 struct lttng_event_desc
*desc
;
56 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
59 desc
->name
= kstrdup(name
, GFP_KERNEL
);
65 desc
->fields
= field
=
66 kzalloc(1 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
72 field
->type
.atype
= atype_integer
;
73 field
->type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
74 field
->type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
75 field
->type
.u
.basic
.integer
.signedness
= is_signed_type(unsigned long);
76 field
->type
.u
.basic
.integer
.reverse_byte_order
= 0;
77 field
->type
.u
.basic
.integer
.base
= 16;
78 field
->type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
79 desc
->owner
= THIS_MODULE
;
91 int lttng_kprobes_register(const char *name
,
92 const char *symbol_name
,
95 struct lttng_event
*event
)
99 /* Kprobes expects a NULL symbol name if unused */
100 if (symbol_name
[0] == '\0')
103 ret
= lttng_create_kprobe_event(name
, event
);
106 memset(&event
->u
.kprobe
.kp
, 0, sizeof(event
->u
.kprobe
.kp
));
107 event
->u
.kprobe
.kp
.pre_handler
= lttng_kprobes_handler_pre
;
109 event
->u
.kprobe
.symbol_name
=
110 kzalloc(LTTNG_SYM_NAME_LEN
* sizeof(char),
112 if (!event
->u
.kprobe
.symbol_name
) {
116 memcpy(event
->u
.kprobe
.symbol_name
, symbol_name
,
117 LTTNG_SYM_NAME_LEN
* sizeof(char));
118 event
->u
.kprobe
.kp
.symbol_name
=
119 event
->u
.kprobe
.symbol_name
;
121 event
->u
.kprobe
.kp
.offset
= offset
;
122 event
->u
.kprobe
.kp
.addr
= (void *) (unsigned long) addr
;
125 * Ensure the memory we just allocated don't trigger page faults.
126 * Well.. kprobes itself puts the page fault handler on the blacklist,
127 * but we can never be too careful.
129 wrapper_vmalloc_sync_all();
131 ret
= register_kprobe(&event
->u
.kprobe
.kp
);
137 kfree(event
->u
.kprobe
.symbol_name
);
139 kfree(event
->desc
->fields
);
140 kfree(event
->desc
->name
);
145 EXPORT_SYMBOL_GPL(lttng_kprobes_register
);
147 void lttng_kprobes_unregister(struct lttng_event
*event
)
149 unregister_kprobe(&event
->u
.kprobe
.kp
);
151 EXPORT_SYMBOL_GPL(lttng_kprobes_unregister
);
153 void lttng_kprobes_destroy_private(struct lttng_event
*event
)
155 kfree(event
->u
.kprobe
.symbol_name
);
156 kfree(event
->desc
->fields
);
157 kfree(event
->desc
->name
);
160 EXPORT_SYMBOL_GPL(lttng_kprobes_destroy_private
);
162 MODULE_LICENSE("GPL and additional rights");
163 MODULE_AUTHOR("Mathieu Desnoyers");
164 MODULE_DESCRIPTION("Linux Trace Toolkit Kprobes Support");