96e7509c328bedb3211e3e3e3dfc4e4d6450f4a8
[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 (!ACCESS_ONCE(chan->session->active))
29 return 0;
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)
34 return 0;
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);
38 return 0;
39 }
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;
48 struct lttng_event_desc *desc;
49 int ret;
50
51 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
52 if (!desc)
53 return -ENOMEM;
54 desc->name = kstrdup(name, GFP_KERNEL);
55 if (!desc->name) {
56 ret = -ENOMEM;
57 goto error_str;
58 }
59 desc->nr_fields = 1;
60 desc->fields = field =
61 kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL);
62 if (!field) {
63 ret = -ENOMEM;
64 goto error_field;
65 }
66 field->name = "ip";
67 field->type.atype = atype_integer;
68 field->type.u.basic.integer.size = sizeof(unsigned long);
69 field->type.u.basic.integer.alignment = ltt_alignof(unsigned long);
70 field->type.u.basic.integer.signedness = 0;
71 field->type.u.basic.integer.reverse_byte_order = 0;
72 field->type.u.basic.integer.base = 16;
73 field->type.u.basic.integer.encoding = lttng_encode_none;
74 event->desc = desc;
75
76 return 0;
77
78 error_field:
79 kfree(desc->name);
80 error_str:
81 kfree(desc);
82 return ret;
83 }
84
85 int lttng_kprobes_register(const char *name,
86 const char *symbol_name,
87 uint64_t offset,
88 uint64_t addr,
89 struct ltt_event *event)
90 {
91 int ret;
92
93 ret = lttng_create_kprobe_event(name, event);
94 if (ret)
95 goto error;
96 memset(&event->u.kprobe.kp, 0, sizeof(event->u.kprobe.kp));
97 event->u.kprobe.kp.pre_handler = lttng_kprobes_handler_pre;
98 event->u.kprobe.symbol_name =
99 kzalloc(LTTNG_SYM_NAME_LEN * sizeof(char),
100 GFP_KERNEL);
101 if (!event->u.kprobe.symbol_name) {
102 ret = -ENOMEM;
103 goto name_error;
104 }
105 memcpy(event->u.kprobe.symbol_name, symbol_name,
106 LTTNG_SYM_NAME_LEN * sizeof(char));
107 event->u.kprobe.kp.symbol_name =
108 event->u.kprobe.symbol_name;
109 event->u.kprobe.kp.offset = offset;
110 event->u.kprobe.kp.addr = (void *) addr;
111
112 /*
113 * Ensure the memory we just allocated don't trigger page faults.
114 * Well.. kprobes itself puts the page fault handler on the blacklist,
115 * but we can never be too careful.
116 */
117 wrapper_vmalloc_sync_all();
118
119 ret = register_kprobe(&event->u.kprobe.kp);
120 if (ret)
121 goto register_error;
122 return 0;
123
124 register_error:
125 kfree(event->u.kprobe.symbol_name);
126 name_error:
127 kfree(event->desc->fields);
128 kfree(event->desc->name);
129 kfree(event->desc);
130 error:
131 return ret;
132 }
133 EXPORT_SYMBOL_GPL(lttng_kprobes_register);
134
135 void lttng_kprobes_unregister(struct ltt_event *event)
136 {
137 unregister_kprobe(&event->u.kprobe.kp);
138 kfree(event->u.kprobe.symbol_name);
139 kfree(event->desc->fields);
140 kfree(event->desc->name);
141 kfree(event->desc);
142 }
143 EXPORT_SYMBOL_GPL(lttng_kprobes_unregister);
144
145 MODULE_LICENSE("GPL and additional rights");
146 MODULE_AUTHOR("Mathieu Desnoyers");
147 MODULE_DESCRIPTION("Linux Trace Toolkit Kprobes Support");
This page took 0.033672 seconds and 4 git commands to generate.