Add support for kvm x86 specific tracepoints
[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
MD
45 struct lttng_event *event = *data;
46 struct lttng_channel *chan = event->chan;
e0a7a7c4
MD
47 struct lib_ring_buffer_ctx ctx;
48 struct {
49 unsigned long ip;
50 unsigned long parent_ip;
51 } payload;
52 int ret;
53
e64957da 54 if (unlikely(!ACCESS_ONCE(chan->session->active)))
5a9479dc 55 return;
e64957da
MD
56 if (unlikely(!ACCESS_ONCE(chan->enabled)))
57 return;
58 if (unlikely(!ACCESS_ONCE(event->enabled)))
59 return;
60
f1676205 61 lib_ring_buffer_ctx_init(&ctx, chan->chan, event,
a90917c3 62 sizeof(payload), lttng_alignof(payload), -1);
4e1f08f4 63 ret = chan->ops->event_reserve(&ctx, event->id);
e0a7a7c4 64 if (ret < 0)
5a9479dc 65 return;
e0a7a7c4
MD
66 payload.ip = ip;
67 payload.parent_ip = parent_ip;
a90917c3 68 lib_ring_buffer_align_ctx(&ctx, lttng_alignof(payload));
e0a7a7c4
MD
69 chan->ops->event_write(&ctx, &payload, sizeof(payload));
70 chan->ops->event_commit(&ctx);
5a9479dc 71 return;
e0a7a7c4
MD
72}
73
74/*
75 * Create event description
76 */
77static
a90917c3 78int lttng_create_ftrace_event(const char *name, struct lttng_event *event)
e0a7a7c4
MD
79{
80 struct lttng_event_field *fields;
81 struct lttng_event_desc *desc;
82 int ret;
83
84 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
85 if (!desc)
86 return -ENOMEM;
87 desc->name = kstrdup(name, GFP_KERNEL);
88 if (!desc->name) {
89 ret = -ENOMEM;
90 goto error_str;
91 }
92 desc->nr_fields = 2;
93 desc->fields = fields =
94 kzalloc(2 * sizeof(struct lttng_event_field), GFP_KERNEL);
0d1a681e
MD
95 if (!desc->fields) {
96 ret = -ENOMEM;
97 goto error_fields;
98 }
e0a7a7c4
MD
99 fields[0].name = "ip";
100 fields[0].type.atype = atype_integer;
ba1f5986 101 fields[0].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
a90917c3 102 fields[0].type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
06254b0f 103 fields[0].type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long);
e0a7a7c4
MD
104 fields[0].type.u.basic.integer.reverse_byte_order = 0;
105 fields[0].type.u.basic.integer.base = 16;
106 fields[0].type.u.basic.integer.encoding = lttng_encode_none;
107
108 fields[1].name = "parent_ip";
109 fields[1].type.atype = atype_integer;
ba1f5986 110 fields[1].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
a90917c3 111 fields[1].type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
06254b0f 112 fields[1].type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long);
e0a7a7c4
MD
113 fields[1].type.u.basic.integer.reverse_byte_order = 0;
114 fields[1].type.u.basic.integer.base = 16;
115 fields[1].type.u.basic.integer.encoding = lttng_encode_none;
116
dc7f600a 117 desc->owner = THIS_MODULE;
e0a7a7c4
MD
118 event->desc = desc;
119
120 return 0;
121
0d1a681e
MD
122error_fields:
123 kfree(desc->name);
e0a7a7c4
MD
124error_str:
125 kfree(desc);
126 return ret;
127}
128
129static
130struct ftrace_probe_ops lttng_ftrace_ops = {
131 .func = lttng_ftrace_handler,
132};
133
134int lttng_ftrace_register(const char *name,
135 const char *symbol_name,
a90917c3 136 struct lttng_event *event)
e0a7a7c4
MD
137{
138 int ret;
139
140 ret = lttng_create_ftrace_event(name, event);
141 if (ret)
142 goto error;
143
8a586098 144 event->u.ftrace.symbol_name = kstrdup(symbol_name, GFP_KERNEL);
e0a7a7c4
MD
145 if (!event->u.ftrace.symbol_name)
146 goto name_error;
147
16a9a591
MD
148 /* Ensure the memory we just allocated don't trigger page faults */
149 wrapper_vmalloc_sync_all();
150
5a9479dc 151 ret = wrapper_register_ftrace_function_probe(event->u.ftrace.symbol_name,
e0a7a7c4 152 &lttng_ftrace_ops, event);
8a586098 153 if (ret < 0)
e0a7a7c4
MD
154 goto register_error;
155 return 0;
156
157register_error:
158 kfree(event->u.ftrace.symbol_name);
159name_error:
160 kfree(event->desc->name);
161 kfree(event->desc);
162error:
163 return ret;
164}
165EXPORT_SYMBOL_GPL(lttng_ftrace_register);
166
a90917c3 167void lttng_ftrace_unregister(struct lttng_event *event)
e0a7a7c4 168{
5a9479dc 169 wrapper_unregister_ftrace_function_probe(event->u.ftrace.symbol_name,
e0a7a7c4 170 &lttng_ftrace_ops, event);
edeb3137
MD
171}
172EXPORT_SYMBOL_GPL(lttng_ftrace_unregister);
173
a90917c3 174void lttng_ftrace_destroy_private(struct lttng_event *event)
edeb3137 175{
e0a7a7c4 176 kfree(event->u.ftrace.symbol_name);
25f53c39 177 kfree(event->desc->fields);
e0a7a7c4
MD
178 kfree(event->desc->name);
179 kfree(event->desc);
180}
edeb3137 181EXPORT_SYMBOL_GPL(lttng_ftrace_destroy_private);
e0a7a7c4 182
e3de3dde
MD
183int lttng_ftrace_init(void)
184{
16a9a591 185 wrapper_vmalloc_sync_all();
e3de3dde
MD
186 return 0;
187}
188module_init(lttng_ftrace_init)
189
1695dc9a
MD
190/*
191 * Ftrace takes care of waiting for a grace period (RCU sched) at probe
192 * unregistration, and disables preemption around probe call.
193 */
194void lttng_ftrace_exit(void)
195{
196}
197module_exit(lttng_ftrace_exit)
198
e0a7a7c4
MD
199MODULE_LICENSE("GPL and additional rights");
200MODULE_AUTHOR("Mathieu Desnoyers");
201MODULE_DESCRIPTION("Linux Trace Toolkit Ftrace Support");
This page took 0.03648 seconds and 4 git commands to generate.