blob: 1e85b6cc26aac0e26255d8a947bc4a5a0f8dd2bf [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>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01003#include <linux/kernel.h>
Jason Baronfb34a082009-08-10 16:52:47 -04004#include <linux/ftrace.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005#include <linux/perf_event.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01006#include <asm/syscall.h>
7
8#include "trace_output.h"
9#include "trace.h"
10
Frederic Weisbecker5be71b62009-03-15 22:10:37 +010011static DEFINE_MUTEX(syscall_trace_lock);
Jason Baronfb34a082009-08-10 16:52:47 -040012static int sys_refcount_enter;
13static int sys_refcount_exit;
Jason Baron57421db2009-08-24 17:40:22 -040014static DECLARE_BITMAP(enabled_enter_syscalls, NR_syscalls);
15static DECLARE_BITMAP(enabled_exit_syscalls, NR_syscalls);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +010016
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020017extern unsigned long __start_syscalls_metadata[];
18extern unsigned long __stop_syscalls_metadata[];
19
20static struct syscall_metadata **syscalls_metadata;
21
22static struct syscall_metadata *find_syscall_meta(unsigned long syscall)
23{
24 struct syscall_metadata *start;
25 struct syscall_metadata *stop;
26 char str[KSYM_SYMBOL_LEN];
27
28
29 start = (struct syscall_metadata *)__start_syscalls_metadata;
30 stop = (struct syscall_metadata *)__stop_syscalls_metadata;
31 kallsyms_lookup(syscall, NULL, NULL, NULL, str);
32
33 for ( ; start < stop; start++) {
34 /*
35 * Only compare after the "sys" prefix. Archs that use
36 * syscall wrappers may have syscalls symbols aliases prefixed
37 * with "SyS" instead of "sys", leading to an unwanted
38 * mismatch.
39 */
40 if (start->name && !strcmp(start->name + 3, str + 3))
41 return start;
42 }
43 return NULL;
44}
45
46static struct syscall_metadata *syscall_nr_to_meta(int nr)
47{
48 if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
49 return NULL;
50
51 return syscalls_metadata[nr];
52}
53
Lai Jiangshanc252f652009-12-01 16:23:47 +080054static int syscall_name_to_nr(const char *name)
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020055{
56 int i;
57
58 if (!syscalls_metadata)
59 return -1;
60
61 for (i = 0; i < NR_syscalls; i++) {
62 if (syscalls_metadata[i]) {
63 if (!strcmp(syscalls_metadata[i]->name, name))
64 return i;
65 }
66 }
67 return -1;
68}
69
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010070enum print_line_t
71print_syscall_enter(struct trace_iterator *iter, int flags)
72{
73 struct trace_seq *s = &iter->seq;
74 struct trace_entry *ent = iter->ent;
75 struct syscall_trace_enter *trace;
76 struct syscall_metadata *entry;
77 int i, ret, syscall;
78
Jason Baron64c12e02009-08-10 16:52:53 -040079 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010080 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010081 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -040082
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010083 if (!entry)
84 goto end;
85
Lai Jiangshanfcc19432009-12-01 16:23:36 +080086 if (entry->enter_event->id != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -040087 WARN_ON_ONCE(1);
88 goto end;
89 }
90
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010091 ret = trace_seq_printf(s, "%s(", entry->name);
92 if (!ret)
93 return TRACE_TYPE_PARTIAL_LINE;
94
95 for (i = 0; i < entry->nb_args; i++) {
96 /* parameter types */
Li Zefanba8b3a42009-08-17 16:55:18 +080097 if (trace_flags & TRACE_ITER_VERBOSE) {
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010098 ret = trace_seq_printf(s, "%s ", entry->types[i]);
99 if (!ret)
100 return TRACE_TYPE_PARTIAL_LINE;
101 }
102 /* parameter values */
Li Zefan4539f072009-08-20 16:13:35 +0800103 ret = trace_seq_printf(s, "%s: %lx%s", entry->args[i],
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100104 trace->args[i],
Li Zefan4539f072009-08-20 16:13:35 +0800105 i == entry->nb_args - 1 ? "" : ", ");
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100106 if (!ret)
107 return TRACE_TYPE_PARTIAL_LINE;
108 }
109
Li Zefan4539f072009-08-20 16:13:35 +0800110 ret = trace_seq_putc(s, ')');
111 if (!ret)
112 return TRACE_TYPE_PARTIAL_LINE;
113
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100114end:
Li Zefan4539f072009-08-20 16:13:35 +0800115 ret = trace_seq_putc(s, '\n');
116 if (!ret)
117 return TRACE_TYPE_PARTIAL_LINE;
118
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100119 return TRACE_TYPE_HANDLED;
120}
121
122enum print_line_t
123print_syscall_exit(struct trace_iterator *iter, int flags)
124{
125 struct trace_seq *s = &iter->seq;
126 struct trace_entry *ent = iter->ent;
127 struct syscall_trace_exit *trace;
128 int syscall;
129 struct syscall_metadata *entry;
130 int ret;
131
Jason Baron64c12e02009-08-10 16:52:53 -0400132 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100133 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100134 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400135
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100136 if (!entry) {
137 trace_seq_printf(s, "\n");
138 return TRACE_TYPE_HANDLED;
139 }
140
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800141 if (entry->exit_event->id != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400142 WARN_ON_ONCE(1);
143 return TRACE_TYPE_UNHANDLED;
144 }
145
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100146 ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
147 trace->ret);
148 if (!ret)
149 return TRACE_TYPE_PARTIAL_LINE;
150
151 return TRACE_TYPE_HANDLED;
152}
153
Li Zefane6971962009-08-19 15:52:25 +0800154extern char *__bad_type_size(void);
155
156#define SYSCALL_FIELD(type, name) \
157 sizeof(type) != sizeof(trace.name) ? \
158 __bad_type_size() : \
Tom Zanussi26a50742009-10-06 01:09:50 -0500159 #type, #name, offsetof(typeof(trace), name), \
160 sizeof(trace.name), is_signed_type(type)
Li Zefane6971962009-08-19 15:52:25 +0800161
Li Zefan10a5b662009-08-19 15:53:05 +0800162int syscall_enter_format(struct ftrace_event_call *call, struct trace_seq *s)
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200163{
164 int i;
Li Zefane6971962009-08-19 15:52:25 +0800165 int ret;
Lai Jiangshan31c16b12009-12-01 16:23:30 +0800166 struct syscall_metadata *entry = call->data;
Li Zefane6971962009-08-19 15:52:25 +0800167 struct syscall_trace_enter trace;
168 int offset = offsetof(struct syscall_trace_enter, args);
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200169
Tom Zanussi26a50742009-10-06 01:09:50 -0500170 ret = trace_seq_printf(s, "\tfield:%s %s;\toffset:%zu;\tsize:%zu;"
171 "\tsigned:%u;\n",
Li Zefane6971962009-08-19 15:52:25 +0800172 SYSCALL_FIELD(int, nr));
173 if (!ret)
174 return 0;
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200175
176 for (i = 0; i < entry->nb_args; i++) {
177 ret = trace_seq_printf(s, "\tfield:%s %s;", entry->types[i],
178 entry->args[i]);
179 if (!ret)
180 return 0;
Tom Zanussi26a50742009-10-06 01:09:50 -0500181 ret = trace_seq_printf(s, "\toffset:%d;\tsize:%zu;"
182 "\tsigned:%u;\n", offset,
183 sizeof(unsigned long),
184 is_signed_type(unsigned long));
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200185 if (!ret)
186 return 0;
187 offset += sizeof(unsigned long);
188 }
189
Li Zefan4539f072009-08-20 16:13:35 +0800190 trace_seq_puts(s, "\nprint fmt: \"");
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200191 for (i = 0; i < entry->nb_args; i++) {
Li Zefane6971962009-08-19 15:52:25 +0800192 ret = trace_seq_printf(s, "%s: 0x%%0%zulx%s", entry->args[i],
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200193 sizeof(unsigned long),
Li Zefan4539f072009-08-20 16:13:35 +0800194 i == entry->nb_args - 1 ? "" : ", ");
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200195 if (!ret)
196 return 0;
197 }
Li Zefan4539f072009-08-20 16:13:35 +0800198 trace_seq_putc(s, '"');
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200199
200 for (i = 0; i < entry->nb_args; i++) {
Li Zefan4539f072009-08-20 16:13:35 +0800201 ret = trace_seq_printf(s, ", ((unsigned long)(REC->%s))",
202 entry->args[i]);
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200203 if (!ret)
204 return 0;
205 }
206
Li Zefan4539f072009-08-20 16:13:35 +0800207 return trace_seq_putc(s, '\n');
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200208}
209
Li Zefan10a5b662009-08-19 15:53:05 +0800210int syscall_exit_format(struct ftrace_event_call *call, struct trace_seq *s)
211{
212 int ret;
213 struct syscall_trace_exit trace;
214
215 ret = trace_seq_printf(s,
Tom Zanussi26a50742009-10-06 01:09:50 -0500216 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;"
217 "\tsigned:%u;\n"
218 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;"
219 "\tsigned:%u;\n",
Li Zefan10a5b662009-08-19 15:53:05 +0800220 SYSCALL_FIELD(int, nr),
Tom Zanussiee949a82009-10-06 01:00:49 -0500221 SYSCALL_FIELD(long, ret));
Li Zefan10a5b662009-08-19 15:53:05 +0800222 if (!ret)
223 return 0;
224
225 return trace_seq_printf(s, "\nprint fmt: \"0x%%lx\", REC->ret\n");
226}
227
Li Zefan540b7b82009-08-19 15:54:51 +0800228int syscall_enter_define_fields(struct ftrace_event_call *call)
229{
230 struct syscall_trace_enter trace;
Lai Jiangshan31c16b12009-12-01 16:23:30 +0800231 struct syscall_metadata *meta = call->data;
Li Zefan540b7b82009-08-19 15:54:51 +0800232 int ret;
Li Zefan540b7b82009-08-19 15:54:51 +0800233 int i;
234 int offset = offsetof(typeof(trace), args);
235
Li Zefan540b7b82009-08-19 15:54:51 +0800236 ret = trace_define_common_fields(call);
237 if (ret)
238 return ret;
239
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800240 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
241 if (ret)
242 return ret;
243
Li Zefan540b7b82009-08-19 15:54:51 +0800244 for (i = 0; i < meta->nb_args; i++) {
Frederic Weisbeckeraeaeae12009-08-27 05:09:51 +0200245 ret = trace_define_field(call, meta->types[i],
246 meta->args[i], offset,
Li Zefan43b51ea2009-08-07 10:33:22 +0800247 sizeof(unsigned long), 0,
248 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800249 offset += sizeof(unsigned long);
250 }
251
252 return ret;
253}
254
255int syscall_exit_define_fields(struct ftrace_event_call *call)
256{
257 struct syscall_trace_exit trace;
258 int ret;
259
260 ret = trace_define_common_fields(call);
261 if (ret)
262 return ret;
263
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800264 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
265 if (ret)
266 return ret;
267
Tom Zanussi26a50742009-10-06 01:09:50 -0500268 ret = trace_define_field(call, SYSCALL_FIELD(long, ret),
Li Zefan43b51ea2009-08-07 10:33:22 +0800269 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800270
271 return ret;
272}
273
Jason Baronfb34a082009-08-10 16:52:47 -0400274void ftrace_syscall_enter(struct pt_regs *regs, long id)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100275{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100276 struct syscall_trace_enter *entry;
277 struct syscall_metadata *sys_data;
278 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400279 struct ring_buffer *buffer;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100280 int size;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100281 int syscall_nr;
282
283 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200284 if (syscall_nr < 0)
285 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400286 if (!test_bit(syscall_nr, enabled_enter_syscalls))
287 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100288
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100289 sys_data = syscall_nr_to_meta(syscall_nr);
290 if (!sys_data)
291 return;
292
293 size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
294
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800295 event = trace_current_buffer_lock_reserve(&buffer,
296 sys_data->enter_event->id, size, 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100297 if (!event)
298 return;
299
300 entry = ring_buffer_event_data(event);
301 entry->nr = syscall_nr;
302 syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
303
Steven Rostedte77405a2009-09-02 14:17:06 -0400304 if (!filter_current_check_discard(buffer, sys_data->enter_event,
305 entry, event))
306 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100307}
308
Jason Baronfb34a082009-08-10 16:52:47 -0400309void ftrace_syscall_exit(struct pt_regs *regs, long ret)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100310{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100311 struct syscall_trace_exit *entry;
312 struct syscall_metadata *sys_data;
313 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400314 struct ring_buffer *buffer;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100315 int syscall_nr;
316
317 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200318 if (syscall_nr < 0)
319 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400320 if (!test_bit(syscall_nr, enabled_exit_syscalls))
321 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100322
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100323 sys_data = syscall_nr_to_meta(syscall_nr);
324 if (!sys_data)
325 return;
326
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800327 event = trace_current_buffer_lock_reserve(&buffer,
328 sys_data->exit_event->id, sizeof(*entry), 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100329 if (!event)
330 return;
331
332 entry = ring_buffer_event_data(event);
333 entry->nr = syscall_nr;
334 entry->ret = syscall_get_return_value(current, regs);
335
Steven Rostedte77405a2009-09-02 14:17:06 -0400336 if (!filter_current_check_discard(buffer, sys_data->exit_event,
337 entry, event))
338 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100339}
340
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400341int reg_event_syscall_enter(struct ftrace_event_call *call)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100342{
Jason Baronfb34a082009-08-10 16:52:47 -0400343 int ret = 0;
344 int num;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100345
Lai Jiangshanc252f652009-12-01 16:23:47 +0800346 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400347 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400348 return -ENOSYS;
349 mutex_lock(&syscall_trace_lock);
350 if (!sys_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700351 ret = register_trace_sys_enter(ftrace_syscall_enter);
Jason Baronfb34a082009-08-10 16:52:47 -0400352 if (ret) {
353 pr_info("event trace: Could not activate"
354 "syscall entry trace point");
355 } else {
356 set_bit(num, enabled_enter_syscalls);
357 sys_refcount_enter++;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100358 }
Jason Baronfb34a082009-08-10 16:52:47 -0400359 mutex_unlock(&syscall_trace_lock);
360 return ret;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100361}
Jason Baronfb34a082009-08-10 16:52:47 -0400362
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400363void unreg_event_syscall_enter(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400364{
365 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400366
Lai Jiangshanc252f652009-12-01 16:23:47 +0800367 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400368 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400369 return;
370 mutex_lock(&syscall_trace_lock);
371 sys_refcount_enter--;
372 clear_bit(num, enabled_enter_syscalls);
373 if (!sys_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700374 unregister_trace_sys_enter(ftrace_syscall_enter);
Jason Baronfb34a082009-08-10 16:52:47 -0400375 mutex_unlock(&syscall_trace_lock);
376}
377
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400378int reg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400379{
380 int ret = 0;
381 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400382
Lai Jiangshanc252f652009-12-01 16:23:47 +0800383 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400384 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400385 return -ENOSYS;
386 mutex_lock(&syscall_trace_lock);
387 if (!sys_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700388 ret = register_trace_sys_exit(ftrace_syscall_exit);
Jason Baronfb34a082009-08-10 16:52:47 -0400389 if (ret) {
390 pr_info("event trace: Could not activate"
391 "syscall exit trace point");
392 } else {
393 set_bit(num, enabled_exit_syscalls);
394 sys_refcount_exit++;
395 }
396 mutex_unlock(&syscall_trace_lock);
397 return ret;
398}
399
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400400void unreg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400401{
402 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400403
Lai Jiangshanc252f652009-12-01 16:23:47 +0800404 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400405 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400406 return;
407 mutex_lock(&syscall_trace_lock);
408 sys_refcount_exit--;
409 clear_bit(num, enabled_exit_syscalls);
410 if (!sys_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700411 unregister_trace_sys_exit(ftrace_syscall_exit);
Jason Baronfb34a082009-08-10 16:52:47 -0400412 mutex_unlock(&syscall_trace_lock);
413}
414
Lai Jiangshana1301da2009-12-01 16:23:55 +0800415int init_syscall_trace(struct ftrace_event_call *call)
416{
417 int id;
418
419 id = register_ftrace_event(call->event);
420 if (!id)
421 return -ENODEV;
422 call->id = id;
423 INIT_LIST_HEAD(&call->fields);
424 return 0;
425}
426
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200427int __init init_ftrace_syscalls(void)
428{
429 struct syscall_metadata *meta;
430 unsigned long addr;
431 int i;
432
433 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
434 NR_syscalls, GFP_KERNEL);
435 if (!syscalls_metadata) {
436 WARN_ON(1);
437 return -ENOMEM;
438 }
439
440 for (i = 0; i < NR_syscalls; i++) {
441 addr = arch_syscall_addr(i);
442 meta = find_syscall_meta(addr);
Lai Jiangshanc252f652009-12-01 16:23:47 +0800443 if (!meta)
444 continue;
445
446 meta->syscall_nr = i;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200447 syscalls_metadata[i] = meta;
448 }
449
450 return 0;
451}
452core_initcall(init_ftrace_syscalls);
453
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400454#ifdef CONFIG_EVENT_PROFILE
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200455
Jason Baron57421db2009-08-24 17:40:22 -0400456static DECLARE_BITMAP(enabled_prof_enter_syscalls, NR_syscalls);
457static DECLARE_BITMAP(enabled_prof_exit_syscalls, NR_syscalls);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400458static int sys_prof_refcount_enter;
459static int sys_prof_refcount_exit;
460
461static void prof_syscall_enter(struct pt_regs *regs, long id)
462{
463 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200464 struct syscall_trace_enter *rec;
465 unsigned long flags;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100466 char *trace_buf;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200467 char *raw_data;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400468 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100469 int rctx;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200470 int size;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200471 int cpu;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400472
473 syscall_nr = syscall_get_nr(current, regs);
474 if (!test_bit(syscall_nr, enabled_prof_enter_syscalls))
475 return;
476
477 sys_data = syscall_nr_to_meta(syscall_nr);
478 if (!sys_data)
479 return;
480
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200481 /* get the size after alignment with the u32 buffer size field */
482 size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
483 size = ALIGN(size + sizeof(u32), sizeof(u64));
484 size -= sizeof(u32);
485
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200486 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
487 "profile buffer not large enough"))
488 return;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200489
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200490 /* Protect the per cpu buffer, begin the rcu read side */
491 local_irq_save(flags);
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200492
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100493 rctx = perf_swevent_get_recursion_context();
494 if (rctx < 0)
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100495 goto end_recursion;
496
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200497 cpu = smp_processor_id();
498
Frederic Weisbecker28889bf2009-11-22 05:21:33 +0100499 trace_buf = rcu_dereference(perf_trace_buf);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200500
Frederic Weisbecker444a2a32009-11-06 04:13:05 +0100501 if (!trace_buf)
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200502 goto end;
503
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100504 raw_data = per_cpu_ptr(trace_buf, cpu);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200505
506 /* zero the dead bytes from align to not leak stack to user */
507 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
508
509 rec = (struct syscall_trace_enter *) raw_data;
510 tracing_generic_entry_update(&rec->ent, 0, 0);
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800511 rec->ent.type = sys_data->enter_event->id;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200512 rec->nr = syscall_nr;
513 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
514 (unsigned long *)&rec->args);
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800515 perf_tp_event(sys_data->enter_event->id, 0, 1, rec, size);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200516
517end:
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100518 perf_swevent_put_recursion_context(rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100519end_recursion:
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200520 local_irq_restore(flags);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400521}
522
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800523int prof_sysenter_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400524{
525 int ret = 0;
526 int num;
527
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800528 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400529
530 mutex_lock(&syscall_trace_lock);
531 if (!sys_prof_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700532 ret = register_trace_sys_enter(prof_syscall_enter);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400533 if (ret) {
534 pr_info("event trace: Could not activate"
535 "syscall entry trace point");
536 } else {
537 set_bit(num, enabled_prof_enter_syscalls);
538 sys_prof_refcount_enter++;
539 }
540 mutex_unlock(&syscall_trace_lock);
541 return ret;
542}
543
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800544void prof_sysenter_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400545{
546 int num;
547
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800548 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400549
550 mutex_lock(&syscall_trace_lock);
551 sys_prof_refcount_enter--;
552 clear_bit(num, enabled_prof_enter_syscalls);
553 if (!sys_prof_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700554 unregister_trace_sys_enter(prof_syscall_enter);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400555 mutex_unlock(&syscall_trace_lock);
556}
557
558static void prof_syscall_exit(struct pt_regs *regs, long ret)
559{
560 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200561 struct syscall_trace_exit *rec;
562 unsigned long flags;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400563 int syscall_nr;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100564 char *trace_buf;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200565 char *raw_data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100566 int rctx;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200567 int size;
568 int cpu;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400569
570 syscall_nr = syscall_get_nr(current, regs);
571 if (!test_bit(syscall_nr, enabled_prof_exit_syscalls))
572 return;
573
574 sys_data = syscall_nr_to_meta(syscall_nr);
575 if (!sys_data)
576 return;
577
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200578 /* We can probably do that at build time */
579 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
580 size -= sizeof(u32);
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200581
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200582 /*
583 * Impossible, but be paranoid with the future
584 * How to put this check outside runtime?
585 */
586 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
587 "exit event has grown above profile buffer size"))
588 return;
589
590 /* Protect the per cpu buffer, begin the rcu read side */
591 local_irq_save(flags);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100592
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100593 rctx = perf_swevent_get_recursion_context();
594 if (rctx < 0)
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100595 goto end_recursion;
596
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200597 cpu = smp_processor_id();
598
Frederic Weisbecker28889bf2009-11-22 05:21:33 +0100599 trace_buf = rcu_dereference(perf_trace_buf);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200600
Frederic Weisbecker444a2a32009-11-06 04:13:05 +0100601 if (!trace_buf)
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200602 goto end;
603
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100604 raw_data = per_cpu_ptr(trace_buf, cpu);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200605
606 /* zero the dead bytes from align to not leak stack to user */
607 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
608
609 rec = (struct syscall_trace_exit *)raw_data;
610
611 tracing_generic_entry_update(&rec->ent, 0, 0);
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800612 rec->ent.type = sys_data->exit_event->id;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200613 rec->nr = syscall_nr;
614 rec->ret = syscall_get_return_value(current, regs);
615
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800616 perf_tp_event(sys_data->exit_event->id, 0, 1, rec, size);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200617
618end:
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100619 perf_swevent_put_recursion_context(rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100620end_recursion:
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200621 local_irq_restore(flags);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400622}
623
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800624int prof_sysexit_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400625{
626 int ret = 0;
627 int num;
628
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800629 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400630
631 mutex_lock(&syscall_trace_lock);
632 if (!sys_prof_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700633 ret = register_trace_sys_exit(prof_syscall_exit);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400634 if (ret) {
635 pr_info("event trace: Could not activate"
636 "syscall entry trace point");
637 } else {
638 set_bit(num, enabled_prof_exit_syscalls);
639 sys_prof_refcount_exit++;
640 }
641 mutex_unlock(&syscall_trace_lock);
642 return ret;
643}
644
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800645void prof_sysexit_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400646{
647 int num;
648
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800649 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400650
651 mutex_lock(&syscall_trace_lock);
652 sys_prof_refcount_exit--;
653 clear_bit(num, enabled_prof_exit_syscalls);
654 if (!sys_prof_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700655 unregister_trace_sys_exit(prof_syscall_exit);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400656 mutex_unlock(&syscall_trace_lock);
657}
658
659#endif
660
661