Merge branch 'master' of ssh://git.lttng.org/home/git/lttng-modules
[lttng-modules.git] / probes / lttng-ftrace.c
CommitLineData
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).
4133fce7
MD
14 *
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.
e3de3dde
MD
18 */
19
e0a7a7c4
MD
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"
5a9479dc 25#include "../wrapper/ftrace.h"
16a9a591 26#include "../wrapper/vmalloc.h"
e0a7a7c4
MD
27#include "../ltt-tracer.h"
28
29static
5a9479dc 30void lttng_ftrace_handler(unsigned long ip, unsigned long parent_ip, void **data)
e0a7a7c4
MD
31{
32 struct ltt_event *event = *data;
33 struct ltt_channel *chan = event->chan;
34 struct lib_ring_buffer_ctx ctx;
35 struct {
36 unsigned long ip;
37 unsigned long parent_ip;
38 } payload;
39 int ret;
40
41 if (!ACCESS_ONCE(chan->session->active))
5a9479dc 42 return;
f1676205 43 lib_ring_buffer_ctx_init(&ctx, chan->chan, event,
e0a7a7c4 44 sizeof(payload), ltt_alignof(payload), -1);
4e1f08f4 45 ret = chan->ops->event_reserve(&ctx, event->id);
e0a7a7c4 46 if (ret < 0)
5a9479dc 47 return;
e0a7a7c4
MD
48 payload.ip = ip;
49 payload.parent_ip = parent_ip;
50 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(payload));
51 chan->ops->event_write(&ctx, &payload, sizeof(payload));
52 chan->ops->event_commit(&ctx);
5a9479dc 53 return;
e0a7a7c4
MD
54}
55
56/*
57 * Create event description
58 */
59static
60int lttng_create_ftrace_event(const char *name, struct ltt_event *event)
61{
62 struct lttng_event_field *fields;
63 struct lttng_event_desc *desc;
64 int ret;
65
66 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
67 if (!desc)
68 return -ENOMEM;
69 desc->name = kstrdup(name, GFP_KERNEL);
70 if (!desc->name) {
71 ret = -ENOMEM;
72 goto error_str;
73 }
74 desc->nr_fields = 2;
75 desc->fields = fields =
76 kzalloc(2 * sizeof(struct lttng_event_field), GFP_KERNEL);
0d1a681e
MD
77 if (!desc->fields) {
78 ret = -ENOMEM;
79 goto error_fields;
80 }
e0a7a7c4
MD
81 fields[0].name = "ip";
82 fields[0].type.atype = atype_integer;
ba1f5986
MD
83 fields[0].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
84 fields[0].type.u.basic.integer.alignment = ltt_alignof(unsigned long) * CHAR_BIT;
9e7e4892 85 fields[0].type.u.basic.integer.signedness = is_signed_type(unsigned long);
e0a7a7c4
MD
86 fields[0].type.u.basic.integer.reverse_byte_order = 0;
87 fields[0].type.u.basic.integer.base = 16;
88 fields[0].type.u.basic.integer.encoding = lttng_encode_none;
89
90 fields[1].name = "parent_ip";
91 fields[1].type.atype = atype_integer;
ba1f5986
MD
92 fields[1].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
93 fields[1].type.u.basic.integer.alignment = ltt_alignof(unsigned long) * CHAR_BIT;
9e7e4892 94 fields[1].type.u.basic.integer.signedness = is_signed_type(unsigned long);
e0a7a7c4
MD
95 fields[1].type.u.basic.integer.reverse_byte_order = 0;
96 fields[1].type.u.basic.integer.base = 16;
97 fields[1].type.u.basic.integer.encoding = lttng_encode_none;
98
dc7f600a 99 desc->owner = THIS_MODULE;
e0a7a7c4
MD
100 event->desc = desc;
101
102 return 0;
103
0d1a681e
MD
104error_fields:
105 kfree(desc->name);
e0a7a7c4
MD
106error_str:
107 kfree(desc);
108 return ret;
109}
110
111static
112struct ftrace_probe_ops lttng_ftrace_ops = {
113 .func = lttng_ftrace_handler,
114};
115
116int lttng_ftrace_register(const char *name,
117 const char *symbol_name,
118 struct ltt_event *event)
119{
120 int ret;
121
122 ret = lttng_create_ftrace_event(name, event);
123 if (ret)
124 goto error;
125
8a586098 126 event->u.ftrace.symbol_name = kstrdup(symbol_name, GFP_KERNEL);
e0a7a7c4
MD
127 if (!event->u.ftrace.symbol_name)
128 goto name_error;
129
16a9a591
MD
130 /* Ensure the memory we just allocated don't trigger page faults */
131 wrapper_vmalloc_sync_all();
132
5a9479dc 133 ret = wrapper_register_ftrace_function_probe(event->u.ftrace.symbol_name,
e0a7a7c4 134 &lttng_ftrace_ops, event);
8a586098 135 if (ret < 0)
e0a7a7c4
MD
136 goto register_error;
137 return 0;
138
139register_error:
140 kfree(event->u.ftrace.symbol_name);
141name_error:
142 kfree(event->desc->name);
143 kfree(event->desc);
144error:
145 return ret;
146}
147EXPORT_SYMBOL_GPL(lttng_ftrace_register);
148
149void lttng_ftrace_unregister(struct ltt_event *event)
150{
5a9479dc 151 wrapper_unregister_ftrace_function_probe(event->u.ftrace.symbol_name,
e0a7a7c4 152 &lttng_ftrace_ops, event);
edeb3137
MD
153}
154EXPORT_SYMBOL_GPL(lttng_ftrace_unregister);
155
156void lttng_ftrace_destroy_private(struct ltt_event *event)
157{
e0a7a7c4 158 kfree(event->u.ftrace.symbol_name);
25f53c39 159 kfree(event->desc->fields);
e0a7a7c4
MD
160 kfree(event->desc->name);
161 kfree(event->desc);
162}
edeb3137 163EXPORT_SYMBOL_GPL(lttng_ftrace_destroy_private);
e0a7a7c4 164
e3de3dde
MD
165int lttng_ftrace_init(void)
166{
16a9a591 167 wrapper_vmalloc_sync_all();
e3de3dde
MD
168 return 0;
169}
170module_init(lttng_ftrace_init)
171
1695dc9a
MD
172/*
173 * Ftrace takes care of waiting for a grace period (RCU sched) at probe
174 * unregistration, and disables preemption around probe call.
175 */
176void lttng_ftrace_exit(void)
177{
178}
179module_exit(lttng_ftrace_exit)
180
e0a7a7c4
MD
181MODULE_LICENSE("GPL and additional rights");
182MODULE_AUTHOR("Mathieu Desnoyers");
183MODULE_DESCRIPTION("Linux Trace Toolkit Ftrace Support");
This page took 0.0374 seconds and 4 git commands to generate.