Commit | Line | Data |
---|---|---|
d6d808f3 MD |
1 | /* |
2 | * (C) Copyright 2009-2011 - | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
5 | * LTTng kprobes integration module. | |
6 | * | |
7 | * Dual LGPL v2.1/GPL v2 license. | |
8 | */ | |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/kprobes.h> | |
f17701fb | 12 | #include <linux/slab.h> |
d6d808f3 MD |
13 | #include "../ltt-events.h" |
14 | #include "../wrapper/ringbuffer/frontend_types.h" | |
16a9a591 | 15 | #include "../wrapper/vmalloc.h" |
d6d808f3 MD |
16 | #include "../ltt-tracer.h" |
17 | ||
f17701fb MD |
18 | static |
19 | int lttng_kprobes_handler_pre(struct kprobe *p, struct pt_regs *regs) | |
d6d808f3 MD |
20 | { |
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; | |
25 | int ret; | |
26 | unsigned long data = (unsigned long) p->addr; | |
27 | ||
28 | if (!ACCESS_ONCE(chan->session->active)) | |
f17701fb | 29 | return 0; |
d6d808f3 MD |
30 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, sizeof(data), |
31 | ltt_alignof(data), -1); | |
32 | ret = chan->ops->event_reserve(&ctx); | |
33 | if (ret < 0) | |
f17701fb | 34 | return 0; |
d6d808f3 MD |
35 | lib_ring_buffer_align_ctx(&ctx, ltt_alignof(data)); |
36 | chan->ops->event_write(&ctx, &data, sizeof(data)); | |
37 | chan->ops->event_commit(&ctx); | |
f17701fb | 38 | return 0; |
d6d808f3 | 39 | } |
f17701fb MD |
40 | |
41 | /* | |
42 | * Create event description | |
43 | */ | |
44 | static | |
45 | int lttng_create_kprobe_event(const char *name, struct ltt_event *event) | |
46 | { | |
47 | struct lttng_event_field *field; | |
d3dbe23c | 48 | struct lttng_event_desc *desc; |
f17701fb MD |
49 | int ret; |
50 | ||
d3dbe23c MD |
51 | desc = kzalloc(sizeof(*event->desc), GFP_KERNEL); |
52 | if (!desc) | |
f17701fb | 53 | return -ENOMEM; |
d3dbe23c MD |
54 | desc->name = kstrdup(name, GFP_KERNEL); |
55 | if (!desc->name) { | |
f17701fb MD |
56 | ret = -ENOMEM; |
57 | goto error_str; | |
58 | } | |
d3dbe23c MD |
59 | desc->nr_fields = 1; |
60 | desc->fields = field = | |
f17701fb MD |
61 | kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL); |
62 | field->name = "ip"; | |
63 | field->type.atype = atype_integer; | |
64 | field->type.u.basic.integer.size = sizeof(unsigned long); | |
65 | field->type.u.basic.integer.alignment = ltt_alignof(unsigned long); | |
66 | field->type.u.basic.integer.signedness = 0; | |
67 | field->type.u.basic.integer.reverse_byte_order = 0; | |
e0a7a7c4 MD |
68 | field->type.u.basic.integer.base = 16; |
69 | field->type.u.basic.integer.encoding = lttng_encode_none; | |
d3dbe23c | 70 | event->desc = desc; |
f17701fb MD |
71 | |
72 | return 0; | |
73 | ||
74 | error_str: | |
d3dbe23c | 75 | kfree(desc); |
f17701fb MD |
76 | return ret; |
77 | } | |
78 | ||
79 | int lttng_kprobes_register(const char *name, | |
80 | const char *symbol_name, | |
81 | uint64_t offset, | |
82 | uint64_t addr, | |
83 | struct ltt_event *event) | |
84 | { | |
85 | int ret; | |
86 | ||
87 | ret = lttng_create_kprobe_event(name, event); | |
88 | if (ret) | |
89 | goto error; | |
90 | memset(&event->u.kprobe.kp, 0, sizeof(event->u.kprobe.kp)); | |
91 | event->u.kprobe.kp.pre_handler = lttng_kprobes_handler_pre; | |
92 | event->u.kprobe.symbol_name = | |
e0a7a7c4 | 93 | kzalloc(LTTNG_SYM_NAME_LEN * sizeof(char), |
f17701fb MD |
94 | GFP_KERNEL); |
95 | if (!event->u.kprobe.symbol_name) { | |
96 | ret = -ENOMEM; | |
97 | goto name_error; | |
98 | } | |
99 | memcpy(event->u.kprobe.symbol_name, symbol_name, | |
e0a7a7c4 | 100 | LTTNG_SYM_NAME_LEN * sizeof(char)); |
f17701fb MD |
101 | event->u.kprobe.kp.symbol_name = |
102 | event->u.kprobe.symbol_name; | |
103 | event->u.kprobe.kp.offset = offset; | |
104 | event->u.kprobe.kp.addr = (void *) addr; | |
16a9a591 MD |
105 | |
106 | /* | |
107 | * Ensure the memory we just allocated don't trigger page faults. | |
108 | * Well.. kprobes itself puts the page fault handler on the blacklist, | |
109 | * but we can never be too careful. | |
110 | */ | |
111 | wrapper_vmalloc_sync_all(); | |
112 | ||
f17701fb MD |
113 | ret = register_kprobe(&event->u.kprobe.kp); |
114 | if (ret) | |
115 | goto register_error; | |
116 | return 0; | |
117 | ||
118 | register_error: | |
119 | kfree(event->u.kprobe.symbol_name); | |
120 | name_error: | |
121 | kfree(event->desc->name); | |
122 | kfree(event->desc); | |
123 | error: | |
124 | return ret; | |
125 | } | |
126 | EXPORT_SYMBOL_GPL(lttng_kprobes_register); | |
127 | ||
128 | void lttng_kprobes_unregister(struct ltt_event *event) | |
129 | { | |
130 | unregister_kprobe(&event->u.kprobe.kp); | |
131 | kfree(event->u.kprobe.symbol_name); | |
132 | kfree(event->desc->name); | |
133 | kfree(event->desc); | |
134 | } | |
135 | EXPORT_SYMBOL_GPL(lttng_kprobes_unregister); | |
d6d808f3 MD |
136 | |
137 | MODULE_LICENSE("GPL and additional rights"); | |
138 | MODULE_AUTHOR("Mathieu Desnoyers"); | |
139 | MODULE_DESCRIPTION("Linux Trace Toolkit Kprobes Support"); |