blob: b957edd0ca3b35750b93e6968ad1f6e09d073239 [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);
Jason Baronfb34a082009-08-10 16:52:47 -0400328 if (ret) {
329 pr_info("event trace: Could not activate"
330 "syscall entry trace point");
331 } else {
332 set_bit(num, enabled_enter_syscalls);
333 sys_refcount_enter++;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100334 }
Jason Baronfb34a082009-08-10 16:52:47 -0400335 mutex_unlock(&syscall_trace_lock);
336 return ret;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100337}
Jason Baronfb34a082009-08-10 16:52:47 -0400338
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400339void unreg_event_syscall_enter(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400340{
341 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400342
Lai Jiangshanc252f652009-12-01 16:23:47 +0800343 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400344 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400345 return;
346 mutex_lock(&syscall_trace_lock);
347 sys_refcount_enter--;
348 clear_bit(num, enabled_enter_syscalls);
349 if (!sys_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700350 unregister_trace_sys_enter(ftrace_syscall_enter);
Jason Baronfb34a082009-08-10 16:52:47 -0400351 mutex_unlock(&syscall_trace_lock);
352}
353
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400354int reg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400355{
356 int ret = 0;
357 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400358
Lai Jiangshanc252f652009-12-01 16:23:47 +0800359 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400360 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400361 return -ENOSYS;
362 mutex_lock(&syscall_trace_lock);
363 if (!sys_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700364 ret = register_trace_sys_exit(ftrace_syscall_exit);
Jason Baronfb34a082009-08-10 16:52:47 -0400365 if (ret) {
366 pr_info("event trace: Could not activate"
367 "syscall exit trace point");
368 } else {
369 set_bit(num, enabled_exit_syscalls);
370 sys_refcount_exit++;
371 }
372 mutex_unlock(&syscall_trace_lock);
373 return ret;
374}
375
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400376void unreg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400377{
378 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400379
Lai Jiangshanc252f652009-12-01 16:23:47 +0800380 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400381 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400382 return;
383 mutex_lock(&syscall_trace_lock);
384 sys_refcount_exit--;
385 clear_bit(num, enabled_exit_syscalls);
386 if (!sys_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700387 unregister_trace_sys_exit(ftrace_syscall_exit);
Jason Baronfb34a082009-08-10 16:52:47 -0400388 mutex_unlock(&syscall_trace_lock);
389}
390
Lai Jiangshana1301da2009-12-01 16:23:55 +0800391int init_syscall_trace(struct ftrace_event_call *call)
392{
393 int id;
394
395 id = register_ftrace_event(call->event);
396 if (!id)
397 return -ENODEV;
398 call->id = id;
399 INIT_LIST_HEAD(&call->fields);
400 return 0;
401}
402
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200403int __init init_ftrace_syscalls(void)
404{
405 struct syscall_metadata *meta;
406 unsigned long addr;
407 int i;
408
409 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
410 NR_syscalls, GFP_KERNEL);
411 if (!syscalls_metadata) {
412 WARN_ON(1);
413 return -ENOMEM;
414 }
415
416 for (i = 0; i < NR_syscalls; i++) {
417 addr = arch_syscall_addr(i);
418 meta = find_syscall_meta(addr);
Lai Jiangshanc252f652009-12-01 16:23:47 +0800419 if (!meta)
420 continue;
421
422 meta->syscall_nr = i;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200423 syscalls_metadata[i] = meta;
424 }
425
426 return 0;
427}
428core_initcall(init_ftrace_syscalls);
429
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400430#ifdef CONFIG_EVENT_PROFILE
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200431
Jason Baron57421db2009-08-24 17:40:22 -0400432static DECLARE_BITMAP(enabled_prof_enter_syscalls, NR_syscalls);
433static DECLARE_BITMAP(enabled_prof_exit_syscalls, NR_syscalls);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400434static int sys_prof_refcount_enter;
435static int sys_prof_refcount_exit;
436
437static void prof_syscall_enter(struct pt_regs *regs, long id)
438{
439 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200440 struct syscall_trace_enter *rec;
441 unsigned long flags;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100442 char *trace_buf;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200443 char *raw_data;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400444 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100445 int rctx;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200446 int size;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200447 int cpu;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400448
449 syscall_nr = syscall_get_nr(current, regs);
450 if (!test_bit(syscall_nr, enabled_prof_enter_syscalls))
451 return;
452
453 sys_data = syscall_nr_to_meta(syscall_nr);
454 if (!sys_data)
455 return;
456
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200457 /* get the size after alignment with the u32 buffer size field */
458 size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
459 size = ALIGN(size + sizeof(u32), sizeof(u64));
460 size -= sizeof(u32);
461
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200462 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
463 "profile buffer not large enough"))
464 return;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200465
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200466 /* Protect the per cpu buffer, begin the rcu read side */
467 local_irq_save(flags);
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200468
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100469 rctx = perf_swevent_get_recursion_context();
470 if (rctx < 0)
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100471 goto end_recursion;
472
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200473 cpu = smp_processor_id();
474
Frederic Weisbecker28889bf2009-11-22 05:21:33 +0100475 trace_buf = rcu_dereference(perf_trace_buf);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200476
Frederic Weisbecker444a2a32009-11-06 04:13:05 +0100477 if (!trace_buf)
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200478 goto end;
479
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100480 raw_data = per_cpu_ptr(trace_buf, cpu);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200481
482 /* zero the dead bytes from align to not leak stack to user */
483 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
484
485 rec = (struct syscall_trace_enter *) raw_data;
486 tracing_generic_entry_update(&rec->ent, 0, 0);
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800487 rec->ent.type = sys_data->enter_event->id;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200488 rec->nr = syscall_nr;
489 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
490 (unsigned long *)&rec->args);
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800491 perf_tp_event(sys_data->enter_event->id, 0, 1, rec, size);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200492
493end:
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100494 perf_swevent_put_recursion_context(rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100495end_recursion:
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200496 local_irq_restore(flags);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400497}
498
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800499int prof_sysenter_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400500{
501 int ret = 0;
502 int num;
503
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800504 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400505
506 mutex_lock(&syscall_trace_lock);
507 if (!sys_prof_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700508 ret = register_trace_sys_enter(prof_syscall_enter);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400509 if (ret) {
510 pr_info("event trace: Could not activate"
511 "syscall entry trace point");
512 } else {
513 set_bit(num, enabled_prof_enter_syscalls);
514 sys_prof_refcount_enter++;
515 }
516 mutex_unlock(&syscall_trace_lock);
517 return ret;
518}
519
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800520void prof_sysenter_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400521{
522 int num;
523
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800524 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400525
526 mutex_lock(&syscall_trace_lock);
527 sys_prof_refcount_enter--;
528 clear_bit(num, enabled_prof_enter_syscalls);
529 if (!sys_prof_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700530 unregister_trace_sys_enter(prof_syscall_enter);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400531 mutex_unlock(&syscall_trace_lock);
532}
533
534static void prof_syscall_exit(struct pt_regs *regs, long ret)
535{
536 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200537 struct syscall_trace_exit *rec;
538 unsigned long flags;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400539 int syscall_nr;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100540 char *trace_buf;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200541 char *raw_data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100542 int rctx;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200543 int size;
544 int cpu;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400545
546 syscall_nr = syscall_get_nr(current, regs);
547 if (!test_bit(syscall_nr, enabled_prof_exit_syscalls))
548 return;
549
550 sys_data = syscall_nr_to_meta(syscall_nr);
551 if (!sys_data)
552 return;
553
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200554 /* We can probably do that at build time */
555 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
556 size -= sizeof(u32);
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200557
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200558 /*
559 * Impossible, but be paranoid with the future
560 * How to put this check outside runtime?
561 */
562 if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
563 "exit event has grown above profile buffer size"))
564 return;
565
566 /* Protect the per cpu buffer, begin the rcu read side */
567 local_irq_save(flags);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100568
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100569 rctx = perf_swevent_get_recursion_context();
570 if (rctx < 0)
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100571 goto end_recursion;
572
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200573 cpu = smp_processor_id();
574
Frederic Weisbecker28889bf2009-11-22 05:21:33 +0100575 trace_buf = rcu_dereference(perf_trace_buf);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200576
Frederic Weisbecker444a2a32009-11-06 04:13:05 +0100577 if (!trace_buf)
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200578 goto end;
579
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100580 raw_data = per_cpu_ptr(trace_buf, cpu);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200581
582 /* zero the dead bytes from align to not leak stack to user */
583 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
584
585 rec = (struct syscall_trace_exit *)raw_data;
586
587 tracing_generic_entry_update(&rec->ent, 0, 0);
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800588 rec->ent.type = sys_data->exit_event->id;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200589 rec->nr = syscall_nr;
590 rec->ret = syscall_get_return_value(current, regs);
591
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800592 perf_tp_event(sys_data->exit_event->id, 0, 1, rec, size);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200593
594end:
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100595 perf_swevent_put_recursion_context(rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100596end_recursion:
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200597 local_irq_restore(flags);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400598}
599
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800600int prof_sysexit_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400601{
602 int ret = 0;
603 int num;
604
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800605 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400606
607 mutex_lock(&syscall_trace_lock);
608 if (!sys_prof_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700609 ret = register_trace_sys_exit(prof_syscall_exit);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400610 if (ret) {
611 pr_info("event trace: Could not activate"
612 "syscall entry trace point");
613 } else {
614 set_bit(num, enabled_prof_exit_syscalls);
615 sys_prof_refcount_exit++;
616 }
617 mutex_unlock(&syscall_trace_lock);
618 return ret;
619}
620
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800621void prof_sysexit_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400622{
623 int num;
624
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800625 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400626
627 mutex_lock(&syscall_trace_lock);
628 sys_prof_refcount_exit--;
629 clear_bit(num, enabled_prof_exit_syscalls);
630 if (!sys_prof_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700631 unregister_trace_sys_exit(prof_syscall_exit);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400632 mutex_unlock(&syscall_trace_lock);
633}
634
635#endif
636
637