4 * Copyright 2010-2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng syscall probes.
8 * Dual LGPL v2.1/GPL v2 license.
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/compat.h>
14 #include <asm/ptrace.h>
15 #include <asm/syscall.h>
17 #include "ltt-events.h"
20 static inline int is_compat_task(void)
26 void syscall_entry_probe(void *__data
, struct pt_regs
*regs
, long id
);
29 * Take care of NOARGS not supported by mainline.
31 #define DECLARE_EVENT_CLASS_NOARGS(name, tstruct, assign, print)
32 #define DEFINE_EVENT_NOARGS(template, name)
33 #define TRACE_EVENT_NOARGS(name, struct, assign, print)
36 * Create LTTng tracepoint probes.
38 #define LTTNG_PACKAGE_BUILD
39 #define CREATE_TRACE_POINTS
40 #define TP_MODULE_OVERRIDE
41 #define TRACE_INCLUDE_PATH ../instrumentation/syscalls/headers
43 /* Hijack probe callback for system calls */
44 #define TP_PROBE_CB(_template) &syscall_entry_probe
45 #include "instrumentation/syscalls/headers/syscalls_integers.h"
46 #include "instrumentation/syscalls/headers/syscalls_pointers.h"
49 #include "instrumentation/syscalls/headers/syscalls_unknown.h"
51 #undef TP_MODULE_OVERRIDE
52 #undef LTTNG_PACKAGE_BUILD
53 #undef CREATE_TRACE_POINTS
55 #undef TRACE_SYSCALL_TABLE
56 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
58 .func = __event_probe__##_template, \
59 .nrargs = (_nrargs), \
60 .fields = __event_fields___##_template, \
61 .desc = &__event_desc___##_name, \
64 #define CREATE_SYSCALL_TABLE
66 static const struct trace_syscall_entry sc_table
[] = {
67 #include "instrumentation/syscalls/headers/syscalls_integers.h"
68 #include "instrumentation/syscalls/headers/syscalls_pointers.h"
71 #undef CREATE_SYSCALL_TABLE
73 //extern const struct trace_syscall_entry compat_sc_table[];
74 //extern const size_t compat_sc_table_len;
76 static const struct trace_syscall_entry compat_sc_table
[] = { };
77 static const size_t compat_sc_table_len
;
79 static void syscall_entry_unknown(struct ltt_event
*event
,
80 struct pt_regs
*regs
, unsigned int id
)
82 unsigned long args
[UNKNOWN_SYSCALL_NRARGS
];
84 syscall_get_arguments(current
, regs
, 0, UNKNOWN_SYSCALL_NRARGS
, args
);
85 __event_probe__sys_unknown(event
, id
, args
);
88 void syscall_entry_probe(void *__data
, struct pt_regs
*regs
, long id
)
90 struct ltt_channel
*chan
= __data
;
91 struct ltt_event
*event
, *unknown_event
;
92 const struct trace_syscall_entry
*table
, *entry
;
95 if (unlikely(is_compat_task())) {
96 table
= compat_sc_table
;
97 table_len
= compat_sc_table_len
;
98 unknown_event
= chan
->sc_compat_unknown
;
101 table_len
= ARRAY_SIZE(sc_table
);
102 unknown_event
= chan
->sc_unknown
;
104 if (unlikely(id
>= table_len
)) {
105 syscall_entry_unknown(unknown_event
, regs
, id
);
108 if (unlikely(is_compat_task()))
109 event
= chan
->compat_sc_table
[id
];
111 event
= chan
->sc_table
[id
];
112 if (unlikely(!event
)) {
113 syscall_entry_unknown(unknown_event
, regs
, id
);
117 WARN_ON_ONCE(!entry
);
119 switch (entry
->nrargs
) {
122 void (*fptr
)(void *__data
) = entry
->func
;
129 void (*fptr
)(void *__data
, unsigned long arg0
) = entry
->func
;
130 unsigned long args
[1];
132 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
133 fptr(event
, args
[0]);
138 void (*fptr
)(void *__data
,
140 unsigned long arg1
) = entry
->func
;
141 unsigned long args
[2];
143 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
144 fptr(event
, args
[0], args
[1]);
149 void (*fptr
)(void *__data
,
152 unsigned long arg2
) = entry
->func
;
153 unsigned long args
[3];
155 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
156 fptr(event
, args
[0], args
[1], args
[2]);
161 void (*fptr
)(void *__data
,
165 unsigned long arg3
) = entry
->func
;
166 unsigned long args
[4];
168 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
169 fptr(event
, args
[0], args
[1], args
[2], args
[3]);
174 void (*fptr
)(void *__data
,
179 unsigned long arg4
) = entry
->func
;
180 unsigned long args
[5];
182 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
183 fptr(event
, args
[0], args
[1], args
[2], args
[3], args
[4]);
188 void (*fptr
)(void *__data
,
194 unsigned long arg5
) = entry
->func
;
195 unsigned long args
[6];
197 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
198 fptr(event
, args
[0], args
[1], args
[2],
199 args
[3], args
[4], args
[5]);
208 int fill_table(const struct trace_syscall_entry
*table
, size_t table_len
,
209 struct ltt_event
**chan_table
, struct ltt_channel
*chan
, void *filter
)
213 /* Allocate events for each syscall, insert into table */
214 for (i
= 0; i
< table_len
; i
++) {
215 struct lttng_kernel_event ev
;
216 const struct lttng_event_desc
*desc
= table
[i
].desc
;
219 /* Unknown syscall */
223 * Skip those already populated by previous failed
224 * register for this channel.
228 memset(&ev
, 0, sizeof(ev
));
229 strncpy(ev
.name
, desc
->name
, LTTNG_SYM_NAME_LEN
);
230 ev
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
231 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
232 chan_table
[i
] = ltt_event_create(chan
, &ev
, filter
,
234 if (!chan_table
[i
]) {
236 * If something goes wrong in event registration
237 * after the first one, we have no choice but to
238 * leave the previous events in there, until
239 * deleted by session teardown.
247 int lttng_syscalls_register(struct ltt_channel
*chan
, void *filter
)
251 wrapper_vmalloc_sync_all();
253 if (!chan
->sc_table
) {
254 /* create syscall table mapping syscall to events */
255 chan
->sc_table
= kzalloc(sizeof(struct ltt_event
*)
256 * ARRAY_SIZE(sc_table
), GFP_KERNEL
);
262 if (!chan
->compat_sc_table
) {
263 /* create syscall table mapping compat syscall to events */
264 chan
->compat_sc_table
= kzalloc(sizeof(struct ltt_event
*)
265 * compat_sc_table_len
, GFP_KERNEL
);
266 if (!chan
->compat_sc_table
)
270 if (!chan
->sc_unknown
) {
271 struct lttng_kernel_event ev
;
272 const struct lttng_event_desc
*desc
=
273 &__event_desc___sys_unknown
;
275 memset(&ev
, 0, sizeof(ev
));
276 strncpy(ev
.name
, desc
->name
, LTTNG_SYM_NAME_LEN
);
277 ev
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
278 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
279 chan
->sc_unknown
= ltt_event_create(chan
, &ev
, filter
,
281 if (!chan
->sc_unknown
) {
286 if (!chan
->sc_compat_unknown
) {
287 struct lttng_kernel_event ev
;
288 const struct lttng_event_desc
*desc
=
289 &__event_desc___compat_sys_unknown
;
291 memset(&ev
, 0, sizeof(ev
));
292 strncpy(ev
.name
, desc
->name
, LTTNG_SYM_NAME_LEN
);
293 ev
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
294 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
295 chan
->sc_compat_unknown
= ltt_event_create(chan
, &ev
, filter
,
297 if (!chan
->sc_compat_unknown
) {
302 if (!chan
->sc_exit
) {
303 struct lttng_kernel_event ev
;
304 const struct lttng_event_desc
*desc
=
305 &__event_desc___exit_syscall
;
307 memset(&ev
, 0, sizeof(ev
));
308 strncpy(ev
.name
, desc
->name
, LTTNG_SYM_NAME_LEN
);
309 ev
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
310 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
311 chan
->sc_exit
= ltt_event_create(chan
, &ev
, filter
,
313 if (!chan
->sc_exit
) {
318 ret
= fill_table(sc_table
, ARRAY_SIZE(sc_table
),
319 chan
->sc_table
, chan
, filter
);
323 ret
= fill_table(compat_sc_table
, compat_sc_table_len
,
324 chan
->compat_sc_table
, chan
, filter
);
328 ret
= tracepoint_probe_register("sys_enter",
329 (void *) syscall_entry_probe
, chan
);
333 * We change the name of sys_exit tracepoint due to namespace
334 * conflict with sys_exit syscall entry.
336 ret
= tracepoint_probe_register("sys_exit",
337 (void *) __event_probe__exit_syscall
,
340 WARN_ON_ONCE(tracepoint_probe_unregister("sys_enter",
341 (void *) syscall_entry_probe
, chan
));
347 * Only called at session destruction.
349 int lttng_syscalls_unregister(struct ltt_channel
*chan
)
355 ret
= tracepoint_probe_unregister("sys_exit",
356 (void *) __event_probe__exit_syscall
,
360 ret
= tracepoint_probe_unregister("sys_enter",
361 (void *) syscall_entry_probe
, chan
);
364 /* ltt_event destroy will be performed by ltt_session_destroy() */
365 kfree(chan
->sc_table
);
367 kfree(chan
->compat_sc_table
);