Fix cast warning
[deliverable/lttng-modules.git] / probes / lttng-kprobes.c
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>
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"
17
18 static
19 int lttng_kprobes_handler_pre(struct kprobe *p, struct pt_regs *regs)
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 (unlikely(!ACCESS_ONCE(chan->session->active)))
29 return 0;
30 if (unlikely(!ACCESS_ONCE(chan->enabled)))
31 return 0;
32 if (unlikely(!ACCESS_ONCE(event->enabled)))
33 return 0;
34
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);
38 if (ret < 0)
39 return 0;
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);
43 return 0;
44 }
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;
53 struct lttng_event_desc *desc;
54 int ret;
55
56 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
57 if (!desc)
58 return -ENOMEM;
59 desc->name = kstrdup(name, GFP_KERNEL);
60 if (!desc->name) {
61 ret = -ENOMEM;
62 goto error_str;
63 }
64 desc->nr_fields = 1;
65 desc->fields = field =
66 kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL);
67 if (!field) {
68 ret = -ENOMEM;
69 goto error_field;
70 }
71 field->name = "ip";
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;
80 event->desc = desc;
81
82 return 0;
83
84 error_field:
85 kfree(desc->name);
86 error_str:
87 kfree(desc);
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 /* Kprobes expects a NULL symbol name if unused */
100 if (symbol_name[0] == '\0')
101 symbol_name = NULL;
102
103 ret = lttng_create_kprobe_event(name, event);
104 if (ret)
105 goto error;
106 memset(&event->u.kprobe.kp, 0, sizeof(event->u.kprobe.kp));
107 event->u.kprobe.kp.pre_handler = lttng_kprobes_handler_pre;
108 if (symbol_name) {
109 event->u.kprobe.symbol_name =
110 kzalloc(LTTNG_SYM_NAME_LEN * sizeof(char),
111 GFP_KERNEL);
112 if (!event->u.kprobe.symbol_name) {
113 ret = -ENOMEM;
114 goto name_error;
115 }
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;
120 }
121 event->u.kprobe.kp.offset = offset;
122 event->u.kprobe.kp.addr = (void *) (unsigned long) addr;
123
124 /*
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.
128 */
129 wrapper_vmalloc_sync_all();
130
131 ret = register_kprobe(&event->u.kprobe.kp);
132 if (ret)
133 goto register_error;
134 return 0;
135
136 register_error:
137 kfree(event->u.kprobe.symbol_name);
138 name_error:
139 kfree(event->desc->fields);
140 kfree(event->desc->name);
141 kfree(event->desc);
142 error:
143 return ret;
144 }
145 EXPORT_SYMBOL_GPL(lttng_kprobes_register);
146
147 void lttng_kprobes_unregister(struct ltt_event *event)
148 {
149 unregister_kprobe(&event->u.kprobe.kp);
150 }
151 EXPORT_SYMBOL_GPL(lttng_kprobes_unregister);
152
153 void lttng_kprobes_destroy_private(struct ltt_event *event)
154 {
155 kfree(event->u.kprobe.symbol_name);
156 kfree(event->desc->fields);
157 kfree(event->desc->name);
158 kfree(event->desc);
159 }
160 EXPORT_SYMBOL_GPL(lttng_kprobes_destroy_private);
161
162 MODULE_LICENSE("GPL and additional rights");
163 MODULE_AUTHOR("Mathieu Desnoyers");
164 MODULE_DESCRIPTION("Linux Trace Toolkit Kprobes Support");
This page took 0.035483 seconds and 5 git commands to generate.