1 /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
3 * probes/lttng-ftrace.c
5 * LTTng function tracer integration module.
7 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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).
15 * TODO: Move to register_ftrace_function() (which is exported for
16 * modules) for Linux >= 3.0. It is faster (only enables the selected
17 * functions), and will stay there.
20 #include <linux/module.h>
21 #include <linux/ftrace.h>
22 #include <linux/slab.h>
23 #include <lttng-events.h>
24 #include <wrapper/ringbuffer/frontend_types.h>
25 #include <wrapper/ftrace.h>
26 #include <wrapper/vmalloc.h>
27 #include <lttng-tracer.h>
29 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,12,0))
31 void lttng_ftrace_handler(unsigned long ip
, unsigned long parent_ip
,
32 struct trace_array
*tr
, struct ftrace_probe_ops
*ops
,
35 struct lttng_event
*event
= data
;
36 struct lttng_probe_ctx lttng_probe_ctx
= {
38 .interruptible
= !irqs_disabled(),
40 struct lttng_channel
*chan
= event
->chan
;
41 struct lib_ring_buffer_ctx ctx
;
44 unsigned long parent_ip
;
48 if (unlikely(!READ_ONCE(chan
->session
->active
)))
50 if (unlikely(!READ_ONCE(chan
->enabled
)))
52 if (unlikely(!READ_ONCE(event
->enabled
)))
55 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
,
56 sizeof(payload
), lttng_alignof(payload
), -1);
57 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
61 payload
.parent_ip
= parent_ip
;
62 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(payload
));
63 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
));
64 chan
->ops
->event_commit(&ctx
);
69 void lttng_ftrace_handler(unsigned long ip
, unsigned long parent_ip
, void **data
)
71 struct lttng_event
*event
= *data
;
72 struct lttng_probe_ctx lttng_probe_ctx
= {
74 .interruptible
= !irqs_disabled(),
76 struct lttng_channel
*chan
= event
->chan
;
77 struct lib_ring_buffer_ctx ctx
;
80 unsigned long parent_ip
;
84 if (unlikely(!READ_ONCE(chan
->session
->active
)))
86 if (unlikely(!READ_ONCE(chan
->enabled
)))
88 if (unlikely(!READ_ONCE(event
->enabled
)))
91 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, <tng_probe_ctx
,
92 sizeof(payload
), lttng_alignof(payload
), -1);
93 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
97 payload
.parent_ip
= parent_ip
;
98 lib_ring_buffer_align_ctx(&ctx
, lttng_alignof(payload
));
99 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
));
100 chan
->ops
->event_commit(&ctx
);
106 * Create event description
109 int lttng_create_ftrace_event(const char *name
, struct lttng_event
*event
)
111 struct lttng_event_field
*fields
;
112 struct lttng_event_desc
*desc
;
115 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
118 desc
->name
= kstrdup(name
, GFP_KERNEL
);
124 desc
->fields
= fields
=
125 kzalloc(2 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
130 fields
[0].name
= "ip";
131 fields
[0].type
.atype
= atype_integer
;
132 fields
[0].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
133 fields
[0].type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
134 fields
[0].type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
135 fields
[0].type
.u
.basic
.integer
.reverse_byte_order
= 0;
136 fields
[0].type
.u
.basic
.integer
.base
= 16;
137 fields
[0].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
139 fields
[1].name
= "parent_ip";
140 fields
[1].type
.atype
= atype_integer
;
141 fields
[1].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
142 fields
[1].type
.u
.basic
.integer
.alignment
= lttng_alignof(unsigned long) * CHAR_BIT
;
143 fields
[1].type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(unsigned long);
144 fields
[1].type
.u
.basic
.integer
.reverse_byte_order
= 0;
145 fields
[1].type
.u
.basic
.integer
.base
= 16;
146 fields
[1].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
148 desc
->owner
= THIS_MODULE
;
161 struct ftrace_probe_ops lttng_ftrace_ops
= {
162 .func
= lttng_ftrace_handler
,
165 int lttng_ftrace_register(const char *name
,
166 const char *symbol_name
,
167 struct lttng_event
*event
)
171 ret
= lttng_create_ftrace_event(name
, event
);
175 event
->u
.ftrace
.symbol_name
= kstrdup(symbol_name
, GFP_KERNEL
);
176 if (!event
->u
.ftrace
.symbol_name
)
179 /* Ensure the memory we just allocated don't trigger page faults */
180 wrapper_vmalloc_sync_all();
182 ret
= wrapper_register_ftrace_function_probe(event
->u
.ftrace
.symbol_name
,
183 <tng_ftrace_ops
, event
);
189 kfree(event
->u
.ftrace
.symbol_name
);
191 kfree(event
->desc
->name
);
196 EXPORT_SYMBOL_GPL(lttng_ftrace_register
);
198 void lttng_ftrace_unregister(struct lttng_event
*event
)
200 wrapper_unregister_ftrace_function_probe(event
->u
.ftrace
.symbol_name
,
201 <tng_ftrace_ops
, event
);
203 EXPORT_SYMBOL_GPL(lttng_ftrace_unregister
);
205 void lttng_ftrace_destroy_private(struct lttng_event
*event
)
207 kfree(event
->u
.ftrace
.symbol_name
);
208 kfree(event
->desc
->fields
);
209 kfree(event
->desc
->name
);
212 EXPORT_SYMBOL_GPL(lttng_ftrace_destroy_private
);
214 int lttng_ftrace_init(void)
216 wrapper_vmalloc_sync_all();
219 module_init(lttng_ftrace_init
)
222 * Ftrace takes care of waiting for a grace period (RCU sched) at probe
223 * unregistration, and disables preemption around probe call.
225 void lttng_ftrace_exit(void)
228 module_exit(lttng_ftrace_exit
)
230 MODULE_LICENSE("GPL and additional rights");
231 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
232 MODULE_DESCRIPTION("LTTng ftrace probes");
233 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION
) "."
234 __stringify(LTTNG_MODULES_MINOR_VERSION
) "."
235 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION
)
236 LTTNG_MODULES_EXTRAVERSION
);