2 * probes/lttng-ftrace.c
4 * LTTng function tracer integration module.
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
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).
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.
33 #include <linux/module.h>
34 #include <linux/ftrace.h>
35 #include <linux/slab.h>
36 #include <lttng-events.h>
37 #include <wrapper/ringbuffer/frontend_types.h>
38 #include <wrapper/ftrace.h>
39 #include <wrapper/vmalloc.h>
40 #include <lttng-tracer.h>
43 void lttng_ftrace_handler(unsigned long ip
, unsigned long parent_ip
, void **data
)
45 struct lttng_event
*event
= *data
;
46 struct lttng_probe_ctx lttng_probe_ctx
= {
48 .interruptible
= !irqs_disabled(),
50 struct lttng_channel
*chan
= event
->chan
;
51 struct lib_ring_buffer_ctx ctx
;
54 unsigned long parent_ip
;
58 if (unlikely(!ACCESS_ONCE(chan
->session
->active
)))
60 if (unlikely(!ACCESS_ONCE(chan
->enabled
)))
62 if (unlikely(!ACCESS_ONCE(event
->enabled
)))
65 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
,
66 sizeof(payload
), lttng_alignof(payload
), -1);
67 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
71 payload
.parent_ip
= parent_ip
;
72 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(payload
));
73 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
));
74 chan
->ops
->event_commit(&ctx
);
79 * Create event description
82 int lttng_create_ftrace_event(const char *name
, struct lttng_event
*event
)
84 struct lttng_event_field
*fields
;
85 struct lttng_event_desc
*desc
;
88 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
91 desc
->name
= kstrdup(name
, GFP_KERNEL
);
97 desc
->fields
= fields
=
98 kzalloc(2 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
103 fields
[0].name
= "ip";
104 fields
[0].type
.atype
= atype_integer
;
105 fields
[0].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
106 fields
[0].type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
107 fields
[0].type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
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
;
112 fields
[1].name
= "parent_ip";
113 fields
[1].type
.atype
= atype_integer
;
114 fields
[1].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
115 fields
[1].type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
116 fields
[1].type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
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
;
121 desc
->owner
= THIS_MODULE
;
134 struct ftrace_probe_ops lttng_ftrace_ops
= {
135 .func
= lttng_ftrace_handler
,
138 int lttng_ftrace_register(const char *name
,
139 const char *symbol_name
,
140 struct lttng_event
*event
)
144 ret
= lttng_create_ftrace_event(name
, event
);
148 event
->u
.ftrace
.symbol_name
= kstrdup(symbol_name
, GFP_KERNEL
);
149 if (!event
->u
.ftrace
.symbol_name
)
152 /* Ensure the memory we just allocated don't trigger page faults */
153 wrapper_vmalloc_sync_all();
155 ret
= wrapper_register_ftrace_function_probe(event
->u
.ftrace
.symbol_name
,
156 <tng_ftrace_ops
, event
);
162 kfree(event
->u
.ftrace
.symbol_name
);
164 kfree(event
->desc
->name
);
169 EXPORT_SYMBOL_GPL(lttng_ftrace_register
);
171 void lttng_ftrace_unregister(struct lttng_event
*event
)
173 wrapper_unregister_ftrace_function_probe(event
->u
.ftrace
.symbol_name
,
174 <tng_ftrace_ops
, event
);
176 EXPORT_SYMBOL_GPL(lttng_ftrace_unregister
);
178 void lttng_ftrace_destroy_private(struct lttng_event
*event
)
180 kfree(event
->u
.ftrace
.symbol_name
);
181 kfree(event
->desc
->fields
);
182 kfree(event
->desc
->name
);
185 EXPORT_SYMBOL_GPL(lttng_ftrace_destroy_private
);
187 int lttng_ftrace_init(void)
189 wrapper_vmalloc_sync_all();
192 module_init(lttng_ftrace_init
)
195 * Ftrace takes care of waiting for a grace period (RCU sched) at probe
196 * unregistration, and disables preemption around probe call.
198 void lttng_ftrace_exit(void)
201 module_exit(lttng_ftrace_exit
)
203 MODULE_LICENSE("GPL and additional rights");
204 MODULE_AUTHOR("Mathieu Desnoyers");
205 MODULE_DESCRIPTION("Linux Trace Toolkit Ftrace Support");
206 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
207 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
208 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
209 LTTNG_MODULES_EXTRAVERSION
);