4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Dual LGPL v2.1/GPL v2 license.
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <asm/ptrace.h>
14 #include <asm/syscall.h>
16 #include "ltt-events.h"
18 static void syscall_entry_probe(void *__data
, struct pt_regs
*regs
, long id
);
21 * Take care of NOARGS not supported by mainline.
23 #define DECLARE_EVENT_CLASS_NOARGS(name, tstruct, assign, print)
24 #define DEFINE_EVENT_NOARGS(template, name)
25 #define TRACE_EVENT_NOARGS(name, struct, assign, print)
28 * Create LTTng tracepoint probes.
30 #define LTTNG_PACKAGE_BUILD
31 #define CREATE_TRACE_POINTS
33 /* Hijack probe callback for system calls */
34 #define TP_PROBE_CB(_template) &syscall_entry_probe
35 #define TP_MODULE_OVERRIDE
37 #define TRACE_INCLUDE_PATH ../instrumentation/syscalls/headers
38 #include "instrumentation/syscalls/headers/syscalls_integers.h"
39 #include "instrumentation/syscalls/headers/syscalls_pointers.h"
40 #include "instrumentation/syscalls/headers/syscalls_unknown.h"
42 #undef TP_MODULE_OVERRIDE
44 #undef LTTNG_PACKAGE_BUILD
45 #undef CREATE_TRACE_POINTS
47 struct trace_syscall_entry
{
49 const struct lttng_event_desc
*desc
;
50 const struct lttng_event_field
*fields
;
54 #define CREATE_SYSCALL_TABLE
56 #undef TRACE_SYSCALL_TABLE
57 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
59 .func = __event_probe__##_template, \
60 .nrargs = (_nrargs), \
61 .fields = __event_fields___##_template, \
62 .desc = &__event_desc___##_name, \
65 static struct trace_syscall_entry sc_table
[] = {
66 #include "instrumentation/syscalls/headers/syscalls_integers.h"
67 #include "instrumentation/syscalls/headers/syscalls_pointers.h"
70 static int sc_table_filled
;
72 #undef CREATE_SYSCALL_TABLE
74 static void syscall_entry_probe(void *__data
, struct pt_regs
*regs
, long id
)
76 struct trace_syscall_entry
*entry
;
77 struct ltt_channel
*chan
= __data
;
78 struct ltt_event
*event
;
80 if (unlikely(id
>= ARRAY_SIZE(sc_table
)))
82 entry
= &sc_table
[id
];
83 if (unlikely(!entry
->func
))
85 event
= chan
->sc_table
[id
];
88 switch (entry
->nrargs
) {
91 void (*fptr
)(void *__data
) = entry
->func
;
98 void (*fptr
)(void *__data
, unsigned long arg0
) = entry
->func
;
99 unsigned long args
[1];
101 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
102 fptr(event
, args
[0]);
107 void (*fptr
)(void *__data
,
109 unsigned long arg1
) = entry
->func
;
110 unsigned long args
[2];
112 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
113 fptr(event
, args
[0], args
[1]);
118 void (*fptr
)(void *__data
,
121 unsigned long arg2
) = entry
->func
;
122 unsigned long args
[3];
124 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
125 fptr(event
, args
[0], args
[1], args
[2]);
130 void (*fptr
)(void *__data
,
134 unsigned long arg3
) = entry
->func
;
135 unsigned long args
[4];
137 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
138 fptr(event
, args
[0], args
[1], args
[2], args
[3]);
143 void (*fptr
)(void *__data
,
148 unsigned long arg4
) = entry
->func
;
149 unsigned long args
[5];
151 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
152 fptr(event
, args
[0], args
[1], args
[2], args
[3], args
[4]);
157 void (*fptr
)(void *__data
,
163 unsigned long arg5
) = entry
->func
;
164 unsigned long args
[6];
166 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
167 fptr(event
, args
[0], args
[1], args
[2],
168 args
[3], args
[4], args
[5]);
176 static void fill_sc_table(void)
180 if (sc_table_filled
) {
181 smp_rmb(); /* read flag before table */
185 for (i
= 0; i
< ARRAY_SIZE(sc_table
); i
++) {
186 if (sc_table
[i
].func
)
188 sc_table
[i
].func
= __event_probe__sys_unknown
;
189 sc_table
[i
].nrargs
= UNKNOWN_SYSCALL_NRARGS
;
190 sc_table
[i
].fields
= __event_fields___sys_unknown
;
191 sc_table
[i
].desc
= &__event_desc___sys_unknown
;
193 smp_wmb(); /* Fill sc table before set flag to 1 */
197 int lttng_syscalls_register(struct ltt_channel
*chan
, void *filter
)
202 wrapper_vmalloc_sync_all();
206 if (!chan
->sc_table
) {
207 /* create syscall table mapping syscall to events */
208 chan
->sc_table
= kzalloc(sizeof(struct ltt_event
*)
209 * ARRAY_SIZE(sc_table
), GFP_KERNEL
);
214 /* Allocate events for each syscall, insert into table */
215 for (i
= 0; i
< ARRAY_SIZE(sc_table
); i
++) {
216 struct lttng_kernel_event ev
;
217 const struct lttng_event_desc
*desc
= sc_table
[i
].desc
;
221 * Skip those already populated by previous failed
222 * register for this channel.
224 if (chan
->sc_table
[i
])
226 memset(&ev
, 0, sizeof(ev
));
227 strncpy(ev
.name
, desc
->name
, LTTNG_SYM_NAME_LEN
);
228 ev
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
229 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
230 chan
->sc_table
[i
] = ltt_event_create(chan
, &ev
, filter
,
232 if (!chan
->sc_table
[i
]) {
234 * If something goes wrong in event registration
235 * after the first one, we have no choice but to
236 * leave the previous events in there, until
237 * deleted by session teardown.
242 ret
= tracepoint_probe_register("sys_enter",
243 (void *) syscall_entry_probe
, chan
);
247 * We change the name of sys_exit tracepoint due to namespace
248 * conflict with sys_exit syscall entry.
250 ret
= tracepoint_probe_register("sys_exit",
251 (void *) __event_probe__exit_syscall
, chan
);
253 WARN_ON_ONCE(tracepoint_probe_unregister("sys_enter",
254 (void *) syscall_entry_probe
, chan
));
260 * Only called at session destruction.
262 int lttng_syscalls_unregister(struct ltt_channel
*chan
)
268 ret
= tracepoint_probe_unregister("sys_exit",
269 (void *) __event_probe__exit_syscall
, chan
);
272 ret
= tracepoint_probe_unregister("sys_enter",
273 (void *) syscall_entry_probe
, chan
);
276 /* ltt_event destroy will be performed by ltt_session_destroy() */
277 kfree(chan
->sc_table
);