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> |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 3 | #include <linux/kernel.h> |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 4 | #include <linux/ftrace.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 5 | #include <linux/perf_event.h> |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 6 | #include <asm/syscall.h> |
| 7 | |
| 8 | #include "trace_output.h" |
| 9 | #include "trace.h" |
| 10 | |
Frederic Weisbecker | 5be71b6 | 2009-03-15 22:10:37 +0100 | [diff] [blame] | 11 | static DEFINE_MUTEX(syscall_trace_lock); |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 12 | static int sys_refcount_enter; |
| 13 | static int sys_refcount_exit; |
Jason Baron | 57421db | 2009-08-24 17:40:22 -0400 | [diff] [blame] | 14 | static DECLARE_BITMAP(enabled_enter_syscalls, NR_syscalls); |
| 15 | static DECLARE_BITMAP(enabled_exit_syscalls, NR_syscalls); |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 16 | |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 17 | extern unsigned long __start_syscalls_metadata[]; |
| 18 | extern unsigned long __stop_syscalls_metadata[]; |
| 19 | |
| 20 | static struct syscall_metadata **syscalls_metadata; |
| 21 | |
| 22 | static 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 | |
| 46 | static 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 Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 54 | enum print_line_t |
| 55 | print_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 Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 63 | trace = (typeof(trace))ent; |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 64 | syscall = trace->nr; |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 65 | entry = syscall_nr_to_meta(syscall); |
Jason Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 66 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 67 | if (!entry) |
| 68 | goto end; |
| 69 | |
Lai Jiangshan | fcc1943 | 2009-12-01 16:23:36 +0800 | [diff] [blame] | 70 | if (entry->enter_event->id != ent->type) { |
Jason Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 71 | WARN_ON_ONCE(1); |
| 72 | goto end; |
| 73 | } |
| 74 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 75 | 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 Zefan | ba8b3a4 | 2009-08-17 16:55:18 +0800 | [diff] [blame] | 81 | if (trace_flags & TRACE_ITER_VERBOSE) { |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 82 | ret = trace_seq_printf(s, "%s ", entry->types[i]); |
| 83 | if (!ret) |
| 84 | return TRACE_TYPE_PARTIAL_LINE; |
| 85 | } |
| 86 | /* parameter values */ |
Li Zefan | 4539f07 | 2009-08-20 16:13:35 +0800 | [diff] [blame] | 87 | ret = trace_seq_printf(s, "%s: %lx%s", entry->args[i], |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 88 | trace->args[i], |
Li Zefan | 4539f07 | 2009-08-20 16:13:35 +0800 | [diff] [blame] | 89 | i == entry->nb_args - 1 ? "" : ", "); |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 90 | if (!ret) |
| 91 | return TRACE_TYPE_PARTIAL_LINE; |
| 92 | } |
| 93 | |
Li Zefan | 4539f07 | 2009-08-20 16:13:35 +0800 | [diff] [blame] | 94 | ret = trace_seq_putc(s, ')'); |
| 95 | if (!ret) |
| 96 | return TRACE_TYPE_PARTIAL_LINE; |
| 97 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 98 | end: |
Li Zefan | 4539f07 | 2009-08-20 16:13:35 +0800 | [diff] [blame] | 99 | ret = trace_seq_putc(s, '\n'); |
| 100 | if (!ret) |
| 101 | return TRACE_TYPE_PARTIAL_LINE; |
| 102 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 103 | return TRACE_TYPE_HANDLED; |
| 104 | } |
| 105 | |
| 106 | enum print_line_t |
| 107 | print_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 Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 116 | trace = (typeof(trace))ent; |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 117 | syscall = trace->nr; |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 118 | entry = syscall_nr_to_meta(syscall); |
Jason Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 119 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 120 | if (!entry) { |
| 121 | trace_seq_printf(s, "\n"); |
| 122 | return TRACE_TYPE_HANDLED; |
| 123 | } |
| 124 | |
Lai Jiangshan | fcc1943 | 2009-12-01 16:23:36 +0800 | [diff] [blame] | 125 | if (entry->exit_event->id != ent->type) { |
Jason Baron | 64c12e0 | 2009-08-10 16:52:53 -0400 | [diff] [blame] | 126 | WARN_ON_ONCE(1); |
| 127 | return TRACE_TYPE_UNHANDLED; |
| 128 | } |
| 129 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 130 | 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 Zefan | e697196 | 2009-08-19 15:52:25 +0800 | [diff] [blame] | 138 | extern char *__bad_type_size(void); |
| 139 | |
| 140 | #define SYSCALL_FIELD(type, name) \ |
| 141 | sizeof(type) != sizeof(trace.name) ? \ |
| 142 | __bad_type_size() : \ |
Tom Zanussi | 26a5074 | 2009-10-06 01:09:50 -0500 | [diff] [blame] | 143 | #type, #name, offsetof(typeof(trace), name), \ |
| 144 | sizeof(trace.name), is_signed_type(type) |
Li Zefan | e697196 | 2009-08-19 15:52:25 +0800 | [diff] [blame] | 145 | |
Lai Jiangshan | 50307a4 | 2009-12-15 15:39:45 +0800 | [diff] [blame] | 146 | static |
| 147 | int __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len) |
| 148 | { |
| 149 | int i; |
| 150 | int pos = 0; |
| 151 | |
| 152 | /* When len=0, we just calculate the needed length */ |
| 153 | #define LEN_OR_ZERO (len ? len - pos : 0) |
| 154 | |
| 155 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); |
| 156 | for (i = 0; i < entry->nb_args; i++) { |
| 157 | pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s", |
| 158 | entry->args[i], sizeof(unsigned long), |
| 159 | i == entry->nb_args - 1 ? "" : ", "); |
| 160 | } |
| 161 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); |
| 162 | |
| 163 | for (i = 0; i < entry->nb_args; i++) { |
| 164 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
| 165 | ", ((unsigned long)(REC->%s))", entry->args[i]); |
| 166 | } |
| 167 | |
| 168 | #undef LEN_OR_ZERO |
| 169 | |
| 170 | /* return the length of print_fmt */ |
| 171 | return pos; |
| 172 | } |
| 173 | |
| 174 | static int set_syscall_print_fmt(struct ftrace_event_call *call) |
| 175 | { |
| 176 | char *print_fmt; |
| 177 | int len; |
| 178 | struct syscall_metadata *entry = call->data; |
| 179 | |
| 180 | if (entry->enter_event != call) { |
| 181 | call->print_fmt = "\"0x%lx\", REC->ret"; |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /* First: called with 0 length to calculate the needed length */ |
| 186 | len = __set_enter_print_fmt(entry, NULL, 0); |
| 187 | |
| 188 | print_fmt = kmalloc(len + 1, GFP_KERNEL); |
| 189 | if (!print_fmt) |
| 190 | return -ENOMEM; |
| 191 | |
| 192 | /* Second: actually write the @print_fmt */ |
| 193 | __set_enter_print_fmt(entry, print_fmt, len + 1); |
| 194 | call->print_fmt = print_fmt; |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static void free_syscall_print_fmt(struct ftrace_event_call *call) |
| 200 | { |
| 201 | struct syscall_metadata *entry = call->data; |
| 202 | |
| 203 | if (entry->enter_event == call) |
| 204 | kfree(call->print_fmt); |
| 205 | } |
| 206 | |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 207 | int syscall_enter_define_fields(struct ftrace_event_call *call) |
| 208 | { |
| 209 | struct syscall_trace_enter trace; |
Lai Jiangshan | 31c16b1 | 2009-12-01 16:23:30 +0800 | [diff] [blame] | 210 | struct syscall_metadata *meta = call->data; |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 211 | int ret; |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 212 | int i; |
| 213 | int offset = offsetof(typeof(trace), args); |
| 214 | |
Lai Jiangshan | 0f1ef51 | 2009-11-26 15:49:33 +0800 | [diff] [blame] | 215 | ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER); |
| 216 | if (ret) |
| 217 | return ret; |
| 218 | |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 219 | for (i = 0; i < meta->nb_args; i++) { |
Frederic Weisbecker | aeaeae1 | 2009-08-27 05:09:51 +0200 | [diff] [blame] | 220 | ret = trace_define_field(call, meta->types[i], |
| 221 | meta->args[i], offset, |
Li Zefan | 43b51ea | 2009-08-07 10:33:22 +0800 | [diff] [blame] | 222 | sizeof(unsigned long), 0, |
| 223 | FILTER_OTHER); |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 224 | offset += sizeof(unsigned long); |
| 225 | } |
| 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | int syscall_exit_define_fields(struct ftrace_event_call *call) |
| 231 | { |
| 232 | struct syscall_trace_exit trace; |
| 233 | int ret; |
| 234 | |
Lai Jiangshan | 0f1ef51 | 2009-11-26 15:49:33 +0800 | [diff] [blame] | 235 | ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER); |
| 236 | if (ret) |
| 237 | return ret; |
| 238 | |
Tom Zanussi | 26a5074 | 2009-10-06 01:09:50 -0500 | [diff] [blame] | 239 | ret = trace_define_field(call, SYSCALL_FIELD(long, ret), |
Li Zefan | 43b51ea | 2009-08-07 10:33:22 +0800 | [diff] [blame] | 240 | FILTER_OTHER); |
Li Zefan | 540b7b8 | 2009-08-19 15:54:51 +0800 | [diff] [blame] | 241 | |
| 242 | return ret; |
| 243 | } |
| 244 | |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 245 | void ftrace_syscall_enter(struct pt_regs *regs, long id) |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 246 | { |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 247 | struct syscall_trace_enter *entry; |
| 248 | struct syscall_metadata *sys_data; |
| 249 | struct ring_buffer_event *event; |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 250 | struct ring_buffer *buffer; |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 251 | int size; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 252 | int syscall_nr; |
| 253 | |
| 254 | syscall_nr = syscall_get_nr(current, regs); |
Hendrik Brueckner | cd0980f | 2009-08-25 14:50:27 +0200 | [diff] [blame] | 255 | if (syscall_nr < 0) |
| 256 | return; |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 257 | if (!test_bit(syscall_nr, enabled_enter_syscalls)) |
| 258 | return; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 259 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 260 | sys_data = syscall_nr_to_meta(syscall_nr); |
| 261 | if (!sys_data) |
| 262 | return; |
| 263 | |
| 264 | size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args; |
| 265 | |
Lai Jiangshan | fcc1943 | 2009-12-01 16:23:36 +0800 | [diff] [blame] | 266 | event = trace_current_buffer_lock_reserve(&buffer, |
| 267 | sys_data->enter_event->id, size, 0, 0); |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 268 | if (!event) |
| 269 | return; |
| 270 | |
| 271 | entry = ring_buffer_event_data(event); |
| 272 | entry->nr = syscall_nr; |
| 273 | syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args); |
| 274 | |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 275 | if (!filter_current_check_discard(buffer, sys_data->enter_event, |
| 276 | entry, event)) |
| 277 | trace_current_buffer_unlock_commit(buffer, event, 0, 0); |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 280 | void ftrace_syscall_exit(struct pt_regs *regs, long ret) |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 281 | { |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 282 | struct syscall_trace_exit *entry; |
| 283 | struct syscall_metadata *sys_data; |
| 284 | struct ring_buffer_event *event; |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 285 | struct ring_buffer *buffer; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 286 | int syscall_nr; |
| 287 | |
| 288 | syscall_nr = syscall_get_nr(current, regs); |
Hendrik Brueckner | cd0980f | 2009-08-25 14:50:27 +0200 | [diff] [blame] | 289 | if (syscall_nr < 0) |
| 290 | return; |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 291 | if (!test_bit(syscall_nr, enabled_exit_syscalls)) |
| 292 | return; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 293 | |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 294 | sys_data = syscall_nr_to_meta(syscall_nr); |
| 295 | if (!sys_data) |
| 296 | return; |
| 297 | |
Lai Jiangshan | fcc1943 | 2009-12-01 16:23:36 +0800 | [diff] [blame] | 298 | event = trace_current_buffer_lock_reserve(&buffer, |
| 299 | sys_data->exit_event->id, sizeof(*entry), 0, 0); |
Frederic Weisbecker | bed1ffc | 2009-03-13 15:42:11 +0100 | [diff] [blame] | 300 | if (!event) |
| 301 | return; |
| 302 | |
| 303 | entry = ring_buffer_event_data(event); |
| 304 | entry->nr = syscall_nr; |
| 305 | entry->ret = syscall_get_return_value(current, regs); |
| 306 | |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 307 | if (!filter_current_check_discard(buffer, sys_data->exit_event, |
| 308 | entry, event)) |
| 309 | trace_current_buffer_unlock_commit(buffer, event, 0, 0); |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Masami Hiramatsu | bd1a5c8 | 2009-08-13 16:34:53 -0400 | [diff] [blame] | 312 | int reg_event_syscall_enter(struct ftrace_event_call *call) |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 313 | { |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 314 | int ret = 0; |
| 315 | int num; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 316 | |
Lai Jiangshan | c252f65 | 2009-12-01 16:23:47 +0800 | [diff] [blame] | 317 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | 57421db | 2009-08-24 17:40:22 -0400 | [diff] [blame] | 318 | if (num < 0 || num >= NR_syscalls) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 319 | return -ENOSYS; |
| 320 | mutex_lock(&syscall_trace_lock); |
| 321 | if (!sys_refcount_enter) |
Josh Stone | 1c569f0 | 2009-08-24 14:43:14 -0700 | [diff] [blame] | 322 | ret = register_trace_sys_enter(ftrace_syscall_enter); |
Li Zefan | 3b8e427 | 2009-12-08 11:14:52 +0800 | [diff] [blame] | 323 | if (!ret) { |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 324 | set_bit(num, enabled_enter_syscalls); |
| 325 | sys_refcount_enter++; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 326 | } |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 327 | mutex_unlock(&syscall_trace_lock); |
| 328 | return ret; |
Frederic Weisbecker | ee08c6e | 2009-03-07 05:52:59 +0100 | [diff] [blame] | 329 | } |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 330 | |
Masami Hiramatsu | bd1a5c8 | 2009-08-13 16:34:53 -0400 | [diff] [blame] | 331 | void unreg_event_syscall_enter(struct ftrace_event_call *call) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 332 | { |
| 333 | int num; |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 334 | |
Lai Jiangshan | c252f65 | 2009-12-01 16:23:47 +0800 | [diff] [blame] | 335 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | 57421db | 2009-08-24 17:40:22 -0400 | [diff] [blame] | 336 | if (num < 0 || num >= NR_syscalls) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 337 | return; |
| 338 | mutex_lock(&syscall_trace_lock); |
| 339 | sys_refcount_enter--; |
| 340 | clear_bit(num, enabled_enter_syscalls); |
| 341 | if (!sys_refcount_enter) |
Josh Stone | 1c569f0 | 2009-08-24 14:43:14 -0700 | [diff] [blame] | 342 | unregister_trace_sys_enter(ftrace_syscall_enter); |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 343 | mutex_unlock(&syscall_trace_lock); |
| 344 | } |
| 345 | |
Masami Hiramatsu | bd1a5c8 | 2009-08-13 16:34:53 -0400 | [diff] [blame] | 346 | int reg_event_syscall_exit(struct ftrace_event_call *call) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 347 | { |
| 348 | int ret = 0; |
| 349 | int num; |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 350 | |
Lai Jiangshan | c252f65 | 2009-12-01 16:23:47 +0800 | [diff] [blame] | 351 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | 57421db | 2009-08-24 17:40:22 -0400 | [diff] [blame] | 352 | if (num < 0 || num >= NR_syscalls) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 353 | return -ENOSYS; |
| 354 | mutex_lock(&syscall_trace_lock); |
| 355 | if (!sys_refcount_exit) |
Josh Stone | 1c569f0 | 2009-08-24 14:43:14 -0700 | [diff] [blame] | 356 | ret = register_trace_sys_exit(ftrace_syscall_exit); |
Li Zefan | 3b8e427 | 2009-12-08 11:14:52 +0800 | [diff] [blame] | 357 | if (!ret) { |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 358 | set_bit(num, enabled_exit_syscalls); |
| 359 | sys_refcount_exit++; |
| 360 | } |
| 361 | mutex_unlock(&syscall_trace_lock); |
| 362 | return ret; |
| 363 | } |
| 364 | |
Masami Hiramatsu | bd1a5c8 | 2009-08-13 16:34:53 -0400 | [diff] [blame] | 365 | void unreg_event_syscall_exit(struct ftrace_event_call *call) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 366 | { |
| 367 | int num; |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 368 | |
Lai Jiangshan | c252f65 | 2009-12-01 16:23:47 +0800 | [diff] [blame] | 369 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | 57421db | 2009-08-24 17:40:22 -0400 | [diff] [blame] | 370 | if (num < 0 || num >= NR_syscalls) |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 371 | return; |
| 372 | mutex_lock(&syscall_trace_lock); |
| 373 | sys_refcount_exit--; |
| 374 | clear_bit(num, enabled_exit_syscalls); |
| 375 | if (!sys_refcount_exit) |
Josh Stone | 1c569f0 | 2009-08-24 14:43:14 -0700 | [diff] [blame] | 376 | unregister_trace_sys_exit(ftrace_syscall_exit); |
Jason Baron | fb34a08 | 2009-08-10 16:52:47 -0400 | [diff] [blame] | 377 | mutex_unlock(&syscall_trace_lock); |
| 378 | } |
| 379 | |
Lai Jiangshan | a1301da | 2009-12-01 16:23:55 +0800 | [diff] [blame] | 380 | int init_syscall_trace(struct ftrace_event_call *call) |
| 381 | { |
| 382 | int id; |
| 383 | |
Lai Jiangshan | 50307a4 | 2009-12-15 15:39:45 +0800 | [diff] [blame] | 384 | if (set_syscall_print_fmt(call) < 0) |
| 385 | return -ENOMEM; |
| 386 | |
Steven Rostedt | c7ef3a9 | 2009-12-28 21:13:59 -0500 | [diff] [blame] | 387 | id = trace_event_raw_init(call); |
| 388 | |
| 389 | if (id < 0) { |
Lai Jiangshan | 50307a4 | 2009-12-15 15:39:45 +0800 | [diff] [blame] | 390 | free_syscall_print_fmt(call); |
Steven Rostedt | c7ef3a9 | 2009-12-28 21:13:59 -0500 | [diff] [blame] | 391 | return id; |
Lai Jiangshan | 50307a4 | 2009-12-15 15:39:45 +0800 | [diff] [blame] | 392 | } |
Steven Rostedt | c7ef3a9 | 2009-12-28 21:13:59 -0500 | [diff] [blame] | 393 | |
| 394 | return id; |
Lai Jiangshan | a1301da | 2009-12-01 16:23:55 +0800 | [diff] [blame] | 395 | } |
| 396 | |
Mike Frysinger | e7b8e67 | 2010-01-26 04:40:03 -0500 | [diff] [blame] | 397 | unsigned long __init arch_syscall_addr(int nr) |
| 398 | { |
| 399 | return (unsigned long)sys_call_table[nr]; |
| 400 | } |
| 401 | |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 402 | int __init init_ftrace_syscalls(void) |
| 403 | { |
| 404 | struct syscall_metadata *meta; |
| 405 | unsigned long addr; |
| 406 | int i; |
| 407 | |
| 408 | syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) * |
| 409 | NR_syscalls, GFP_KERNEL); |
| 410 | if (!syscalls_metadata) { |
| 411 | WARN_ON(1); |
| 412 | return -ENOMEM; |
| 413 | } |
| 414 | |
| 415 | for (i = 0; i < NR_syscalls; i++) { |
| 416 | addr = arch_syscall_addr(i); |
| 417 | meta = find_syscall_meta(addr); |
Lai Jiangshan | c252f65 | 2009-12-01 16:23:47 +0800 | [diff] [blame] | 418 | if (!meta) |
| 419 | continue; |
| 420 | |
| 421 | meta->syscall_nr = i; |
Frederic Weisbecker | c44fc77 | 2009-09-19 06:50:42 +0200 | [diff] [blame] | 422 | syscalls_metadata[i] = meta; |
| 423 | } |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | core_initcall(init_ftrace_syscalls); |
| 428 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 429 | #ifdef CONFIG_PERF_EVENTS |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 430 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 431 | static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls); |
| 432 | static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls); |
| 433 | static int sys_perf_refcount_enter; |
| 434 | static int sys_perf_refcount_exit; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 435 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 436 | static void perf_syscall_enter(struct pt_regs *regs, long id) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 437 | { |
| 438 | struct syscall_metadata *sys_data; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 439 | struct syscall_trace_enter *rec; |
| 440 | unsigned long flags; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 441 | int syscall_nr; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 442 | int rctx; |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 443 | int size; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 444 | |
| 445 | syscall_nr = syscall_get_nr(current, regs); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 446 | if (!test_bit(syscall_nr, enabled_perf_enter_syscalls)) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 447 | return; |
| 448 | |
| 449 | sys_data = syscall_nr_to_meta(syscall_nr); |
| 450 | if (!sys_data) |
| 451 | return; |
| 452 | |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 453 | /* get the size after alignment with the u32 buffer size field */ |
| 454 | size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec); |
| 455 | size = ALIGN(size + sizeof(u32), sizeof(u64)); |
| 456 | size -= sizeof(u32); |
| 457 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 458 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, |
| 459 | "perf buffer not large enough")) |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 460 | return; |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 461 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 462 | rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size, |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 463 | sys_data->enter_event->id, &rctx, &flags); |
| 464 | if (!rec) |
| 465 | return; |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 466 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 467 | rec->nr = syscall_nr; |
| 468 | syscall_get_arguments(current, regs, 0, sys_data->nb_args, |
| 469 | (unsigned long *)&rec->args); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 470 | perf_trace_buf_submit(rec, size, rctx, 0, 1, flags, regs); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 471 | } |
| 472 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 473 | int perf_sysenter_enable(struct ftrace_event_call *call) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 474 | { |
| 475 | int ret = 0; |
| 476 | int num; |
| 477 | |
Lai Jiangshan | 3bbe84e | 2009-12-01 16:24:01 +0800 | [diff] [blame] | 478 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 479 | |
| 480 | mutex_lock(&syscall_trace_lock); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 481 | if (!sys_perf_refcount_enter) |
| 482 | ret = register_trace_sys_enter(perf_syscall_enter); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 483 | if (ret) { |
| 484 | pr_info("event trace: Could not activate" |
| 485 | "syscall entry trace point"); |
| 486 | } else { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 487 | set_bit(num, enabled_perf_enter_syscalls); |
| 488 | sys_perf_refcount_enter++; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 489 | } |
| 490 | mutex_unlock(&syscall_trace_lock); |
| 491 | return ret; |
| 492 | } |
| 493 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 494 | void perf_sysenter_disable(struct ftrace_event_call *call) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 495 | { |
| 496 | int num; |
| 497 | |
Lai Jiangshan | 3bbe84e | 2009-12-01 16:24:01 +0800 | [diff] [blame] | 498 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 499 | |
| 500 | mutex_lock(&syscall_trace_lock); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 501 | sys_perf_refcount_enter--; |
| 502 | clear_bit(num, enabled_perf_enter_syscalls); |
| 503 | if (!sys_perf_refcount_enter) |
| 504 | unregister_trace_sys_enter(perf_syscall_enter); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 505 | mutex_unlock(&syscall_trace_lock); |
| 506 | } |
| 507 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 508 | static void perf_syscall_exit(struct pt_regs *regs, long ret) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 509 | { |
| 510 | struct syscall_metadata *sys_data; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 511 | struct syscall_trace_exit *rec; |
| 512 | unsigned long flags; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 513 | int syscall_nr; |
Peter Zijlstra | 4ed7c92 | 2009-11-23 11:37:29 +0100 | [diff] [blame] | 514 | int rctx; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 515 | int size; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 516 | |
| 517 | syscall_nr = syscall_get_nr(current, regs); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 518 | if (!test_bit(syscall_nr, enabled_perf_exit_syscalls)) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 519 | return; |
| 520 | |
| 521 | sys_data = syscall_nr_to_meta(syscall_nr); |
| 522 | if (!sys_data) |
| 523 | return; |
| 524 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 525 | /* We can probably do that at build time */ |
| 526 | size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64)); |
| 527 | size -= sizeof(u32); |
Frederic Weisbecker | 19007a6 | 2009-08-11 20:22:53 +0200 | [diff] [blame] | 528 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 529 | /* |
| 530 | * Impossible, but be paranoid with the future |
| 531 | * How to put this check outside runtime? |
| 532 | */ |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 533 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, |
| 534 | "exit event has grown above perf buffer size")) |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 535 | return; |
| 536 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 537 | rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size, |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 538 | sys_data->exit_event->id, &rctx, &flags); |
| 539 | if (!rec) |
| 540 | return; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 541 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 542 | rec->nr = syscall_nr; |
| 543 | rec->ret = syscall_get_return_value(current, regs); |
| 544 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 545 | perf_trace_buf_submit(rec, size, rctx, 0, 1, flags, regs); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 546 | } |
| 547 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 548 | int perf_sysexit_enable(struct ftrace_event_call *call) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 549 | { |
| 550 | int ret = 0; |
| 551 | int num; |
| 552 | |
Lai Jiangshan | 3bbe84e | 2009-12-01 16:24:01 +0800 | [diff] [blame] | 553 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 554 | |
| 555 | mutex_lock(&syscall_trace_lock); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 556 | if (!sys_perf_refcount_exit) |
| 557 | ret = register_trace_sys_exit(perf_syscall_exit); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 558 | if (ret) { |
| 559 | pr_info("event trace: Could not activate" |
Wenji Huang | 6574658 | 2010-02-24 15:40:22 +0800 | [diff] [blame] | 560 | "syscall exit trace point"); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 561 | } else { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 562 | set_bit(num, enabled_perf_exit_syscalls); |
| 563 | sys_perf_refcount_exit++; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 564 | } |
| 565 | mutex_unlock(&syscall_trace_lock); |
| 566 | return ret; |
| 567 | } |
| 568 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 569 | void perf_sysexit_disable(struct ftrace_event_call *call) |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 570 | { |
| 571 | int num; |
| 572 | |
Lai Jiangshan | 3bbe84e | 2009-12-01 16:24:01 +0800 | [diff] [blame] | 573 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 574 | |
| 575 | mutex_lock(&syscall_trace_lock); |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame^] | 576 | sys_perf_refcount_exit--; |
| 577 | clear_bit(num, enabled_perf_exit_syscalls); |
| 578 | if (!sys_perf_refcount_exit) |
| 579 | unregister_trace_sys_exit(perf_syscall_exit); |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 580 | mutex_unlock(&syscall_trace_lock); |
| 581 | } |
| 582 | |
Li Zefan | 07b139c | 2009-12-21 14:27:35 +0800 | [diff] [blame] | 583 | #endif /* CONFIG_PERF_EVENTS */ |
Jason Baron | f4b5ffc | 2009-08-10 16:53:02 -0400 | [diff] [blame] | 584 | |