tracing/syscalls: Fix to print parameter types
[safe/jmp/linux-2.6] / kernel / trace / trace_events.c
index 8d0fae3..b568ade 100644 (file)
@@ -17,6 +17,8 @@
 #include <linux/ctype.h>
 #include <linux/delay.h>
 
+#include <asm/setup.h>
+
 #include "trace_output.h"
 
 #define TRACE_SYSTEM "TRACE_SYSTEM"
@@ -76,70 +78,46 @@ static void trace_destroy_fields(struct ftrace_event_call *call)
 
 #endif /* CONFIG_MODULES */
 
-static void ftrace_clear_events(void)
-{
-       struct ftrace_event_call *call;
-
-       mutex_lock(&event_mutex);
-       list_for_each_entry(call, &ftrace_events, list) {
-
-               if (call->enabled) {
-                       call->enabled = 0;
-                       call->unregfunc();
-               }
-       }
-       mutex_unlock(&event_mutex);
-}
-
 static void ftrace_event_enable_disable(struct ftrace_event_call *call,
                                        int enable)
 {
-
        switch (enable) {
        case 0:
                if (call->enabled) {
                        call->enabled = 0;
-                       call->unregfunc();
+                       tracing_stop_cmdline_record();
+                       call->unregfunc(call->data);
                }
                break;
        case 1:
                if (!call->enabled) {
                        call->enabled = 1;
-                       call->regfunc();
+                       tracing_start_cmdline_record();
+                       call->regfunc(call->data);
                }
                break;
        }
 }
 
-static int ftrace_set_clr_event(char *buf, int set)
+static void ftrace_clear_events(void)
 {
        struct ftrace_event_call *call;
-       char *event = NULL, *sub = NULL, *match;
-       int ret = -EINVAL;
-
-       /*
-        * The buf format can be <subsystem>:<event-name>
-        *  *:<event-name> means any event by that name.
-        *  :<event-name> is the same.
-        *
-        *  <subsystem>:* means all events in that subsystem
-        *  <subsystem>: means the same.
-        *
-        *  <name> (no ':') means all events in a subsystem with
-        *  the name <name> or any event that matches <name>
-        */
-
-       match = strsep(&buf, ":");
-       if (buf) {
-               sub = match;
-               event = buf;
-               match = NULL;
 
-               if (!strlen(sub) || strcmp(sub, "*") == 0)
-                       sub = NULL;
-               if (!strlen(event) || strcmp(event, "*") == 0)
-                       event = NULL;
+       mutex_lock(&event_mutex);
+       list_for_each_entry(call, &ftrace_events, list) {
+               ftrace_event_enable_disable(call, 0);
        }
+       mutex_unlock(&event_mutex);
+}
+
+/*
+ * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
+ */
+static int __ftrace_set_clr_event(const char *match, const char *sub,
+                                 const char *event, int set)
+{
+       struct ftrace_event_call *call;
+       int ret = -EINVAL;
 
        mutex_lock(&event_mutex);
        list_for_each_entry(call, &ftrace_events, list) {
@@ -167,6 +145,54 @@ static int ftrace_set_clr_event(char *buf, int set)
        return ret;
 }
 
+static int ftrace_set_clr_event(char *buf, int set)
+{
+       char *event = NULL, *sub = NULL, *match;
+
+       /*
+        * The buf format can be <subsystem>:<event-name>
+        *  *:<event-name> means any event by that name.
+        *  :<event-name> is the same.
+        *
+        *  <subsystem>:* means all events in that subsystem
+        *  <subsystem>: means the same.
+        *
+        *  <name> (no ':') means all events in a subsystem with
+        *  the name <name> or any event that matches <name>
+        */
+
+       match = strsep(&buf, ":");
+       if (buf) {
+               sub = match;
+               event = buf;
+               match = NULL;
+
+               if (!strlen(sub) || strcmp(sub, "*") == 0)
+                       sub = NULL;
+               if (!strlen(event) || strcmp(event, "*") == 0)
+                       event = NULL;
+       }
+
+       return __ftrace_set_clr_event(match, sub, event, set);
+}
+
+/**
+ * trace_set_clr_event - enable or disable an event
+ * @system: system name to match (NULL for any system)
+ * @event: event name to match (NULL for all events, within system)
+ * @set: 1 to enable, 0 to disable
+ *
+ * This is a way for other parts of the kernel to enable or disable
+ * event recording.
+ *
+ * Returns 0 on success, -EINVAL if the parameters do not match any
+ * registered events.
+ */
+int trace_set_clr_event(const char *system, const char *event, int set)
+{
+       return __ftrace_set_clr_event(NULL, system, event, set);
+}
+
 /* 128 should be much more than enough */
 #define EVENT_BUF_SIZE         127
 
@@ -276,10 +302,18 @@ t_next(struct seq_file *m, void *v, loff_t *pos)
 
 static void *t_start(struct seq_file *m, loff_t *pos)
 {
+       struct ftrace_event_call *call = NULL;
+       loff_t l;
+
        mutex_lock(&event_mutex);
-       if (*pos == 0)
-               m->private = ftrace_events.next;
-       return t_next(m, NULL, pos);
+
+       m->private = ftrace_events.next;
+       for (l = 0; l <= *pos; ) {
+               call = t_next(m, NULL, &l);
+               if (!call)
+                       break;
+       }
+       return call;
 }
 
 static void *
@@ -308,10 +342,18 @@ s_next(struct seq_file *m, void *v, loff_t *pos)
 
 static void *s_start(struct seq_file *m, loff_t *pos)
 {
+       struct ftrace_event_call *call = NULL;
+       loff_t l;
+
        mutex_lock(&event_mutex);
-       if (*pos == 0)
-               m->private = ftrace_events.next;
-       return s_next(m, NULL, pos);
+
+       m->private = ftrace_events.next;
+       for (l = 0; l <= *pos; ) {
+               call = s_next(m, NULL, &l);
+               if (!call)
+                       break;
+       }
+       return call;
 }
 
 static int t_show(struct seq_file *m, void *v)
@@ -336,7 +378,7 @@ ftrace_event_seq_open(struct inode *inode, struct file *file)
        const struct seq_operations *seq_ops;
 
        if ((file->f_mode & FMODE_WRITE) &&
-           !(file->f_flags & O_APPEND))
+           (file->f_flags & O_TRUNC))
                ftrace_clear_events();
 
        seq_ops = inode->i_private;
@@ -404,22 +446,19 @@ static ssize_t
 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
                   loff_t *ppos)
 {
+       const char set_to_char[4] = { '?', '0', '1', 'X' };
        const char *system = filp->private_data;
        struct ftrace_event_call *call;
        char buf[2];
-       int set = -1;
-       int all = 0;
+       int set = 0;
        int ret;
 
-       if (system[0] == '*')
-               all = 1;
-
        mutex_lock(&event_mutex);
        list_for_each_entry(call, &ftrace_events, list) {
                if (!call->name || !call->regfunc)
                        continue;
 
-               if (!all && strcmp(call->system, system) != 0)
+               if (system && strcmp(call->system, system) != 0)
                        continue;
 
                /*
@@ -427,47 +466,18 @@ system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
                 * or if all events or cleared, or if we have
                 * a mixture.
                 */
-               if (call->enabled) {
-                       switch (set) {
-                       case -1:
-                               set = 1;
-                               break;
-                       case 0:
-                               set = 2;
-                               break;
-                       }
-               } else {
-                       switch (set) {
-                       case -1:
-                               set = 0;
-                               break;
-                       case 1:
-                               set = 2;
-                               break;
-                       }
-               }
+               set |= (1 << !!call->enabled);
+
                /*
                 * If we have a mixture, no need to look further.
                 */
-               if (set == 2)
+               if (set == 3)
                        break;
        }
        mutex_unlock(&event_mutex);
 
+       buf[0] = set_to_char[set];
        buf[1] = '\n';
-       switch (set) {
-       case 0:
-               buf[0] = '0';
-               break;
-       case 1:
-               buf[0] = '1';
-               break;
-       case 2:
-               buf[0] = 'X';
-               break;
-       default:
-               buf[0] = '?';
-       }
 
        ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
 
@@ -480,7 +490,6 @@ system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
 {
        const char *system = filp->private_data;
        unsigned long val;
-       char *command;
        char buf[64];
        ssize_t ret;
 
@@ -500,30 +509,16 @@ system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
        if (ret < 0)
                return ret;
 
-       switch (val) {
-       case 0:
-       case 1:
-               break;
-
-       default:
+       if (val != 0 && val != 1)
                return -EINVAL;
-       }
 
-       /* +3 for the ":*\0" */
-       command = kmalloc(strlen(system)+3, GFP_KERNEL);
-       if (!command)
-               return -ENOMEM;
-       sprintf(command, "%s:*", system);
-
-       ret = ftrace_set_clr_event(command, val);
+       ret = __ftrace_set_clr_event(NULL, system, NULL, val);
        if (ret)
-               goto out_free;
+               goto out;
 
        ret = cnt;
 
- out_free:
-       kfree(command);
-
+out:
        *ppos += cnt;
 
        return ret;
@@ -581,7 +576,7 @@ event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
        trace_seq_printf(s, "format:\n");
        trace_write_header(s);
 
-       r = call->show_format(s);
+       r = call->show_format(call, s);
        if (!r) {
                /*
                 * ug!  The format output is bigger than a PAGE!!
@@ -856,8 +851,10 @@ event_subsystem_dir(const char *name, struct dentry *d_events)
 
        /* First see if we did not already create this dir */
        list_for_each_entry(system, &event_subsystems, list) {
-               if (strcmp(system->name, name) == 0)
+               if (strcmp(system->name, name) == 0) {
+                       system->nr_events++;
                        return system->entry;
+               }
        }
 
        /* need to create new entry */
@@ -876,6 +873,7 @@ event_subsystem_dir(const char *name, struct dentry *d_events)
                return d_events;
        }
 
+       system->nr_events = 1;
        system->name = kstrdup(name, GFP_KERNEL);
        if (!system->name) {
                debugfs_remove(system->entry);
@@ -927,15 +925,6 @@ event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
        if (strcmp(call->system, TRACE_SYSTEM) != 0)
                d_events = event_subsystem_dir(call->system, d_events);
 
-       if (call->raw_init) {
-               ret = call->raw_init();
-               if (ret < 0) {
-                       pr_warning("Could not initialize trace point"
-                                  " events/%s\n", call->name);
-                       return ret;
-               }
-       }
-
        call->dir = debugfs_create_dir(call->name, d_events);
        if (!call->dir) {
                pr_warning("Could not create debugfs "
@@ -947,7 +936,7 @@ event_create_dir(struct ftrace_event_call *call, struct dentry *d_events,
                entry = trace_create_file("enable", 0644, call->dir, call,
                                          enable);
 
-       if (call->id)
+       if (call->id && call->profile_enable)
                entry = trace_create_file("id", 0444, call->dir, call,
                                          id);
 
@@ -994,6 +983,32 @@ struct ftrace_module_file_ops {
        struct file_operations          filter;
 };
 
+static void remove_subsystem_dir(const char *name)
+{
+       struct event_subsystem *system;
+
+       if (strcmp(name, TRACE_SYSTEM) == 0)
+               return;
+
+       list_for_each_entry(system, &event_subsystems, list) {
+               if (strcmp(system->name, name) == 0) {
+                       if (!--system->nr_events) {
+                               struct event_filter *filter = system->filter;
+
+                               debugfs_remove_recursive(system->entry);
+                               list_del(&system->list);
+                               if (filter) {
+                                       kfree(filter->filter_string);
+                                       kfree(filter);
+                               }
+                               kfree(system->name);
+                               kfree(system);
+                       }
+                       break;
+               }
+       }
+}
+
 static struct ftrace_module_file_ops *
 trace_create_file_ops(struct module *mod)
 {
@@ -1034,6 +1049,7 @@ static void trace_module_add_events(struct module *mod)
        struct ftrace_module_file_ops *file_ops = NULL;
        struct ftrace_event_call *call, *start, *end;
        struct dentry *d_events;
+       int ret;
 
        start = mod->trace_events;
        end = mod->trace_events + mod->num_trace_events;
@@ -1049,7 +1065,15 @@ static void trace_module_add_events(struct module *mod)
                /* The linker may leave blanks */
                if (!call->name)
                        continue;
-
+               if (call->raw_init) {
+                       ret = call->raw_init();
+                       if (ret < 0) {
+                               if (ret != -ENOSYS)
+                                       pr_warning("Could not initialize trace "
+                                       "point events/%s\n", call->name);
+                               continue;
+                       }
+               }
                /*
                 * This module has events, create file ops for this module
                 * if not already done.
@@ -1073,19 +1097,18 @@ static void trace_module_remove_events(struct module *mod)
        struct ftrace_event_call *call, *p;
        bool found = false;
 
+       down_write(&trace_event_mutex);
        list_for_each_entry_safe(call, p, &ftrace_events, list) {
                if (call->mod == mod) {
                        found = true;
-                       if (call->enabled) {
-                               call->enabled = 0;
-                               call->unregfunc();
-                       }
+                       ftrace_event_enable_disable(call, 0);
                        if (call->event)
-                               unregister_ftrace_event(call->event);
+                               __unregister_ftrace_event(call->event);
                        debugfs_remove_recursive(call->dir);
                        list_del(&call->list);
                        trace_destroy_fields(call);
                        destroy_preds(call);
+                       remove_subsystem_dir(call->system);
                }
        }
 
@@ -1105,6 +1128,7 @@ static void trace_module_remove_events(struct module *mod)
         */
        if (found)
                tracing_reset_current_online_cpus();
+       up_write(&trace_event_mutex);
 }
 
 static int trace_module_notify(struct notifier_block *self,
@@ -1141,6 +1165,18 @@ struct notifier_block trace_module_nb = {
 extern struct ftrace_event_call __start_ftrace_events[];
 extern struct ftrace_event_call __stop_ftrace_events[];
 
+static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
+
+static __init int setup_trace_event(char *str)
+{
+       strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
+       ring_buffer_expanded = 1;
+       tracing_selftest_disabled = 1;
+
+       return 1;
+}
+__setup("trace_event=", setup_trace_event);
+
 static __init int event_trace_init(void)
 {
        struct ftrace_event_call *call;
@@ -1148,6 +1184,8 @@ static __init int event_trace_init(void)
        struct dentry *entry;
        struct dentry *d_events;
        int ret;
+       char *buf = bootup_event_buf;
+       char *token;
 
        d_tracer = tracing_init_dentry();
        if (!d_tracer)
@@ -1181,20 +1219,42 @@ static __init int event_trace_init(void)
                          &ftrace_show_header_fops);
 
        trace_create_file("enable", 0644, d_events,
-                         "*", &ftrace_system_enable_fops);
+                         NULL, &ftrace_system_enable_fops);
 
        for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
                /* The linker may leave blanks */
                if (!call->name)
                        continue;
+               if (call->raw_init) {
+                       ret = call->raw_init();
+                       if (ret < 0) {
+                               if (ret != -ENOSYS)
+                                       pr_warning("Could not initialize trace "
+                                       "point events/%s\n", call->name);
+                               continue;
+                       }
+               }
                list_add(&call->list, &ftrace_events);
                event_create_dir(call, d_events, &ftrace_event_id_fops,
                                 &ftrace_enable_fops, &ftrace_event_filter_fops,
                                 &ftrace_event_format_fops);
        }
 
+       while (true) {
+               token = strsep(&buf, ",");
+
+               if (!token)
+                       break;
+               if (!*token)
+                       continue;
+
+               ret = ftrace_set_clr_event(token, 1);
+               if (ret)
+                       pr_warning("Failed to enable trace event: %s\n", token);
+       }
+
        ret = register_module_notifier(&trace_module_nb);
-       if (!ret)
+       if (ret)
                pr_warning("Failed to register trace events module notifier\n");
 
        return 0;
@@ -1259,7 +1319,6 @@ static __init void event_trace_self_tests(void)
 {
        struct ftrace_event_call *call;
        struct event_subsystem *system;
-       char *sysname;
        int ret;
 
        pr_info("Running tests on trace events:\n");
@@ -1282,13 +1341,9 @@ static __init void event_trace_self_tests(void)
                        continue;
                }
 
-               call->enabled = 1;
-               call->regfunc();
-
+               ftrace_event_enable_disable(call, 1);
                event_test_stuff();
-
-               call->unregfunc();
-               call->enabled = 0;
+               ftrace_event_enable_disable(call, 0);
 
                pr_cont("OK\n");
        }
@@ -1305,14 +1360,7 @@ static __init void event_trace_self_tests(void)
 
                pr_info("Testing event system %s: ", system->name);
 
-               /* ftrace_set_clr_event can modify the name passed in. */
-               sysname = kstrdup(system->name, GFP_KERNEL);
-               if (WARN_ON(!sysname)) {
-                       pr_warning("Can't allocate memory, giving up!\n");
-                       return;
-               }
-               ret = ftrace_set_clr_event(sysname, 1);
-               kfree(sysname);
+               ret = __ftrace_set_clr_event(NULL, system->name, NULL, 1);
                if (WARN_ON_ONCE(ret)) {
                        pr_warning("error enabling system %s\n",
                                   system->name);
@@ -1321,14 +1369,7 @@ static __init void event_trace_self_tests(void)
 
                event_test_stuff();
 
-               sysname = kstrdup(system->name, GFP_KERNEL);
-               if (WARN_ON(!sysname)) {
-                       pr_warning("Can't allocate memory, giving up!\n");
-                       return;
-               }
-               ret = ftrace_set_clr_event(sysname, 0);
-               kfree(sysname);
-
+               ret = __ftrace_set_clr_event(NULL, system->name, NULL, 0);
                if (WARN_ON_ONCE(ret))
                        pr_warning("error disabling system %s\n",
                                   system->name);
@@ -1341,15 +1382,8 @@ static __init void event_trace_self_tests(void)
        pr_info("Running tests on all trace events:\n");
        pr_info("Testing all events: ");
 
-       sysname = kmalloc(4, GFP_KERNEL);
-       if (WARN_ON(!sysname)) {
-               pr_warning("Can't allocate memory, giving up!\n");
-               return;
-       }
-       memcpy(sysname, "*:*", 4);
-       ret = ftrace_set_clr_event(sysname, 1);
+       ret = __ftrace_set_clr_event(NULL, NULL, NULL, 1);
        if (WARN_ON_ONCE(ret)) {
-               kfree(sysname);
                pr_warning("error enabling all events\n");
                return;
        }
@@ -1357,10 +1391,7 @@ static __init void event_trace_self_tests(void)
        event_test_stuff();
 
        /* reset sysname */
-       memcpy(sysname, "*:*", 4);
-       ret = ftrace_set_clr_event(sysname, 0);
-       kfree(sysname);
-
+       ret = __ftrace_set_clr_event(NULL, NULL, NULL, 0);
        if (WARN_ON_ONCE(ret)) {
                pr_warning("error disabling all events\n");
                return;
@@ -1429,10 +1460,10 @@ static __init void event_trace_self_test_with_function(void)
 
 static __init int event_trace_self_tests_init(void)
 {
-
-       event_trace_self_tests();
-
-       event_trace_self_test_with_function();
+       if (!tracing_selftest_disabled) {
+               event_trace_self_tests();
+               event_trace_self_test_with_function();
+       }
 
        return 0;
 }