1 #include <trace/syscall.h>
2 #include <linux/kernel.h>
3 #include <linux/ftrace.h>
4 #include <asm/syscall.h>
6 #include "trace_output.h"
9 static DEFINE_MUTEX(syscall_trace_lock);
10 static int sys_refcount_enter;
11 static int sys_refcount_exit;
12 static DECLARE_BITMAP(enabled_enter_syscalls, FTRACE_SYSCALL_MAX);
13 static DECLARE_BITMAP(enabled_exit_syscalls, FTRACE_SYSCALL_MAX);
15 /* Option to display the parameters types */
17 TRACE_SYSCALLS_OPT_TYPES = 0x1,
20 static struct tracer_opt syscalls_opts[] = {
21 { TRACER_OPT(syscall_arg_type, TRACE_SYSCALLS_OPT_TYPES) },
25 static struct tracer_flags syscalls_flags = {
26 .val = 0, /* By default: no parameters types */
31 print_syscall_enter(struct trace_iterator *iter, int flags)
33 struct trace_seq *s = &iter->seq;
34 struct trace_entry *ent = iter->ent;
35 struct syscall_trace_enter *trace;
36 struct syscall_metadata *entry;
39 trace = (typeof(trace))ent;
41 entry = syscall_nr_to_meta(syscall);
46 if (entry->enter_id != ent->type) {
51 ret = trace_seq_printf(s, "%s(", entry->name);
53 return TRACE_TYPE_PARTIAL_LINE;
55 for (i = 0; i < entry->nb_args; i++) {
57 if (syscalls_flags.val & TRACE_SYSCALLS_OPT_TYPES) {
58 ret = trace_seq_printf(s, "%s ", entry->types[i]);
60 return TRACE_TYPE_PARTIAL_LINE;
62 /* parameter values */
63 ret = trace_seq_printf(s, "%s: %lx%s ", entry->args[i],
65 i == entry->nb_args - 1 ? ")" : ",");
67 return TRACE_TYPE_PARTIAL_LINE;
71 trace_seq_printf(s, "\n");
72 return TRACE_TYPE_HANDLED;
76 print_syscall_exit(struct trace_iterator *iter, int flags)
78 struct trace_seq *s = &iter->seq;
79 struct trace_entry *ent = iter->ent;
80 struct syscall_trace_exit *trace;
82 struct syscall_metadata *entry;
85 trace = (typeof(trace))ent;
87 entry = syscall_nr_to_meta(syscall);
90 trace_seq_printf(s, "\n");
91 return TRACE_TYPE_HANDLED;
94 if (entry->exit_id != ent->type) {
96 return TRACE_TYPE_UNHANDLED;
99 ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
102 return TRACE_TYPE_PARTIAL_LINE;
104 return TRACE_TYPE_HANDLED;
107 void ftrace_syscall_enter(struct pt_regs *regs, long id)
109 struct syscall_trace_enter *entry;
110 struct syscall_metadata *sys_data;
111 struct ring_buffer_event *event;
115 syscall_nr = syscall_get_nr(current, regs);
116 if (!test_bit(syscall_nr, enabled_enter_syscalls))
119 sys_data = syscall_nr_to_meta(syscall_nr);
123 size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
125 event = trace_current_buffer_lock_reserve(sys_data->enter_id, size,
130 entry = ring_buffer_event_data(event);
131 entry->nr = syscall_nr;
132 syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
134 trace_current_buffer_unlock_commit(event, 0, 0);
138 void ftrace_syscall_exit(struct pt_regs *regs, long ret)
140 struct syscall_trace_exit *entry;
141 struct syscall_metadata *sys_data;
142 struct ring_buffer_event *event;
145 syscall_nr = syscall_get_nr(current, regs);
146 if (!test_bit(syscall_nr, enabled_exit_syscalls))
149 sys_data = syscall_nr_to_meta(syscall_nr);
153 event = trace_current_buffer_lock_reserve(sys_data->exit_id,
154 sizeof(*entry), 0, 0);
158 entry = ring_buffer_event_data(event);
159 entry->nr = syscall_nr;
160 entry->ret = syscall_get_return_value(current, regs);
162 trace_current_buffer_unlock_commit(event, 0, 0);
166 int reg_event_syscall_enter(void *ptr)
173 num = syscall_name_to_nr(name);
174 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
176 mutex_lock(&syscall_trace_lock);
177 if (!sys_refcount_enter)
178 ret = register_trace_syscall_enter(ftrace_syscall_enter);
180 pr_info("event trace: Could not activate"
181 "syscall entry trace point");
183 set_bit(num, enabled_enter_syscalls);
184 sys_refcount_enter++;
186 mutex_unlock(&syscall_trace_lock);
190 void unreg_event_syscall_enter(void *ptr)
196 num = syscall_name_to_nr(name);
197 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
199 mutex_lock(&syscall_trace_lock);
200 sys_refcount_enter--;
201 clear_bit(num, enabled_enter_syscalls);
202 if (!sys_refcount_enter)
203 unregister_trace_syscall_enter(ftrace_syscall_enter);
204 mutex_unlock(&syscall_trace_lock);
207 int reg_event_syscall_exit(void *ptr)
214 num = syscall_name_to_nr(name);
215 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
217 mutex_lock(&syscall_trace_lock);
218 if (!sys_refcount_exit)
219 ret = register_trace_syscall_exit(ftrace_syscall_exit);
221 pr_info("event trace: Could not activate"
222 "syscall exit trace point");
224 set_bit(num, enabled_exit_syscalls);
227 mutex_unlock(&syscall_trace_lock);
231 void unreg_event_syscall_exit(void *ptr)
237 num = syscall_name_to_nr(name);
238 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
240 mutex_lock(&syscall_trace_lock);
242 clear_bit(num, enabled_exit_syscalls);
243 if (!sys_refcount_exit)
244 unregister_trace_syscall_exit(ftrace_syscall_exit);
245 mutex_unlock(&syscall_trace_lock);
248 struct trace_event event_syscall_enter = {
249 .trace = print_syscall_enter,
252 struct trace_event event_syscall_exit = {
253 .trace = print_syscall_exit,