blob: 423094288fb58ca1ca637a58a4c7a5412c883de2 [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
Steven Rostedt3d56e332011-02-02 17:06:09 -050063static __init struct syscall_metadata *
64find_syscall_meta(unsigned long syscall)
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020065{
Steven Rostedt3d56e332011-02-02 17:06:09 -050066 struct syscall_metadata **start;
67 struct syscall_metadata **stop;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020068 char str[KSYM_SYMBOL_LEN];
69
70
Steven Rostedt3d56e332011-02-02 17:06:09 -050071 start = __start_syscalls_metadata;
72 stop = __stop_syscalls_metadata;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020073 kallsyms_lookup(syscall, NULL, NULL, NULL, str);
74
75 for ( ; start < stop; start++) {
76 /*
77 * Only compare after the "sys" prefix. Archs that use
78 * syscall wrappers may have syscalls symbols aliases prefixed
79 * with "SyS" instead of "sys", leading to an unwanted
80 * mismatch.
81 */
Steven Rostedt3d56e332011-02-02 17:06:09 -050082 if ((*start)->name && !strcmp((*start)->name + 3, str + 3))
83 return *start;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020084 }
85 return NULL;
86}
87
88static struct syscall_metadata *syscall_nr_to_meta(int nr)
89{
90 if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
91 return NULL;
92
93 return syscalls_metadata[nr];
94}
95
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010096enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -040097print_syscall_enter(struct trace_iterator *iter, int flags,
98 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010099{
100 struct trace_seq *s = &iter->seq;
101 struct trace_entry *ent = iter->ent;
102 struct syscall_trace_enter *trace;
103 struct syscall_metadata *entry;
104 int i, ret, syscall;
105
Jason Baron64c12e02009-08-10 16:52:53 -0400106 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100107 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100108 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400109
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100110 if (!entry)
111 goto end;
112
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400113 if (entry->enter_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400114 WARN_ON_ONCE(1);
115 goto end;
116 }
117
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100118 ret = trace_seq_printf(s, "%s(", entry->name);
119 if (!ret)
120 return TRACE_TYPE_PARTIAL_LINE;
121
122 for (i = 0; i < entry->nb_args; i++) {
123 /* parameter types */
Li Zefanba8b3a42009-08-17 16:55:18 +0800124 if (trace_flags & TRACE_ITER_VERBOSE) {
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100125 ret = trace_seq_printf(s, "%s ", entry->types[i]);
126 if (!ret)
127 return TRACE_TYPE_PARTIAL_LINE;
128 }
129 /* parameter values */
Li Zefan4539f072009-08-20 16:13:35 +0800130 ret = trace_seq_printf(s, "%s: %lx%s", entry->args[i],
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100131 trace->args[i],
Li Zefan4539f072009-08-20 16:13:35 +0800132 i == entry->nb_args - 1 ? "" : ", ");
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100133 if (!ret)
134 return TRACE_TYPE_PARTIAL_LINE;
135 }
136
Li Zefan4539f072009-08-20 16:13:35 +0800137 ret = trace_seq_putc(s, ')');
138 if (!ret)
139 return TRACE_TYPE_PARTIAL_LINE;
140
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100141end:
Li Zefan4539f072009-08-20 16:13:35 +0800142 ret = trace_seq_putc(s, '\n');
143 if (!ret)
144 return TRACE_TYPE_PARTIAL_LINE;
145
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100146 return TRACE_TYPE_HANDLED;
147}
148
149enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -0400150print_syscall_exit(struct trace_iterator *iter, int flags,
151 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100152{
153 struct trace_seq *s = &iter->seq;
154 struct trace_entry *ent = iter->ent;
155 struct syscall_trace_exit *trace;
156 int syscall;
157 struct syscall_metadata *entry;
158 int ret;
159
Jason Baron64c12e02009-08-10 16:52:53 -0400160 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100161 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100162 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400163
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100164 if (!entry) {
165 trace_seq_printf(s, "\n");
166 return TRACE_TYPE_HANDLED;
167 }
168
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400169 if (entry->exit_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400170 WARN_ON_ONCE(1);
171 return TRACE_TYPE_UNHANDLED;
172 }
173
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100174 ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
175 trace->ret);
176 if (!ret)
177 return TRACE_TYPE_PARTIAL_LINE;
178
179 return TRACE_TYPE_HANDLED;
180}
181
Li Zefane6971962009-08-19 15:52:25 +0800182extern char *__bad_type_size(void);
183
184#define SYSCALL_FIELD(type, name) \
185 sizeof(type) != sizeof(trace.name) ? \
186 __bad_type_size() : \
Tom Zanussi26a50742009-10-06 01:09:50 -0500187 #type, #name, offsetof(typeof(trace), name), \
188 sizeof(trace.name), is_signed_type(type)
Li Zefane6971962009-08-19 15:52:25 +0800189
Lai Jiangshan50307a42009-12-15 15:39:45 +0800190static
191int __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
192{
193 int i;
194 int pos = 0;
195
196 /* When len=0, we just calculate the needed length */
197#define LEN_OR_ZERO (len ? len - pos : 0)
198
199 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
200 for (i = 0; i < entry->nb_args; i++) {
201 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s",
202 entry->args[i], sizeof(unsigned long),
203 i == entry->nb_args - 1 ? "" : ", ");
204 }
205 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
206
207 for (i = 0; i < entry->nb_args; i++) {
208 pos += snprintf(buf + pos, LEN_OR_ZERO,
209 ", ((unsigned long)(REC->%s))", entry->args[i]);
210 }
211
212#undef LEN_OR_ZERO
213
214 /* return the length of print_fmt */
215 return pos;
216}
217
218static int set_syscall_print_fmt(struct ftrace_event_call *call)
219{
220 char *print_fmt;
221 int len;
222 struct syscall_metadata *entry = call->data;
223
224 if (entry->enter_event != call) {
225 call->print_fmt = "\"0x%lx\", REC->ret";
226 return 0;
227 }
228
229 /* First: called with 0 length to calculate the needed length */
230 len = __set_enter_print_fmt(entry, NULL, 0);
231
232 print_fmt = kmalloc(len + 1, GFP_KERNEL);
233 if (!print_fmt)
234 return -ENOMEM;
235
236 /* Second: actually write the @print_fmt */
237 __set_enter_print_fmt(entry, print_fmt, len + 1);
238 call->print_fmt = print_fmt;
239
240 return 0;
241}
242
243static void free_syscall_print_fmt(struct ftrace_event_call *call)
244{
245 struct syscall_metadata *entry = call->data;
246
247 if (entry->enter_event == call)
248 kfree(call->print_fmt);
249}
250
Steven Rostedt2e33af02010-04-22 10:35:55 -0400251static int syscall_enter_define_fields(struct ftrace_event_call *call)
Li Zefan540b7b82009-08-19 15:54:51 +0800252{
253 struct syscall_trace_enter trace;
Lai Jiangshan31c16b12009-12-01 16:23:30 +0800254 struct syscall_metadata *meta = call->data;
Li Zefan540b7b82009-08-19 15:54:51 +0800255 int ret;
Li Zefan540b7b82009-08-19 15:54:51 +0800256 int i;
257 int offset = offsetof(typeof(trace), args);
258
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800259 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
260 if (ret)
261 return ret;
262
Li Zefan540b7b82009-08-19 15:54:51 +0800263 for (i = 0; i < meta->nb_args; i++) {
Frederic Weisbeckeraeaeae12009-08-27 05:09:51 +0200264 ret = trace_define_field(call, meta->types[i],
265 meta->args[i], offset,
Li Zefan43b51ea2009-08-07 10:33:22 +0800266 sizeof(unsigned long), 0,
267 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800268 offset += sizeof(unsigned long);
269 }
270
271 return ret;
272}
273
Steven Rostedt2e33af02010-04-22 10:35:55 -0400274static int syscall_exit_define_fields(struct ftrace_event_call *call)
Li Zefan540b7b82009-08-19 15:54:51 +0800275{
276 struct syscall_trace_exit trace;
277 int ret;
278
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800279 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
280 if (ret)
281 return ret;
282
Tom Zanussi26a50742009-10-06 01:09:50 -0500283 ret = trace_define_field(call, SYSCALL_FIELD(long, ret),
Li Zefan43b51ea2009-08-07 10:33:22 +0800284 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800285
286 return ret;
287}
288
Steven Rostedt38516ab2010-04-20 17:04:50 -0400289void ftrace_syscall_enter(void *ignore, struct pt_regs *regs, long id)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100290{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100291 struct syscall_trace_enter *entry;
292 struct syscall_metadata *sys_data;
293 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400294 struct ring_buffer *buffer;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100295 int size;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100296 int syscall_nr;
297
298 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200299 if (syscall_nr < 0)
300 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400301 if (!test_bit(syscall_nr, enabled_enter_syscalls))
302 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100303
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100304 sys_data = syscall_nr_to_meta(syscall_nr);
305 if (!sys_data)
306 return;
307
308 size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
309
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800310 event = trace_current_buffer_lock_reserve(&buffer,
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400311 sys_data->enter_event->event.type, size, 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100312 if (!event)
313 return;
314
315 entry = ring_buffer_event_data(event);
316 entry->nr = syscall_nr;
317 syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
318
Steven Rostedte77405a2009-09-02 14:17:06 -0400319 if (!filter_current_check_discard(buffer, sys_data->enter_event,
320 entry, event))
321 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100322}
323
Steven Rostedt38516ab2010-04-20 17:04:50 -0400324void ftrace_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100325{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100326 struct syscall_trace_exit *entry;
327 struct syscall_metadata *sys_data;
328 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400329 struct ring_buffer *buffer;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100330 int syscall_nr;
331
332 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200333 if (syscall_nr < 0)
334 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400335 if (!test_bit(syscall_nr, enabled_exit_syscalls))
336 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100337
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100338 sys_data = syscall_nr_to_meta(syscall_nr);
339 if (!sys_data)
340 return;
341
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800342 event = trace_current_buffer_lock_reserve(&buffer,
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400343 sys_data->exit_event->event.type, sizeof(*entry), 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100344 if (!event)
345 return;
346
347 entry = ring_buffer_event_data(event);
348 entry->nr = syscall_nr;
349 entry->ret = syscall_get_return_value(current, regs);
350
Steven Rostedte77405a2009-09-02 14:17:06 -0400351 if (!filter_current_check_discard(buffer, sys_data->exit_event,
352 entry, event))
353 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100354}
355
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400356int reg_event_syscall_enter(struct ftrace_event_call *call)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100357{
Jason Baronfb34a082009-08-10 16:52:47 -0400358 int ret = 0;
359 int num;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100360
Lai Jiangshanc252f652009-12-01 16:23:47 +0800361 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100362 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400363 return -ENOSYS;
364 mutex_lock(&syscall_trace_lock);
365 if (!sys_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400366 ret = register_trace_sys_enter(ftrace_syscall_enter, NULL);
Li Zefan3b8e4272009-12-08 11:14:52 +0800367 if (!ret) {
Jason Baronfb34a082009-08-10 16:52:47 -0400368 set_bit(num, enabled_enter_syscalls);
369 sys_refcount_enter++;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100370 }
Jason Baronfb34a082009-08-10 16:52:47 -0400371 mutex_unlock(&syscall_trace_lock);
372 return ret;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100373}
Jason Baronfb34a082009-08-10 16:52:47 -0400374
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400375void unreg_event_syscall_enter(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400376{
377 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400378
Lai Jiangshanc252f652009-12-01 16:23:47 +0800379 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100380 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400381 return;
382 mutex_lock(&syscall_trace_lock);
383 sys_refcount_enter--;
384 clear_bit(num, enabled_enter_syscalls);
385 if (!sys_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400386 unregister_trace_sys_enter(ftrace_syscall_enter, NULL);
Jason Baronfb34a082009-08-10 16:52:47 -0400387 mutex_unlock(&syscall_trace_lock);
388}
389
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400390int reg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400391{
392 int ret = 0;
393 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400394
Lai Jiangshanc252f652009-12-01 16:23:47 +0800395 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100396 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400397 return -ENOSYS;
398 mutex_lock(&syscall_trace_lock);
399 if (!sys_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400400 ret = register_trace_sys_exit(ftrace_syscall_exit, NULL);
Li Zefan3b8e4272009-12-08 11:14:52 +0800401 if (!ret) {
Jason Baronfb34a082009-08-10 16:52:47 -0400402 set_bit(num, enabled_exit_syscalls);
403 sys_refcount_exit++;
404 }
405 mutex_unlock(&syscall_trace_lock);
406 return ret;
407}
408
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400409void unreg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400410{
411 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400412
Lai Jiangshanc252f652009-12-01 16:23:47 +0800413 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100414 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400415 return;
416 mutex_lock(&syscall_trace_lock);
417 sys_refcount_exit--;
418 clear_bit(num, enabled_exit_syscalls);
419 if (!sys_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400420 unregister_trace_sys_exit(ftrace_syscall_exit, NULL);
Jason Baronfb34a082009-08-10 16:52:47 -0400421 mutex_unlock(&syscall_trace_lock);
422}
423
Lai Jiangshana1301da2009-12-01 16:23:55 +0800424int init_syscall_trace(struct ftrace_event_call *call)
425{
426 int id;
Ian Munsieba976972011-02-03 14:27:20 +1100427 int num;
428
429 num = ((struct syscall_metadata *)call->data)->syscall_nr;
430 if (num < 0 || num >= NR_syscalls) {
431 pr_debug("syscall %s metadata not mapped, disabling ftrace event\n",
432 ((struct syscall_metadata *)call->data)->name);
433 return -ENOSYS;
434 }
Lai Jiangshana1301da2009-12-01 16:23:55 +0800435
Lai Jiangshan50307a42009-12-15 15:39:45 +0800436 if (set_syscall_print_fmt(call) < 0)
437 return -ENOMEM;
438
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500439 id = trace_event_raw_init(call);
440
441 if (id < 0) {
Lai Jiangshan50307a42009-12-15 15:39:45 +0800442 free_syscall_print_fmt(call);
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500443 return id;
Lai Jiangshan50307a42009-12-15 15:39:45 +0800444 }
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500445
446 return id;
Lai Jiangshana1301da2009-12-01 16:23:55 +0800447}
448
Mike Frysingere7b8e672010-01-26 04:40:03 -0500449unsigned long __init arch_syscall_addr(int nr)
450{
451 return (unsigned long)sys_call_table[nr];
452}
453
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200454int __init init_ftrace_syscalls(void)
455{
456 struct syscall_metadata *meta;
457 unsigned long addr;
458 int i;
459
460 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
461 NR_syscalls, GFP_KERNEL);
462 if (!syscalls_metadata) {
463 WARN_ON(1);
464 return -ENOMEM;
465 }
466
467 for (i = 0; i < NR_syscalls; i++) {
468 addr = arch_syscall_addr(i);
469 meta = find_syscall_meta(addr);
Lai Jiangshanc252f652009-12-01 16:23:47 +0800470 if (!meta)
471 continue;
472
473 meta->syscall_nr = i;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200474 syscalls_metadata[i] = meta;
475 }
476
477 return 0;
478}
479core_initcall(init_ftrace_syscalls);
480
Li Zefan07b139c2009-12-21 14:27:35 +0800481#ifdef CONFIG_PERF_EVENTS
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200482
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100483static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls);
484static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
485static int sys_perf_refcount_enter;
486static int sys_perf_refcount_exit;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400487
Steven Rostedt38516ab2010-04-20 17:04:50 -0400488static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400489{
490 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200491 struct syscall_trace_enter *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200492 struct hlist_head *head;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400493 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100494 int rctx;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200495 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400496
497 syscall_nr = syscall_get_nr(current, regs);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100498 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400499 return;
500
501 sys_data = syscall_nr_to_meta(syscall_nr);
502 if (!sys_data)
503 return;
504
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200505 /* get the size after alignment with the u32 buffer size field */
506 size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
507 size = ALIGN(size + sizeof(u32), sizeof(u64));
508 size -= sizeof(u32);
509
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100510 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
511 "perf buffer not large enough"))
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200512 return;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200513
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100514 rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size,
Steven Rostedtff5f1492010-05-21 11:49:57 -0400515 sys_data->enter_event->event.type, regs, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800516 if (!rec)
517 return;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200518
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200519 rec->nr = syscall_nr;
520 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
521 (unsigned long *)&rec->args);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200522
Peter Zijlstra3771f072010-05-21 12:31:09 +0200523 head = this_cpu_ptr(sys_data->enter_event->perf_events);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200524 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400525}
526
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100527int perf_sysenter_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400528{
529 int ret = 0;
530 int num;
531
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800532 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400533
534 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100535 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400536 ret = register_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400537 if (ret) {
538 pr_info("event trace: Could not activate"
539 "syscall entry trace point");
540 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100541 set_bit(num, enabled_perf_enter_syscalls);
542 sys_perf_refcount_enter++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400543 }
544 mutex_unlock(&syscall_trace_lock);
545 return ret;
546}
547
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100548void perf_sysenter_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400549{
550 int num;
551
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800552 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400553
554 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100555 sys_perf_refcount_enter--;
556 clear_bit(num, enabled_perf_enter_syscalls);
557 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400558 unregister_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400559 mutex_unlock(&syscall_trace_lock);
560}
561
Steven Rostedt38516ab2010-04-20 17:04:50 -0400562static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400563{
564 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200565 struct syscall_trace_exit *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200566 struct hlist_head *head;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400567 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100568 int rctx;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200569 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400570
571 syscall_nr = syscall_get_nr(current, regs);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100572 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400573 return;
574
575 sys_data = syscall_nr_to_meta(syscall_nr);
576 if (!sys_data)
577 return;
578
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200579 /* We can probably do that at build time */
580 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
581 size -= sizeof(u32);
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200582
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200583 /*
584 * Impossible, but be paranoid with the future
585 * How to put this check outside runtime?
586 */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100587 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
588 "exit event has grown above perf buffer size"))
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200589 return;
590
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100591 rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size,
Steven Rostedtff5f1492010-05-21 11:49:57 -0400592 sys_data->exit_event->event.type, regs, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800593 if (!rec)
594 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100595
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200596 rec->nr = syscall_nr;
597 rec->ret = syscall_get_return_value(current, regs);
598
Peter Zijlstra3771f072010-05-21 12:31:09 +0200599 head = this_cpu_ptr(sys_data->exit_event->perf_events);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200600 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400601}
602
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100603int perf_sysexit_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400604{
605 int ret = 0;
606 int num;
607
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800608 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400609
610 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100611 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400612 ret = register_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400613 if (ret) {
614 pr_info("event trace: Could not activate"
Wenji Huang65746582010-02-24 15:40:22 +0800615 "syscall exit trace point");
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400616 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100617 set_bit(num, enabled_perf_exit_syscalls);
618 sys_perf_refcount_exit++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400619 }
620 mutex_unlock(&syscall_trace_lock);
621 return ret;
622}
623
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100624void perf_sysexit_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400625{
626 int num;
627
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800628 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400629
630 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100631 sys_perf_refcount_exit--;
632 clear_bit(num, enabled_perf_exit_syscalls);
633 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400634 unregister_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400635 mutex_unlock(&syscall_trace_lock);
636}
637
Li Zefan07b139c2009-12-21 14:27:35 +0800638#endif /* CONFIG_PERF_EVENTS */
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400639
Steven Rostedt22392912010-04-21 12:27:06 -0400640static int syscall_enter_register(struct ftrace_event_call *event,
641 enum trace_reg type)
642{
643 switch (type) {
644 case TRACE_REG_REGISTER:
645 return reg_event_syscall_enter(event);
646 case TRACE_REG_UNREGISTER:
647 unreg_event_syscall_enter(event);
648 return 0;
649
650#ifdef CONFIG_PERF_EVENTS
651 case TRACE_REG_PERF_REGISTER:
652 return perf_sysenter_enable(event);
653 case TRACE_REG_PERF_UNREGISTER:
654 perf_sysenter_disable(event);
655 return 0;
656#endif
657 }
658 return 0;
659}
660
661static int syscall_exit_register(struct ftrace_event_call *event,
662 enum trace_reg type)
663{
664 switch (type) {
665 case TRACE_REG_REGISTER:
666 return reg_event_syscall_exit(event);
667 case TRACE_REG_UNREGISTER:
668 unreg_event_syscall_exit(event);
669 return 0;
670
671#ifdef CONFIG_PERF_EVENTS
672 case TRACE_REG_PERF_REGISTER:
673 return perf_sysexit_enable(event);
674 case TRACE_REG_PERF_UNREGISTER:
675 perf_sysexit_disable(event);
676 return 0;
677#endif
678 }
679 return 0;
680}