blob: 75289f372dd270816742e7dd94c1d5c2474442d8 [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
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010054enum print_line_t
55print_syscall_enter(struct trace_iterator *iter, int flags)
56{
57 struct trace_seq *s = &iter->seq;
58 struct trace_entry *ent = iter->ent;
59 struct syscall_trace_enter *trace;
60 struct syscall_metadata *entry;
61 int i, ret, syscall;
62
Jason Baron64c12e02009-08-10 16:52:53 -040063 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010064 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010065 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -040066
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010067 if (!entry)
68 goto end;
69
Lai Jiangshanfcc19432009-12-01 16:23:36 +080070 if (entry->enter_event->id != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -040071 WARN_ON_ONCE(1);
72 goto end;
73 }
74
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010075 ret = trace_seq_printf(s, "%s(", entry->name);
76 if (!ret)
77 return TRACE_TYPE_PARTIAL_LINE;
78
79 for (i = 0; i < entry->nb_args; i++) {
80 /* parameter types */
Li Zefanba8b3a42009-08-17 16:55:18 +080081 if (trace_flags & TRACE_ITER_VERBOSE) {
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010082 ret = trace_seq_printf(s, "%s ", entry->types[i]);
83 if (!ret)
84 return TRACE_TYPE_PARTIAL_LINE;
85 }
86 /* parameter values */
Li Zefan4539f072009-08-20 16:13:35 +080087 ret = trace_seq_printf(s, "%s: %lx%s", entry->args[i],
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010088 trace->args[i],
Li Zefan4539f072009-08-20 16:13:35 +080089 i == entry->nb_args - 1 ? "" : ", ");
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010090 if (!ret)
91 return TRACE_TYPE_PARTIAL_LINE;
92 }
93
Li Zefan4539f072009-08-20 16:13:35 +080094 ret = trace_seq_putc(s, ')');
95 if (!ret)
96 return TRACE_TYPE_PARTIAL_LINE;
97
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010098end:
Li Zefan4539f072009-08-20 16:13:35 +080099 ret = trace_seq_putc(s, '\n');
100 if (!ret)
101 return TRACE_TYPE_PARTIAL_LINE;
102
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100103 return TRACE_TYPE_HANDLED;
104}
105
106enum print_line_t
107print_syscall_exit(struct trace_iterator *iter, int flags)
108{
109 struct trace_seq *s = &iter->seq;
110 struct trace_entry *ent = iter->ent;
111 struct syscall_trace_exit *trace;
112 int syscall;
113 struct syscall_metadata *entry;
114 int ret;
115
Jason Baron64c12e02009-08-10 16:52:53 -0400116 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100117 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100118 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400119
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100120 if (!entry) {
121 trace_seq_printf(s, "\n");
122 return TRACE_TYPE_HANDLED;
123 }
124
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800125 if (entry->exit_event->id != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400126 WARN_ON_ONCE(1);
127 return TRACE_TYPE_UNHANDLED;
128 }
129
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100130 ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
131 trace->ret);
132 if (!ret)
133 return TRACE_TYPE_PARTIAL_LINE;
134
135 return TRACE_TYPE_HANDLED;
136}
137
Li Zefane6971962009-08-19 15:52:25 +0800138extern char *__bad_type_size(void);
139
140#define SYSCALL_FIELD(type, name) \
141 sizeof(type) != sizeof(trace.name) ? \
142 __bad_type_size() : \
Tom Zanussi26a50742009-10-06 01:09:50 -0500143 #type, #name, offsetof(typeof(trace), name), \
144 sizeof(trace.name), is_signed_type(type)
Li Zefane6971962009-08-19 15:52:25 +0800145
Li Zefan10a5b662009-08-19 15:53:05 +0800146int syscall_enter_format(struct ftrace_event_call *call, struct trace_seq *s)
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200147{
148 int i;
Li Zefane6971962009-08-19 15:52:25 +0800149 int ret;
Lai Jiangshan31c16b12009-12-01 16:23:30 +0800150 struct syscall_metadata *entry = call->data;
Li Zefane6971962009-08-19 15:52:25 +0800151 struct syscall_trace_enter trace;
152 int offset = offsetof(struct syscall_trace_enter, args);
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200153
Tom Zanussi26a50742009-10-06 01:09:50 -0500154 ret = trace_seq_printf(s, "\tfield:%s %s;\toffset:%zu;\tsize:%zu;"
155 "\tsigned:%u;\n",
Li Zefane6971962009-08-19 15:52:25 +0800156 SYSCALL_FIELD(int, nr));
157 if (!ret)
158 return 0;
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200159
160 for (i = 0; i < entry->nb_args; i++) {
161 ret = trace_seq_printf(s, "\tfield:%s %s;", entry->types[i],
162 entry->args[i]);
163 if (!ret)
164 return 0;
Tom Zanussi26a50742009-10-06 01:09:50 -0500165 ret = trace_seq_printf(s, "\toffset:%d;\tsize:%zu;"
166 "\tsigned:%u;\n", offset,
167 sizeof(unsigned long),
168 is_signed_type(unsigned long));
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200169 if (!ret)
170 return 0;
171 offset += sizeof(unsigned long);
172 }
173
Li Zefan4539f072009-08-20 16:13:35 +0800174 trace_seq_puts(s, "\nprint fmt: \"");
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200175 for (i = 0; i < entry->nb_args; i++) {
Li Zefane6971962009-08-19 15:52:25 +0800176 ret = trace_seq_printf(s, "%s: 0x%%0%zulx%s", entry->args[i],
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200177 sizeof(unsigned long),
Li Zefan4539f072009-08-20 16:13:35 +0800178 i == entry->nb_args - 1 ? "" : ", ");
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200179 if (!ret)
180 return 0;
181 }
Li Zefan4539f072009-08-20 16:13:35 +0800182 trace_seq_putc(s, '"');
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200183
184 for (i = 0; i < entry->nb_args; i++) {
Li Zefan4539f072009-08-20 16:13:35 +0800185 ret = trace_seq_printf(s, ", ((unsigned long)(REC->%s))",
186 entry->args[i]);
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200187 if (!ret)
188 return 0;
189 }
190
Li Zefan4539f072009-08-20 16:13:35 +0800191 return trace_seq_putc(s, '\n');
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200192}
193
Li Zefan10a5b662009-08-19 15:53:05 +0800194int syscall_exit_format(struct ftrace_event_call *call, struct trace_seq *s)
195{
196 int ret;
197 struct syscall_trace_exit trace;
198
199 ret = trace_seq_printf(s,
Tom Zanussi26a50742009-10-06 01:09:50 -0500200 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;"
201 "\tsigned:%u;\n"
202 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;"
203 "\tsigned:%u;\n",
Li Zefan10a5b662009-08-19 15:53:05 +0800204 SYSCALL_FIELD(int, nr),
Tom Zanussiee949a82009-10-06 01:00:49 -0500205 SYSCALL_FIELD(long, ret));
Li Zefan10a5b662009-08-19 15:53:05 +0800206 if (!ret)
207 return 0;
208
209 return trace_seq_printf(s, "\nprint fmt: \"0x%%lx\", REC->ret\n");
210}
211
Li Zefan540b7b82009-08-19 15:54:51 +0800212int syscall_enter_define_fields(struct ftrace_event_call *call)
213{
214 struct syscall_trace_enter trace;
Lai Jiangshan31c16b12009-12-01 16:23:30 +0800215 struct syscall_metadata *meta = call->data;
Li Zefan540b7b82009-08-19 15:54:51 +0800216 int ret;
Li Zefan540b7b82009-08-19 15:54:51 +0800217 int i;
218 int offset = offsetof(typeof(trace), args);
219
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800220 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
221 if (ret)
222 return ret;
223
Li Zefan540b7b82009-08-19 15:54:51 +0800224 for (i = 0; i < meta->nb_args; i++) {
Frederic Weisbeckeraeaeae12009-08-27 05:09:51 +0200225 ret = trace_define_field(call, meta->types[i],
226 meta->args[i], offset,
Li Zefan43b51ea2009-08-07 10:33:22 +0800227 sizeof(unsigned long), 0,
228 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800229 offset += sizeof(unsigned long);
230 }
231
232 return ret;
233}
234
235int syscall_exit_define_fields(struct ftrace_event_call *call)
236{
237 struct syscall_trace_exit trace;
238 int 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
Tom Zanussi26a50742009-10-06 01:09:50 -0500244 ret = trace_define_field(call, SYSCALL_FIELD(long, ret),
Li Zefan43b51ea2009-08-07 10:33:22 +0800245 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800246
247 return ret;
248}
249
Jason Baronfb34a082009-08-10 16:52:47 -0400250void ftrace_syscall_enter(struct pt_regs *regs, long id)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100251{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100252 struct syscall_trace_enter *entry;
253 struct syscall_metadata *sys_data;
254 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400255 struct ring_buffer *buffer;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100256 int size;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100257 int syscall_nr;
258
259 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200260 if (syscall_nr < 0)
261 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400262 if (!test_bit(syscall_nr, enabled_enter_syscalls))
263 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100264
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100265 sys_data = syscall_nr_to_meta(syscall_nr);
266 if (!sys_data)
267 return;
268
269 size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
270
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800271 event = trace_current_buffer_lock_reserve(&buffer,
272 sys_data->enter_event->id, size, 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100273 if (!event)
274 return;
275
276 entry = ring_buffer_event_data(event);
277 entry->nr = syscall_nr;
278 syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
279
Steven Rostedte77405a2009-09-02 14:17:06 -0400280 if (!filter_current_check_discard(buffer, sys_data->enter_event,
281 entry, event))
282 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100283}
284
Jason Baronfb34a082009-08-10 16:52:47 -0400285void ftrace_syscall_exit(struct pt_regs *regs, long ret)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100286{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100287 struct syscall_trace_exit *entry;
288 struct syscall_metadata *sys_data;
289 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400290 struct ring_buffer *buffer;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100291 int syscall_nr;
292
293 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200294 if (syscall_nr < 0)
295 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400296 if (!test_bit(syscall_nr, enabled_exit_syscalls))
297 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100298
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100299 sys_data = syscall_nr_to_meta(syscall_nr);
300 if (!sys_data)
301 return;
302
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800303 event = trace_current_buffer_lock_reserve(&buffer,
304 sys_data->exit_event->id, sizeof(*entry), 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100305 if (!event)
306 return;
307
308 entry = ring_buffer_event_data(event);
309 entry->nr = syscall_nr;
310 entry->ret = syscall_get_return_value(current, regs);
311
Steven Rostedte77405a2009-09-02 14:17:06 -0400312 if (!filter_current_check_discard(buffer, sys_data->exit_event,
313 entry, event))
314 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100315}
316
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400317int reg_event_syscall_enter(struct ftrace_event_call *call)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100318{
Jason Baronfb34a082009-08-10 16:52:47 -0400319 int ret = 0;
320 int num;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100321
Lai Jiangshanc252f652009-12-01 16:23:47 +0800322 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400323 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400324 return -ENOSYS;
325 mutex_lock(&syscall_trace_lock);
326 if (!sys_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700327 ret = register_trace_sys_enter(ftrace_syscall_enter);
Li Zefan3b8e4272009-12-08 11:14:52 +0800328 if (!ret) {
Jason Baronfb34a082009-08-10 16:52:47 -0400329 set_bit(num, enabled_enter_syscalls);
330 sys_refcount_enter++;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100331 }
Jason Baronfb34a082009-08-10 16:52:47 -0400332 mutex_unlock(&syscall_trace_lock);
333 return ret;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100334}
Jason Baronfb34a082009-08-10 16:52:47 -0400335
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400336void unreg_event_syscall_enter(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400337{
338 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400339
Lai Jiangshanc252f652009-12-01 16:23:47 +0800340 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400341 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400342 return;
343 mutex_lock(&syscall_trace_lock);
344 sys_refcount_enter--;
345 clear_bit(num, enabled_enter_syscalls);
346 if (!sys_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700347 unregister_trace_sys_enter(ftrace_syscall_enter);
Jason Baronfb34a082009-08-10 16:52:47 -0400348 mutex_unlock(&syscall_trace_lock);
349}
350
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400351int reg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400352{
353 int ret = 0;
354 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400355
Lai Jiangshanc252f652009-12-01 16:23:47 +0800356 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400357 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400358 return -ENOSYS;
359 mutex_lock(&syscall_trace_lock);
360 if (!sys_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700361 ret = register_trace_sys_exit(ftrace_syscall_exit);
Li Zefan3b8e4272009-12-08 11:14:52 +0800362 if (!ret) {
Jason Baronfb34a082009-08-10 16:52:47 -0400363 set_bit(num, enabled_exit_syscalls);
364 sys_refcount_exit++;
365 }
366 mutex_unlock(&syscall_trace_lock);
367 return ret;
368}
369
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400370void unreg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400371{
372 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400373
Lai Jiangshanc252f652009-12-01 16:23:47 +0800374 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400375 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400376 return;
377 mutex_lock(&syscall_trace_lock);
378 sys_refcount_exit--;
379 clear_bit(num, enabled_exit_syscalls);
380 if (!sys_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700381 unregister_trace_sys_exit(ftrace_syscall_exit);
Jason Baronfb34a082009-08-10 16:52:47 -0400382 mutex_unlock(&syscall_trace_lock);
383}
384
Lai Jiangshana1301da2009-12-01 16:23:55 +0800385int init_syscall_trace(struct ftrace_event_call *call)
386{
387 int id;
388
389 id = register_ftrace_event(call->event);
390 if (!id)
391 return -ENODEV;
392 call->id = id;
393 INIT_LIST_HEAD(&call->fields);
394 return 0;
395}
396
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200397int __init init_ftrace_syscalls(void)
398{
399 struct syscall_metadata *meta;
400 unsigned long addr;
401 int i;
402
403 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
404 NR_syscalls, GFP_KERNEL);
405 if (!syscalls_metadata) {
406 WARN_ON(1);
407 return -ENOMEM;
408 }
409
410 for (i = 0; i < NR_syscalls; i++) {
411 addr = arch_syscall_addr(i);
412 meta = find_syscall_meta(addr);
Lai Jiangshanc252f652009-12-01 16:23:47 +0800413 if (!meta)
414 continue;
415
416 meta->syscall_nr = i;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200417 syscalls_metadata[i] = meta;
418 }
419
420 return 0;
421}
422core_initcall(init_ftrace_syscalls);
423
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400424#ifdef CONFIG_EVENT_PROFILE
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200425
Jason Baron57421db2009-08-24 17:40:22 -0400426static DECLARE_BITMAP(enabled_prof_enter_syscalls, NR_syscalls);
427static DECLARE_BITMAP(enabled_prof_exit_syscalls, NR_syscalls);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400428static int sys_prof_refcount_enter;
429static int sys_prof_refcount_exit;
430
431static void prof_syscall_enter(struct pt_regs *regs, long id)
432{
433 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200434 struct syscall_trace_enter *rec;
435 unsigned long flags;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100436 char *trace_buf;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200437 char *raw_data;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400438 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100439 int rctx;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200440 int size;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200441 int cpu;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400442
443 syscall_nr = syscall_get_nr(current, regs);
444 if (!test_bit(syscall_nr, enabled_prof_enter_syscalls))
445 return;
446
447 sys_data = syscall_nr_to_meta(syscall_nr);
448 if (!sys_data)
449 return;
450
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200451 /* get the size after alignment with the u32 buffer size field */
452 size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
453 size = ALIGN(size + sizeof(u32), sizeof(u64));
454 size -= sizeof(u32);
455
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200456 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
457 "profile buffer not large enough"))
458 return;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200459
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200460 /* Protect the per cpu buffer, begin the rcu read side */
461 local_irq_save(flags);
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200462
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100463 rctx = perf_swevent_get_recursion_context();
464 if (rctx < 0)
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100465 goto end_recursion;
466
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200467 cpu = smp_processor_id();
468
Frederic Weisbecker28889bf2009-11-22 05:21:33 +0100469 trace_buf = rcu_dereference(perf_trace_buf);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200470
Frederic Weisbecker444a2a32009-11-06 04:13:05 +0100471 if (!trace_buf)
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200472 goto end;
473
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100474 raw_data = per_cpu_ptr(trace_buf, cpu);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200475
476 /* zero the dead bytes from align to not leak stack to user */
477 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
478
479 rec = (struct syscall_trace_enter *) raw_data;
480 tracing_generic_entry_update(&rec->ent, 0, 0);
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800481 rec->ent.type = sys_data->enter_event->id;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200482 rec->nr = syscall_nr;
483 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
484 (unsigned long *)&rec->args);
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800485 perf_tp_event(sys_data->enter_event->id, 0, 1, rec, size);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200486
487end:
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100488 perf_swevent_put_recursion_context(rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100489end_recursion:
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200490 local_irq_restore(flags);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400491}
492
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800493int prof_sysenter_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400494{
495 int ret = 0;
496 int num;
497
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800498 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400499
500 mutex_lock(&syscall_trace_lock);
501 if (!sys_prof_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700502 ret = register_trace_sys_enter(prof_syscall_enter);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400503 if (ret) {
504 pr_info("event trace: Could not activate"
505 "syscall entry trace point");
506 } else {
507 set_bit(num, enabled_prof_enter_syscalls);
508 sys_prof_refcount_enter++;
509 }
510 mutex_unlock(&syscall_trace_lock);
511 return ret;
512}
513
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800514void prof_sysenter_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400515{
516 int num;
517
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800518 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400519
520 mutex_lock(&syscall_trace_lock);
521 sys_prof_refcount_enter--;
522 clear_bit(num, enabled_prof_enter_syscalls);
523 if (!sys_prof_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700524 unregister_trace_sys_enter(prof_syscall_enter);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400525 mutex_unlock(&syscall_trace_lock);
526}
527
528static void prof_syscall_exit(struct pt_regs *regs, long ret)
529{
530 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200531 struct syscall_trace_exit *rec;
532 unsigned long flags;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400533 int syscall_nr;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100534 char *trace_buf;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200535 char *raw_data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100536 int rctx;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200537 int size;
538 int cpu;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400539
540 syscall_nr = syscall_get_nr(current, regs);
541 if (!test_bit(syscall_nr, enabled_prof_exit_syscalls))
542 return;
543
544 sys_data = syscall_nr_to_meta(syscall_nr);
545 if (!sys_data)
546 return;
547
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200548 /* We can probably do that at build time */
549 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
550 size -= sizeof(u32);
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200551
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200552 /*
553 * Impossible, but be paranoid with the future
554 * How to put this check outside runtime?
555 */
556 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
557 "exit event has grown above profile buffer size"))
558 return;
559
560 /* Protect the per cpu buffer, begin the rcu read side */
561 local_irq_save(flags);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100562
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100563 rctx = perf_swevent_get_recursion_context();
564 if (rctx < 0)
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100565 goto end_recursion;
566
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200567 cpu = smp_processor_id();
568
Frederic Weisbecker28889bf2009-11-22 05:21:33 +0100569 trace_buf = rcu_dereference(perf_trace_buf);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200570
Frederic Weisbecker444a2a32009-11-06 04:13:05 +0100571 if (!trace_buf)
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200572 goto end;
573
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100574 raw_data = per_cpu_ptr(trace_buf, cpu);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200575
576 /* zero the dead bytes from align to not leak stack to user */
577 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
578
579 rec = (struct syscall_trace_exit *)raw_data;
580
581 tracing_generic_entry_update(&rec->ent, 0, 0);
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800582 rec->ent.type = sys_data->exit_event->id;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200583 rec->nr = syscall_nr;
584 rec->ret = syscall_get_return_value(current, regs);
585
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800586 perf_tp_event(sys_data->exit_event->id, 0, 1, rec, size);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200587
588end:
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100589 perf_swevent_put_recursion_context(rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100590end_recursion:
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200591 local_irq_restore(flags);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400592}
593
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800594int prof_sysexit_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400595{
596 int ret = 0;
597 int num;
598
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800599 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400600
601 mutex_lock(&syscall_trace_lock);
602 if (!sys_prof_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700603 ret = register_trace_sys_exit(prof_syscall_exit);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400604 if (ret) {
605 pr_info("event trace: Could not activate"
606 "syscall entry trace point");
607 } else {
608 set_bit(num, enabled_prof_exit_syscalls);
609 sys_prof_refcount_exit++;
610 }
611 mutex_unlock(&syscall_trace_lock);
612 return ret;
613}
614
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800615void prof_sysexit_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400616{
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);
622 sys_prof_refcount_exit--;
623 clear_bit(num, enabled_prof_exit_syscalls);
624 if (!sys_prof_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700625 unregister_trace_sys_exit(prof_syscall_exit);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400626 mutex_unlock(&syscall_trace_lock);
627}
628
629#endif
630
631