blob: cb654542c1a1621fc325cba70699f8b2d1be9658 [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>
Paul Gortmaker56d82e02011-05-26 17:53:52 -04005#include <linux/module.h> /* for MODULE_NAME_LEN via KSYM_SYMBOL_LEN */
Jason Baronfb34a082009-08-10 16:52:47 -04006#include <linux/ftrace.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007#include <linux/perf_event.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01008#include <asm/syscall.h>
9
10#include "trace_output.h"
11#include "trace.h"
12
Frederic Weisbecker5be71b62009-03-15 22:10:37 +010013static DEFINE_MUTEX(syscall_trace_lock);
Jason Baronfb34a082009-08-10 16:52:47 -040014static int sys_refcount_enter;
15static int sys_refcount_exit;
Jason Baron57421db2009-08-24 17:40:22 -040016static DECLARE_BITMAP(enabled_enter_syscalls, NR_syscalls);
17static DECLARE_BITMAP(enabled_exit_syscalls, NR_syscalls);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +010018
Steven Rostedt22392912010-04-21 12:27:06 -040019static int syscall_enter_register(struct ftrace_event_call *event,
20 enum trace_reg type);
21static int syscall_exit_register(struct ftrace_event_call *event,
22 enum trace_reg type);
23
Steven Rostedt2e33af02010-04-22 10:35:55 -040024static int syscall_enter_define_fields(struct ftrace_event_call *call);
25static int syscall_exit_define_fields(struct ftrace_event_call *call);
26
27static struct list_head *
28syscall_get_enter_fields(struct ftrace_event_call *call)
29{
30 struct syscall_metadata *entry = call->data;
31
32 return &entry->enter_fields;
33}
34
Steven Rostedt80decc72010-04-23 10:00:22 -040035struct trace_event_functions enter_syscall_print_funcs = {
Lai Jiangshan7f858032011-01-14 17:49:02 +080036 .trace = print_syscall_enter,
Steven Rostedt80decc72010-04-23 10:00:22 -040037};
38
39struct trace_event_functions exit_syscall_print_funcs = {
Lai Jiangshan7f858032011-01-14 17:49:02 +080040 .trace = print_syscall_exit,
Steven Rostedt80decc72010-04-23 10:00:22 -040041};
42
Steven Rostedt22392912010-04-21 12:27:06 -040043struct ftrace_event_class event_class_syscall_enter = {
Lai Jiangshan7f858032011-01-14 17:49:02 +080044 .system = "syscalls",
45 .reg = syscall_enter_register,
46 .define_fields = syscall_enter_define_fields,
47 .get_fields = syscall_get_enter_fields,
48 .raw_init = init_syscall_trace,
Steven Rostedt22392912010-04-21 12:27:06 -040049};
50
51struct ftrace_event_class event_class_syscall_exit = {
Lai Jiangshan7f858032011-01-14 17:49:02 +080052 .system = "syscalls",
53 .reg = syscall_exit_register,
54 .define_fields = syscall_exit_define_fields,
55 .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields),
56 .raw_init = init_syscall_trace,
Steven Rostedt8f082012010-04-20 10:47:33 -040057};
58
Steven Rostedt3d56e332011-02-02 17:06:09 -050059extern struct syscall_metadata *__start_syscalls_metadata[];
60extern struct syscall_metadata *__stop_syscalls_metadata[];
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020061
62static struct syscall_metadata **syscalls_metadata;
63
Ian Munsieb2d55492011-02-03 14:27:23 +110064#ifndef ARCH_HAS_SYSCALL_MATCH_SYM_NAME
65static inline bool arch_syscall_match_sym_name(const char *sym, const char *name)
66{
67 /*
68 * Only compare after the "sys" prefix. Archs that use
69 * syscall wrappers may have syscalls symbols aliases prefixed
70 * with "SyS" instead of "sys", leading to an unwanted
71 * mismatch.
72 */
73 return !strcmp(sym + 3, name + 3);
74}
75#endif
76
Steven Rostedt3d56e332011-02-02 17:06:09 -050077static __init struct syscall_metadata *
78find_syscall_meta(unsigned long syscall)
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020079{
Steven Rostedt3d56e332011-02-02 17:06:09 -050080 struct syscall_metadata **start;
81 struct syscall_metadata **stop;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020082 char str[KSYM_SYMBOL_LEN];
83
84
Steven Rostedt3d56e332011-02-02 17:06:09 -050085 start = __start_syscalls_metadata;
86 stop = __stop_syscalls_metadata;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020087 kallsyms_lookup(syscall, NULL, NULL, NULL, str);
88
Ian Munsieae07f552011-02-03 14:27:25 +110089 if (arch_syscall_match_sym_name(str, "sys_ni_syscall"))
90 return NULL;
91
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020092 for ( ; start < stop; start++) {
Ian Munsieb2d55492011-02-03 14:27:23 +110093 if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name))
Steven Rostedt3d56e332011-02-02 17:06:09 -050094 return *start;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020095 }
96 return NULL;
97}
98
99static struct syscall_metadata *syscall_nr_to_meta(int nr)
100{
101 if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
102 return NULL;
103
104 return syscalls_metadata[nr];
105}
106
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100107enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -0400108print_syscall_enter(struct trace_iterator *iter, int flags,
109 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100110{
111 struct trace_seq *s = &iter->seq;
112 struct trace_entry *ent = iter->ent;
113 struct syscall_trace_enter *trace;
114 struct syscall_metadata *entry;
115 int i, ret, syscall;
116
Jason Baron64c12e02009-08-10 16:52:53 -0400117 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100118 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100119 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400120
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100121 if (!entry)
122 goto end;
123
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400124 if (entry->enter_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400125 WARN_ON_ONCE(1);
126 goto end;
127 }
128
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100129 ret = trace_seq_printf(s, "%s(", entry->name);
130 if (!ret)
131 return TRACE_TYPE_PARTIAL_LINE;
132
133 for (i = 0; i < entry->nb_args; i++) {
134 /* parameter types */
Li Zefanba8b3a42009-08-17 16:55:18 +0800135 if (trace_flags & TRACE_ITER_VERBOSE) {
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100136 ret = trace_seq_printf(s, "%s ", entry->types[i]);
137 if (!ret)
138 return TRACE_TYPE_PARTIAL_LINE;
139 }
140 /* parameter values */
Li Zefan4539f072009-08-20 16:13:35 +0800141 ret = trace_seq_printf(s, "%s: %lx%s", entry->args[i],
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100142 trace->args[i],
Li Zefan4539f072009-08-20 16:13:35 +0800143 i == entry->nb_args - 1 ? "" : ", ");
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100144 if (!ret)
145 return TRACE_TYPE_PARTIAL_LINE;
146 }
147
Li Zefan4539f072009-08-20 16:13:35 +0800148 ret = trace_seq_putc(s, ')');
149 if (!ret)
150 return TRACE_TYPE_PARTIAL_LINE;
151
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100152end:
Li Zefan4539f072009-08-20 16:13:35 +0800153 ret = trace_seq_putc(s, '\n');
154 if (!ret)
155 return TRACE_TYPE_PARTIAL_LINE;
156
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100157 return TRACE_TYPE_HANDLED;
158}
159
160enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -0400161print_syscall_exit(struct trace_iterator *iter, int flags,
162 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100163{
164 struct trace_seq *s = &iter->seq;
165 struct trace_entry *ent = iter->ent;
166 struct syscall_trace_exit *trace;
167 int syscall;
168 struct syscall_metadata *entry;
169 int ret;
170
Jason Baron64c12e02009-08-10 16:52:53 -0400171 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100172 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100173 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400174
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100175 if (!entry) {
176 trace_seq_printf(s, "\n");
177 return TRACE_TYPE_HANDLED;
178 }
179
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400180 if (entry->exit_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400181 WARN_ON_ONCE(1);
182 return TRACE_TYPE_UNHANDLED;
183 }
184
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100185 ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
186 trace->ret);
187 if (!ret)
188 return TRACE_TYPE_PARTIAL_LINE;
189
190 return TRACE_TYPE_HANDLED;
191}
192
Li Zefane6971962009-08-19 15:52:25 +0800193extern char *__bad_type_size(void);
194
195#define SYSCALL_FIELD(type, name) \
196 sizeof(type) != sizeof(trace.name) ? \
197 __bad_type_size() : \
Tom Zanussi26a50742009-10-06 01:09:50 -0500198 #type, #name, offsetof(typeof(trace), name), \
199 sizeof(trace.name), is_signed_type(type)
Li Zefane6971962009-08-19 15:52:25 +0800200
Lai Jiangshan50307a42009-12-15 15:39:45 +0800201static
202int __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
203{
204 int i;
205 int pos = 0;
206
207 /* When len=0, we just calculate the needed length */
208#define LEN_OR_ZERO (len ? len - pos : 0)
209
210 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
211 for (i = 0; i < entry->nb_args; i++) {
212 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s",
213 entry->args[i], sizeof(unsigned long),
214 i == entry->nb_args - 1 ? "" : ", ");
215 }
216 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
217
218 for (i = 0; i < entry->nb_args; i++) {
219 pos += snprintf(buf + pos, LEN_OR_ZERO,
220 ", ((unsigned long)(REC->%s))", entry->args[i]);
221 }
222
223#undef LEN_OR_ZERO
224
225 /* return the length of print_fmt */
226 return pos;
227}
228
229static int set_syscall_print_fmt(struct ftrace_event_call *call)
230{
231 char *print_fmt;
232 int len;
233 struct syscall_metadata *entry = call->data;
234
235 if (entry->enter_event != call) {
236 call->print_fmt = "\"0x%lx\", REC->ret";
237 return 0;
238 }
239
240 /* First: called with 0 length to calculate the needed length */
241 len = __set_enter_print_fmt(entry, NULL, 0);
242
243 print_fmt = kmalloc(len + 1, GFP_KERNEL);
244 if (!print_fmt)
245 return -ENOMEM;
246
247 /* Second: actually write the @print_fmt */
248 __set_enter_print_fmt(entry, print_fmt, len + 1);
249 call->print_fmt = print_fmt;
250
251 return 0;
252}
253
254static void free_syscall_print_fmt(struct ftrace_event_call *call)
255{
256 struct syscall_metadata *entry = call->data;
257
258 if (entry->enter_event == call)
259 kfree(call->print_fmt);
260}
261
Steven Rostedt2e33af02010-04-22 10:35:55 -0400262static int syscall_enter_define_fields(struct ftrace_event_call *call)
Li Zefan540b7b82009-08-19 15:54:51 +0800263{
264 struct syscall_trace_enter trace;
Lai Jiangshan31c16b12009-12-01 16:23:30 +0800265 struct syscall_metadata *meta = call->data;
Li Zefan540b7b82009-08-19 15:54:51 +0800266 int ret;
Li Zefan540b7b82009-08-19 15:54:51 +0800267 int i;
268 int offset = offsetof(typeof(trace), args);
269
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800270 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
271 if (ret)
272 return ret;
273
Li Zefan540b7b82009-08-19 15:54:51 +0800274 for (i = 0; i < meta->nb_args; i++) {
Frederic Weisbeckeraeaeae12009-08-27 05:09:51 +0200275 ret = trace_define_field(call, meta->types[i],
276 meta->args[i], offset,
Li Zefan43b51ea2009-08-07 10:33:22 +0800277 sizeof(unsigned long), 0,
278 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800279 offset += sizeof(unsigned long);
280 }
281
282 return ret;
283}
284
Steven Rostedt2e33af02010-04-22 10:35:55 -0400285static int syscall_exit_define_fields(struct ftrace_event_call *call)
Li Zefan540b7b82009-08-19 15:54:51 +0800286{
287 struct syscall_trace_exit trace;
288 int ret;
289
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800290 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
291 if (ret)
292 return ret;
293
Tom Zanussi26a50742009-10-06 01:09:50 -0500294 ret = trace_define_field(call, SYSCALL_FIELD(long, ret),
Li Zefan43b51ea2009-08-07 10:33:22 +0800295 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800296
297 return ret;
298}
299
Steven Rostedt38516ab2010-04-20 17:04:50 -0400300void ftrace_syscall_enter(void *ignore, struct pt_regs *regs, long id)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100301{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100302 struct syscall_trace_enter *entry;
303 struct syscall_metadata *sys_data;
304 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400305 struct ring_buffer *buffer;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100306 int size;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100307 int syscall_nr;
308
309 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200310 if (syscall_nr < 0)
311 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400312 if (!test_bit(syscall_nr, enabled_enter_syscalls))
313 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100314
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100315 sys_data = syscall_nr_to_meta(syscall_nr);
316 if (!sys_data)
317 return;
318
319 size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
320
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800321 event = trace_current_buffer_lock_reserve(&buffer,
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400322 sys_data->enter_event->event.type, size, 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100323 if (!event)
324 return;
325
326 entry = ring_buffer_event_data(event);
327 entry->nr = syscall_nr;
328 syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
329
Steven Rostedte77405a2009-09-02 14:17:06 -0400330 if (!filter_current_check_discard(buffer, sys_data->enter_event,
331 entry, event))
332 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100333}
334
Steven Rostedt38516ab2010-04-20 17:04:50 -0400335void ftrace_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100336{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100337 struct syscall_trace_exit *entry;
338 struct syscall_metadata *sys_data;
339 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400340 struct ring_buffer *buffer;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100341 int syscall_nr;
342
343 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200344 if (syscall_nr < 0)
345 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400346 if (!test_bit(syscall_nr, enabled_exit_syscalls))
347 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100348
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100349 sys_data = syscall_nr_to_meta(syscall_nr);
350 if (!sys_data)
351 return;
352
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800353 event = trace_current_buffer_lock_reserve(&buffer,
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400354 sys_data->exit_event->event.type, sizeof(*entry), 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100355 if (!event)
356 return;
357
358 entry = ring_buffer_event_data(event);
359 entry->nr = syscall_nr;
360 entry->ret = syscall_get_return_value(current, regs);
361
Steven Rostedte77405a2009-09-02 14:17:06 -0400362 if (!filter_current_check_discard(buffer, sys_data->exit_event,
363 entry, event))
364 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100365}
366
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400367int reg_event_syscall_enter(struct ftrace_event_call *call)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100368{
Jason Baronfb34a082009-08-10 16:52:47 -0400369 int ret = 0;
370 int num;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100371
Lai Jiangshanc252f652009-12-01 16:23:47 +0800372 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100373 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400374 return -ENOSYS;
375 mutex_lock(&syscall_trace_lock);
376 if (!sys_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400377 ret = register_trace_sys_enter(ftrace_syscall_enter, NULL);
Li Zefan3b8e4272009-12-08 11:14:52 +0800378 if (!ret) {
Jason Baronfb34a082009-08-10 16:52:47 -0400379 set_bit(num, enabled_enter_syscalls);
380 sys_refcount_enter++;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100381 }
Jason Baronfb34a082009-08-10 16:52:47 -0400382 mutex_unlock(&syscall_trace_lock);
383 return ret;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100384}
Jason Baronfb34a082009-08-10 16:52:47 -0400385
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400386void unreg_event_syscall_enter(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400387{
388 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400389
Lai Jiangshanc252f652009-12-01 16:23:47 +0800390 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100391 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400392 return;
393 mutex_lock(&syscall_trace_lock);
394 sys_refcount_enter--;
395 clear_bit(num, enabled_enter_syscalls);
396 if (!sys_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400397 unregister_trace_sys_enter(ftrace_syscall_enter, NULL);
Jason Baronfb34a082009-08-10 16:52:47 -0400398 mutex_unlock(&syscall_trace_lock);
399}
400
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400401int reg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400402{
403 int ret = 0;
404 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400405
Lai Jiangshanc252f652009-12-01 16:23:47 +0800406 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100407 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400408 return -ENOSYS;
409 mutex_lock(&syscall_trace_lock);
410 if (!sys_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400411 ret = register_trace_sys_exit(ftrace_syscall_exit, NULL);
Li Zefan3b8e4272009-12-08 11:14:52 +0800412 if (!ret) {
Jason Baronfb34a082009-08-10 16:52:47 -0400413 set_bit(num, enabled_exit_syscalls);
414 sys_refcount_exit++;
415 }
416 mutex_unlock(&syscall_trace_lock);
417 return ret;
418}
419
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400420void unreg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400421{
422 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400423
Lai Jiangshanc252f652009-12-01 16:23:47 +0800424 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100425 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400426 return;
427 mutex_lock(&syscall_trace_lock);
428 sys_refcount_exit--;
429 clear_bit(num, enabled_exit_syscalls);
430 if (!sys_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400431 unregister_trace_sys_exit(ftrace_syscall_exit, NULL);
Jason Baronfb34a082009-08-10 16:52:47 -0400432 mutex_unlock(&syscall_trace_lock);
433}
434
Lai Jiangshana1301da2009-12-01 16:23:55 +0800435int init_syscall_trace(struct ftrace_event_call *call)
436{
437 int id;
Ian Munsieba976972011-02-03 14:27:20 +1100438 int num;
439
440 num = ((struct syscall_metadata *)call->data)->syscall_nr;
441 if (num < 0 || num >= NR_syscalls) {
442 pr_debug("syscall %s metadata not mapped, disabling ftrace event\n",
443 ((struct syscall_metadata *)call->data)->name);
444 return -ENOSYS;
445 }
Lai Jiangshana1301da2009-12-01 16:23:55 +0800446
Lai Jiangshan50307a42009-12-15 15:39:45 +0800447 if (set_syscall_print_fmt(call) < 0)
448 return -ENOMEM;
449
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500450 id = trace_event_raw_init(call);
451
452 if (id < 0) {
Lai Jiangshan50307a42009-12-15 15:39:45 +0800453 free_syscall_print_fmt(call);
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500454 return id;
Lai Jiangshan50307a42009-12-15 15:39:45 +0800455 }
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500456
457 return id;
Lai Jiangshana1301da2009-12-01 16:23:55 +0800458}
459
Ian Munsiec763ba02011-02-03 14:27:22 +1100460unsigned long __init __weak arch_syscall_addr(int nr)
Mike Frysingere7b8e672010-01-26 04:40:03 -0500461{
462 return (unsigned long)sys_call_table[nr];
463}
464
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200465int __init init_ftrace_syscalls(void)
466{
467 struct syscall_metadata *meta;
468 unsigned long addr;
469 int i;
470
471 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
472 NR_syscalls, GFP_KERNEL);
473 if (!syscalls_metadata) {
474 WARN_ON(1);
475 return -ENOMEM;
476 }
477
478 for (i = 0; i < NR_syscalls; i++) {
479 addr = arch_syscall_addr(i);
480 meta = find_syscall_meta(addr);
Lai Jiangshanc252f652009-12-01 16:23:47 +0800481 if (!meta)
482 continue;
483
484 meta->syscall_nr = i;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200485 syscalls_metadata[i] = meta;
486 }
487
488 return 0;
489}
490core_initcall(init_ftrace_syscalls);
491
Li Zefan07b139c2009-12-21 14:27:35 +0800492#ifdef CONFIG_PERF_EVENTS
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200493
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100494static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls);
495static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
496static int sys_perf_refcount_enter;
497static int sys_perf_refcount_exit;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400498
Steven Rostedt38516ab2010-04-20 17:04:50 -0400499static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400500{
501 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200502 struct syscall_trace_enter *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200503 struct hlist_head *head;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400504 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100505 int rctx;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200506 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400507
508 syscall_nr = syscall_get_nr(current, regs);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100509 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400510 return;
511
512 sys_data = syscall_nr_to_meta(syscall_nr);
513 if (!sys_data)
514 return;
515
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200516 /* get the size after alignment with the u32 buffer size field */
517 size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
518 size = ALIGN(size + sizeof(u32), sizeof(u64));
519 size -= sizeof(u32);
520
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100521 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
522 "perf buffer not large enough"))
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200523 return;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200524
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100525 rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size,
Steven Rostedtff5f1492010-05-21 11:49:57 -0400526 sys_data->enter_event->event.type, regs, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800527 if (!rec)
528 return;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200529
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200530 rec->nr = syscall_nr;
531 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
532 (unsigned long *)&rec->args);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200533
Peter Zijlstra3771f072010-05-21 12:31:09 +0200534 head = this_cpu_ptr(sys_data->enter_event->perf_events);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200535 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400536}
537
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100538int perf_sysenter_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400539{
540 int ret = 0;
541 int num;
542
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800543 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400544
545 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100546 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400547 ret = register_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400548 if (ret) {
549 pr_info("event trace: Could not activate"
550 "syscall entry trace point");
551 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100552 set_bit(num, enabled_perf_enter_syscalls);
553 sys_perf_refcount_enter++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400554 }
555 mutex_unlock(&syscall_trace_lock);
556 return ret;
557}
558
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100559void perf_sysenter_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400560{
561 int num;
562
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800563 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400564
565 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100566 sys_perf_refcount_enter--;
567 clear_bit(num, enabled_perf_enter_syscalls);
568 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400569 unregister_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400570 mutex_unlock(&syscall_trace_lock);
571}
572
Steven Rostedt38516ab2010-04-20 17:04:50 -0400573static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400574{
575 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200576 struct syscall_trace_exit *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200577 struct hlist_head *head;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400578 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100579 int rctx;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200580 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400581
582 syscall_nr = syscall_get_nr(current, regs);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100583 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400584 return;
585
586 sys_data = syscall_nr_to_meta(syscall_nr);
587 if (!sys_data)
588 return;
589
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200590 /* We can probably do that at build time */
591 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
592 size -= sizeof(u32);
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200593
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200594 /*
595 * Impossible, but be paranoid with the future
596 * How to put this check outside runtime?
597 */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100598 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
599 "exit event has grown above perf buffer size"))
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200600 return;
601
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100602 rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size,
Steven Rostedtff5f1492010-05-21 11:49:57 -0400603 sys_data->exit_event->event.type, regs, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800604 if (!rec)
605 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100606
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200607 rec->nr = syscall_nr;
608 rec->ret = syscall_get_return_value(current, regs);
609
Peter Zijlstra3771f072010-05-21 12:31:09 +0200610 head = this_cpu_ptr(sys_data->exit_event->perf_events);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200611 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400612}
613
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100614int perf_sysexit_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400615{
616 int ret = 0;
617 int num;
618
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800619 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400620
621 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100622 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400623 ret = register_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400624 if (ret) {
625 pr_info("event trace: Could not activate"
Wenji Huang65746582010-02-24 15:40:22 +0800626 "syscall exit trace point");
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400627 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100628 set_bit(num, enabled_perf_exit_syscalls);
629 sys_perf_refcount_exit++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400630 }
631 mutex_unlock(&syscall_trace_lock);
632 return ret;
633}
634
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100635void perf_sysexit_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400636{
637 int num;
638
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800639 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400640
641 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100642 sys_perf_refcount_exit--;
643 clear_bit(num, enabled_perf_exit_syscalls);
644 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400645 unregister_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400646 mutex_unlock(&syscall_trace_lock);
647}
648
Li Zefan07b139c2009-12-21 14:27:35 +0800649#endif /* CONFIG_PERF_EVENTS */
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400650
Steven Rostedt22392912010-04-21 12:27:06 -0400651static int syscall_enter_register(struct ftrace_event_call *event,
652 enum trace_reg type)
653{
654 switch (type) {
655 case TRACE_REG_REGISTER:
656 return reg_event_syscall_enter(event);
657 case TRACE_REG_UNREGISTER:
658 unreg_event_syscall_enter(event);
659 return 0;
660
661#ifdef CONFIG_PERF_EVENTS
662 case TRACE_REG_PERF_REGISTER:
663 return perf_sysenter_enable(event);
664 case TRACE_REG_PERF_UNREGISTER:
665 perf_sysenter_disable(event);
666 return 0;
667#endif
668 }
669 return 0;
670}
671
672static int syscall_exit_register(struct ftrace_event_call *event,
673 enum trace_reg type)
674{
675 switch (type) {
676 case TRACE_REG_REGISTER:
677 return reg_event_syscall_exit(event);
678 case TRACE_REG_UNREGISTER:
679 unreg_event_syscall_exit(event);
680 return 0;
681
682#ifdef CONFIG_PERF_EVENTS
683 case TRACE_REG_PERF_REGISTER:
684 return perf_sysexit_enable(event);
685 case TRACE_REG_PERF_UNREGISTER:
686 perf_sysexit_disable(event);
687 return 0;
688#endif
689 }
690 return 0;
691}