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 | ||
e64957da | 28 | if (unlikely(!ACCESS_ONCE(chan->session->active))) |
f17701fb | 29 | return 0; |
e64957da MD |
30 | if (unlikely(!ACCESS_ONCE(chan->enabled))) |
31 | return 0; | |
32 | if (unlikely(!ACCESS_ONCE(event->enabled))) | |
33 | return 0; | |
34 | ||
f1676205 | 35 | lib_ring_buffer_ctx_init(&ctx, chan->chan, event, sizeof(data), |
d6d808f3 | 36 | ltt_alignof(data), -1); |
4e1f08f4 | 37 | ret = chan->ops->event_reserve(&ctx, event->id); |
d6d808f3 | 38 | if (ret < 0) |
f17701fb | 39 | return 0; |
d6d808f3 MD |
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); | |
f17701fb | 43 | return 0; |
d6d808f3 | 44 | } |
f17701fb MD |
45 | |
46 | /* | |
47 | * Create event description | |
48 | */ | |
49 | static | |
50 | int lttng_create_kprobe_event(const char *name, struct ltt_event *event) | |
51 | { | |
52 | struct lttng_event_field *field; | |
d3dbe23c | 53 | struct lttng_event_desc *desc; |
f17701fb MD |
54 | int ret; |
55 | ||
d3dbe23c MD |
56 | desc = kzalloc(sizeof(*event->desc), GFP_KERNEL); |
57 | if (!desc) | |
f17701fb | 58 | return -ENOMEM; |
d3dbe23c MD |
59 | desc->name = kstrdup(name, GFP_KERNEL); |
60 | if (!desc->name) { | |
f17701fb MD |
61 | ret = -ENOMEM; |
62 | goto error_str; | |
63 | } | |
d3dbe23c MD |
64 | desc->nr_fields = 1; |
65 | desc->fields = field = | |
f17701fb | 66 | kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL); |
0d1a681e MD |
67 | if (!field) { |
68 | ret = -ENOMEM; | |
69 | goto error_field; | |
70 | } | |
f17701fb MD |
71 | field->name = "ip"; |
72 | field->type.atype = atype_integer; | |
ba1f5986 MD |
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; | |
9e7e4892 | 75 | field->type.u.basic.integer.signedness = is_signed_type(unsigned long); |
f17701fb | 76 | field->type.u.basic.integer.reverse_byte_order = 0; |
e0a7a7c4 MD |
77 | field->type.u.basic.integer.base = 16; |
78 | field->type.u.basic.integer.encoding = lttng_encode_none; | |
dc7f600a | 79 | desc->owner = THIS_MODULE; |
d3dbe23c | 80 | event->desc = desc; |
f17701fb MD |
81 | |
82 | return 0; | |
83 | ||
0d1a681e MD |
84 | error_field: |
85 | kfree(desc->name); | |
f17701fb | 86 | error_str: |
d3dbe23c | 87 | kfree(desc); |
f17701fb MD |
88 | return ret; |
89 | } | |
90 | ||
91 | int lttng_kprobes_register(const char *name, | |
92 | const char *symbol_name, | |
93 | uint64_t offset, | |
94 | uint64_t addr, | |
95 | struct ltt_event *event) | |
96 | { | |
97 | int ret; | |
98 | ||
99 | ret = lttng_create_kprobe_event(name, event); | |
100 | if (ret) | |
101 | goto error; | |
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 = | |
e0a7a7c4 | 105 | kzalloc(LTTNG_SYM_NAME_LEN * sizeof(char), |
f17701fb MD |
106 | GFP_KERNEL); |
107 | if (!event->u.kprobe.symbol_name) { | |
108 | ret = -ENOMEM; | |
109 | goto name_error; | |
110 | } | |
111 | memcpy(event->u.kprobe.symbol_name, symbol_name, | |
e0a7a7c4 | 112 | LTTNG_SYM_NAME_LEN * sizeof(char)); |
f17701fb MD |
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; | |
16a9a591 MD |
117 | |
118 | /* | |
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. | |
122 | */ | |
123 | wrapper_vmalloc_sync_all(); | |
124 | ||
f17701fb MD |
125 | ret = register_kprobe(&event->u.kprobe.kp); |
126 | if (ret) | |
127 | goto register_error; | |
128 | return 0; | |
129 | ||
130 | register_error: | |
131 | kfree(event->u.kprobe.symbol_name); | |
132 | name_error: | |
0d1a681e | 133 | kfree(event->desc->fields); |
f17701fb MD |
134 | kfree(event->desc->name); |
135 | kfree(event->desc); | |
136 | error: | |
137 | return ret; | |
138 | } | |
139 | EXPORT_SYMBOL_GPL(lttng_kprobes_register); | |
140 | ||
141 | void lttng_kprobes_unregister(struct ltt_event *event) | |
142 | { | |
143 | unregister_kprobe(&event->u.kprobe.kp); | |
edeb3137 MD |
144 | } |
145 | EXPORT_SYMBOL_GPL(lttng_kprobes_unregister); | |
146 | ||
147 | void lttng_kprobes_destroy_private(struct ltt_event *event) | |
148 | { | |
f17701fb | 149 | kfree(event->u.kprobe.symbol_name); |
0d1a681e | 150 | kfree(event->desc->fields); |
f17701fb MD |
151 | kfree(event->desc->name); |
152 | kfree(event->desc); | |
153 | } | |
edeb3137 | 154 | EXPORT_SYMBOL_GPL(lttng_kprobes_destroy_private); |
d6d808f3 MD |
155 | |
156 | MODULE_LICENSE("GPL and additional rights"); | |
157 | MODULE_AUTHOR("Mathieu Desnoyers"); | |
158 | MODULE_DESCRIPTION("Linux Trace Toolkit Kprobes Support"); |