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 <linux/err.h>
27 #include <asm/ptrace.h>
28 #include <asm/syscall.h>
30 #include "wrapper/tracepoint.h"
31 #include "lttng-events.h"
34 # ifndef is_compat_task
35 # define is_compat_task() (0)
40 void syscall_entry_probe(void *__data
, struct pt_regs
*regs
, long id
);
43 * Forward declarations for old kernels.
47 struct oldold_utsname
;
49 struct sel_arg_struct
;
50 struct mmap_arg_struct
;
54 * Take care of NOARGS not supported by mainline.
56 #define DECLARE_EVENT_CLASS_NOARGS(name, tstruct, assign, print)
57 #define DEFINE_EVENT_NOARGS(template, name)
58 #define TRACE_EVENT_NOARGS(name, struct, assign, print)
61 * Create LTTng tracepoint probes.
63 #define LTTNG_PACKAGE_BUILD
64 #define CREATE_TRACE_POINTS
65 #define TP_MODULE_NOINIT
66 #define TRACE_INCLUDE_PATH ../instrumentation/syscalls/headers
68 #define PARAMS(args...) args
70 /* Hijack probe callback for system calls */
72 #define TP_PROBE_CB(_template) &syscall_entry_probe
73 #define SC_TRACE_EVENT(_name, _proto, _args, _struct, _assign, _printk) \
74 TRACE_EVENT(_name, PARAMS(_proto), PARAMS(_args),\
75 PARAMS(_struct), PARAMS(_assign), PARAMS(_printk))
76 #define SC_DECLARE_EVENT_CLASS_NOARGS(_name, _struct, _assign, _printk) \
77 DECLARE_EVENT_CLASS_NOARGS(_name, PARAMS(_struct), PARAMS(_assign),\
79 #define SC_DEFINE_EVENT_NOARGS(_template, _name) \
80 DEFINE_EVENT_NOARGS(_template, _name)
82 #define TRACE_SYSTEM syscalls_integers
83 #include "instrumentation/syscalls/headers/syscalls_integers.h"
85 #define TRACE_SYSTEM syscalls_pointers
86 #include "instrumentation/syscalls/headers/syscalls_pointers.h"
89 #undef SC_DECLARE_EVENT_CLASS_NOARGS
90 #undef SC_DEFINE_EVENT_NOARGS
92 #define TRACE_SYSTEM syscalls_unknown
93 #include "instrumentation/syscalls/headers/syscalls_unknown.h"
96 /* For compat syscalls */
97 #undef _TRACE_SYSCALLS_integers_H
98 #undef _TRACE_SYSCALLS_pointers_H
100 /* Hijack probe callback for system calls */
102 #define TP_PROBE_CB(_template) &syscall_entry_probe
103 #define SC_TRACE_EVENT(_name, _proto, _args, _struct, _assign, _printk) \
104 TRACE_EVENT(compat_##_name, PARAMS(_proto), PARAMS(_args), \
105 PARAMS(_struct), PARAMS(_assign), \
107 #define SC_DECLARE_EVENT_CLASS_NOARGS(_name, _struct, _assign, _printk) \
108 DECLARE_EVENT_CLASS_NOARGS(compat_##_name, PARAMS(_struct), \
109 PARAMS(_assign), PARAMS(_printk))
110 #define SC_DEFINE_EVENT_NOARGS(_template, _name) \
111 DEFINE_EVENT_NOARGS(compat_##_template, compat_##_name)
112 #define TRACE_SYSTEM compat_syscalls_integers
113 #include "instrumentation/syscalls/headers/compat_syscalls_integers.h"
115 #define TRACE_SYSTEM compat_syscalls_pointers
116 #include "instrumentation/syscalls/headers/compat_syscalls_pointers.h"
118 #undef SC_TRACE_EVENT
119 #undef SC_DECLARE_EVENT_CLASS_NOARGS
120 #undef SC_DEFINE_EVENT_NOARGS
123 #undef TP_MODULE_NOINIT
124 #undef LTTNG_PACKAGE_BUILD
125 #undef CREATE_TRACE_POINTS
127 struct trace_syscall_entry
{
129 const struct lttng_event_desc
*desc
;
130 const struct lttng_event_field
*fields
;
134 #define CREATE_SYSCALL_TABLE
136 #undef TRACE_SYSCALL_TABLE
137 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
139 .func = __event_probe__##_template, \
140 .nrargs = (_nrargs), \
141 .fields = __event_fields___##_template, \
142 .desc = &__event_desc___##_name, \
145 static const struct trace_syscall_entry sc_table
[] = {
146 #include "instrumentation/syscalls/headers/syscalls_integers.h"
147 #include "instrumentation/syscalls/headers/syscalls_pointers.h"
150 #undef TRACE_SYSCALL_TABLE
151 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
153 .func = __event_probe__##compat_##_template, \
154 .nrargs = (_nrargs), \
155 .fields = __event_fields___##compat_##_template,\
156 .desc = &__event_desc___##compat_##_name, \
159 /* Create compatibility syscall table */
160 const struct trace_syscall_entry compat_sc_table
[] = {
161 #include "instrumentation/syscalls/headers/compat_syscalls_integers.h"
162 #include "instrumentation/syscalls/headers/compat_syscalls_pointers.h"
165 #undef CREATE_SYSCALL_TABLE
167 static void syscall_entry_unknown(struct lttng_event
*event
,
168 struct pt_regs
*regs
, unsigned int id
)
170 unsigned long args
[UNKNOWN_SYSCALL_NRARGS
];
172 syscall_get_arguments(current
, regs
, 0, UNKNOWN_SYSCALL_NRARGS
, args
);
173 if (unlikely(is_compat_task()))
174 __event_probe__compat_sys_unknown(event
, id
, args
);
176 __event_probe__sys_unknown(event
, id
, args
);
179 void syscall_entry_probe(void *__data
, struct pt_regs
*regs
, long id
)
181 struct lttng_channel
*chan
= __data
;
182 struct lttng_event
*event
, *unknown_event
;
183 const struct trace_syscall_entry
*table
, *entry
;
186 if (unlikely(is_compat_task())) {
187 table
= compat_sc_table
;
188 table_len
= ARRAY_SIZE(compat_sc_table
);
189 unknown_event
= chan
->sc_compat_unknown
;
192 table_len
= ARRAY_SIZE(sc_table
);
193 unknown_event
= chan
->sc_unknown
;
195 if (unlikely(id
>= table_len
)) {
196 syscall_entry_unknown(unknown_event
, regs
, id
);
199 if (unlikely(is_compat_task()))
200 event
= chan
->compat_sc_table
[id
];
202 event
= chan
->sc_table
[id
];
203 if (unlikely(!event
)) {
204 syscall_entry_unknown(unknown_event
, regs
, id
);
208 WARN_ON_ONCE(!entry
);
210 switch (entry
->nrargs
) {
213 void (*fptr
)(void *__data
) = entry
->func
;
220 void (*fptr
)(void *__data
, unsigned long arg0
) = entry
->func
;
221 unsigned long args
[1];
223 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
224 fptr(event
, args
[0]);
229 void (*fptr
)(void *__data
,
231 unsigned long arg1
) = entry
->func
;
232 unsigned long args
[2];
234 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
235 fptr(event
, args
[0], args
[1]);
240 void (*fptr
)(void *__data
,
243 unsigned long arg2
) = entry
->func
;
244 unsigned long args
[3];
246 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
247 fptr(event
, args
[0], args
[1], args
[2]);
252 void (*fptr
)(void *__data
,
256 unsigned long arg3
) = entry
->func
;
257 unsigned long args
[4];
259 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
260 fptr(event
, args
[0], args
[1], args
[2], args
[3]);
265 void (*fptr
)(void *__data
,
270 unsigned long arg4
) = entry
->func
;
271 unsigned long args
[5];
273 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
274 fptr(event
, args
[0], args
[1], args
[2], args
[3], args
[4]);
279 void (*fptr
)(void *__data
,
285 unsigned long arg5
) = entry
->func
;
286 unsigned long args
[6];
288 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
289 fptr(event
, args
[0], args
[1], args
[2],
290 args
[3], args
[4], args
[5]);
298 /* noinline to diminish caller stack size */
300 int fill_table(const struct trace_syscall_entry
*table
, size_t table_len
,
301 struct lttng_event
**chan_table
, struct lttng_channel
*chan
, void *filter
)
303 const struct lttng_event_desc
*desc
;
306 /* Allocate events for each syscall, insert into table */
307 for (i
= 0; i
< table_len
; i
++) {
308 struct lttng_kernel_event ev
;
309 desc
= table
[i
].desc
;
312 /* Unknown syscall */
316 * Skip those already populated by previous failed
317 * register for this channel.
321 memset(&ev
, 0, sizeof(ev
));
322 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
323 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
324 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
325 chan_table
[i
] = lttng_event_create(chan
, &ev
, filter
,
327 WARN_ON_ONCE(!chan_table
[i
]);
328 if (IS_ERR(chan_table
[i
])) {
330 * If something goes wrong in event registration
331 * after the first one, we have no choice but to
332 * leave the previous events in there, until
333 * deleted by session teardown.
335 return PTR_ERR(chan_table
[i
]);
341 int lttng_syscalls_register(struct lttng_channel
*chan
, void *filter
)
343 struct lttng_kernel_event ev
;
346 wrapper_vmalloc_sync_all();
348 if (!chan
->sc_table
) {
349 /* create syscall table mapping syscall to events */
350 chan
->sc_table
= kzalloc(sizeof(struct lttng_event
*)
351 * ARRAY_SIZE(sc_table
), GFP_KERNEL
);
357 if (!chan
->compat_sc_table
) {
358 /* create syscall table mapping compat syscall to events */
359 chan
->compat_sc_table
= kzalloc(sizeof(struct lttng_event
*)
360 * ARRAY_SIZE(compat_sc_table
), GFP_KERNEL
);
361 if (!chan
->compat_sc_table
)
365 if (!chan
->sc_unknown
) {
366 const struct lttng_event_desc
*desc
=
367 &__event_desc___sys_unknown
;
369 memset(&ev
, 0, sizeof(ev
));
370 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
371 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
372 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
373 chan
->sc_unknown
= lttng_event_create(chan
, &ev
, filter
,
375 WARN_ON_ONCE(!chan
->sc_unknown
);
376 if (IS_ERR(chan
->sc_unknown
)) {
377 return PTR_ERR(chan
->sc_unknown
);
381 if (!chan
->sc_compat_unknown
) {
382 const struct lttng_event_desc
*desc
=
383 &__event_desc___compat_sys_unknown
;
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_compat_unknown
= lttng_event_create(chan
, &ev
, filter
,
391 WARN_ON_ONCE(!chan
->sc_unknown
);
392 if (IS_ERR(chan
->sc_compat_unknown
)) {
393 return PTR_ERR(chan
->sc_compat_unknown
);
397 if (!chan
->sc_exit
) {
398 const struct lttng_event_desc
*desc
=
399 &__event_desc___exit_syscall
;
401 memset(&ev
, 0, sizeof(ev
));
402 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
403 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
404 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
405 chan
->sc_exit
= lttng_event_create(chan
, &ev
, filter
,
407 WARN_ON_ONCE(!chan
->sc_exit
);
408 if (IS_ERR(chan
->sc_exit
)) {
409 return PTR_ERR(chan
->sc_exit
);
413 ret
= fill_table(sc_table
, ARRAY_SIZE(sc_table
),
414 chan
->sc_table
, chan
, filter
);
418 ret
= fill_table(compat_sc_table
, ARRAY_SIZE(compat_sc_table
),
419 chan
->compat_sc_table
, chan
, filter
);
423 ret
= lttng_wrapper_tracepoint_probe_register("sys_enter",
424 (void *) syscall_entry_probe
, chan
);
428 * We change the name of sys_exit tracepoint due to namespace
429 * conflict with sys_exit syscall entry.
431 ret
= lttng_wrapper_tracepoint_probe_register("sys_exit",
432 (void *) __event_probe__exit_syscall
,
435 WARN_ON_ONCE(lttng_wrapper_tracepoint_probe_unregister("sys_enter",
436 (void *) syscall_entry_probe
, chan
));
442 * Only called at session destruction.
444 int lttng_syscalls_unregister(struct lttng_channel
*chan
)
450 ret
= lttng_wrapper_tracepoint_probe_unregister("sys_exit",
451 (void *) __event_probe__exit_syscall
,
455 ret
= lttng_wrapper_tracepoint_probe_unregister("sys_enter",
456 (void *) syscall_entry_probe
, chan
);
459 /* lttng_event destroy will be performed by lttng_session_destroy() */
460 kfree(chan
->sc_table
);
462 kfree(chan
->compat_sc_table
);