Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 1 | /* |
| 2 | * event tracer |
| 3 | * |
| 4 | * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/debugfs.h> |
| 9 | #include <linux/uaccess.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/ctype.h> |
| 12 | |
| 13 | #include "trace_events.h" |
| 14 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame^] | 15 | #define events_for_each(event) \ |
| 16 | for (event = __start_ftrace_events; \ |
| 17 | (unsigned long)event < (unsigned long)__stop_ftrace_events; \ |
| 18 | event++) |
| 19 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 20 | void event_trace_printk(unsigned long ip, const char *fmt, ...) |
| 21 | { |
| 22 | va_list ap; |
| 23 | |
| 24 | va_start(ap, fmt); |
| 25 | tracing_record_cmdline(current); |
| 26 | trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap); |
| 27 | va_end(ap); |
| 28 | } |
| 29 | |
| 30 | static void ftrace_clear_events(void) |
| 31 | { |
| 32 | struct ftrace_event_call *call = (void *)__start_ftrace_events; |
| 33 | |
| 34 | |
| 35 | while ((unsigned long)call < (unsigned long)__stop_ftrace_events) { |
| 36 | |
| 37 | if (call->enabled) { |
| 38 | call->enabled = 0; |
| 39 | call->unregfunc(); |
| 40 | } |
| 41 | call++; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | static int ftrace_set_clr_event(char *buf, int set) |
| 46 | { |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame^] | 47 | struct ftrace_event_call *call = __start_ftrace_events; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 48 | |
| 49 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame^] | 50 | events_for_each(call) { |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 51 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame^] | 52 | if (!call->name) |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 53 | continue; |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame^] | 54 | |
| 55 | if (strcmp(buf, call->name) != 0) |
| 56 | continue; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 57 | |
| 58 | if (set) { |
| 59 | /* Already set? */ |
| 60 | if (call->enabled) |
| 61 | return 0; |
| 62 | call->enabled = 1; |
| 63 | call->regfunc(); |
| 64 | } else { |
| 65 | /* Already cleared? */ |
| 66 | if (!call->enabled) |
| 67 | return 0; |
| 68 | call->enabled = 0; |
| 69 | call->unregfunc(); |
| 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | return -EINVAL; |
| 74 | } |
| 75 | |
| 76 | /* 128 should be much more than enough */ |
| 77 | #define EVENT_BUF_SIZE 127 |
| 78 | |
| 79 | static ssize_t |
| 80 | ftrace_event_write(struct file *file, const char __user *ubuf, |
| 81 | size_t cnt, loff_t *ppos) |
| 82 | { |
| 83 | size_t read = 0; |
| 84 | int i, set = 1; |
| 85 | ssize_t ret; |
| 86 | char *buf; |
| 87 | char ch; |
| 88 | |
| 89 | if (!cnt || cnt < 0) |
| 90 | return 0; |
| 91 | |
| 92 | ret = get_user(ch, ubuf++); |
| 93 | if (ret) |
| 94 | return ret; |
| 95 | read++; |
| 96 | cnt--; |
| 97 | |
| 98 | /* skip white space */ |
| 99 | while (cnt && isspace(ch)) { |
| 100 | ret = get_user(ch, ubuf++); |
| 101 | if (ret) |
| 102 | return ret; |
| 103 | read++; |
| 104 | cnt--; |
| 105 | } |
| 106 | |
| 107 | /* Only white space found? */ |
| 108 | if (isspace(ch)) { |
| 109 | file->f_pos += read; |
| 110 | ret = read; |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL); |
| 115 | if (!buf) |
| 116 | return -ENOMEM; |
| 117 | |
| 118 | if (cnt > EVENT_BUF_SIZE) |
| 119 | cnt = EVENT_BUF_SIZE; |
| 120 | |
| 121 | i = 0; |
| 122 | while (cnt && !isspace(ch)) { |
| 123 | if (!i && ch == '!') |
| 124 | set = 0; |
| 125 | else |
| 126 | buf[i++] = ch; |
| 127 | |
| 128 | ret = get_user(ch, ubuf++); |
| 129 | if (ret) |
| 130 | goto out_free; |
| 131 | read++; |
| 132 | cnt--; |
| 133 | } |
| 134 | buf[i] = 0; |
| 135 | |
| 136 | file->f_pos += read; |
| 137 | |
| 138 | ret = ftrace_set_clr_event(buf, set); |
| 139 | if (ret) |
| 140 | goto out_free; |
| 141 | |
| 142 | ret = read; |
| 143 | |
| 144 | out_free: |
| 145 | kfree(buf); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | static void * |
| 151 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 152 | { |
| 153 | struct ftrace_event_call *call = m->private; |
| 154 | struct ftrace_event_call *next = call; |
| 155 | |
| 156 | (*pos)++; |
| 157 | |
| 158 | if ((unsigned long)call >= (unsigned long)__stop_ftrace_events) |
| 159 | return NULL; |
| 160 | |
| 161 | m->private = ++next; |
| 162 | |
| 163 | return call; |
| 164 | } |
| 165 | |
| 166 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 167 | { |
| 168 | return t_next(m, NULL, pos); |
| 169 | } |
| 170 | |
| 171 | static void * |
| 172 | s_next(struct seq_file *m, void *v, loff_t *pos) |
| 173 | { |
| 174 | struct ftrace_event_call *call = m->private; |
| 175 | struct ftrace_event_call *next; |
| 176 | |
| 177 | (*pos)++; |
| 178 | |
| 179 | retry: |
| 180 | if ((unsigned long)call >= (unsigned long)__stop_ftrace_events) |
| 181 | return NULL; |
| 182 | |
| 183 | if (!call->enabled) { |
| 184 | call++; |
| 185 | goto retry; |
| 186 | } |
| 187 | |
| 188 | next = call; |
| 189 | m->private = ++next; |
| 190 | |
| 191 | return call; |
| 192 | } |
| 193 | |
| 194 | static void *s_start(struct seq_file *m, loff_t *pos) |
| 195 | { |
| 196 | return s_next(m, NULL, pos); |
| 197 | } |
| 198 | |
| 199 | static int t_show(struct seq_file *m, void *v) |
| 200 | { |
| 201 | struct ftrace_event_call *call = v; |
| 202 | |
| 203 | seq_printf(m, "%s\n", call->name); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static void t_stop(struct seq_file *m, void *p) |
| 209 | { |
| 210 | } |
| 211 | |
| 212 | static int |
| 213 | ftrace_event_seq_open(struct inode *inode, struct file *file) |
| 214 | { |
| 215 | int ret; |
| 216 | const struct seq_operations *seq_ops; |
| 217 | |
| 218 | if ((file->f_mode & FMODE_WRITE) && |
| 219 | !(file->f_flags & O_APPEND)) |
| 220 | ftrace_clear_events(); |
| 221 | |
| 222 | seq_ops = inode->i_private; |
| 223 | ret = seq_open(file, seq_ops); |
| 224 | if (!ret) { |
| 225 | struct seq_file *m = file->private_data; |
| 226 | |
| 227 | m->private = __start_ftrace_events; |
| 228 | } |
| 229 | return ret; |
| 230 | } |
| 231 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame^] | 232 | static ssize_t |
| 233 | event_enable_read(struct file *filp, char __user *ubuf, size_t cnt, |
| 234 | loff_t *ppos) |
| 235 | { |
| 236 | struct ftrace_event_call *call = filp->private_data; |
| 237 | char *buf; |
| 238 | |
| 239 | if (call->enabled) |
| 240 | buf = "1\n"; |
| 241 | else |
| 242 | buf = "0\n"; |
| 243 | |
| 244 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2); |
| 245 | } |
| 246 | |
| 247 | static ssize_t |
| 248 | event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt, |
| 249 | loff_t *ppos) |
| 250 | { |
| 251 | struct ftrace_event_call *call = filp->private_data; |
| 252 | char buf[64]; |
| 253 | unsigned long val; |
| 254 | int ret; |
| 255 | |
| 256 | if (cnt >= sizeof(buf)) |
| 257 | return -EINVAL; |
| 258 | |
| 259 | if (copy_from_user(&buf, ubuf, cnt)) |
| 260 | return -EFAULT; |
| 261 | |
| 262 | buf[cnt] = 0; |
| 263 | |
| 264 | ret = strict_strtoul(buf, 10, &val); |
| 265 | if (ret < 0) |
| 266 | return ret; |
| 267 | |
| 268 | switch (val) { |
| 269 | case 0: |
| 270 | if (!call->enabled) |
| 271 | break; |
| 272 | |
| 273 | call->enabled = 0; |
| 274 | call->unregfunc(); |
| 275 | break; |
| 276 | case 1: |
| 277 | if (call->enabled) |
| 278 | break; |
| 279 | |
| 280 | call->enabled = 1; |
| 281 | call->regfunc(); |
| 282 | break; |
| 283 | |
| 284 | default: |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | *ppos += cnt; |
| 289 | |
| 290 | return cnt; |
| 291 | } |
| 292 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 293 | static const struct seq_operations show_event_seq_ops = { |
| 294 | .start = t_start, |
| 295 | .next = t_next, |
| 296 | .show = t_show, |
| 297 | .stop = t_stop, |
| 298 | }; |
| 299 | |
| 300 | static const struct seq_operations show_set_event_seq_ops = { |
| 301 | .start = s_start, |
| 302 | .next = s_next, |
| 303 | .show = t_show, |
| 304 | .stop = t_stop, |
| 305 | }; |
| 306 | |
| 307 | static const struct file_operations ftrace_avail_fops = { |
| 308 | .open = ftrace_event_seq_open, |
| 309 | .read = seq_read, |
| 310 | .llseek = seq_lseek, |
| 311 | .release = seq_release, |
| 312 | }; |
| 313 | |
| 314 | static const struct file_operations ftrace_set_event_fops = { |
| 315 | .open = ftrace_event_seq_open, |
| 316 | .read = seq_read, |
| 317 | .write = ftrace_event_write, |
| 318 | .llseek = seq_lseek, |
| 319 | .release = seq_release, |
| 320 | }; |
| 321 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame^] | 322 | static const struct file_operations ftrace_enable_fops = { |
| 323 | .open = tracing_open_generic, |
| 324 | .read = event_enable_read, |
| 325 | .write = event_enable_write, |
| 326 | }; |
| 327 | |
| 328 | static struct dentry *event_trace_events_dir(void) |
| 329 | { |
| 330 | static struct dentry *d_tracer; |
| 331 | static struct dentry *d_events; |
| 332 | |
| 333 | if (d_events) |
| 334 | return d_events; |
| 335 | |
| 336 | d_tracer = tracing_init_dentry(); |
| 337 | if (!d_tracer) |
| 338 | return NULL; |
| 339 | |
| 340 | d_events = debugfs_create_dir("events", d_tracer); |
| 341 | if (!d_events) |
| 342 | pr_warning("Could not create debugfs " |
| 343 | "'events' directory\n"); |
| 344 | |
| 345 | return d_events; |
| 346 | } |
| 347 | |
| 348 | static int |
| 349 | event_create_dir(struct ftrace_event_call *call, struct dentry *d_events) |
| 350 | { |
| 351 | struct dentry *entry; |
| 352 | |
| 353 | call->dir = debugfs_create_dir(call->name, d_events); |
| 354 | if (!call->dir) { |
| 355 | pr_warning("Could not create debugfs " |
| 356 | "'%s' directory\n", call->name); |
| 357 | return -1; |
| 358 | } |
| 359 | |
| 360 | entry = debugfs_create_file("enable", 0644, call->dir, call, |
| 361 | &ftrace_enable_fops); |
| 362 | if (!entry) |
| 363 | pr_warning("Could not create debugfs " |
| 364 | "'%s/enable' entry\n", call->name); |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 369 | static __init int event_trace_init(void) |
| 370 | { |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame^] | 371 | struct ftrace_event_call *call = __start_ftrace_events; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 372 | struct dentry *d_tracer; |
| 373 | struct dentry *entry; |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame^] | 374 | struct dentry *d_events; |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 375 | |
| 376 | d_tracer = tracing_init_dentry(); |
| 377 | if (!d_tracer) |
| 378 | return 0; |
| 379 | |
| 380 | entry = debugfs_create_file("available_events", 0444, d_tracer, |
| 381 | (void *)&show_event_seq_ops, |
| 382 | &ftrace_avail_fops); |
| 383 | if (!entry) |
| 384 | pr_warning("Could not create debugfs " |
| 385 | "'available_events' entry\n"); |
| 386 | |
| 387 | entry = debugfs_create_file("set_event", 0644, d_tracer, |
| 388 | (void *)&show_set_event_seq_ops, |
| 389 | &ftrace_set_event_fops); |
| 390 | if (!entry) |
| 391 | pr_warning("Could not create debugfs " |
| 392 | "'set_event' entry\n"); |
| 393 | |
Steven Rostedt | 1473e44 | 2009-02-24 14:15:08 -0500 | [diff] [blame^] | 394 | d_events = event_trace_events_dir(); |
| 395 | if (!d_events) |
| 396 | return 0; |
| 397 | |
| 398 | events_for_each(call) { |
| 399 | /* The linker may leave blanks */ |
| 400 | if (!call->name) |
| 401 | continue; |
| 402 | event_create_dir(call, d_events); |
| 403 | } |
| 404 | |
Steven Rostedt | b77e38a | 2009-02-24 10:21:36 -0500 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | fs_initcall(event_trace_init); |