Commit | Line | Data |
---|---|---|
e0a7a7c4 MD |
1 | /* |
2 | * (C) Copyright 2009-2011 - | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
5 | * LTTng function tracer integration module. | |
6 | * | |
7 | * Dual LGPL v2.1/GPL v2 license. | |
8 | */ | |
9 | ||
e3de3dde MD |
10 | /* |
11 | * Ftrace function tracer does not seem to provide synchronization between probe | |
12 | * teardown and callback execution. Therefore, we make this module permanently | |
13 | * loaded (unloadable). | |
14 | */ | |
15 | ||
e0a7a7c4 MD |
16 | #include <linux/module.h> |
17 | #include <linux/ftrace.h> | |
18 | #include <linux/slab.h> | |
19 | #include "../ltt-events.h" | |
20 | #include "../wrapper/ringbuffer/frontend_types.h" | |
5a9479dc | 21 | #include "../wrapper/ftrace.h" |
16a9a591 | 22 | #include "../wrapper/vmalloc.h" |
e0a7a7c4 MD |
23 | #include "../ltt-tracer.h" |
24 | ||
25 | static | |
5a9479dc | 26 | void lttng_ftrace_handler(unsigned long ip, unsigned long parent_ip, void **data) |
e0a7a7c4 MD |
27 | { |
28 | struct ltt_event *event = *data; | |
29 | struct ltt_channel *chan = event->chan; | |
30 | struct lib_ring_buffer_ctx ctx; | |
31 | struct { | |
32 | unsigned long ip; | |
33 | unsigned long parent_ip; | |
34 | } payload; | |
35 | int ret; | |
36 | ||
37 | if (!ACCESS_ONCE(chan->session->active)) | |
5a9479dc | 38 | return; |
e0a7a7c4 MD |
39 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, |
40 | sizeof(payload), ltt_alignof(payload), -1); | |
4e1f08f4 | 41 | ret = chan->ops->event_reserve(&ctx, event->id); |
e0a7a7c4 | 42 | if (ret < 0) |
5a9479dc | 43 | return; |
e0a7a7c4 MD |
44 | payload.ip = ip; |
45 | payload.parent_ip = parent_ip; | |
46 | lib_ring_buffer_align_ctx(&ctx, ltt_alignof(payload)); | |
47 | chan->ops->event_write(&ctx, &payload, sizeof(payload)); | |
48 | chan->ops->event_commit(&ctx); | |
5a9479dc | 49 | return; |
e0a7a7c4 MD |
50 | } |
51 | ||
52 | /* | |
53 | * Create event description | |
54 | */ | |
55 | static | |
56 | int lttng_create_ftrace_event(const char *name, struct ltt_event *event) | |
57 | { | |
58 | struct lttng_event_field *fields; | |
59 | struct lttng_event_desc *desc; | |
60 | int ret; | |
61 | ||
62 | desc = kzalloc(sizeof(*event->desc), GFP_KERNEL); | |
63 | if (!desc) | |
64 | return -ENOMEM; | |
65 | desc->name = kstrdup(name, GFP_KERNEL); | |
66 | if (!desc->name) { | |
67 | ret = -ENOMEM; | |
68 | goto error_str; | |
69 | } | |
70 | desc->nr_fields = 2; | |
71 | desc->fields = fields = | |
72 | kzalloc(2 * sizeof(struct lttng_event_field), GFP_KERNEL); | |
0d1a681e MD |
73 | if (!desc->fields) { |
74 | ret = -ENOMEM; | |
75 | goto error_fields; | |
76 | } | |
e0a7a7c4 MD |
77 | fields[0].name = "ip"; |
78 | fields[0].type.atype = atype_integer; | |
ba1f5986 MD |
79 | fields[0].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT; |
80 | fields[0].type.u.basic.integer.alignment = ltt_alignof(unsigned long) * CHAR_BIT; | |
e0a7a7c4 MD |
81 | fields[0].type.u.basic.integer.signedness = 0; |
82 | fields[0].type.u.basic.integer.reverse_byte_order = 0; | |
83 | fields[0].type.u.basic.integer.base = 16; | |
84 | fields[0].type.u.basic.integer.encoding = lttng_encode_none; | |
85 | ||
86 | fields[1].name = "parent_ip"; | |
87 | fields[1].type.atype = atype_integer; | |
ba1f5986 MD |
88 | fields[1].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT; |
89 | fields[1].type.u.basic.integer.alignment = ltt_alignof(unsigned long) * CHAR_BIT; | |
e0a7a7c4 MD |
90 | fields[1].type.u.basic.integer.signedness = 0; |
91 | fields[1].type.u.basic.integer.reverse_byte_order = 0; | |
92 | fields[1].type.u.basic.integer.base = 16; | |
93 | fields[1].type.u.basic.integer.encoding = lttng_encode_none; | |
94 | ||
95 | event->desc = desc; | |
96 | ||
97 | return 0; | |
98 | ||
0d1a681e MD |
99 | error_fields: |
100 | kfree(desc->name); | |
e0a7a7c4 MD |
101 | error_str: |
102 | kfree(desc); | |
103 | return ret; | |
104 | } | |
105 | ||
106 | static | |
107 | struct ftrace_probe_ops lttng_ftrace_ops = { | |
108 | .func = lttng_ftrace_handler, | |
109 | }; | |
110 | ||
111 | int lttng_ftrace_register(const char *name, | |
112 | const char *symbol_name, | |
113 | struct ltt_event *event) | |
114 | { | |
115 | int ret; | |
116 | ||
117 | ret = lttng_create_ftrace_event(name, event); | |
118 | if (ret) | |
119 | goto error; | |
120 | ||
121 | event->u.ftrace.symbol_name = kstrdup(name, GFP_KERNEL); | |
122 | if (!event->u.ftrace.symbol_name) | |
123 | goto name_error; | |
124 | ||
16a9a591 MD |
125 | /* Ensure the memory we just allocated don't trigger page faults */ |
126 | wrapper_vmalloc_sync_all(); | |
127 | ||
5a9479dc | 128 | ret = wrapper_register_ftrace_function_probe(event->u.ftrace.symbol_name, |
e0a7a7c4 MD |
129 | <tng_ftrace_ops, event); |
130 | if (ret) | |
131 | goto register_error; | |
132 | return 0; | |
133 | ||
134 | register_error: | |
135 | kfree(event->u.ftrace.symbol_name); | |
136 | name_error: | |
137 | kfree(event->desc->name); | |
138 | kfree(event->desc); | |
139 | error: | |
140 | return ret; | |
141 | } | |
142 | EXPORT_SYMBOL_GPL(lttng_ftrace_register); | |
143 | ||
144 | void lttng_ftrace_unregister(struct ltt_event *event) | |
145 | { | |
5a9479dc | 146 | wrapper_unregister_ftrace_function_probe(event->u.ftrace.symbol_name, |
e0a7a7c4 MD |
147 | <tng_ftrace_ops, event); |
148 | kfree(event->u.ftrace.symbol_name); | |
25f53c39 | 149 | kfree(event->desc->fields); |
e0a7a7c4 MD |
150 | kfree(event->desc->name); |
151 | kfree(event->desc); | |
152 | } | |
153 | EXPORT_SYMBOL_GPL(lttng_ftrace_unregister); | |
154 | ||
e3de3dde MD |
155 | int lttng_ftrace_init(void) |
156 | { | |
16a9a591 | 157 | wrapper_vmalloc_sync_all(); |
e3de3dde MD |
158 | return 0; |
159 | } | |
160 | module_init(lttng_ftrace_init) | |
161 | ||
1695dc9a MD |
162 | /* |
163 | * Ftrace takes care of waiting for a grace period (RCU sched) at probe | |
164 | * unregistration, and disables preemption around probe call. | |
165 | */ | |
166 | void lttng_ftrace_exit(void) | |
167 | { | |
168 | } | |
169 | module_exit(lttng_ftrace_exit) | |
170 | ||
e0a7a7c4 MD |
171 | MODULE_LICENSE("GPL and additional rights"); |
172 | MODULE_AUTHOR("Mathieu Desnoyers"); | |
173 | MODULE_DESCRIPTION("Linux Trace Toolkit Ftrace Support"); |