blob: d2c859cec9ea85b6f2e32c4c016937bee450aacb [file] [log] [blame]
Frederic Weisbecker47788c52009-04-08 20:40:59 +02001#include <trace/syscall.h>
Josh Stone1c569f02009-08-24 14:43:14 -07002#include <trace/events/syscalls.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/slab.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01004#include <linux/kernel.h>
Jason Baronfb34a082009-08-10 16:52:47 -04005#include <linux/ftrace.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006#include <linux/perf_event.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01007#include <asm/syscall.h>
8
9#include "trace_output.h"
10#include "trace.h"
11
Frederic Weisbecker5be71b62009-03-15 22:10:37 +010012static DEFINE_MUTEX(syscall_trace_lock);
Jason Baronfb34a082009-08-10 16:52:47 -040013static int sys_refcount_enter;
14static int sys_refcount_exit;
Jason Baron57421db2009-08-24 17:40:22 -040015static DECLARE_BITMAP(enabled_enter_syscalls, NR_syscalls);
16static DECLARE_BITMAP(enabled_exit_syscalls, NR_syscalls);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +010017
Steven Rostedt22392912010-04-21 12:27:06 -040018static int syscall_enter_register(struct ftrace_event_call *event,
19 enum trace_reg type);
20static int syscall_exit_register(struct ftrace_event_call *event,
21 enum trace_reg type);
22
Steven Rostedt2e33af02010-04-22 10:35:55 -040023static int syscall_enter_define_fields(struct ftrace_event_call *call);
24static int syscall_exit_define_fields(struct ftrace_event_call *call);
25
26static struct list_head *
27syscall_get_enter_fields(struct ftrace_event_call *call)
28{
29 struct syscall_metadata *entry = call->data;
30
31 return &entry->enter_fields;
32}
33
34static struct list_head *
35syscall_get_exit_fields(struct ftrace_event_call *call)
36{
37 struct syscall_metadata *entry = call->data;
38
39 return &entry->exit_fields;
40}
41
Steven Rostedt80decc72010-04-23 10:00:22 -040042struct trace_event_functions enter_syscall_print_funcs = {
43 .trace = print_syscall_enter,
44};
45
46struct trace_event_functions exit_syscall_print_funcs = {
47 .trace = print_syscall_exit,
48};
49
Steven Rostedt22392912010-04-21 12:27:06 -040050struct ftrace_event_class event_class_syscall_enter = {
51 .system = "syscalls",
Steven Rostedt2e33af02010-04-22 10:35:55 -040052 .reg = syscall_enter_register,
53 .define_fields = syscall_enter_define_fields,
54 .get_fields = syscall_get_enter_fields,
Steven Rostedt0405ab82010-04-22 11:46:44 -040055 .raw_init = init_syscall_trace,
Steven Rostedt22392912010-04-21 12:27:06 -040056};
57
58struct ftrace_event_class event_class_syscall_exit = {
59 .system = "syscalls",
Steven Rostedt2e33af02010-04-22 10:35:55 -040060 .reg = syscall_exit_register,
61 .define_fields = syscall_exit_define_fields,
62 .get_fields = syscall_get_exit_fields,
Steven Rostedt0405ab82010-04-22 11:46:44 -040063 .raw_init = init_syscall_trace,
Steven Rostedt8f082012010-04-20 10:47:33 -040064};
65
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020066extern unsigned long __start_syscalls_metadata[];
67extern unsigned long __stop_syscalls_metadata[];
68
69static struct syscall_metadata **syscalls_metadata;
70
71static struct syscall_metadata *find_syscall_meta(unsigned long syscall)
72{
73 struct syscall_metadata *start;
74 struct syscall_metadata *stop;
75 char str[KSYM_SYMBOL_LEN];
76
77
78 start = (struct syscall_metadata *)__start_syscalls_metadata;
79 stop = (struct syscall_metadata *)__stop_syscalls_metadata;
80 kallsyms_lookup(syscall, NULL, NULL, NULL, str);
81
82 for ( ; start < stop; start++) {
83 /*
84 * Only compare after the "sys" prefix. Archs that use
85 * syscall wrappers may have syscalls symbols aliases prefixed
86 * with "SyS" instead of "sys", leading to an unwanted
87 * mismatch.
88 */
89 if (start->name && !strcmp(start->name + 3, str + 3))
90 return start;
91 }
92 return NULL;
93}
94
95static struct syscall_metadata *syscall_nr_to_meta(int nr)
96{
97 if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
98 return NULL;
99
100 return syscalls_metadata[nr];
101}
102
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100103enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -0400104print_syscall_enter(struct trace_iterator *iter, int flags,
105 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100106{
107 struct trace_seq *s = &iter->seq;
108 struct trace_entry *ent = iter->ent;
109 struct syscall_trace_enter *trace;
110 struct syscall_metadata *entry;
111 int i, ret, syscall;
112
Jason Baron64c12e02009-08-10 16:52:53 -0400113 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100114 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100115 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400116
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100117 if (!entry)
118 goto end;
119
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400120 if (entry->enter_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400121 WARN_ON_ONCE(1);
122 goto end;
123 }
124
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100125 ret = trace_seq_printf(s, "%s(", entry->name);
126 if (!ret)
127 return TRACE_TYPE_PARTIAL_LINE;
128
129 for (i = 0; i < entry->nb_args; i++) {
130 /* parameter types */
Li Zefanba8b3a42009-08-17 16:55:18 +0800131 if (trace_flags & TRACE_ITER_VERBOSE) {
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100132 ret = trace_seq_printf(s, "%s ", entry->types[i]);
133 if (!ret)
134 return TRACE_TYPE_PARTIAL_LINE;
135 }
136 /* parameter values */
Li Zefan4539f072009-08-20 16:13:35 +0800137 ret = trace_seq_printf(s, "%s: %lx%s", entry->args[i],
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100138 trace->args[i],
Li Zefan4539f072009-08-20 16:13:35 +0800139 i == entry->nb_args - 1 ? "" : ", ");
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100140 if (!ret)
141 return TRACE_TYPE_PARTIAL_LINE;
142 }
143
Li Zefan4539f072009-08-20 16:13:35 +0800144 ret = trace_seq_putc(s, ')');
145 if (!ret)
146 return TRACE_TYPE_PARTIAL_LINE;
147
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100148end:
Li Zefan4539f072009-08-20 16:13:35 +0800149 ret = trace_seq_putc(s, '\n');
150 if (!ret)
151 return TRACE_TYPE_PARTIAL_LINE;
152
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100153 return TRACE_TYPE_HANDLED;
154}
155
156enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -0400157print_syscall_exit(struct trace_iterator *iter, int flags,
158 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100159{
160 struct trace_seq *s = &iter->seq;
161 struct trace_entry *ent = iter->ent;
162 struct syscall_trace_exit *trace;
163 int syscall;
164 struct syscall_metadata *entry;
165 int ret;
166
Jason Baron64c12e02009-08-10 16:52:53 -0400167 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100168 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100169 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400170
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100171 if (!entry) {
172 trace_seq_printf(s, "\n");
173 return TRACE_TYPE_HANDLED;
174 }
175
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400176 if (entry->exit_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400177 WARN_ON_ONCE(1);
178 return TRACE_TYPE_UNHANDLED;
179 }
180
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100181 ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
182 trace->ret);
183 if (!ret)
184 return TRACE_TYPE_PARTIAL_LINE;
185
186 return TRACE_TYPE_HANDLED;
187}
188
Li Zefane6971962009-08-19 15:52:25 +0800189extern char *__bad_type_size(void);
190
191#define SYSCALL_FIELD(type, name) \
192 sizeof(type) != sizeof(trace.name) ? \
193 __bad_type_size() : \
Tom Zanussi26a50742009-10-06 01:09:50 -0500194 #type, #name, offsetof(typeof(trace), name), \
195 sizeof(trace.name), is_signed_type(type)
Li Zefane6971962009-08-19 15:52:25 +0800196
Lai Jiangshan50307a42009-12-15 15:39:45 +0800197static
198int __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
199{
200 int i;
201 int pos = 0;
202
203 /* When len=0, we just calculate the needed length */
204#define LEN_OR_ZERO (len ? len - pos : 0)
205
206 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
207 for (i = 0; i < entry->nb_args; i++) {
208 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s",
209 entry->args[i], sizeof(unsigned long),
210 i == entry->nb_args - 1 ? "" : ", ");
211 }
212 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
213
214 for (i = 0; i < entry->nb_args; i++) {
215 pos += snprintf(buf + pos, LEN_OR_ZERO,
216 ", ((unsigned long)(REC->%s))", entry->args[i]);
217 }
218
219#undef LEN_OR_ZERO
220
221 /* return the length of print_fmt */
222 return pos;
223}
224
225static int set_syscall_print_fmt(struct ftrace_event_call *call)
226{
227 char *print_fmt;
228 int len;
229 struct syscall_metadata *entry = call->data;
230
231 if (entry->enter_event != call) {
232 call->print_fmt = "\"0x%lx\", REC->ret";
233 return 0;
234 }
235
236 /* First: called with 0 length to calculate the needed length */
237 len = __set_enter_print_fmt(entry, NULL, 0);
238
239 print_fmt = kmalloc(len + 1, GFP_KERNEL);
240 if (!print_fmt)
241 return -ENOMEM;
242
243 /* Second: actually write the @print_fmt */
244 __set_enter_print_fmt(entry, print_fmt, len + 1);
245 call->print_fmt = print_fmt;
246
247 return 0;
248}
249
250static void free_syscall_print_fmt(struct ftrace_event_call *call)
251{
252 struct syscall_metadata *entry = call->data;
253
254 if (entry->enter_event == call)
255 kfree(call->print_fmt);
256}
257
Steven Rostedt2e33af02010-04-22 10:35:55 -0400258static int syscall_enter_define_fields(struct ftrace_event_call *call)
Li Zefan540b7b82009-08-19 15:54:51 +0800259{
260 struct syscall_trace_enter trace;
Lai Jiangshan31c16b12009-12-01 16:23:30 +0800261 struct syscall_metadata *meta = call->data;
Li Zefan540b7b82009-08-19 15:54:51 +0800262 int ret;
Li Zefan540b7b82009-08-19 15:54:51 +0800263 int i;
264 int offset = offsetof(typeof(trace), args);
265
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800266 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
267 if (ret)
268 return ret;
269
Li Zefan540b7b82009-08-19 15:54:51 +0800270 for (i = 0; i < meta->nb_args; i++) {
Frederic Weisbeckeraeaeae12009-08-27 05:09:51 +0200271 ret = trace_define_field(call, meta->types[i],
272 meta->args[i], offset,
Li Zefan43b51ea2009-08-07 10:33:22 +0800273 sizeof(unsigned long), 0,
274 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800275 offset += sizeof(unsigned long);
276 }
277
278 return ret;
279}
280
Steven Rostedt2e33af02010-04-22 10:35:55 -0400281static int syscall_exit_define_fields(struct ftrace_event_call *call)
Li Zefan540b7b82009-08-19 15:54:51 +0800282{
283 struct syscall_trace_exit trace;
284 int ret;
285
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800286 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
287 if (ret)
288 return ret;
289
Tom Zanussi26a50742009-10-06 01:09:50 -0500290 ret = trace_define_field(call, SYSCALL_FIELD(long, ret),
Li Zefan43b51ea2009-08-07 10:33:22 +0800291 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800292
293 return ret;
294}
295
Steven Rostedt38516ab2010-04-20 17:04:50 -0400296void ftrace_syscall_enter(void *ignore, struct pt_regs *regs, long id)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100297{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100298 struct syscall_trace_enter *entry;
299 struct syscall_metadata *sys_data;
300 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400301 struct ring_buffer *buffer;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100302 int size;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100303 int syscall_nr;
304
305 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200306 if (syscall_nr < 0)
307 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400308 if (!test_bit(syscall_nr, enabled_enter_syscalls))
309 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100310
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100311 sys_data = syscall_nr_to_meta(syscall_nr);
312 if (!sys_data)
313 return;
314
315 size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
316
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800317 event = trace_current_buffer_lock_reserve(&buffer,
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400318 sys_data->enter_event->event.type, size, 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100319 if (!event)
320 return;
321
322 entry = ring_buffer_event_data(event);
323 entry->nr = syscall_nr;
324 syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
325
Steven Rostedte77405a2009-09-02 14:17:06 -0400326 if (!filter_current_check_discard(buffer, sys_data->enter_event,
327 entry, event))
328 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100329}
330
Steven Rostedt38516ab2010-04-20 17:04:50 -0400331void ftrace_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100332{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100333 struct syscall_trace_exit *entry;
334 struct syscall_metadata *sys_data;
335 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400336 struct ring_buffer *buffer;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100337 int syscall_nr;
338
339 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200340 if (syscall_nr < 0)
341 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400342 if (!test_bit(syscall_nr, enabled_exit_syscalls))
343 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100344
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100345 sys_data = syscall_nr_to_meta(syscall_nr);
346 if (!sys_data)
347 return;
348
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800349 event = trace_current_buffer_lock_reserve(&buffer,
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400350 sys_data->exit_event->event.type, sizeof(*entry), 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100351 if (!event)
352 return;
353
354 entry = ring_buffer_event_data(event);
355 entry->nr = syscall_nr;
356 entry->ret = syscall_get_return_value(current, regs);
357
Steven Rostedte77405a2009-09-02 14:17:06 -0400358 if (!filter_current_check_discard(buffer, sys_data->exit_event,
359 entry, event))
360 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100361}
362
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400363int reg_event_syscall_enter(struct ftrace_event_call *call)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100364{
Jason Baronfb34a082009-08-10 16:52:47 -0400365 int ret = 0;
366 int num;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100367
Lai Jiangshanc252f652009-12-01 16:23:47 +0800368 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400369 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400370 return -ENOSYS;
371 mutex_lock(&syscall_trace_lock);
372 if (!sys_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400373 ret = register_trace_sys_enter(ftrace_syscall_enter, NULL);
Li Zefan3b8e4272009-12-08 11:14:52 +0800374 if (!ret) {
Jason Baronfb34a082009-08-10 16:52:47 -0400375 set_bit(num, enabled_enter_syscalls);
376 sys_refcount_enter++;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100377 }
Jason Baronfb34a082009-08-10 16:52:47 -0400378 mutex_unlock(&syscall_trace_lock);
379 return ret;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100380}
Jason Baronfb34a082009-08-10 16:52:47 -0400381
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400382void unreg_event_syscall_enter(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400383{
384 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400385
Lai Jiangshanc252f652009-12-01 16:23:47 +0800386 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400387 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400388 return;
389 mutex_lock(&syscall_trace_lock);
390 sys_refcount_enter--;
391 clear_bit(num, enabled_enter_syscalls);
392 if (!sys_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400393 unregister_trace_sys_enter(ftrace_syscall_enter, NULL);
Jason Baronfb34a082009-08-10 16:52:47 -0400394 mutex_unlock(&syscall_trace_lock);
395}
396
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400397int reg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400398{
399 int ret = 0;
400 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400401
Lai Jiangshanc252f652009-12-01 16:23:47 +0800402 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400403 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400404 return -ENOSYS;
405 mutex_lock(&syscall_trace_lock);
406 if (!sys_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400407 ret = register_trace_sys_exit(ftrace_syscall_exit, NULL);
Li Zefan3b8e4272009-12-08 11:14:52 +0800408 if (!ret) {
Jason Baronfb34a082009-08-10 16:52:47 -0400409 set_bit(num, enabled_exit_syscalls);
410 sys_refcount_exit++;
411 }
412 mutex_unlock(&syscall_trace_lock);
413 return ret;
414}
415
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400416void unreg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400417{
418 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400419
Lai Jiangshanc252f652009-12-01 16:23:47 +0800420 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400421 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400422 return;
423 mutex_lock(&syscall_trace_lock);
424 sys_refcount_exit--;
425 clear_bit(num, enabled_exit_syscalls);
426 if (!sys_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400427 unregister_trace_sys_exit(ftrace_syscall_exit, NULL);
Jason Baronfb34a082009-08-10 16:52:47 -0400428 mutex_unlock(&syscall_trace_lock);
429}
430
Lai Jiangshana1301da2009-12-01 16:23:55 +0800431int init_syscall_trace(struct ftrace_event_call *call)
432{
433 int id;
434
Lai Jiangshan50307a42009-12-15 15:39:45 +0800435 if (set_syscall_print_fmt(call) < 0)
436 return -ENOMEM;
437
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500438 id = trace_event_raw_init(call);
439
440 if (id < 0) {
Lai Jiangshan50307a42009-12-15 15:39:45 +0800441 free_syscall_print_fmt(call);
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500442 return id;
Lai Jiangshan50307a42009-12-15 15:39:45 +0800443 }
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500444
445 return id;
Lai Jiangshana1301da2009-12-01 16:23:55 +0800446}
447
Mike Frysingere7b8e672010-01-26 04:40:03 -0500448unsigned long __init arch_syscall_addr(int nr)
449{
450 return (unsigned long)sys_call_table[nr];
451}
452
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200453int __init init_ftrace_syscalls(void)
454{
455 struct syscall_metadata *meta;
456 unsigned long addr;
457 int i;
458
459 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
460 NR_syscalls, GFP_KERNEL);
461 if (!syscalls_metadata) {
462 WARN_ON(1);
463 return -ENOMEM;
464 }
465
466 for (i = 0; i < NR_syscalls; i++) {
467 addr = arch_syscall_addr(i);
468 meta = find_syscall_meta(addr);
Lai Jiangshanc252f652009-12-01 16:23:47 +0800469 if (!meta)
470 continue;
471
472 meta->syscall_nr = i;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200473 syscalls_metadata[i] = meta;
474 }
475
476 return 0;
477}
478core_initcall(init_ftrace_syscalls);
479
Li Zefan07b139c2009-12-21 14:27:35 +0800480#ifdef CONFIG_PERF_EVENTS
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200481
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100482static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls);
483static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
484static int sys_perf_refcount_enter;
485static int sys_perf_refcount_exit;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400486
Steven Rostedt38516ab2010-04-20 17:04:50 -0400487static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400488{
489 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200490 struct syscall_trace_enter *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200491 struct hlist_head *head;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400492 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100493 int rctx;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200494 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400495
496 syscall_nr = syscall_get_nr(current, regs);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100497 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400498 return;
499
500 sys_data = syscall_nr_to_meta(syscall_nr);
501 if (!sys_data)
502 return;
503
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200504 /* get the size after alignment with the u32 buffer size field */
505 size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
506 size = ALIGN(size + sizeof(u32), sizeof(u64));
507 size -= sizeof(u32);
508
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100509 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
510 "perf buffer not large enough"))
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200511 return;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200512
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100513 rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size,
Steven Rostedtff5f1492010-05-21 11:49:57 -0400514 sys_data->enter_event->event.type, regs, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800515 if (!rec)
516 return;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200517
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200518 rec->nr = syscall_nr;
519 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
520 (unsigned long *)&rec->args);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200521
522 head = per_cpu_ptr(sys_data->enter_event->perf_events, smp_processor_id());
523 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400524}
525
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100526int perf_sysenter_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400527{
528 int ret = 0;
529 int num;
530
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800531 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400532
533 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100534 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400535 ret = register_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400536 if (ret) {
537 pr_info("event trace: Could not activate"
538 "syscall entry trace point");
539 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100540 set_bit(num, enabled_perf_enter_syscalls);
541 sys_perf_refcount_enter++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400542 }
543 mutex_unlock(&syscall_trace_lock);
544 return ret;
545}
546
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100547void perf_sysenter_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400548{
549 int num;
550
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800551 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400552
553 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100554 sys_perf_refcount_enter--;
555 clear_bit(num, enabled_perf_enter_syscalls);
556 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400557 unregister_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400558 mutex_unlock(&syscall_trace_lock);
559}
560
Steven Rostedt38516ab2010-04-20 17:04:50 -0400561static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400562{
563 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200564 struct syscall_trace_exit *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200565 struct hlist_head *head;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400566 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100567 int rctx;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200568 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400569
570 syscall_nr = syscall_get_nr(current, regs);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100571 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400572 return;
573
574 sys_data = syscall_nr_to_meta(syscall_nr);
575 if (!sys_data)
576 return;
577
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200578 /* We can probably do that at build time */
579 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
580 size -= sizeof(u32);
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200581
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200582 /*
583 * Impossible, but be paranoid with the future
584 * How to put this check outside runtime?
585 */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100586 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
587 "exit event has grown above perf buffer size"))
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200588 return;
589
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100590 rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size,
Steven Rostedtff5f1492010-05-21 11:49:57 -0400591 sys_data->exit_event->event.type, regs, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800592 if (!rec)
593 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100594
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200595 rec->nr = syscall_nr;
596 rec->ret = syscall_get_return_value(current, regs);
597
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200598 head = per_cpu_ptr(sys_data->exit_event->perf_events, smp_processor_id());
599 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400600}
601
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100602int perf_sysexit_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400603{
604 int ret = 0;
605 int num;
606
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800607 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400608
609 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100610 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400611 ret = register_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400612 if (ret) {
613 pr_info("event trace: Could not activate"
Wenji Huang65746582010-02-24 15:40:22 +0800614 "syscall exit trace point");
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400615 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100616 set_bit(num, enabled_perf_exit_syscalls);
617 sys_perf_refcount_exit++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400618 }
619 mutex_unlock(&syscall_trace_lock);
620 return ret;
621}
622
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100623void perf_sysexit_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400624{
625 int num;
626
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800627 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400628
629 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100630 sys_perf_refcount_exit--;
631 clear_bit(num, enabled_perf_exit_syscalls);
632 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400633 unregister_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400634 mutex_unlock(&syscall_trace_lock);
635}
636
Li Zefan07b139c2009-12-21 14:27:35 +0800637#endif /* CONFIG_PERF_EVENTS */
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400638
Steven Rostedt22392912010-04-21 12:27:06 -0400639static int syscall_enter_register(struct ftrace_event_call *event,
640 enum trace_reg type)
641{
642 switch (type) {
643 case TRACE_REG_REGISTER:
644 return reg_event_syscall_enter(event);
645 case TRACE_REG_UNREGISTER:
646 unreg_event_syscall_enter(event);
647 return 0;
648
649#ifdef CONFIG_PERF_EVENTS
650 case TRACE_REG_PERF_REGISTER:
651 return perf_sysenter_enable(event);
652 case TRACE_REG_PERF_UNREGISTER:
653 perf_sysenter_disable(event);
654 return 0;
655#endif
656 }
657 return 0;
658}
659
660static int syscall_exit_register(struct ftrace_event_call *event,
661 enum trace_reg type)
662{
663 switch (type) {
664 case TRACE_REG_REGISTER:
665 return reg_event_syscall_exit(event);
666 case TRACE_REG_UNREGISTER:
667 unreg_event_syscall_exit(event);
668 return 0;
669
670#ifdef CONFIG_PERF_EVENTS
671 case TRACE_REG_PERF_REGISTER:
672 return perf_sysexit_enable(event);
673 case TRACE_REG_PERF_UNREGISTER:
674 perf_sysexit_disable(event);
675 return 0;
676#endif
677 }
678 return 0;
679}