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 "../ltt-events.h"
14 #include "../wrapper/ringbuffer/frontend_types.h"
15 #include "../wrapper/vmalloc.h"
16 #include "../ltt-tracer.h"
19 int lttng_kprobes_handler_pre(struct kprobe
*p
, struct pt_regs
*regs
)
21 struct ltt_event
*event
=
22 container_of(p
, struct ltt_event
, u
.kprobe
.kp
);
23 struct ltt_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 ltt_alignof(data
), -1);
37 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
40 lib_ring_buffer_align_ctx(&ctx
, ltt_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 ltt_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
= ltt_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 ltt_event
*event
)
99 ret
= lttng_create_kprobe_event(name
, event
);
102 memset(&event
->u
.kprobe
.kp
, 0, sizeof(event
->u
.kprobe
.kp
));
103 event
->u
.kprobe
.kp
.pre_handler
= lttng_kprobes_handler_pre
;
104 event
->u
.kprobe
.symbol_name
=
105 kzalloc(LTTNG_SYM_NAME_LEN
* sizeof(char),
107 if (!event
->u
.kprobe
.symbol_name
) {
111 memcpy(event
->u
.kprobe
.symbol_name
, symbol_name
,
112 LTTNG_SYM_NAME_LEN
* sizeof(char));
113 event
->u
.kprobe
.kp
.symbol_name
=
114 event
->u
.kprobe
.symbol_name
;
115 event
->u
.kprobe
.kp
.offset
= offset
;
116 event
->u
.kprobe
.kp
.addr
= (void *) addr
;
119 * Ensure the memory we just allocated don't trigger page faults.
120 * Well.. kprobes itself puts the page fault handler on the blacklist,
121 * but we can never be too careful.
123 wrapper_vmalloc_sync_all();
125 ret
= register_kprobe(&event
->u
.kprobe
.kp
);
131 kfree(event
->u
.kprobe
.symbol_name
);
133 kfree(event
->desc
->fields
);
134 kfree(event
->desc
->name
);
139 EXPORT_SYMBOL_GPL(lttng_kprobes_register
);
141 void lttng_kprobes_unregister(struct ltt_event
*event
)
143 unregister_kprobe(&event
->u
.kprobe
.kp
);
145 EXPORT_SYMBOL_GPL(lttng_kprobes_unregister
);
147 void lttng_kprobes_destroy_private(struct ltt_event
*event
)
149 kfree(event
->u
.kprobe
.symbol_name
);
150 kfree(event
->desc
->fields
);
151 kfree(event
->desc
->name
);
154 EXPORT_SYMBOL_GPL(lttng_kprobes_destroy_private
);
156 MODULE_LICENSE("GPL and additional rights");
157 MODULE_AUTHOR("Mathieu Desnoyers");
158 MODULE_DESCRIPTION("Linux Trace Toolkit Kprobes Support");