2 * (C) Copyright 2009-2011 -
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * LTTng function tracer integration module.
7 * Dual LGPL v2.1/GPL v2 license.
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 "../ltt-events.h"
24 #include "../wrapper/ringbuffer/frontend_types.h"
25 #include "../wrapper/ftrace.h"
26 #include "../wrapper/vmalloc.h"
27 #include "../ltt-tracer.h"
30 void lttng_ftrace_handler(unsigned long ip
, unsigned long parent_ip
, void **data
)
32 struct ltt_event
*event
= *data
;
33 struct ltt_channel
*chan
= event
->chan
;
34 struct lib_ring_buffer_ctx ctx
;
37 unsigned long parent_ip
;
41 if (unlikely(!ACCESS_ONCE(chan
->session
->active
)))
43 if (unlikely(!ACCESS_ONCE(chan
->enabled
)))
45 if (unlikely(!ACCESS_ONCE(event
->enabled
)))
48 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, event
,
49 sizeof(payload
), ltt_alignof(payload
), -1);
50 ret
= chan
->ops
->event_reserve(&ctx
, event
->id
);
54 payload
.parent_ip
= parent_ip
;
55 lib_ring_buffer_align_ctx(&ctx
, ltt_alignof(payload
));
56 chan
->ops
->event_write(&ctx
, &payload
, sizeof(payload
));
57 chan
->ops
->event_commit(&ctx
);
62 * Create event description
65 int lttng_create_ftrace_event(const char *name
, struct ltt_event
*event
)
67 struct lttng_event_field
*fields
;
68 struct lttng_event_desc
*desc
;
71 desc
= kzalloc(sizeof(*event
->desc
), GFP_KERNEL
);
74 desc
->name
= kstrdup(name
, GFP_KERNEL
);
80 desc
->fields
= fields
=
81 kzalloc(2 * sizeof(struct lttng_event_field
), GFP_KERNEL
);
86 fields
[0].name
= "ip";
87 fields
[0].type
.atype
= atype_integer
;
88 fields
[0].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
89 fields
[0].type
.u
.basic
.integer
.alignment
= ltt_alignof(unsigned long) * CHAR_BIT
;
90 fields
[0].type
.u
.basic
.integer
.signedness
= is_signed_type(unsigned long);
91 fields
[0].type
.u
.basic
.integer
.reverse_byte_order
= 0;
92 fields
[0].type
.u
.basic
.integer
.base
= 16;
93 fields
[0].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
95 fields
[1].name
= "parent_ip";
96 fields
[1].type
.atype
= atype_integer
;
97 fields
[1].type
.u
.basic
.integer
.size
= sizeof(unsigned long) * CHAR_BIT
;
98 fields
[1].type
.u
.basic
.integer
.alignment
= ltt_alignof(unsigned long) * CHAR_BIT
;
99 fields
[1].type
.u
.basic
.integer
.signedness
= is_signed_type(unsigned long);
100 fields
[1].type
.u
.basic
.integer
.reverse_byte_order
= 0;
101 fields
[1].type
.u
.basic
.integer
.base
= 16;
102 fields
[1].type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
104 desc
->owner
= THIS_MODULE
;
117 struct ftrace_probe_ops lttng_ftrace_ops
= {
118 .func
= lttng_ftrace_handler
,
121 int lttng_ftrace_register(const char *name
,
122 const char *symbol_name
,
123 struct ltt_event
*event
)
127 ret
= lttng_create_ftrace_event(name
, event
);
131 event
->u
.ftrace
.symbol_name
= kstrdup(symbol_name
, GFP_KERNEL
);
132 if (!event
->u
.ftrace
.symbol_name
)
135 /* Ensure the memory we just allocated don't trigger page faults */
136 wrapper_vmalloc_sync_all();
138 ret
= wrapper_register_ftrace_function_probe(event
->u
.ftrace
.symbol_name
,
139 <tng_ftrace_ops
, event
);
145 kfree(event
->u
.ftrace
.symbol_name
);
147 kfree(event
->desc
->name
);
152 EXPORT_SYMBOL_GPL(lttng_ftrace_register
);
154 void lttng_ftrace_unregister(struct ltt_event
*event
)
156 wrapper_unregister_ftrace_function_probe(event
->u
.ftrace
.symbol_name
,
157 <tng_ftrace_ops
, event
);
159 EXPORT_SYMBOL_GPL(lttng_ftrace_unregister
);
161 void lttng_ftrace_destroy_private(struct ltt_event
*event
)
163 kfree(event
->u
.ftrace
.symbol_name
);
164 kfree(event
->desc
->fields
);
165 kfree(event
->desc
->name
);
168 EXPORT_SYMBOL_GPL(lttng_ftrace_destroy_private
);
170 int lttng_ftrace_init(void)
172 wrapper_vmalloc_sync_all();
175 module_init(lttng_ftrace_init
)
178 * Ftrace takes care of waiting for a grace period (RCU sched) at probe
179 * unregistration, and disables preemption around probe call.
181 void lttng_ftrace_exit(void)
184 module_exit(lttng_ftrace_exit
)
186 MODULE_LICENSE("GPL and additional rights");
187 MODULE_AUTHOR("Mathieu Desnoyers");
188 MODULE_DESCRIPTION("Linux Trace Toolkit Ftrace Support");