Use system include paths in lttng-types.h
[deliverable/lttng-modules.git] / probes / lttng-ftrace.c
CommitLineData
e0a7a7c4 1/*
886d51a3 2 * probes/lttng-ftrace.c
e0a7a7c4
MD
3 *
4 * LTTng function tracer integration module.
5 *
886d51a3
MD
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e0a7a7c4
MD
21 */
22
e3de3dde
MD
23/*
24 * Ftrace function tracer does not seem to provide synchronization between probe
25 * teardown and callback execution. Therefore, we make this module permanently
26 * loaded (unloadable).
4133fce7
MD
27 *
28 * TODO: Move to register_ftrace_function() (which is exported for
29 * modules) for Linux >= 3.0. It is faster (only enables the selected
30 * functions), and will stay there.
e3de3dde
MD
31 */
32
e0a7a7c4
MD
33#include <linux/module.h>
34#include <linux/ftrace.h>
35#include <linux/slab.h>
a90917c3 36#include "../lttng-events.h"
e0a7a7c4 37#include "../wrapper/ringbuffer/frontend_types.h"
5a9479dc 38#include "../wrapper/ftrace.h"
16a9a591 39#include "../wrapper/vmalloc.h"
a90917c3 40#include "../lttng-tracer.h"
e0a7a7c4
MD
41
42static
5a9479dc 43void lttng_ftrace_handler(unsigned long ip, unsigned long parent_ip, void **data)
e0a7a7c4 44{
a90917c3 45 struct lttng_event *event = *data;
79150a49
JD
46 struct lttng_probe_ctx lttng_probe_ctx = {
47 .event = event,
ccecf3fb 48 .interruptible = !irqs_disabled(),
79150a49 49 };
a90917c3 50 struct lttng_channel *chan = event->chan;
e0a7a7c4
MD
51 struct lib_ring_buffer_ctx ctx;
52 struct {
53 unsigned long ip;
54 unsigned long parent_ip;
55 } payload;
56 int ret;
57
e64957da 58 if (unlikely(!ACCESS_ONCE(chan->session->active)))
5a9479dc 59 return;
e64957da
MD
60 if (unlikely(!ACCESS_ONCE(chan->enabled)))
61 return;
62 if (unlikely(!ACCESS_ONCE(event->enabled)))
63 return;
64
79150a49 65 lib_ring_buffer_ctx_init(&ctx, chan->chan, &lttng_probe_ctx,
a90917c3 66 sizeof(payload), lttng_alignof(payload), -1);
4e1f08f4 67 ret = chan->ops->event_reserve(&ctx, event->id);
e0a7a7c4 68 if (ret < 0)
5a9479dc 69 return;
e0a7a7c4
MD
70 payload.ip = ip;
71 payload.parent_ip = parent_ip;
a90917c3 72 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(payload));
e0a7a7c4
MD
73 chan->ops->event_write(&ctx, &payload, sizeof(payload));
74 chan->ops->event_commit(&ctx);
5a9479dc 75 return;
e0a7a7c4
MD
76}
77
78/*
79 * Create event description
80 */
81static
a90917c3 82int lttng_create_ftrace_event(const char *name, struct lttng_event *event)
e0a7a7c4
MD
83{
84 struct lttng_event_field *fields;
85 struct lttng_event_desc *desc;
86 int ret;
87
88 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
89 if (!desc)
90 return -ENOMEM;
91 desc->name = kstrdup(name, GFP_KERNEL);
92 if (!desc->name) {
93 ret = -ENOMEM;
94 goto error_str;
95 }
96 desc->nr_fields = 2;
97 desc->fields = fields =
98 kzalloc(2 * sizeof(struct lttng_event_field), GFP_KERNEL);
0d1a681e
MD
99 if (!desc->fields) {
100 ret = -ENOMEM;
101 goto error_fields;
102 }
e0a7a7c4
MD
103 fields[0].name = "ip";
104 fields[0].type.atype = atype_integer;
ba1f5986 105 fields[0].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
a90917c3 106 fields[0].type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
06254b0f 107 fields[0].type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long);
e0a7a7c4
MD
108 fields[0].type.u.basic.integer.reverse_byte_order = 0;
109 fields[0].type.u.basic.integer.base = 16;
110 fields[0].type.u.basic.integer.encoding = lttng_encode_none;
111
112 fields[1].name = "parent_ip";
113 fields[1].type.atype = atype_integer;
ba1f5986 114 fields[1].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
a90917c3 115 fields[1].type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
06254b0f 116 fields[1].type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long);
e0a7a7c4
MD
117 fields[1].type.u.basic.integer.reverse_byte_order = 0;
118 fields[1].type.u.basic.integer.base = 16;
119 fields[1].type.u.basic.integer.encoding = lttng_encode_none;
120
dc7f600a 121 desc->owner = THIS_MODULE;
e0a7a7c4
MD
122 event->desc = desc;
123
124 return 0;
125
0d1a681e
MD
126error_fields:
127 kfree(desc->name);
e0a7a7c4
MD
128error_str:
129 kfree(desc);
130 return ret;
131}
132
133static
134struct ftrace_probe_ops lttng_ftrace_ops = {
135 .func = lttng_ftrace_handler,
136};
137
138int lttng_ftrace_register(const char *name,
139 const char *symbol_name,
a90917c3 140 struct lttng_event *event)
e0a7a7c4
MD
141{
142 int ret;
143
144 ret = lttng_create_ftrace_event(name, event);
145 if (ret)
146 goto error;
147
8a586098 148 event->u.ftrace.symbol_name = kstrdup(symbol_name, GFP_KERNEL);
e0a7a7c4
MD
149 if (!event->u.ftrace.symbol_name)
150 goto name_error;
151
16a9a591
MD
152 /* Ensure the memory we just allocated don't trigger page faults */
153 wrapper_vmalloc_sync_all();
154
5a9479dc 155 ret = wrapper_register_ftrace_function_probe(event->u.ftrace.symbol_name,
e0a7a7c4 156 &lttng_ftrace_ops, event);
8a586098 157 if (ret < 0)
e0a7a7c4
MD
158 goto register_error;
159 return 0;
160
161register_error:
162 kfree(event->u.ftrace.symbol_name);
163name_error:
164 kfree(event->desc->name);
165 kfree(event->desc);
166error:
167 return ret;
168}
169EXPORT_SYMBOL_GPL(lttng_ftrace_register);
170
a90917c3 171void lttng_ftrace_unregister(struct lttng_event *event)
e0a7a7c4 172{
5a9479dc 173 wrapper_unregister_ftrace_function_probe(event->u.ftrace.symbol_name,
e0a7a7c4 174 &lttng_ftrace_ops, event);
edeb3137
MD
175}
176EXPORT_SYMBOL_GPL(lttng_ftrace_unregister);
177
a90917c3 178void lttng_ftrace_destroy_private(struct lttng_event *event)
edeb3137 179{
e0a7a7c4 180 kfree(event->u.ftrace.symbol_name);
25f53c39 181 kfree(event->desc->fields);
e0a7a7c4
MD
182 kfree(event->desc->name);
183 kfree(event->desc);
184}
edeb3137 185EXPORT_SYMBOL_GPL(lttng_ftrace_destroy_private);
e0a7a7c4 186
e3de3dde
MD
187int lttng_ftrace_init(void)
188{
16a9a591 189 wrapper_vmalloc_sync_all();
e3de3dde
MD
190 return 0;
191}
192module_init(lttng_ftrace_init)
193
1695dc9a
MD
194/*
195 * Ftrace takes care of waiting for a grace period (RCU sched) at probe
196 * unregistration, and disables preemption around probe call.
197 */
198void lttng_ftrace_exit(void)
199{
200}
201module_exit(lttng_ftrace_exit)
202
e0a7a7c4
MD
203MODULE_LICENSE("GPL and additional rights");
204MODULE_AUTHOR("Mathieu Desnoyers");
205MODULE_DESCRIPTION("Linux Trace Toolkit Ftrace Support");
13ab8b0a
MD
206MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
207 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
208 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
209 LTTNG_MODULES_EXTRAVERSION);
This page took 0.039808 seconds and 5 git commands to generate.