Fix metadata header
[lttng-modules.git] / probes / lttng-ftrace.c
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
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).
14 */
15
16 #include <linux/module.h>
17 #include <linux/ftrace.h>
18 #include <linux/slab.h>
19 #include "../ltt-events.h"
20 #include "../wrapper/ringbuffer/frontend_types.h"
21 #include "../wrapper/ftrace.h"
22 #include "../wrapper/vmalloc.h"
23 #include "../ltt-tracer.h"
24
25 static
26 void lttng_ftrace_handler(unsigned long ip, unsigned long parent_ip, void **data)
27 {
28 struct ltt_event *event = *data;
29 struct ltt_channel *chan = event->chan;
30 struct lib_ring_buffer_ctx ctx;
31 struct {
32 unsigned long ip;
33 unsigned long parent_ip;
34 } payload;
35 int ret;
36
37 if (!ACCESS_ONCE(chan->session->active))
38 return;
39 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL,
40 sizeof(payload), ltt_alignof(payload), -1);
41 ret = chan->ops->event_reserve(&ctx);
42 if (ret < 0)
43 return;
44 payload.ip = ip;
45 payload.parent_ip = parent_ip;
46 lib_ring_buffer_align_ctx(&ctx, ltt_alignof(payload));
47 chan->ops->event_write(&ctx, &payload, sizeof(payload));
48 chan->ops->event_commit(&ctx);
49 return;
50 }
51
52 /*
53 * Create event description
54 */
55 static
56 int lttng_create_ftrace_event(const char *name, struct ltt_event *event)
57 {
58 struct lttng_event_field *fields;
59 struct lttng_event_desc *desc;
60 int ret;
61
62 desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
63 if (!desc)
64 return -ENOMEM;
65 desc->name = kstrdup(name, GFP_KERNEL);
66 if (!desc->name) {
67 ret = -ENOMEM;
68 goto error_str;
69 }
70 desc->nr_fields = 2;
71 desc->fields = fields =
72 kzalloc(2 * sizeof(struct lttng_event_field), GFP_KERNEL);
73 if (!desc->fields) {
74 ret = -ENOMEM;
75 goto error_fields;
76 }
77 fields[0].name = "ip";
78 fields[0].type.atype = atype_integer;
79 fields[0].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
80 fields[0].type.u.basic.integer.alignment = ltt_alignof(unsigned long) * CHAR_BIT;
81 fields[0].type.u.basic.integer.signedness = 0;
82 fields[0].type.u.basic.integer.reverse_byte_order = 0;
83 fields[0].type.u.basic.integer.base = 16;
84 fields[0].type.u.basic.integer.encoding = lttng_encode_none;
85
86 fields[1].name = "parent_ip";
87 fields[1].type.atype = atype_integer;
88 fields[1].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
89 fields[1].type.u.basic.integer.alignment = ltt_alignof(unsigned long) * CHAR_BIT;
90 fields[1].type.u.basic.integer.signedness = 0;
91 fields[1].type.u.basic.integer.reverse_byte_order = 0;
92 fields[1].type.u.basic.integer.base = 16;
93 fields[1].type.u.basic.integer.encoding = lttng_encode_none;
94
95 event->desc = desc;
96
97 return 0;
98
99 error_fields:
100 kfree(desc->name);
101 error_str:
102 kfree(desc);
103 return ret;
104 }
105
106 static
107 struct ftrace_probe_ops lttng_ftrace_ops = {
108 .func = lttng_ftrace_handler,
109 };
110
111 int lttng_ftrace_register(const char *name,
112 const char *symbol_name,
113 struct ltt_event *event)
114 {
115 int ret;
116
117 ret = lttng_create_ftrace_event(name, event);
118 if (ret)
119 goto error;
120
121 event->u.ftrace.symbol_name = kstrdup(name, GFP_KERNEL);
122 if (!event->u.ftrace.symbol_name)
123 goto name_error;
124
125 /* Ensure the memory we just allocated don't trigger page faults */
126 wrapper_vmalloc_sync_all();
127
128 ret = wrapper_register_ftrace_function_probe(event->u.ftrace.symbol_name,
129 &lttng_ftrace_ops, event);
130 if (ret)
131 goto register_error;
132 return 0;
133
134 register_error:
135 kfree(event->u.ftrace.symbol_name);
136 name_error:
137 kfree(event->desc->name);
138 kfree(event->desc);
139 error:
140 return ret;
141 }
142 EXPORT_SYMBOL_GPL(lttng_ftrace_register);
143
144 void lttng_ftrace_unregister(struct ltt_event *event)
145 {
146 wrapper_unregister_ftrace_function_probe(event->u.ftrace.symbol_name,
147 &lttng_ftrace_ops, event);
148 kfree(event->u.ftrace.symbol_name);
149 kfree(event->desc->fields);
150 kfree(event->desc->name);
151 kfree(event->desc);
152 }
153 EXPORT_SYMBOL_GPL(lttng_ftrace_unregister);
154
155 /* This module is permanent. */
156 int lttng_ftrace_init(void)
157 {
158 wrapper_vmalloc_sync_all();
159 return 0;
160 }
161 module_init(lttng_ftrace_init)
162
163 MODULE_LICENSE("GPL and additional rights");
164 MODULE_AUTHOR("Mathieu Desnoyers");
165 MODULE_DESCRIPTION("Linux Trace Toolkit Ftrace Support");
This page took 0.035376 seconds and 5 git commands to generate.