blob: f97f6e3a676ce35b1606b58fc9da694e38d83204 [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>
Steven Rostedtf431b632013-02-12 16:18:59 -05003#include <linux/syscalls.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01005#include <linux/kernel.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -04006#include <linux/module.h> /* for MODULE_NAME_LEN via KSYM_SYMBOL_LEN */
Jason Baronfb34a082009-08-10 16:52:47 -04007#include <linux/ftrace.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008#include <linux/perf_event.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01009#include <asm/syscall.h>
10
11#include "trace_output.h"
12#include "trace.h"
13
Frederic Weisbecker5be71b62009-03-15 22:10:37 +010014static DEFINE_MUTEX(syscall_trace_lock);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +010015
Steven Rostedt22392912010-04-21 12:27:06 -040016static int syscall_enter_register(struct ftrace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +010017 enum trace_reg type, void *data);
Steven Rostedt22392912010-04-21 12:27:06 -040018static int syscall_exit_register(struct ftrace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +010019 enum trace_reg type, void *data);
Steven Rostedt22392912010-04-21 12:27:06 -040020
Steven Rostedt2e33af02010-04-22 10:35:55 -040021static struct list_head *
22syscall_get_enter_fields(struct ftrace_event_call *call)
23{
24 struct syscall_metadata *entry = call->data;
25
26 return &entry->enter_fields;
27}
28
Steven Rostedt3d56e332011-02-02 17:06:09 -050029extern struct syscall_metadata *__start_syscalls_metadata[];
30extern struct syscall_metadata *__stop_syscalls_metadata[];
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020031
32static struct syscall_metadata **syscalls_metadata;
33
Ian Munsieb2d55492011-02-03 14:27:23 +110034#ifndef ARCH_HAS_SYSCALL_MATCH_SYM_NAME
35static inline bool arch_syscall_match_sym_name(const char *sym, const char *name)
36{
37 /*
38 * Only compare after the "sys" prefix. Archs that use
39 * syscall wrappers may have syscalls symbols aliases prefixed
zhangwei(Jovi)36a78e92013-03-11 15:13:51 +080040 * with ".SyS" or ".sys" instead of "sys", leading to an unwanted
Ian Munsieb2d55492011-02-03 14:27:23 +110041 * mismatch.
42 */
43 return !strcmp(sym + 3, name + 3);
44}
45#endif
46
Steven Rostedtf431b632013-02-12 16:18:59 -050047#ifdef ARCH_TRACE_IGNORE_COMPAT_SYSCALLS
48/*
49 * Some architectures that allow for 32bit applications
50 * to run on a 64bit kernel, do not map the syscalls for
51 * the 32bit tasks the same as they do for 64bit tasks.
52 *
53 * *cough*x86*cough*
54 *
55 * In such a case, instead of reporting the wrong syscalls,
56 * simply ignore them.
57 *
58 * For an arch to ignore the compat syscalls it needs to
59 * define ARCH_TRACE_IGNORE_COMPAT_SYSCALLS as well as
60 * define the function arch_trace_is_compat_syscall() to let
61 * the tracing system know that it should ignore it.
62 */
63static int
64trace_get_syscall_nr(struct task_struct *task, struct pt_regs *regs)
65{
66 if (unlikely(arch_trace_is_compat_syscall(regs)))
67 return -1;
68
69 return syscall_get_nr(task, regs);
70}
71#else
72static inline int
73trace_get_syscall_nr(struct task_struct *task, struct pt_regs *regs)
74{
75 return syscall_get_nr(task, regs);
76}
77#endif /* ARCH_TRACE_IGNORE_COMPAT_SYSCALLS */
78
Steven Rostedt3d56e332011-02-02 17:06:09 -050079static __init struct syscall_metadata *
80find_syscall_meta(unsigned long syscall)
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020081{
Steven Rostedt3d56e332011-02-02 17:06:09 -050082 struct syscall_metadata **start;
83 struct syscall_metadata **stop;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020084 char str[KSYM_SYMBOL_LEN];
85
86
Steven Rostedt3d56e332011-02-02 17:06:09 -050087 start = __start_syscalls_metadata;
88 stop = __stop_syscalls_metadata;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020089 kallsyms_lookup(syscall, NULL, NULL, NULL, str);
90
Ian Munsieae07f552011-02-03 14:27:25 +110091 if (arch_syscall_match_sym_name(str, "sys_ni_syscall"))
92 return NULL;
93
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020094 for ( ; start < stop; start++) {
Ian Munsieb2d55492011-02-03 14:27:23 +110095 if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name))
Steven Rostedt3d56e332011-02-02 17:06:09 -050096 return *start;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020097 }
98 return NULL;
99}
100
101static struct syscall_metadata *syscall_nr_to_meta(int nr)
102{
103 if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
104 return NULL;
105
106 return syscalls_metadata[nr];
107}
108
Fengguang Wu6aea49c2012-11-21 15:13:47 +0800109static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -0400110print_syscall_enter(struct trace_iterator *iter, int flags,
111 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100112{
113 struct trace_seq *s = &iter->seq;
114 struct trace_entry *ent = iter->ent;
115 struct syscall_trace_enter *trace;
116 struct syscall_metadata *entry;
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500117 int i, syscall;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100118
Jason Baron64c12e02009-08-10 16:52:53 -0400119 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100120 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100121 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400122
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100123 if (!entry)
124 goto end;
125
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400126 if (entry->enter_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400127 WARN_ON_ONCE(1);
128 goto end;
129 }
130
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500131 trace_seq_printf(s, "%s(", entry->name);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100132
133 for (i = 0; i < entry->nb_args; i++) {
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500134
135 if (trace_seq_has_overflowed(s))
136 goto end;
137
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100138 /* parameter types */
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500139 if (trace_flags & TRACE_ITER_VERBOSE)
140 trace_seq_printf(s, "%s ", entry->types[i]);
141
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100142 /* parameter values */
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500143 trace_seq_printf(s, "%s: %lx%s", entry->args[i],
144 trace->args[i],
145 i == entry->nb_args - 1 ? "" : ", ");
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100146 }
147
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500148 trace_seq_putc(s, ')');
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100149end:
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500150 trace_seq_putc(s, '\n');
Li Zefan4539f072009-08-20 16:13:35 +0800151
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500152 return trace_handle_return(s);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100153}
154
Fengguang Wu6aea49c2012-11-21 15:13:47 +0800155static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -0400156print_syscall_exit(struct trace_iterator *iter, int flags,
157 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100158{
159 struct trace_seq *s = &iter->seq;
160 struct trace_entry *ent = iter->ent;
161 struct syscall_trace_exit *trace;
162 int syscall;
163 struct syscall_metadata *entry;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100164
Jason Baron64c12e02009-08-10 16:52:53 -0400165 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100166 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100167 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400168
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100169 if (!entry) {
zhangwei(Jovi)146c3442013-07-15 16:32:44 +0800170 trace_seq_putc(s, '\n');
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500171 goto out;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100172 }
173
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400174 if (entry->exit_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400175 WARN_ON_ONCE(1);
176 return TRACE_TYPE_UNHANDLED;
177 }
178
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500179 trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100180 trace->ret);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100181
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500182 out:
183 return trace_handle_return(s);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100184}
185
Li Zefane6971962009-08-19 15:52:25 +0800186extern char *__bad_type_size(void);
187
188#define SYSCALL_FIELD(type, name) \
189 sizeof(type) != sizeof(trace.name) ? \
190 __bad_type_size() : \
Tom Zanussi26a50742009-10-06 01:09:50 -0500191 #type, #name, offsetof(typeof(trace), name), \
192 sizeof(trace.name), is_signed_type(type)
Li Zefane6971962009-08-19 15:52:25 +0800193
Li Zefan3ddc77f2013-03-27 14:15:37 +0800194static int __init
195__set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
Lai Jiangshan50307a42009-12-15 15:39:45 +0800196{
197 int i;
198 int pos = 0;
199
200 /* When len=0, we just calculate the needed length */
201#define LEN_OR_ZERO (len ? len - pos : 0)
202
203 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
204 for (i = 0; i < entry->nb_args; i++) {
205 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s",
206 entry->args[i], sizeof(unsigned long),
207 i == entry->nb_args - 1 ? "" : ", ");
208 }
209 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
210
211 for (i = 0; i < entry->nb_args; i++) {
212 pos += snprintf(buf + pos, LEN_OR_ZERO,
213 ", ((unsigned long)(REC->%s))", entry->args[i]);
214 }
215
216#undef LEN_OR_ZERO
217
218 /* return the length of print_fmt */
219 return pos;
220}
221
Li Zefan3ddc77f2013-03-27 14:15:37 +0800222static int __init set_syscall_print_fmt(struct ftrace_event_call *call)
Lai Jiangshan50307a42009-12-15 15:39:45 +0800223{
224 char *print_fmt;
225 int len;
226 struct syscall_metadata *entry = call->data;
227
228 if (entry->enter_event != call) {
229 call->print_fmt = "\"0x%lx\", REC->ret";
230 return 0;
231 }
232
233 /* First: called with 0 length to calculate the needed length */
234 len = __set_enter_print_fmt(entry, NULL, 0);
235
236 print_fmt = kmalloc(len + 1, GFP_KERNEL);
237 if (!print_fmt)
238 return -ENOMEM;
239
240 /* Second: actually write the @print_fmt */
241 __set_enter_print_fmt(entry, print_fmt, len + 1);
242 call->print_fmt = print_fmt;
243
244 return 0;
245}
246
Li Zefan3ddc77f2013-03-27 14:15:37 +0800247static void __init free_syscall_print_fmt(struct ftrace_event_call *call)
Lai Jiangshan50307a42009-12-15 15:39:45 +0800248{
249 struct syscall_metadata *entry = call->data;
250
251 if (entry->enter_event == call)
252 kfree(call->print_fmt);
253}
254
Li Zefanb8aae392013-02-21 10:33:58 +0800255static int __init syscall_enter_define_fields(struct ftrace_event_call *call)
Li Zefan540b7b82009-08-19 15:54:51 +0800256{
257 struct syscall_trace_enter trace;
Lai Jiangshan31c16b12009-12-01 16:23:30 +0800258 struct syscall_metadata *meta = call->data;
Li Zefan540b7b82009-08-19 15:54:51 +0800259 int ret;
Li Zefan540b7b82009-08-19 15:54:51 +0800260 int i;
261 int offset = offsetof(typeof(trace), args);
262
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800263 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
264 if (ret)
265 return ret;
266
Li Zefan540b7b82009-08-19 15:54:51 +0800267 for (i = 0; i < meta->nb_args; i++) {
Frederic Weisbeckeraeaeae12009-08-27 05:09:51 +0200268 ret = trace_define_field(call, meta->types[i],
269 meta->args[i], offset,
Li Zefan43b51ea2009-08-07 10:33:22 +0800270 sizeof(unsigned long), 0,
271 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800272 offset += sizeof(unsigned long);
273 }
274
275 return ret;
276}
277
Li Zefanb8aae392013-02-21 10:33:58 +0800278static int __init syscall_exit_define_fields(struct ftrace_event_call *call)
Li Zefan540b7b82009-08-19 15:54:51 +0800279{
280 struct syscall_trace_exit trace;
281 int ret;
282
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800283 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
284 if (ret)
285 return ret;
286
Tom Zanussi26a50742009-10-06 01:09:50 -0500287 ret = trace_define_field(call, SYSCALL_FIELD(long, ret),
Li Zefan43b51ea2009-08-07 10:33:22 +0800288 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800289
290 return ret;
291}
292
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400293static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100294{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400295 struct trace_array *tr = data;
Tom Zanussid562aff2013-10-24 08:34:19 -0500296 struct ftrace_event_file *ftrace_file;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100297 struct syscall_trace_enter *entry;
298 struct syscall_metadata *sys_data;
299 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400300 struct ring_buffer *buffer;
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800301 unsigned long irq_flags;
302 int pc;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100303 int syscall_nr;
Steven Rostedtf431b632013-02-12 16:18:59 -0500304 int size;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100305
Steven Rostedtf431b632013-02-12 16:18:59 -0500306 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100307 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200308 return;
Tom Zanussid562aff2013-10-24 08:34:19 -0500309
310 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE) */
311 ftrace_file = rcu_dereference_sched(tr->enter_syscall_files[syscall_nr]);
312 if (!ftrace_file)
313 return;
314
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500315 if (ftrace_trigger_soft_disabled(ftrace_file))
316 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100317
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100318 sys_data = syscall_nr_to_meta(syscall_nr);
319 if (!sys_data)
320 return;
321
322 size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
323
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800324 local_save_flags(irq_flags);
325 pc = preempt_count();
326
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500327 buffer = tr->trace_buffer.buffer;
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400328 event = trace_buffer_lock_reserve(buffer,
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800329 sys_data->enter_event->event.type, size, irq_flags, pc);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100330 if (!event)
331 return;
332
333 entry = ring_buffer_event_data(event);
334 entry->nr = syscall_nr;
335 syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
336
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500337 event_trigger_unlock_commit(ftrace_file, buffer, event, entry,
338 irq_flags, pc);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100339}
340
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400341static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100342{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400343 struct trace_array *tr = data;
Tom Zanussid562aff2013-10-24 08:34:19 -0500344 struct ftrace_event_file *ftrace_file;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100345 struct syscall_trace_exit *entry;
346 struct syscall_metadata *sys_data;
347 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400348 struct ring_buffer *buffer;
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800349 unsigned long irq_flags;
350 int pc;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100351 int syscall_nr;
352
Steven Rostedtf431b632013-02-12 16:18:59 -0500353 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100354 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200355 return;
Tom Zanussid562aff2013-10-24 08:34:19 -0500356
357 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE()) */
358 ftrace_file = rcu_dereference_sched(tr->exit_syscall_files[syscall_nr]);
359 if (!ftrace_file)
360 return;
361
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500362 if (ftrace_trigger_soft_disabled(ftrace_file))
363 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100364
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100365 sys_data = syscall_nr_to_meta(syscall_nr);
366 if (!sys_data)
367 return;
368
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800369 local_save_flags(irq_flags);
370 pc = preempt_count();
371
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500372 buffer = tr->trace_buffer.buffer;
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400373 event = trace_buffer_lock_reserve(buffer,
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800374 sys_data->exit_event->event.type, sizeof(*entry),
375 irq_flags, pc);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100376 if (!event)
377 return;
378
379 entry = ring_buffer_event_data(event);
380 entry->nr = syscall_nr;
381 entry->ret = syscall_get_return_value(current, regs);
382
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500383 event_trigger_unlock_commit(ftrace_file, buffer, event, entry,
384 irq_flags, pc);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100385}
386
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400387static int reg_event_syscall_enter(struct ftrace_event_file *file,
388 struct ftrace_event_call *call)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100389{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400390 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400391 int ret = 0;
392 int num;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100393
Lai Jiangshanc252f652009-12-01 16:23:47 +0800394 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100395 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400396 return -ENOSYS;
397 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400398 if (!tr->sys_refcount_enter)
399 ret = register_trace_sys_enter(ftrace_syscall_enter, tr);
Li Zefan3b8e4272009-12-08 11:14:52 +0800400 if (!ret) {
Tom Zanussid562aff2013-10-24 08:34:19 -0500401 rcu_assign_pointer(tr->enter_syscall_files[num], file);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400402 tr->sys_refcount_enter++;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100403 }
Jason Baronfb34a082009-08-10 16:52:47 -0400404 mutex_unlock(&syscall_trace_lock);
405 return ret;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100406}
Jason Baronfb34a082009-08-10 16:52:47 -0400407
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400408static void unreg_event_syscall_enter(struct ftrace_event_file *file,
409 struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400410{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400411 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400412 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400413
Lai Jiangshanc252f652009-12-01 16:23:47 +0800414 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100415 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400416 return;
417 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400418 tr->sys_refcount_enter--;
Andreea-Cristina Bernatfb5a6132014-08-22 17:28:22 +0300419 RCU_INIT_POINTER(tr->enter_syscall_files[num], NULL);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400420 if (!tr->sys_refcount_enter)
421 unregister_trace_sys_enter(ftrace_syscall_enter, tr);
Jason Baronfb34a082009-08-10 16:52:47 -0400422 mutex_unlock(&syscall_trace_lock);
423}
424
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400425static int reg_event_syscall_exit(struct ftrace_event_file *file,
426 struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400427{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400428 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400429 int ret = 0;
430 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400431
Lai Jiangshanc252f652009-12-01 16:23:47 +0800432 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100433 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400434 return -ENOSYS;
435 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400436 if (!tr->sys_refcount_exit)
437 ret = register_trace_sys_exit(ftrace_syscall_exit, tr);
Li Zefan3b8e4272009-12-08 11:14:52 +0800438 if (!ret) {
Tom Zanussid562aff2013-10-24 08:34:19 -0500439 rcu_assign_pointer(tr->exit_syscall_files[num], file);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400440 tr->sys_refcount_exit++;
Jason Baronfb34a082009-08-10 16:52:47 -0400441 }
442 mutex_unlock(&syscall_trace_lock);
443 return ret;
444}
445
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400446static void unreg_event_syscall_exit(struct ftrace_event_file *file,
447 struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400448{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400449 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400450 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400451
Lai Jiangshanc252f652009-12-01 16:23:47 +0800452 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100453 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400454 return;
455 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400456 tr->sys_refcount_exit--;
Andreea-Cristina Bernatfb5a6132014-08-22 17:28:22 +0300457 RCU_INIT_POINTER(tr->exit_syscall_files[num], NULL);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400458 if (!tr->sys_refcount_exit)
459 unregister_trace_sys_exit(ftrace_syscall_exit, tr);
Jason Baronfb34a082009-08-10 16:52:47 -0400460 mutex_unlock(&syscall_trace_lock);
461}
462
Li Zefan3ddc77f2013-03-27 14:15:37 +0800463static int __init init_syscall_trace(struct ftrace_event_call *call)
Lai Jiangshana1301da2009-12-01 16:23:55 +0800464{
465 int id;
Ian Munsieba976972011-02-03 14:27:20 +1100466 int num;
467
468 num = ((struct syscall_metadata *)call->data)->syscall_nr;
469 if (num < 0 || num >= NR_syscalls) {
470 pr_debug("syscall %s metadata not mapped, disabling ftrace event\n",
471 ((struct syscall_metadata *)call->data)->name);
472 return -ENOSYS;
473 }
Lai Jiangshana1301da2009-12-01 16:23:55 +0800474
Lai Jiangshan50307a42009-12-15 15:39:45 +0800475 if (set_syscall_print_fmt(call) < 0)
476 return -ENOMEM;
477
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500478 id = trace_event_raw_init(call);
479
480 if (id < 0) {
Lai Jiangshan50307a42009-12-15 15:39:45 +0800481 free_syscall_print_fmt(call);
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500482 return id;
Lai Jiangshan50307a42009-12-15 15:39:45 +0800483 }
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500484
485 return id;
Lai Jiangshana1301da2009-12-01 16:23:55 +0800486}
487
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700488struct trace_event_functions enter_syscall_print_funcs = {
489 .trace = print_syscall_enter,
490};
491
492struct trace_event_functions exit_syscall_print_funcs = {
493 .trace = print_syscall_exit,
494};
495
Li Zefan523c8112013-03-04 14:15:59 +0800496struct ftrace_event_class __refdata event_class_syscall_enter = {
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700497 .system = "syscalls",
498 .reg = syscall_enter_register,
499 .define_fields = syscall_enter_define_fields,
500 .get_fields = syscall_get_enter_fields,
501 .raw_init = init_syscall_trace,
502};
503
Li Zefan523c8112013-03-04 14:15:59 +0800504struct ftrace_event_class __refdata event_class_syscall_exit = {
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700505 .system = "syscalls",
506 .reg = syscall_exit_register,
507 .define_fields = syscall_exit_define_fields,
508 .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields),
509 .raw_init = init_syscall_trace,
510};
511
Ian Munsiec763ba02011-02-03 14:27:22 +1100512unsigned long __init __weak arch_syscall_addr(int nr)
Mike Frysingere7b8e672010-01-26 04:40:03 -0500513{
514 return (unsigned long)sys_call_table[nr];
515}
516
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -0500517void __init init_ftrace_syscalls(void)
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200518{
519 struct syscall_metadata *meta;
520 unsigned long addr;
521 int i;
522
Thomas Meyer47b0edc2011-11-29 22:08:00 +0100523 syscalls_metadata = kcalloc(NR_syscalls, sizeof(*syscalls_metadata),
524 GFP_KERNEL);
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200525 if (!syscalls_metadata) {
526 WARN_ON(1);
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -0500527 return;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200528 }
529
530 for (i = 0; i < NR_syscalls; i++) {
531 addr = arch_syscall_addr(i);
532 meta = find_syscall_meta(addr);
Lai Jiangshanc252f652009-12-01 16:23:47 +0800533 if (!meta)
534 continue;
535
536 meta->syscall_nr = i;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200537 syscalls_metadata[i] = meta;
538 }
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200539}
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200540
Li Zefan07b139c2009-12-21 14:27:35 +0800541#ifdef CONFIG_PERF_EVENTS
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200542
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100543static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls);
544static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
545static int sys_perf_refcount_enter;
546static int sys_perf_refcount_exit;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400547
Steven Rostedt38516ab2010-04-20 17:04:50 -0400548static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400549{
550 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200551 struct syscall_trace_enter *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200552 struct hlist_head *head;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400553 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100554 int rctx;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200555 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400556
Steven Rostedtf431b632013-02-12 16:18:59 -0500557 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100558 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Will Deacon60916a92012-08-16 18:14:14 +0100559 return;
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100560 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400561 return;
562
563 sys_data = syscall_nr_to_meta(syscall_nr);
564 if (!sys_data)
565 return;
566
Oleg Nesterov421c7862013-06-17 19:02:07 +0200567 head = this_cpu_ptr(sys_data->enter_event->perf_events);
568 if (hlist_empty(head))
569 return;
570
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200571 /* get the size after alignment with the u32 buffer size field */
572 size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
573 size = ALIGN(size + sizeof(u32), sizeof(u64));
574 size -= sizeof(u32);
575
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100576 rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size,
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +0100577 sys_data->enter_event->event.type, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800578 if (!rec)
579 return;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200580
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200581 rec->nr = syscall_nr;
582 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
583 (unsigned long *)&rec->args);
Andrew Vagine6dab5f2012-07-11 18:14:58 +0400584 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400585}
586
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700587static int perf_sysenter_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400588{
589 int ret = 0;
590 int num;
591
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800592 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400593
594 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100595 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400596 ret = register_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400597 if (ret) {
598 pr_info("event trace: Could not activate"
599 "syscall entry trace point");
600 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100601 set_bit(num, enabled_perf_enter_syscalls);
602 sys_perf_refcount_enter++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400603 }
604 mutex_unlock(&syscall_trace_lock);
605 return ret;
606}
607
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700608static void perf_sysenter_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400609{
610 int num;
611
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800612 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400613
614 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100615 sys_perf_refcount_enter--;
616 clear_bit(num, enabled_perf_enter_syscalls);
617 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400618 unregister_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400619 mutex_unlock(&syscall_trace_lock);
620}
621
Steven Rostedt38516ab2010-04-20 17:04:50 -0400622static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400623{
624 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200625 struct syscall_trace_exit *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200626 struct hlist_head *head;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400627 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100628 int rctx;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200629 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400630
Steven Rostedtf431b632013-02-12 16:18:59 -0500631 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100632 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Will Deacon60916a92012-08-16 18:14:14 +0100633 return;
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100634 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400635 return;
636
637 sys_data = syscall_nr_to_meta(syscall_nr);
638 if (!sys_data)
639 return;
640
Oleg Nesterov421c7862013-06-17 19:02:07 +0200641 head = this_cpu_ptr(sys_data->exit_event->perf_events);
642 if (hlist_empty(head))
643 return;
644
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200645 /* We can probably do that at build time */
646 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
647 size -= sizeof(u32);
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200648
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100649 rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size,
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +0100650 sys_data->exit_event->event.type, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800651 if (!rec)
652 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100653
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200654 rec->nr = syscall_nr;
655 rec->ret = syscall_get_return_value(current, regs);
Andrew Vagine6dab5f2012-07-11 18:14:58 +0400656 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400657}
658
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700659static int perf_sysexit_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400660{
661 int ret = 0;
662 int num;
663
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800664 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400665
666 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100667 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400668 ret = register_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400669 if (ret) {
670 pr_info("event trace: Could not activate"
Wenji Huang65746582010-02-24 15:40:22 +0800671 "syscall exit trace point");
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400672 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100673 set_bit(num, enabled_perf_exit_syscalls);
674 sys_perf_refcount_exit++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400675 }
676 mutex_unlock(&syscall_trace_lock);
677 return ret;
678}
679
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700680static void perf_sysexit_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400681{
682 int num;
683
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800684 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400685
686 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100687 sys_perf_refcount_exit--;
688 clear_bit(num, enabled_perf_exit_syscalls);
689 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400690 unregister_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400691 mutex_unlock(&syscall_trace_lock);
692}
693
Li Zefan07b139c2009-12-21 14:27:35 +0800694#endif /* CONFIG_PERF_EVENTS */
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400695
Steven Rostedt22392912010-04-21 12:27:06 -0400696static int syscall_enter_register(struct ftrace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +0100697 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -0400698{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400699 struct ftrace_event_file *file = data;
700
Steven Rostedt22392912010-04-21 12:27:06 -0400701 switch (type) {
702 case TRACE_REG_REGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400703 return reg_event_syscall_enter(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400704 case TRACE_REG_UNREGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400705 unreg_event_syscall_enter(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400706 return 0;
707
708#ifdef CONFIG_PERF_EVENTS
709 case TRACE_REG_PERF_REGISTER:
710 return perf_sysenter_enable(event);
711 case TRACE_REG_PERF_UNREGISTER:
712 perf_sysenter_disable(event);
713 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100714 case TRACE_REG_PERF_OPEN:
715 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100716 case TRACE_REG_PERF_ADD:
717 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100718 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -0400719#endif
720 }
721 return 0;
722}
723
724static int syscall_exit_register(struct ftrace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +0100725 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -0400726{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400727 struct ftrace_event_file *file = data;
728
Steven Rostedt22392912010-04-21 12:27:06 -0400729 switch (type) {
730 case TRACE_REG_REGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400731 return reg_event_syscall_exit(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400732 case TRACE_REG_UNREGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400733 unreg_event_syscall_exit(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400734 return 0;
735
736#ifdef CONFIG_PERF_EVENTS
737 case TRACE_REG_PERF_REGISTER:
738 return perf_sysexit_enable(event);
739 case TRACE_REG_PERF_UNREGISTER:
740 perf_sysexit_disable(event);
741 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100742 case TRACE_REG_PERF_OPEN:
743 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100744 case TRACE_REG_PERF_ADD:
745 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100746 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -0400747#endif
748 }
749 return 0;
750}