4 * LTTng syscall probes.
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/compat.h>
26 #include <asm/ptrace.h>
27 #include <asm/syscall.h>
29 #include "lttng-events.h"
32 # ifndef is_compat_task
33 # define is_compat_task() (0)
38 void syscall_entry_probe(void *__data
, struct pt_regs
*regs
, long id
);
41 * Take care of NOARGS not supported by mainline.
43 #define DECLARE_EVENT_CLASS_NOARGS(name, tstruct, assign, print)
44 #define DEFINE_EVENT_NOARGS(template, name)
45 #define TRACE_EVENT_NOARGS(name, struct, assign, print)
48 * Create LTTng tracepoint probes.
50 #define LTTNG_PACKAGE_BUILD
51 #define CREATE_TRACE_POINTS
52 #define TP_MODULE_NOINIT
53 #define TRACE_INCLUDE_PATH ../instrumentation/syscalls/headers
55 #define PARAMS(args...) args
57 /* Hijack probe callback for system calls */
59 #define TP_PROBE_CB(_template) &syscall_entry_probe
60 #define SC_TRACE_EVENT(_name, _proto, _args, _struct, _assign, _printk) \
61 TRACE_EVENT(_name, PARAMS(_proto), PARAMS(_args),\
62 PARAMS(_struct), PARAMS(_assign), PARAMS(_printk))
63 #define SC_DECLARE_EVENT_CLASS_NOARGS(_name, _struct, _assign, _printk) \
64 DECLARE_EVENT_CLASS_NOARGS(_name, PARAMS(_struct), PARAMS(_assign),\
66 #define SC_DEFINE_EVENT_NOARGS(_template, _name) \
67 DEFINE_EVENT_NOARGS(_template, _name)
69 #define TRACE_SYSTEM syscalls_integers
70 #include "instrumentation/syscalls/headers/syscalls_integers.h"
72 #define TRACE_SYSTEM syscalls_pointers
73 #include "instrumentation/syscalls/headers/syscalls_pointers.h"
76 #undef SC_DECLARE_EVENT_CLASS_NOARGS
77 #undef SC_DEFINE_EVENT_NOARGS
79 #define TRACE_SYSTEM syscalls_unknown
80 #include "instrumentation/syscalls/headers/syscalls_unknown.h"
83 /* For compat syscalls */
84 #undef _TRACE_SYSCALLS_integers_H
85 #undef _TRACE_SYSCALLS_pointers_H
87 /* Hijack probe callback for system calls */
89 #define TP_PROBE_CB(_template) &syscall_entry_probe
90 #define SC_TRACE_EVENT(_name, _proto, _args, _struct, _assign, _printk) \
91 TRACE_EVENT(compat_##_name, PARAMS(_proto), PARAMS(_args), \
92 PARAMS(_struct), PARAMS(_assign), \
94 #define SC_DECLARE_EVENT_CLASS_NOARGS(_name, _struct, _assign, _printk) \
95 DECLARE_EVENT_CLASS_NOARGS(compat_##_name, PARAMS(_struct), \
96 PARAMS(_assign), PARAMS(_printk))
97 #define SC_DEFINE_EVENT_NOARGS(_template, _name) \
98 DEFINE_EVENT_NOARGS(compat_##_template, compat_##_name)
99 #define TRACE_SYSTEM compat_syscalls_integers
100 #include "instrumentation/syscalls/headers/compat_syscalls_integers.h"
102 #define TRACE_SYSTEM compat_syscalls_pointers
103 #include "instrumentation/syscalls/headers/compat_syscalls_pointers.h"
105 #undef SC_TRACE_EVENT
106 #undef SC_DECLARE_EVENT_CLASS_NOARGS
107 #undef SC_DEFINE_EVENT_NOARGS
110 #undef TP_MODULE_NOINIT
111 #undef LTTNG_PACKAGE_BUILD
112 #undef CREATE_TRACE_POINTS
114 struct trace_syscall_entry
{
116 const struct lttng_event_desc
*desc
;
117 const struct lttng_event_field
*fields
;
121 #define CREATE_SYSCALL_TABLE
123 #undef TRACE_SYSCALL_TABLE
124 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
126 .func = __event_probe__##_template, \
127 .nrargs = (_nrargs), \
128 .fields = __event_fields___##_template, \
129 .desc = &__event_desc___##_name, \
132 static const struct trace_syscall_entry sc_table
[] = {
133 #include "instrumentation/syscalls/headers/syscalls_integers.h"
134 #include "instrumentation/syscalls/headers/syscalls_pointers.h"
137 #undef TRACE_SYSCALL_TABLE
138 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
140 .func = __event_probe__##compat_##_template, \
141 .nrargs = (_nrargs), \
142 .fields = __event_fields___##compat_##_template,\
143 .desc = &__event_desc___##compat_##_name, \
146 /* Create compatibility syscall table */
147 const struct trace_syscall_entry compat_sc_table
[] = {
148 #include "instrumentation/syscalls/headers/compat_syscalls_integers.h"
149 #include "instrumentation/syscalls/headers/compat_syscalls_pointers.h"
152 #undef CREATE_SYSCALL_TABLE
154 static void syscall_entry_unknown(struct lttng_event
*event
,
155 struct pt_regs
*regs
, unsigned int id
)
157 unsigned long args
[UNKNOWN_SYSCALL_NRARGS
];
159 syscall_get_arguments(current
, regs
, 0, UNKNOWN_SYSCALL_NRARGS
, args
);
160 if (unlikely(is_compat_task()))
161 __event_probe__compat_sys_unknown(event
, id
, args
);
163 __event_probe__sys_unknown(event
, id
, args
);
166 void syscall_entry_probe(void *__data
, struct pt_regs
*regs
, long id
)
168 struct lttng_channel
*chan
= __data
;
169 struct lttng_event
*event
, *unknown_event
;
170 const struct trace_syscall_entry
*table
, *entry
;
173 if (unlikely(is_compat_task())) {
174 table
= compat_sc_table
;
175 table_len
= ARRAY_SIZE(compat_sc_table
);
176 unknown_event
= chan
->sc_compat_unknown
;
179 table_len
= ARRAY_SIZE(sc_table
);
180 unknown_event
= chan
->sc_unknown
;
182 if (unlikely(id
>= table_len
)) {
183 syscall_entry_unknown(unknown_event
, regs
, id
);
186 if (unlikely(is_compat_task()))
187 event
= chan
->compat_sc_table
[id
];
189 event
= chan
->sc_table
[id
];
190 if (unlikely(!event
)) {
191 syscall_entry_unknown(unknown_event
, regs
, id
);
195 WARN_ON_ONCE(!entry
);
197 switch (entry
->nrargs
) {
200 void (*fptr
)(void *__data
) = entry
->func
;
207 void (*fptr
)(void *__data
, unsigned long arg0
) = entry
->func
;
208 unsigned long args
[1];
210 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
211 fptr(event
, args
[0]);
216 void (*fptr
)(void *__data
,
218 unsigned long arg1
) = entry
->func
;
219 unsigned long args
[2];
221 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
222 fptr(event
, args
[0], args
[1]);
227 void (*fptr
)(void *__data
,
230 unsigned long arg2
) = entry
->func
;
231 unsigned long args
[3];
233 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
234 fptr(event
, args
[0], args
[1], args
[2]);
239 void (*fptr
)(void *__data
,
243 unsigned long arg3
) = entry
->func
;
244 unsigned long args
[4];
246 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
247 fptr(event
, args
[0], args
[1], args
[2], args
[3]);
252 void (*fptr
)(void *__data
,
257 unsigned long arg4
) = entry
->func
;
258 unsigned long args
[5];
260 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
261 fptr(event
, args
[0], args
[1], args
[2], args
[3], args
[4]);
266 void (*fptr
)(void *__data
,
272 unsigned long arg5
) = entry
->func
;
273 unsigned long args
[6];
275 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
276 fptr(event
, args
[0], args
[1], args
[2],
277 args
[3], args
[4], args
[5]);
285 /* noinline to diminish caller stack size */
287 int fill_table(const struct trace_syscall_entry
*table
, size_t table_len
,
288 struct lttng_event
**chan_table
, struct lttng_channel
*chan
, void *filter
)
290 const struct lttng_event_desc
*desc
;
293 /* Allocate events for each syscall, insert into table */
294 for (i
= 0; i
< table_len
; i
++) {
295 struct lttng_kernel_event ev
;
296 desc
= table
[i
].desc
;
299 /* Unknown syscall */
303 * Skip those already populated by previous failed
304 * register for this channel.
308 memset(&ev
, 0, sizeof(ev
));
309 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
310 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
311 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
312 chan_table
[i
] = lttng_event_create(chan
, &ev
, filter
,
314 if (!chan_table
[i
]) {
316 * If something goes wrong in event registration
317 * after the first one, we have no choice but to
318 * leave the previous events in there, until
319 * deleted by session teardown.
327 int lttng_syscalls_register(struct lttng_channel
*chan
, void *filter
)
329 struct lttng_kernel_event ev
;
332 wrapper_vmalloc_sync_all();
334 if (!chan
->sc_table
) {
335 /* create syscall table mapping syscall to events */
336 chan
->sc_table
= kzalloc(sizeof(struct lttng_event
*)
337 * ARRAY_SIZE(sc_table
), GFP_KERNEL
);
343 if (!chan
->compat_sc_table
) {
344 /* create syscall table mapping compat syscall to events */
345 chan
->compat_sc_table
= kzalloc(sizeof(struct lttng_event
*)
346 * ARRAY_SIZE(compat_sc_table
), GFP_KERNEL
);
347 if (!chan
->compat_sc_table
)
351 if (!chan
->sc_unknown
) {
352 const struct lttng_event_desc
*desc
=
353 &__event_desc___sys_unknown
;
355 memset(&ev
, 0, sizeof(ev
));
356 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
357 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
358 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
359 chan
->sc_unknown
= lttng_event_create(chan
, &ev
, filter
,
361 if (!chan
->sc_unknown
) {
366 if (!chan
->sc_compat_unknown
) {
367 const struct lttng_event_desc
*desc
=
368 &__event_desc___compat_sys_unknown
;
370 memset(&ev
, 0, sizeof(ev
));
371 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
372 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
373 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
374 chan
->sc_compat_unknown
= lttng_event_create(chan
, &ev
, filter
,
376 if (!chan
->sc_compat_unknown
) {
381 if (!chan
->sc_exit
) {
382 const struct lttng_event_desc
*desc
=
383 &__event_desc___exit_syscall
;
385 memset(&ev
, 0, sizeof(ev
));
386 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
387 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
388 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
389 chan
->sc_exit
= lttng_event_create(chan
, &ev
, filter
,
391 if (!chan
->sc_exit
) {
396 ret
= fill_table(sc_table
, ARRAY_SIZE(sc_table
),
397 chan
->sc_table
, chan
, filter
);
401 ret
= fill_table(compat_sc_table
, ARRAY_SIZE(compat_sc_table
),
402 chan
->compat_sc_table
, chan
, filter
);
406 ret
= tracepoint_probe_register("sys_enter",
407 (void *) syscall_entry_probe
, chan
);
411 * We change the name of sys_exit tracepoint due to namespace
412 * conflict with sys_exit syscall entry.
414 ret
= tracepoint_probe_register("sys_exit",
415 (void *) __event_probe__exit_syscall
,
418 WARN_ON_ONCE(tracepoint_probe_unregister("sys_enter",
419 (void *) syscall_entry_probe
, chan
));
425 * Only called at session destruction.
427 int lttng_syscalls_unregister(struct lttng_channel
*chan
)
433 ret
= tracepoint_probe_unregister("sys_exit",
434 (void *) __event_probe__exit_syscall
,
438 ret
= tracepoint_probe_unregister("sys_enter",
439 (void *) syscall_entry_probe
, chan
);
442 /* lttng_event destroy will be performed by lttng_session_destroy() */
443 kfree(chan
->sc_table
);
445 kfree(chan
->compat_sc_table
);