blob: 86a23e7de031fe1f9d21bab64e1c8080729ac6df [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
Steven Rostedt80decc72010-04-23 10:00:22 -040034struct trace_event_functions enter_syscall_print_funcs = {
Lai Jiangshan7f858032011-01-14 17:49:02 +080035 .trace = print_syscall_enter,
Steven Rostedt80decc72010-04-23 10:00:22 -040036};
37
38struct trace_event_functions exit_syscall_print_funcs = {
Lai Jiangshan7f858032011-01-14 17:49:02 +080039 .trace = print_syscall_exit,
Steven Rostedt80decc72010-04-23 10:00:22 -040040};
41
Steven Rostedt22392912010-04-21 12:27:06 -040042struct ftrace_event_class event_class_syscall_enter = {
Lai Jiangshan7f858032011-01-14 17:49:02 +080043 .system = "syscalls",
44 .reg = syscall_enter_register,
45 .define_fields = syscall_enter_define_fields,
46 .get_fields = syscall_get_enter_fields,
47 .raw_init = init_syscall_trace,
Steven Rostedt22392912010-04-21 12:27:06 -040048};
49
50struct ftrace_event_class event_class_syscall_exit = {
Lai Jiangshan7f858032011-01-14 17:49:02 +080051 .system = "syscalls",
52 .reg = syscall_exit_register,
53 .define_fields = syscall_exit_define_fields,
54 .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields),
55 .raw_init = init_syscall_trace,
Steven Rostedt8f082012010-04-20 10:47:33 -040056};
57
Steven Rostedt3d56e332011-02-02 17:06:09 -050058extern struct syscall_metadata *__start_syscalls_metadata[];
59extern struct syscall_metadata *__stop_syscalls_metadata[];
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020060
61static struct syscall_metadata **syscalls_metadata;
62
Ian Munsieb2d55492011-02-03 14:27:23 +110063#ifndef ARCH_HAS_SYSCALL_MATCH_SYM_NAME
64static inline bool arch_syscall_match_sym_name(const char *sym, const char *name)
65{
66 /*
67 * Only compare after the "sys" prefix. Archs that use
68 * syscall wrappers may have syscalls symbols aliases prefixed
69 * with "SyS" instead of "sys", leading to an unwanted
70 * mismatch.
71 */
72 return !strcmp(sym + 3, name + 3);
73}
74#endif
75
Steven Rostedt3d56e332011-02-02 17:06:09 -050076static __init struct syscall_metadata *
77find_syscall_meta(unsigned long syscall)
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020078{
Steven Rostedt3d56e332011-02-02 17:06:09 -050079 struct syscall_metadata **start;
80 struct syscall_metadata **stop;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020081 char str[KSYM_SYMBOL_LEN];
82
83
Steven Rostedt3d56e332011-02-02 17:06:09 -050084 start = __start_syscalls_metadata;
85 stop = __stop_syscalls_metadata;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020086 kallsyms_lookup(syscall, NULL, NULL, NULL, str);
87
88 for ( ; start < stop; start++) {
Ian Munsieb2d55492011-02-03 14:27:23 +110089 if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name))
Steven Rostedt3d56e332011-02-02 17:06:09 -050090 return *start;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020091 }
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;
Ian Munsie3773b3892011-02-03 14:27:21 +1100369 if (WARN_ON_ONCE(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;
Ian Munsie3773b3892011-02-03 14:27:21 +1100387 if (WARN_ON_ONCE(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;
Ian Munsie3773b3892011-02-03 14:27:21 +1100403 if (WARN_ON_ONCE(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;
Ian Munsie3773b3892011-02-03 14:27:21 +1100421 if (WARN_ON_ONCE(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;
Ian Munsieba976972011-02-03 14:27:20 +1100434 int num;
435
436 num = ((struct syscall_metadata *)call->data)->syscall_nr;
437 if (num < 0 || num >= NR_syscalls) {
438 pr_debug("syscall %s metadata not mapped, disabling ftrace event\n",
439 ((struct syscall_metadata *)call->data)->name);
440 return -ENOSYS;
441 }
Lai Jiangshana1301da2009-12-01 16:23:55 +0800442
Lai Jiangshan50307a42009-12-15 15:39:45 +0800443 if (set_syscall_print_fmt(call) < 0)
444 return -ENOMEM;
445
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500446 id = trace_event_raw_init(call);
447
448 if (id < 0) {
Lai Jiangshan50307a42009-12-15 15:39:45 +0800449 free_syscall_print_fmt(call);
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500450 return id;
Lai Jiangshan50307a42009-12-15 15:39:45 +0800451 }
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500452
453 return id;
Lai Jiangshana1301da2009-12-01 16:23:55 +0800454}
455
Ian Munsiec763ba02011-02-03 14:27:22 +1100456unsigned long __init __weak arch_syscall_addr(int nr)
Mike Frysingere7b8e672010-01-26 04:40:03 -0500457{
458 return (unsigned long)sys_call_table[nr];
459}
460
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200461int __init init_ftrace_syscalls(void)
462{
463 struct syscall_metadata *meta;
464 unsigned long addr;
465 int i;
466
467 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
468 NR_syscalls, GFP_KERNEL);
469 if (!syscalls_metadata) {
470 WARN_ON(1);
471 return -ENOMEM;
472 }
473
474 for (i = 0; i < NR_syscalls; i++) {
475 addr = arch_syscall_addr(i);
476 meta = find_syscall_meta(addr);
Lai Jiangshanc252f652009-12-01 16:23:47 +0800477 if (!meta)
478 continue;
479
480 meta->syscall_nr = i;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200481 syscalls_metadata[i] = meta;
482 }
483
484 return 0;
485}
486core_initcall(init_ftrace_syscalls);
487
Li Zefan07b139c2009-12-21 14:27:35 +0800488#ifdef CONFIG_PERF_EVENTS
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200489
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100490static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls);
491static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
492static int sys_perf_refcount_enter;
493static int sys_perf_refcount_exit;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400494
Steven Rostedt38516ab2010-04-20 17:04:50 -0400495static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400496{
497 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200498 struct syscall_trace_enter *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200499 struct hlist_head *head;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400500 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100501 int rctx;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200502 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400503
504 syscall_nr = syscall_get_nr(current, regs);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100505 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400506 return;
507
508 sys_data = syscall_nr_to_meta(syscall_nr);
509 if (!sys_data)
510 return;
511
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200512 /* get the size after alignment with the u32 buffer size field */
513 size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
514 size = ALIGN(size + sizeof(u32), sizeof(u64));
515 size -= sizeof(u32);
516
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100517 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
518 "perf buffer not large enough"))
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200519 return;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200520
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100521 rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size,
Steven Rostedtff5f1492010-05-21 11:49:57 -0400522 sys_data->enter_event->event.type, regs, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800523 if (!rec)
524 return;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200525
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200526 rec->nr = syscall_nr;
527 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
528 (unsigned long *)&rec->args);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200529
Peter Zijlstra3771f072010-05-21 12:31:09 +0200530 head = this_cpu_ptr(sys_data->enter_event->perf_events);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200531 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400532}
533
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100534int perf_sysenter_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400535{
536 int ret = 0;
537 int num;
538
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800539 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400540
541 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100542 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400543 ret = register_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400544 if (ret) {
545 pr_info("event trace: Could not activate"
546 "syscall entry trace point");
547 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100548 set_bit(num, enabled_perf_enter_syscalls);
549 sys_perf_refcount_enter++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400550 }
551 mutex_unlock(&syscall_trace_lock);
552 return ret;
553}
554
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100555void perf_sysenter_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400556{
557 int num;
558
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800559 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400560
561 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100562 sys_perf_refcount_enter--;
563 clear_bit(num, enabled_perf_enter_syscalls);
564 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400565 unregister_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400566 mutex_unlock(&syscall_trace_lock);
567}
568
Steven Rostedt38516ab2010-04-20 17:04:50 -0400569static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400570{
571 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200572 struct syscall_trace_exit *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200573 struct hlist_head *head;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400574 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100575 int rctx;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200576 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400577
578 syscall_nr = syscall_get_nr(current, regs);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100579 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400580 return;
581
582 sys_data = syscall_nr_to_meta(syscall_nr);
583 if (!sys_data)
584 return;
585
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200586 /* We can probably do that at build time */
587 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
588 size -= sizeof(u32);
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200589
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200590 /*
591 * Impossible, but be paranoid with the future
592 * How to put this check outside runtime?
593 */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100594 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
595 "exit event has grown above perf buffer size"))
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200596 return;
597
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100598 rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size,
Steven Rostedtff5f1492010-05-21 11:49:57 -0400599 sys_data->exit_event->event.type, regs, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800600 if (!rec)
601 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100602
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200603 rec->nr = syscall_nr;
604 rec->ret = syscall_get_return_value(current, regs);
605
Peter Zijlstra3771f072010-05-21 12:31:09 +0200606 head = this_cpu_ptr(sys_data->exit_event->perf_events);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200607 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400608}
609
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100610int perf_sysexit_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400611{
612 int ret = 0;
613 int num;
614
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800615 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400616
617 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100618 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400619 ret = register_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400620 if (ret) {
621 pr_info("event trace: Could not activate"
Wenji Huang65746582010-02-24 15:40:22 +0800622 "syscall exit trace point");
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400623 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100624 set_bit(num, enabled_perf_exit_syscalls);
625 sys_perf_refcount_exit++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400626 }
627 mutex_unlock(&syscall_trace_lock);
628 return ret;
629}
630
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100631void perf_sysexit_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400632{
633 int num;
634
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800635 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400636
637 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100638 sys_perf_refcount_exit--;
639 clear_bit(num, enabled_perf_exit_syscalls);
640 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400641 unregister_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400642 mutex_unlock(&syscall_trace_lock);
643}
644
Li Zefan07b139c2009-12-21 14:27:35 +0800645#endif /* CONFIG_PERF_EVENTS */
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400646
Steven Rostedt22392912010-04-21 12:27:06 -0400647static int syscall_enter_register(struct ftrace_event_call *event,
648 enum trace_reg type)
649{
650 switch (type) {
651 case TRACE_REG_REGISTER:
652 return reg_event_syscall_enter(event);
653 case TRACE_REG_UNREGISTER:
654 unreg_event_syscall_enter(event);
655 return 0;
656
657#ifdef CONFIG_PERF_EVENTS
658 case TRACE_REG_PERF_REGISTER:
659 return perf_sysenter_enable(event);
660 case TRACE_REG_PERF_UNREGISTER:
661 perf_sysenter_disable(event);
662 return 0;
663#endif
664 }
665 return 0;
666}
667
668static int syscall_exit_register(struct ftrace_event_call *event,
669 enum trace_reg type)
670{
671 switch (type) {
672 case TRACE_REG_REGISTER:
673 return reg_event_syscall_exit(event);
674 case TRACE_REG_UNREGISTER:
675 unreg_event_syscall_exit(event);
676 return 0;
677
678#ifdef CONFIG_PERF_EVENTS
679 case TRACE_REG_PERF_REGISTER:
680 return perf_sysexit_enable(event);
681 case TRACE_REG_PERF_UNREGISTER:
682 perf_sysexit_disable(event);
683 return 0;
684#endif
685 }
686 return 0;
687}