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 * Create LTTng tracepoint probes.
23 #define LTTNG_PACKAGE_BUILD
24 #define CREATE_TRACE_POINTS
26 /* Hijack probe callback for system calls */
27 #define TP_PROBE_CB(_template) &syscall_entry_probe
28 #define TP_MODULE_OVERRIDE
30 #define TRACE_INCLUDE_PATH ../instrumentation/syscalls/headers
32 #include "instrumentation/syscalls/headers/syscalls.h"
34 #undef TP_MODULE_OVERRIDE
36 #undef LTTNG_PACKAGE_BUILD
37 #undef CREATE_TRACE_POINTS
39 struct trace_syscall_entry
{
41 const struct lttng_event_desc
*desc
; /* Set dynamically */
42 const struct lttng_event_field
*fields
;
46 static int sc_table_desc_filled
;
48 #define CREATE_SYSCALL_TABLE
50 #undef TRACE_SYSCALL_TABLE
51 #define TRACE_SYSCALL_TABLE(_name, _nr, _nrargs) \
53 .func = __event_probe__##_name, \
54 .nrargs = (_nrargs), \
55 .fields = __event_fields___##_name, \
58 static struct trace_syscall_entry sc_table
[] = {
59 #include "instrumentation/syscalls/headers/syscalls.h"
62 #undef CREATE_SYSCALL_TABLE
64 static void syscall_entry_probe(void *__data
, struct pt_regs
*regs
, long id
)
66 struct trace_syscall_entry
*entry
;
67 struct ltt_channel
*chan
= __data
;
68 struct ltt_event
*event
;
70 if (unlikely(id
>= ARRAY_SIZE(sc_table
)))
72 entry
= &sc_table
[id
];
73 if (unlikely(!entry
->func
))
75 event
= chan
->sc_table
[id
];
78 switch (entry
->nrargs
) {
81 void (*fptr
)(void *__data
) = entry
->func
;
88 void (*fptr
)(void *__data
, unsigned long arg0
) = entry
->func
;
89 unsigned long args
[1];
91 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
97 void (*fptr
)(void *__data
,
99 unsigned long arg1
) = entry
->func
;
100 unsigned long args
[2];
102 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
103 fptr(event
, args
[0], args
[1]);
108 void (*fptr
)(void *__data
,
111 unsigned long arg2
) = entry
->func
;
112 unsigned long args
[3];
114 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
115 fptr(event
, args
[0], args
[1], args
[2]);
120 void (*fptr
)(void *__data
,
124 unsigned long arg3
) = entry
->func
;
125 unsigned long args
[4];
127 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
128 fptr(event
, args
[0], args
[1], args
[2], args
[3]);
133 void (*fptr
)(void *__data
,
138 unsigned long arg4
) = entry
->func
;
139 unsigned long args
[5];
141 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
142 fptr(event
, args
[0], args
[1], args
[2], args
[3], args
[4]);
147 void (*fptr
)(void *__data
,
153 unsigned long arg5
) = entry
->func
;
154 unsigned long args
[6];
156 syscall_get_arguments(current
, regs
, 0, entry
->nrargs
, args
);
157 fptr(event
, args
[0], args
[1], args
[2],
158 args
[3], args
[4], args
[5]);
166 static const struct lttng_event_desc
*find_syscall_desc(unsigned int id
)
170 for (i
= 0; i
< __probe_desc___syscalls
.nr_events
; i
++) {
171 if (__probe_desc___syscalls
.event_desc
[i
].fields
172 == sc_table
[id
].fields
)
173 return &__probe_desc___syscalls
.event_desc
[i
];
179 static void fill_sc_table_desc(void)
183 if (sc_table_desc_filled
)
186 * This is O(n^2), but rare. Eventually get the TRACE_EVENT code
187 * to emit per-event symbols to skip this.
189 for (i
= 0; i
< ARRAY_SIZE(sc_table
); i
++) {
190 const struct lttng_event_desc
**desc
= &sc_table
[i
].desc
;
192 if (!sc_table
[i
].func
)
194 (*desc
) = find_syscall_desc(i
);
196 sc_table_desc_filled
= 1;
200 int lttng_syscalls_register(struct ltt_channel
*chan
, void *filter
)
205 wrapper_vmalloc_sync_all();
206 fill_sc_table_desc();
208 if (!chan
->sc_table
) {
209 /* create syscall table mapping syscall to events */
210 chan
->sc_table
= kzalloc(sizeof(struct ltt_event
*)
211 * ARRAY_SIZE(sc_table
), GFP_KERNEL
);
216 /* Allocate events for each syscall, insert into table */
217 for (i
= 0; i
< ARRAY_SIZE(sc_table
); i
++) {
218 struct lttng_kernel_event ev
;
219 const struct lttng_event_desc
*desc
= sc_table
[i
].desc
;
224 * Skip those already populated by previous failed
225 * register for this channel.
227 if (chan
->sc_table
[i
])
229 memset(&ev
, 0, sizeof(ev
));
230 strncpy(ev
.name
, desc
->name
, LTTNG_SYM_NAME_LEN
);
231 ev
.name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
232 ev
.instrumentation
= LTTNG_KERNEL_NOOP
;
233 chan
->sc_table
[i
] = ltt_event_create(chan
, &ev
, filter
,
235 if (!chan
->sc_table
[i
]) {
237 * If something goes wrong in event registration
238 * after the first one, we have no choice but to
239 * leave the previous events in there, until
240 * deleted by session teardown.
245 ret
= tracepoint_probe_register("sys_enter",
246 (void *) syscall_entry_probe
, chan
);
251 * Only called at session destruction.
253 int lttng_syscalls_unregister(struct ltt_channel
*chan
)
259 ret
= tracepoint_probe_unregister("sys_enter",
260 (void *) syscall_entry_probe
, chan
);
263 /* ltt_event destroy will be performed by ltt_session_destroy() */
264 kfree(chan
->sc_table
);