Frederic Weisbecker | 47788c5 | 2009-04-08 20:40:59 +0200 | [diff] [blame] | 1 | #include <trace/syscall.h> |
Josh Stone | 1c569f0 | 2009-08-24 14:43:14 -0700 | [diff] [blame] | 2 | #include <trace/events/syscalls.h> |
Steven Rostedt | f431b63 | 2013-02-12 16:18:59 -0500 | [diff] [blame] | 3 | #include <linux/syscalls.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 4 | #include <linux/slab.h> |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 5 | #include <linux/kernel.h> |
Paul Gortmaker | 56d82e0 | 2011-05-26 17:53:52 -0400 | [diff] [blame] | 6 | #include <linux/module.h> /* for MODULE_NAME_LEN via KSYM_SYMBOL_LEN */ |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 7 | #include <linux/ftrace.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 8 | #include <linux/perf_event.h> |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 9 | #include <asm/syscall.h> |
| 10 | |
| 11 | #include "trace_output.h" |
| 12 | #include "trace.h" |
| 13 | |
Frederic Weisbecker | 5be71b6 | 2009-03-15 22:10:37 +0100 | [diff] [blame] | 14 | static DEFINE_MUTEX(syscall_trace_lock); |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 15 | static int sys_refcount_enter; |
| 16 | static int sys_refcount_exit; |
Jason Baron | 57421db | 2009-08-24 17:40:22 -0400 | [diff] [blame] | 17 | static DECLARE_BITMAP(enabled_enter_syscalls, NR_syscalls); |
| 18 | static DECLARE_BITMAP(enabled_exit_syscalls, NR_syscalls); |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 19 | |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 20 | static int syscall_enter_register(struct ftrace_event_call *event, |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 21 | enum trace_reg type, void *data); |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 22 | static int syscall_exit_register(struct ftrace_event_call *event, |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 23 | enum trace_reg type, void *data); |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 24 | |
Steven Rostedt | 2e33af0 | 2010-04-22 10:35:55 -0400 | [diff] [blame] | 25 | static struct list_head * |
| 26 | syscall_get_enter_fields(struct ftrace_event_call *call) |
| 27 | { |
| 28 | struct syscall_metadata *entry = call->data; |
| 29 | |
| 30 | return &entry->enter_fields; |
| 31 | } |
| 32 | |
Steven Rostedt | 3d56e33 | 2011-02-02 17:06:09 -0500 | [diff] [blame] | 33 | extern struct syscall_metadata *__start_syscalls_metadata[]; |
| 34 | extern struct syscall_metadata *__stop_syscalls_metadata[]; |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 35 | |
| 36 | static struct syscall_metadata **syscalls_metadata; |
| 37 | |
Ian Munsie | b2d5549 | 2011-02-03 14:27:23 +1100 | [diff] [blame] | 38 | #ifndef ARCH_HAS_SYSCALL_MATCH_SYM_NAME |
| 39 | static inline bool arch_syscall_match_sym_name(const char *sym, const char *name) |
| 40 | { |
| 41 | /* |
| 42 | * Only compare after the "sys" prefix. Archs that use |
| 43 | * syscall wrappers may have syscalls symbols aliases prefixed |
| 44 | * with "SyS" instead of "sys", leading to an unwanted |
| 45 | * mismatch. |
| 46 | */ |
| 47 | return !strcmp(sym + 3, name + 3); |
| 48 | } |
| 49 | #endif |
| 50 | |
Steven Rostedt | f431b63 | 2013-02-12 16:18:59 -0500 | [diff] [blame] | 51 | #ifdef ARCH_TRACE_IGNORE_COMPAT_SYSCALLS |
| 52 | /* |
| 53 | * Some architectures that allow for 32bit applications |
| 54 | * to run on a 64bit kernel, do not map the syscalls for |
| 55 | * the 32bit tasks the same as they do for 64bit tasks. |
| 56 | * |
| 57 | * *cough*x86*cough* |
| 58 | * |
| 59 | * In such a case, instead of reporting the wrong syscalls, |
| 60 | * simply ignore them. |
| 61 | * |
| 62 | * For an arch to ignore the compat syscalls it needs to |
| 63 | * define ARCH_TRACE_IGNORE_COMPAT_SYSCALLS as well as |
| 64 | * define the function arch_trace_is_compat_syscall() to let |
| 65 | * the tracing system know that it should ignore it. |
| 66 | */ |
| 67 | static int |
| 68 | trace_get_syscall_nr(struct task_struct *task, struct pt_regs *regs) |
| 69 | { |
| 70 | if (unlikely(arch_trace_is_compat_syscall(regs))) |
| 71 | return -1; |
| 72 | |
| 73 | return syscall_get_nr(task, regs); |
| 74 | } |
| 75 | #else |
| 76 | static inline int |
| 77 | trace_get_syscall_nr(struct task_struct *task, struct pt_regs *regs) |
| 78 | { |
| 79 | return syscall_get_nr(task, regs); |
| 80 | } |
| 81 | #endif /* ARCH_TRACE_IGNORE_COMPAT_SYSCALLS */ |
| 82 | |
Steven Rostedt | 3d56e33 | 2011-02-02 17:06:09 -0500 | [diff] [blame] | 83 | static __init struct syscall_metadata * |
| 84 | find_syscall_meta(unsigned long syscall) |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 85 | { |
Steven Rostedt | 3d56e33 | 2011-02-02 17:06:09 -0500 | [diff] [blame] | 86 | struct syscall_metadata **start; |
| 87 | struct syscall_metadata **stop; |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 88 | char str[KSYM_SYMBOL_LEN]; |
| 89 | |
| 90 | |
Steven Rostedt | 3d56e33 | 2011-02-02 17:06:09 -0500 | [diff] [blame] | 91 | start = __start_syscalls_metadata; |
| 92 | stop = __stop_syscalls_metadata; |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 93 | kallsyms_lookup(syscall, NULL, NULL, NULL, str); |
| 94 | |
Ian Munsie | ae07f55 | 2011-02-03 14:27:25 +1100 | [diff] [blame] | 95 | if (arch_syscall_match_sym_name(str, "sys_ni_syscall")) |
| 96 | return NULL; |
| 97 | |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 98 | for ( ; start < stop; start++) { |
Ian Munsie | b2d5549 | 2011-02-03 14:27:23 +1100 | [diff] [blame] | 99 | if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name)) |
Steven Rostedt | 3d56e33 | 2011-02-02 17:06:09 -0500 | [diff] [blame] | 100 | return *start; |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 101 | } |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | static struct syscall_metadata *syscall_nr_to_meta(int nr) |
| 106 | { |
| 107 | if (!syscalls_metadata || nr >= NR_syscalls || nr < 0) |
| 108 | return NULL; |
| 109 | |
| 110 | return syscalls_metadata[nr]; |
| 111 | } |
| 112 | |
Fengguang Wu | 6aea49c | 2012-11-21 15:13:47 +0800 | [diff] [blame] | 113 | static enum print_line_t |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 114 | print_syscall_enter(struct trace_iterator *iter, int flags, |
| 115 | struct trace_event *event) |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 116 | { |
| 117 | struct trace_seq *s = &iter->seq; |
| 118 | struct trace_entry *ent = iter->ent; |
| 119 | struct syscall_trace_enter *trace; |
| 120 | struct syscall_metadata *entry; |
| 121 | int i, ret, syscall; |
| 122 | |
Jason Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 123 | trace = (typeof(trace))ent; |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 124 | syscall = trace->nr; |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 125 | entry = syscall_nr_to_meta(syscall); |
Jason Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 126 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 127 | if (!entry) |
| 128 | goto end; |
| 129 | |
Steven Rostedt | 32c0eda | 2010-04-23 10:38:03 -0400 | [diff] [blame] | 130 | if (entry->enter_event->event.type != ent->type) { |
Jason Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 131 | WARN_ON_ONCE(1); |
| 132 | goto end; |
| 133 | } |
| 134 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 135 | ret = trace_seq_printf(s, "%s(", entry->name); |
| 136 | if (!ret) |
| 137 | return TRACE_TYPE_PARTIAL_LINE; |
| 138 | |
| 139 | for (i = 0; i < entry->nb_args; i++) { |
| 140 | /* parameter types */ |
Li Zefan | ba8b3a4 | 2009-08-17 16:55:18 +0800 | [diff] [blame] | 141 | if (trace_flags & TRACE_ITER_VERBOSE) { |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 142 | ret = trace_seq_printf(s, "%s ", entry->types[i]); |
| 143 | if (!ret) |
| 144 | return TRACE_TYPE_PARTIAL_LINE; |
| 145 | } |
| 146 | /* parameter values */ |
Li Zefan | 4539f07 | 2009-08-20 16:13:35 +0800 | [diff] [blame] | 147 | ret = trace_seq_printf(s, "%s: %lx%s", entry->args[i], |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 148 | trace->args[i], |
Li Zefan | 4539f07 | 2009-08-20 16:13:35 +0800 | [diff] [blame] | 149 | i == entry->nb_args - 1 ? "" : ", "); |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 150 | if (!ret) |
| 151 | return TRACE_TYPE_PARTIAL_LINE; |
| 152 | } |
| 153 | |
Li Zefan | 4539f07 | 2009-08-20 16:13:35 +0800 | [diff] [blame] | 154 | ret = trace_seq_putc(s, ')'); |
| 155 | if (!ret) |
| 156 | return TRACE_TYPE_PARTIAL_LINE; |
| 157 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 158 | end: |
Li Zefan | 4539f07 | 2009-08-20 16:13:35 +0800 | [diff] [blame] | 159 | ret = trace_seq_putc(s, '\n'); |
| 160 | if (!ret) |
| 161 | return TRACE_TYPE_PARTIAL_LINE; |
| 162 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 163 | return TRACE_TYPE_HANDLED; |
| 164 | } |
| 165 | |
Fengguang Wu | 6aea49c | 2012-11-21 15:13:47 +0800 | [diff] [blame] | 166 | static enum print_line_t |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 167 | print_syscall_exit(struct trace_iterator *iter, int flags, |
| 168 | struct trace_event *event) |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 169 | { |
| 170 | struct trace_seq *s = &iter->seq; |
| 171 | struct trace_entry *ent = iter->ent; |
| 172 | struct syscall_trace_exit *trace; |
| 173 | int syscall; |
| 174 | struct syscall_metadata *entry; |
| 175 | int ret; |
| 176 | |
Jason Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 177 | trace = (typeof(trace))ent; |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 178 | syscall = trace->nr; |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 179 | entry = syscall_nr_to_meta(syscall); |
Jason Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 180 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 181 | if (!entry) { |
| 182 | trace_seq_printf(s, "\n"); |
| 183 | return TRACE_TYPE_HANDLED; |
| 184 | } |
| 185 | |
Steven Rostedt | 32c0eda | 2010-04-23 10:38:03 -0400 | [diff] [blame] | 186 | if (entry->exit_event->event.type != ent->type) { |
Jason Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 187 | WARN_ON_ONCE(1); |
| 188 | return TRACE_TYPE_UNHANDLED; |
| 189 | } |
| 190 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 191 | ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name, |
| 192 | trace->ret); |
| 193 | if (!ret) |
| 194 | return TRACE_TYPE_PARTIAL_LINE; |
| 195 | |
| 196 | return TRACE_TYPE_HANDLED; |
| 197 | } |
| 198 | |
Li Zefan | e697196 | 2009-08-19 15:52:25 +0800 | [diff] [blame] | 199 | extern char *__bad_type_size(void); |
| 200 | |
| 201 | #define SYSCALL_FIELD(type, name) \ |
| 202 | sizeof(type) != sizeof(trace.name) ? \ |
| 203 | __bad_type_size() : \ |
Tom Zanussi | 26a5074 | 2009-10-06 01:09:50 -0500 | [diff] [blame] | 204 | #type, #name, offsetof(typeof(trace), name), \ |
| 205 | sizeof(trace.name), is_signed_type(type) |
Li Zefan | e697196 | 2009-08-19 15:52:25 +0800 | [diff] [blame] | 206 | |
Lai Jiangshan | 50307a4 | 2009-12-15 15:39:45 +0800 | [diff] [blame] | 207 | static |
| 208 | int __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len) |
| 209 | { |
| 210 | int i; |
| 211 | int pos = 0; |
| 212 | |
| 213 | /* When len=0, we just calculate the needed length */ |
| 214 | #define LEN_OR_ZERO (len ? len - pos : 0) |
| 215 | |
| 216 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); |
| 217 | for (i = 0; i < entry->nb_args; i++) { |
| 218 | pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s", |
| 219 | entry->args[i], sizeof(unsigned long), |
| 220 | i == entry->nb_args - 1 ? "" : ", "); |
| 221 | } |
| 222 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); |
| 223 | |
| 224 | for (i = 0; i < entry->nb_args; i++) { |
| 225 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
| 226 | ", ((unsigned long)(REC->%s))", entry->args[i]); |
| 227 | } |
| 228 | |
| 229 | #undef LEN_OR_ZERO |
| 230 | |
| 231 | /* return the length of print_fmt */ |
| 232 | return pos; |
| 233 | } |
| 234 | |
| 235 | static int set_syscall_print_fmt(struct ftrace_event_call *call) |
| 236 | { |
| 237 | char *print_fmt; |
| 238 | int len; |
| 239 | struct syscall_metadata *entry = call->data; |
| 240 | |
| 241 | if (entry->enter_event != call) { |
| 242 | call->print_fmt = "\"0x%lx\", REC->ret"; |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /* First: called with 0 length to calculate the needed length */ |
| 247 | len = __set_enter_print_fmt(entry, NULL, 0); |
| 248 | |
| 249 | print_fmt = kmalloc(len + 1, GFP_KERNEL); |
| 250 | if (!print_fmt) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | /* Second: actually write the @print_fmt */ |
| 254 | __set_enter_print_fmt(entry, print_fmt, len + 1); |
| 255 | call->print_fmt = print_fmt; |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static void free_syscall_print_fmt(struct ftrace_event_call *call) |
| 261 | { |
| 262 | struct syscall_metadata *entry = call->data; |
| 263 | |
| 264 | if (entry->enter_event == call) |
| 265 | kfree(call->print_fmt); |
| 266 | } |
| 267 | |
Steven Rostedt | 2e33af0 | 2010-04-22 10:35:55 -0400 | [diff] [blame] | 268 | static int syscall_enter_define_fields(struct ftrace_event_call *call) |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 269 | { |
| 270 | struct syscall_trace_enter trace; |
Lai Jiangshan | 31c16b1 | 2009-12-01 16:23:30 +0800 | [diff] [blame] | 271 | struct syscall_metadata *meta = call->data; |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 272 | int ret; |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 273 | int i; |
| 274 | int offset = offsetof(typeof(trace), args); |
| 275 | |
Lai Jiangshan | 0f1ef51 | 2009-11-26 15:49:33 +0800 | [diff] [blame] | 276 | ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER); |
| 277 | if (ret) |
| 278 | return ret; |
| 279 | |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 280 | for (i = 0; i < meta->nb_args; i++) { |
Frederic Weisbecker | aeaeae1 | 2009-08-27 05:09:51 +0200 | [diff] [blame] | 281 | ret = trace_define_field(call, meta->types[i], |
| 282 | meta->args[i], offset, |
Li Zefan | 43b51ea | 2009-08-07 10:33:22 +0800 | [diff] [blame] | 283 | sizeof(unsigned long), 0, |
| 284 | FILTER_OTHER); |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 285 | offset += sizeof(unsigned long); |
| 286 | } |
| 287 | |
| 288 | return ret; |
| 289 | } |
| 290 | |
Steven Rostedt | 2e33af0 | 2010-04-22 10:35:55 -0400 | [diff] [blame] | 291 | static int syscall_exit_define_fields(struct ftrace_event_call *call) |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 292 | { |
| 293 | struct syscall_trace_exit trace; |
| 294 | int ret; |
| 295 | |
Lai Jiangshan | 0f1ef51 | 2009-11-26 15:49:33 +0800 | [diff] [blame] | 296 | ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER); |
| 297 | if (ret) |
| 298 | return ret; |
| 299 | |
Tom Zanussi | 26a5074 | 2009-10-06 01:09:50 -0500 | [diff] [blame] | 300 | ret = trace_define_field(call, SYSCALL_FIELD(long, ret), |
Li Zefan | 43b51ea | 2009-08-07 10:33:22 +0800 | [diff] [blame] | 301 | FILTER_OTHER); |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 302 | |
| 303 | return ret; |
| 304 | } |
| 305 | |
Fengguang Wu | 6aea49c | 2012-11-21 15:13:47 +0800 | [diff] [blame] | 306 | static void ftrace_syscall_enter(void *ignore, struct pt_regs *regs, long id) |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 307 | { |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 308 | struct syscall_trace_enter *entry; |
| 309 | struct syscall_metadata *sys_data; |
| 310 | struct ring_buffer_event *event; |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 311 | struct ring_buffer *buffer; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 312 | int syscall_nr; |
Steven Rostedt | f431b63 | 2013-02-12 16:18:59 -0500 | [diff] [blame] | 313 | int size; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 314 | |
Steven Rostedt | f431b63 | 2013-02-12 16:18:59 -0500 | [diff] [blame] | 315 | syscall_nr = trace_get_syscall_nr(current, regs); |
Hendrik Brueckner | cd0980f | 2009-08-25 14:50:27 +0200 | [diff] [blame] | 316 | if (syscall_nr < 0) |
| 317 | return; |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 318 | if (!test_bit(syscall_nr, enabled_enter_syscalls)) |
| 319 | return; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 320 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 321 | sys_data = syscall_nr_to_meta(syscall_nr); |
| 322 | if (!sys_data) |
| 323 | return; |
| 324 | |
| 325 | size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args; |
| 326 | |
Lai Jiangshan | fcc1943 | 2009-12-01 16:23:36 +0800 | [diff] [blame] | 327 | event = trace_current_buffer_lock_reserve(&buffer, |
Steven Rostedt | 32c0eda | 2010-04-23 10:38:03 -0400 | [diff] [blame] | 328 | sys_data->enter_event->event.type, size, 0, 0); |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 329 | if (!event) |
| 330 | return; |
| 331 | |
| 332 | entry = ring_buffer_event_data(event); |
| 333 | entry->nr = syscall_nr; |
| 334 | syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args); |
| 335 | |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 336 | if (!filter_current_check_discard(buffer, sys_data->enter_event, |
| 337 | entry, event)) |
| 338 | trace_current_buffer_unlock_commit(buffer, event, 0, 0); |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Fengguang Wu | 6aea49c | 2012-11-21 15:13:47 +0800 | [diff] [blame] | 341 | static void ftrace_syscall_exit(void *ignore, struct pt_regs *regs, long ret) |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 342 | { |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 343 | struct syscall_trace_exit *entry; |
| 344 | struct syscall_metadata *sys_data; |
| 345 | struct ring_buffer_event *event; |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 346 | struct ring_buffer *buffer; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 347 | int syscall_nr; |
| 348 | |
Steven Rostedt | f431b63 | 2013-02-12 16:18:59 -0500 | [diff] [blame] | 349 | syscall_nr = trace_get_syscall_nr(current, regs); |
Hendrik Brueckner | cd0980f | 2009-08-25 14:50:27 +0200 | [diff] [blame] | 350 | if (syscall_nr < 0) |
| 351 | return; |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 352 | if (!test_bit(syscall_nr, enabled_exit_syscalls)) |
| 353 | return; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 354 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 355 | sys_data = syscall_nr_to_meta(syscall_nr); |
| 356 | if (!sys_data) |
| 357 | return; |
| 358 | |
Lai Jiangshan | fcc1943 | 2009-12-01 16:23:36 +0800 | [diff] [blame] | 359 | event = trace_current_buffer_lock_reserve(&buffer, |
Steven Rostedt | 32c0eda | 2010-04-23 10:38:03 -0400 | [diff] [blame] | 360 | sys_data->exit_event->event.type, sizeof(*entry), 0, 0); |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 361 | if (!event) |
| 362 | return; |
| 363 | |
| 364 | entry = ring_buffer_event_data(event); |
| 365 | entry->nr = syscall_nr; |
| 366 | entry->ret = syscall_get_return_value(current, regs); |
| 367 | |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 368 | if (!filter_current_check_discard(buffer, sys_data->exit_event, |
| 369 | entry, event)) |
| 370 | trace_current_buffer_unlock_commit(buffer, event, 0, 0); |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Fengguang Wu | 6aea49c | 2012-11-21 15:13:47 +0800 | [diff] [blame] | 373 | static int reg_event_syscall_enter(struct ftrace_event_call *call) |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 374 | { |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 375 | int ret = 0; |
| 376 | int num; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 377 | |
Lai Jiangshan | c252f65 | 2009-12-01 16:23:47 +0800 | [diff] [blame] | 378 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Ian Munsie | 3773b389 | 2011-02-03 14:27:21 +1100 | [diff] [blame] | 379 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 380 | return -ENOSYS; |
| 381 | mutex_lock(&syscall_trace_lock); |
| 382 | if (!sys_refcount_enter) |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 383 | ret = register_trace_sys_enter(ftrace_syscall_enter, NULL); |
Li Zefan | 3b8e427 | 2009-12-08 11:14:52 +0800 | [diff] [blame] | 384 | if (!ret) { |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 385 | set_bit(num, enabled_enter_syscalls); |
| 386 | sys_refcount_enter++; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 387 | } |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 388 | mutex_unlock(&syscall_trace_lock); |
| 389 | return ret; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 390 | } |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 391 | |
Fengguang Wu | 6aea49c | 2012-11-21 15:13:47 +0800 | [diff] [blame] | 392 | static void unreg_event_syscall_enter(struct ftrace_event_call *call) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 393 | { |
| 394 | int num; |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 395 | |
Lai Jiangshan | c252f65 | 2009-12-01 16:23:47 +0800 | [diff] [blame] | 396 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Ian Munsie | 3773b389 | 2011-02-03 14:27:21 +1100 | [diff] [blame] | 397 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 398 | return; |
| 399 | mutex_lock(&syscall_trace_lock); |
| 400 | sys_refcount_enter--; |
| 401 | clear_bit(num, enabled_enter_syscalls); |
| 402 | if (!sys_refcount_enter) |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 403 | unregister_trace_sys_enter(ftrace_syscall_enter, NULL); |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 404 | mutex_unlock(&syscall_trace_lock); |
| 405 | } |
| 406 | |
Fengguang Wu | 6aea49c | 2012-11-21 15:13:47 +0800 | [diff] [blame] | 407 | static int reg_event_syscall_exit(struct ftrace_event_call *call) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 408 | { |
| 409 | int ret = 0; |
| 410 | int num; |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 411 | |
Lai Jiangshan | c252f65 | 2009-12-01 16:23:47 +0800 | [diff] [blame] | 412 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Ian Munsie | 3773b389 | 2011-02-03 14:27:21 +1100 | [diff] [blame] | 413 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 414 | return -ENOSYS; |
| 415 | mutex_lock(&syscall_trace_lock); |
| 416 | if (!sys_refcount_exit) |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 417 | ret = register_trace_sys_exit(ftrace_syscall_exit, NULL); |
Li Zefan | 3b8e427 | 2009-12-08 11:14:52 +0800 | [diff] [blame] | 418 | if (!ret) { |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 419 | set_bit(num, enabled_exit_syscalls); |
| 420 | sys_refcount_exit++; |
| 421 | } |
| 422 | mutex_unlock(&syscall_trace_lock); |
| 423 | return ret; |
| 424 | } |
| 425 | |
Fengguang Wu | 6aea49c | 2012-11-21 15:13:47 +0800 | [diff] [blame] | 426 | static void unreg_event_syscall_exit(struct ftrace_event_call *call) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 427 | { |
| 428 | int num; |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 429 | |
Lai Jiangshan | c252f65 | 2009-12-01 16:23:47 +0800 | [diff] [blame] | 430 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Ian Munsie | 3773b389 | 2011-02-03 14:27:21 +1100 | [diff] [blame] | 431 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 432 | return; |
| 433 | mutex_lock(&syscall_trace_lock); |
| 434 | sys_refcount_exit--; |
| 435 | clear_bit(num, enabled_exit_syscalls); |
| 436 | if (!sys_refcount_exit) |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 437 | unregister_trace_sys_exit(ftrace_syscall_exit, NULL); |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 438 | mutex_unlock(&syscall_trace_lock); |
| 439 | } |
| 440 | |
Vaibhav Nagarnaik | 6f86ab9 | 2012-06-07 16:46:25 -0700 | [diff] [blame] | 441 | static int init_syscall_trace(struct ftrace_event_call *call) |
Lai Jiangshan | a1301da | 2009-12-01 16:23:55 +0800 | [diff] [blame] | 442 | { |
| 443 | int id; |
Ian Munsie | ba97697 | 2011-02-03 14:27:20 +1100 | [diff] [blame] | 444 | int num; |
| 445 | |
| 446 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
| 447 | if (num < 0 || num >= NR_syscalls) { |
| 448 | pr_debug("syscall %s metadata not mapped, disabling ftrace event\n", |
| 449 | ((struct syscall_metadata *)call->data)->name); |
| 450 | return -ENOSYS; |
| 451 | } |
Lai Jiangshan | a1301da | 2009-12-01 16:23:55 +0800 | [diff] [blame] | 452 | |
Lai Jiangshan | 50307a4 | 2009-12-15 15:39:45 +0800 | [diff] [blame] | 453 | if (set_syscall_print_fmt(call) < 0) |
| 454 | return -ENOMEM; |
| 455 | |
Steven Rostedt | c7ef3a9 | 2009-12-28 21:13:59 -0500 | [diff] [blame] | 456 | id = trace_event_raw_init(call); |
| 457 | |
| 458 | if (id < 0) { |
Lai Jiangshan | 50307a4 | 2009-12-15 15:39:45 +0800 | [diff] [blame] | 459 | free_syscall_print_fmt(call); |
Steven Rostedt | c7ef3a9 | 2009-12-28 21:13:59 -0500 | [diff] [blame] | 460 | return id; |
Lai Jiangshan | 50307a4 | 2009-12-15 15:39:45 +0800 | [diff] [blame] | 461 | } |
Steven Rostedt | c7ef3a9 | 2009-12-28 21:13:59 -0500 | [diff] [blame] | 462 | |
| 463 | return id; |
Lai Jiangshan | a1301da | 2009-12-01 16:23:55 +0800 | [diff] [blame] | 464 | } |
| 465 | |
Vaibhav Nagarnaik | 6f86ab9 | 2012-06-07 16:46:25 -0700 | [diff] [blame] | 466 | struct trace_event_functions enter_syscall_print_funcs = { |
| 467 | .trace = print_syscall_enter, |
| 468 | }; |
| 469 | |
| 470 | struct trace_event_functions exit_syscall_print_funcs = { |
| 471 | .trace = print_syscall_exit, |
| 472 | }; |
| 473 | |
| 474 | struct ftrace_event_class event_class_syscall_enter = { |
| 475 | .system = "syscalls", |
| 476 | .reg = syscall_enter_register, |
| 477 | .define_fields = syscall_enter_define_fields, |
| 478 | .get_fields = syscall_get_enter_fields, |
| 479 | .raw_init = init_syscall_trace, |
| 480 | }; |
| 481 | |
| 482 | struct ftrace_event_class event_class_syscall_exit = { |
| 483 | .system = "syscalls", |
| 484 | .reg = syscall_exit_register, |
| 485 | .define_fields = syscall_exit_define_fields, |
| 486 | .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields), |
| 487 | .raw_init = init_syscall_trace, |
| 488 | }; |
| 489 | |
Ian Munsie | c763ba0 | 2011-02-03 14:27:22 +1100 | [diff] [blame] | 490 | unsigned long __init __weak arch_syscall_addr(int nr) |
Mike Frysinger | e7b8e67 | 2010-01-26 04:40:03 -0500 | [diff] [blame] | 491 | { |
| 492 | return (unsigned long)sys_call_table[nr]; |
| 493 | } |
| 494 | |
Fengguang Wu | 6aea49c | 2012-11-21 15:13:47 +0800 | [diff] [blame] | 495 | static int __init init_ftrace_syscalls(void) |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 496 | { |
| 497 | struct syscall_metadata *meta; |
| 498 | unsigned long addr; |
| 499 | int i; |
| 500 | |
Thomas Meyer | 47b0edc | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 501 | syscalls_metadata = kcalloc(NR_syscalls, sizeof(*syscalls_metadata), |
| 502 | GFP_KERNEL); |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 503 | if (!syscalls_metadata) { |
| 504 | WARN_ON(1); |
| 505 | return -ENOMEM; |
| 506 | } |
| 507 | |
| 508 | for (i = 0; i < NR_syscalls; i++) { |
| 509 | addr = arch_syscall_addr(i); |
| 510 | meta = find_syscall_meta(addr); |
Lai Jiangshan | c252f65 | 2009-12-01 16:23:47 +0800 | [diff] [blame] | 511 | if (!meta) |
| 512 | continue; |
| 513 | |
| 514 | meta->syscall_nr = i; |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 515 | syscalls_metadata[i] = meta; |
| 516 | } |
| 517 | |
| 518 | return 0; |
| 519 | } |
Ezequiel Garcia | 8781915 | 2012-09-12 11:47:57 -0300 | [diff] [blame] | 520 | early_initcall(init_ftrace_syscalls); |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 521 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 522 | #ifdef CONFIG_PERF_EVENTS |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 523 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 524 | static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls); |
| 525 | static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls); |
| 526 | static int sys_perf_refcount_enter; |
| 527 | static int sys_perf_refcount_exit; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 528 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 529 | static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 530 | { |
| 531 | struct syscall_metadata *sys_data; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 532 | struct syscall_trace_enter *rec; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 533 | struct hlist_head *head; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 534 | int syscall_nr; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 535 | int rctx; |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 536 | int size; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 537 | |
Steven Rostedt | f431b63 | 2013-02-12 16:18:59 -0500 | [diff] [blame] | 538 | syscall_nr = trace_get_syscall_nr(current, regs); |
Will Deacon | 60916a9 | 2012-08-16 18:14:14 +0100 | [diff] [blame] | 539 | if (syscall_nr < 0) |
| 540 | return; |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 541 | if (!test_bit(syscall_nr, enabled_perf_enter_syscalls)) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 542 | return; |
| 543 | |
| 544 | sys_data = syscall_nr_to_meta(syscall_nr); |
| 545 | if (!sys_data) |
| 546 | return; |
| 547 | |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 548 | /* get the size after alignment with the u32 buffer size field */ |
| 549 | size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec); |
| 550 | size = ALIGN(size + sizeof(u32), sizeof(u64)); |
| 551 | size -= sizeof(u32); |
| 552 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 553 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, |
| 554 | "perf buffer not large enough")) |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 555 | return; |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 556 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 557 | rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size, |
Steven Rostedt | ff5f149 | 2010-05-21 11:49:57 -0400 | [diff] [blame] | 558 | sys_data->enter_event->event.type, regs, &rctx); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 559 | if (!rec) |
| 560 | return; |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 561 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 562 | rec->nr = syscall_nr; |
| 563 | syscall_get_arguments(current, regs, 0, sys_data->nb_args, |
| 564 | (unsigned long *)&rec->args); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 565 | |
Peter Zijlstra | 3771f07 | 2010-05-21 12:31:09 +0200 | [diff] [blame] | 566 | head = this_cpu_ptr(sys_data->enter_event->perf_events); |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 567 | perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head, NULL); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 568 | } |
| 569 | |
Vaibhav Nagarnaik | 6f86ab9 | 2012-06-07 16:46:25 -0700 | [diff] [blame] | 570 | static int perf_sysenter_enable(struct ftrace_event_call *call) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 571 | { |
| 572 | int ret = 0; |
| 573 | int num; |
| 574 | |
Lai Jiangshan | 3bbe84e | 2009-12-01 16:24:01 +0800 | [diff] [blame] | 575 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 576 | |
| 577 | mutex_lock(&syscall_trace_lock); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 578 | if (!sys_perf_refcount_enter) |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 579 | ret = register_trace_sys_enter(perf_syscall_enter, NULL); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 580 | if (ret) { |
| 581 | pr_info("event trace: Could not activate" |
| 582 | "syscall entry trace point"); |
| 583 | } else { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 584 | set_bit(num, enabled_perf_enter_syscalls); |
| 585 | sys_perf_refcount_enter++; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 586 | } |
| 587 | mutex_unlock(&syscall_trace_lock); |
| 588 | return ret; |
| 589 | } |
| 590 | |
Vaibhav Nagarnaik | 6f86ab9 | 2012-06-07 16:46:25 -0700 | [diff] [blame] | 591 | static void perf_sysenter_disable(struct ftrace_event_call *call) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 592 | { |
| 593 | int num; |
| 594 | |
Lai Jiangshan | 3bbe84e | 2009-12-01 16:24:01 +0800 | [diff] [blame] | 595 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 596 | |
| 597 | mutex_lock(&syscall_trace_lock); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 598 | sys_perf_refcount_enter--; |
| 599 | clear_bit(num, enabled_perf_enter_syscalls); |
| 600 | if (!sys_perf_refcount_enter) |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 601 | unregister_trace_sys_enter(perf_syscall_enter, NULL); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 602 | mutex_unlock(&syscall_trace_lock); |
| 603 | } |
| 604 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 605 | static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 606 | { |
| 607 | struct syscall_metadata *sys_data; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 608 | struct syscall_trace_exit *rec; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 609 | struct hlist_head *head; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 610 | int syscall_nr; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 611 | int rctx; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 612 | int size; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 613 | |
Steven Rostedt | f431b63 | 2013-02-12 16:18:59 -0500 | [diff] [blame] | 614 | syscall_nr = trace_get_syscall_nr(current, regs); |
Will Deacon | 60916a9 | 2012-08-16 18:14:14 +0100 | [diff] [blame] | 615 | if (syscall_nr < 0) |
| 616 | return; |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 617 | if (!test_bit(syscall_nr, enabled_perf_exit_syscalls)) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 618 | return; |
| 619 | |
| 620 | sys_data = syscall_nr_to_meta(syscall_nr); |
| 621 | if (!sys_data) |
| 622 | return; |
| 623 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 624 | /* We can probably do that at build time */ |
| 625 | size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64)); |
| 626 | size -= sizeof(u32); |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 627 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 628 | /* |
| 629 | * Impossible, but be paranoid with the future |
| 630 | * How to put this check outside runtime? |
| 631 | */ |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 632 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, |
| 633 | "exit event has grown above perf buffer size")) |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 634 | return; |
| 635 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 636 | rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size, |
Steven Rostedt | ff5f149 | 2010-05-21 11:49:57 -0400 | [diff] [blame] | 637 | sys_data->exit_event->event.type, regs, &rctx); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 638 | if (!rec) |
| 639 | return; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 640 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 641 | rec->nr = syscall_nr; |
| 642 | rec->ret = syscall_get_return_value(current, regs); |
| 643 | |
Peter Zijlstra | 3771f07 | 2010-05-21 12:31:09 +0200 | [diff] [blame] | 644 | head = this_cpu_ptr(sys_data->exit_event->perf_events); |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 645 | perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head, NULL); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 646 | } |
| 647 | |
Vaibhav Nagarnaik | 6f86ab9 | 2012-06-07 16:46:25 -0700 | [diff] [blame] | 648 | static int perf_sysexit_enable(struct ftrace_event_call *call) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 649 | { |
| 650 | int ret = 0; |
| 651 | int num; |
| 652 | |
Lai Jiangshan | 3bbe84e | 2009-12-01 16:24:01 +0800 | [diff] [blame] | 653 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 654 | |
| 655 | mutex_lock(&syscall_trace_lock); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 656 | if (!sys_perf_refcount_exit) |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 657 | ret = register_trace_sys_exit(perf_syscall_exit, NULL); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 658 | if (ret) { |
| 659 | pr_info("event trace: Could not activate" |
Wenji Huang | 6574658 | 2010-02-24 15:40:22 +0800 | [diff] [blame] | 660 | "syscall exit trace point"); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 661 | } else { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 662 | set_bit(num, enabled_perf_exit_syscalls); |
| 663 | sys_perf_refcount_exit++; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 664 | } |
| 665 | mutex_unlock(&syscall_trace_lock); |
| 666 | return ret; |
| 667 | } |
| 668 | |
Vaibhav Nagarnaik | 6f86ab9 | 2012-06-07 16:46:25 -0700 | [diff] [blame] | 669 | static void perf_sysexit_disable(struct ftrace_event_call *call) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 670 | { |
| 671 | int num; |
| 672 | |
Lai Jiangshan | 3bbe84e | 2009-12-01 16:24:01 +0800 | [diff] [blame] | 673 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 674 | |
| 675 | mutex_lock(&syscall_trace_lock); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 676 | sys_perf_refcount_exit--; |
| 677 | clear_bit(num, enabled_perf_exit_syscalls); |
| 678 | if (!sys_perf_refcount_exit) |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 679 | unregister_trace_sys_exit(perf_syscall_exit, NULL); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 680 | mutex_unlock(&syscall_trace_lock); |
| 681 | } |
| 682 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 683 | #endif /* CONFIG_PERF_EVENTS */ |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 684 | |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 685 | static int syscall_enter_register(struct ftrace_event_call *event, |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 686 | enum trace_reg type, void *data) |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 687 | { |
| 688 | switch (type) { |
| 689 | case TRACE_REG_REGISTER: |
| 690 | return reg_event_syscall_enter(event); |
| 691 | case TRACE_REG_UNREGISTER: |
| 692 | unreg_event_syscall_enter(event); |
| 693 | return 0; |
| 694 | |
| 695 | #ifdef CONFIG_PERF_EVENTS |
| 696 | case TRACE_REG_PERF_REGISTER: |
| 697 | return perf_sysenter_enable(event); |
| 698 | case TRACE_REG_PERF_UNREGISTER: |
| 699 | perf_sysenter_disable(event); |
| 700 | return 0; |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 701 | case TRACE_REG_PERF_OPEN: |
| 702 | case TRACE_REG_PERF_CLOSE: |
Jiri Olsa | 489c75c | 2012-02-15 15:51:50 +0100 | [diff] [blame] | 703 | case TRACE_REG_PERF_ADD: |
| 704 | case TRACE_REG_PERF_DEL: |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 705 | return 0; |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 706 | #endif |
| 707 | } |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | static int syscall_exit_register(struct ftrace_event_call *event, |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 712 | enum trace_reg type, void *data) |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 713 | { |
| 714 | switch (type) { |
| 715 | case TRACE_REG_REGISTER: |
| 716 | return reg_event_syscall_exit(event); |
| 717 | case TRACE_REG_UNREGISTER: |
| 718 | unreg_event_syscall_exit(event); |
| 719 | return 0; |
| 720 | |
| 721 | #ifdef CONFIG_PERF_EVENTS |
| 722 | case TRACE_REG_PERF_REGISTER: |
| 723 | return perf_sysexit_enable(event); |
| 724 | case TRACE_REG_PERF_UNREGISTER: |
| 725 | perf_sysexit_disable(event); |
| 726 | return 0; |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 727 | case TRACE_REG_PERF_OPEN: |
| 728 | case TRACE_REG_PERF_CLOSE: |
Jiri Olsa | 489c75c | 2012-02-15 15:51:50 +0100 | [diff] [blame] | 729 | case TRACE_REG_PERF_ADD: |
| 730 | case TRACE_REG_PERF_DEL: |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 731 | return 0; |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 732 | #endif |
| 733 | } |
| 734 | return 0; |
| 735 | } |