tracing/kprobes: Fix probe offset to be unsigned
[pandora-kernel.git] / kernel / trace / trace_kprobe.c
index 6d488ef..c24b7e9 100644 (file)
@@ -34,6 +34,7 @@
 
 #define MAX_TRACE_ARGS 128
 #define MAX_ARGSTR_LEN 63
+#define MAX_EVENT_NAME_LEN 64
 
 /* currently, trace_kprobe only supports X86. */
 
@@ -183,8 +184,10 @@ struct trace_probe {
                struct kprobe           kp;
                struct kretprobe        rp;
        };
+       unsigned long           nhit;
        const char              *symbol;        /* symbol name */
        struct ftrace_event_call        call;
+       struct trace_event              event;
        unsigned int            nr_args;
        struct fetch_func       args[];
 };
@@ -207,7 +210,7 @@ static __kprobes const char *probe_symbol(struct trace_probe *tp)
        return tp->symbol ? tp->symbol : "unknown";
 }
 
-static __kprobes long probe_offset(struct trace_probe *tp)
+static __kprobes unsigned int probe_offset(struct trace_probe *tp)
 {
        return (probe_is_return(tp)) ? tp->rp.kp.offset : tp->kp.offset;
 }
@@ -217,7 +220,7 @@ static __kprobes void *probe_address(struct trace_probe *tp)
        return (probe_is_return(tp)) ? tp->rp.kp.addr : tp->kp.addr;
 }
 
-static int trace_arg_string(char *buf, size_t n, struct fetch_func *ff)
+static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
 {
        int ret = -EINVAL;
 
@@ -247,7 +250,7 @@ static int trace_arg_string(char *buf, size_t n, struct fetch_func *ff)
                if (ret >= n)
                        goto end;
                l += ret;
-               ret = trace_arg_string(buf + l, n - l, &id->orig);
+               ret = probe_arg_string(buf + l, n - l, &id->orig);
                if (ret < 0)
                        goto end;
                l += ret;
@@ -280,11 +283,11 @@ static struct trace_probe *alloc_trace_probe(const char *symbol,
                if (!tp->symbol)
                        goto error;
        }
-       if (event) {
-               tp->call.name = kstrdup(event, GFP_KERNEL);
-               if (!tp->call.name)
-                       goto error;
-       }
+       if (!event)
+               goto error;
+       tp->call.name = kstrdup(event, GFP_KERNEL);
+       if (!tp->call.name)
+               goto error;
 
        INIT_LIST_HEAD(&tp->list);
        return tp;
@@ -314,7 +317,7 @@ static struct trace_probe *find_probe_event(const char *event)
        struct trace_probe *tp;
 
        list_for_each_entry(tp, &probe_list, list)
-               if (tp->call.name && !strcmp(tp->call.name, event))
+               if (!strcmp(tp->call.name, event))
                        return tp;
        return NULL;
 }
@@ -330,8 +333,7 @@ static void __unregister_trace_probe(struct trace_probe *tp)
 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
 static void unregister_trace_probe(struct trace_probe *tp)
 {
-       if (tp->call.name)
-               unregister_probe_event(tp);
+       unregister_probe_event(tp);
        __unregister_trace_probe(tp);
        list_del(&tp->list);
 }
@@ -360,18 +362,16 @@ static int register_trace_probe(struct trace_probe *tp)
                goto end;
        }
        /* register as an event */
-       if (tp->call.name) {
-               old_tp = find_probe_event(tp->call.name);
-               if (old_tp) {
-                       /* delete old event */
-                       unregister_trace_probe(old_tp);
-                       free_trace_probe(old_tp);
-               }
-               ret = register_probe_event(tp);
-               if (ret) {
-                       pr_warning("Faild to register probe event(%d)\n", ret);
-                       __unregister_trace_probe(tp);
-               }
+       old_tp = find_probe_event(tp->call.name);
+       if (old_tp) {
+               /* delete old event */
+               unregister_trace_probe(old_tp);
+               free_trace_probe(old_tp);
+       }
+       ret = register_probe_event(tp);
+       if (ret) {
+               pr_warning("Faild to register probe event(%d)\n", ret);
+               __unregister_trace_probe(tp);
        }
        list_add_tail(&tp->list, &probe_list);
 end:
@@ -380,7 +380,7 @@ end:
 }
 
 /* Split symbol and offset. */
-static int split_symbol_offset(char *symbol, long *offset)
+static int split_symbol_offset(char *symbol, unsigned long *offset)
 {
        char *tmp;
        int ret;
@@ -389,16 +389,11 @@ static int split_symbol_offset(char *symbol, long *offset)
                return -EINVAL;
 
        tmp = strchr(symbol, '+');
-       if (!tmp)
-               tmp = strchr(symbol, '-');
-
        if (tmp) {
                /* skip sign because strict_strtol doesn't accept '+' */
-               ret = strict_strtol(tmp + 1, 0, offset);
+               ret = strict_strtoul(tmp + 1, 0, offset);
                if (ret)
                        return ret;
-               if (*tmp == '-')
-                       *offset = -(*offset);
                *tmp = '\0';
        } else
                *offset = 0;
@@ -408,7 +403,7 @@ static int split_symbol_offset(char *symbol, long *offset)
 #define PARAM_MAX_ARGS 16
 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
 
-static int parse_trace_arg(char *arg, struct fetch_func *ff, int is_return)
+static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
 {
        int ret = 0;
        unsigned long param;
@@ -499,7 +494,7 @@ static int parse_trace_arg(char *arg, struct fetch_func *ff, int is_return)
                        if (!id)
                                return -ENOMEM;
                        id->offset = offset;
-                       ret = parse_trace_arg(arg, &id->orig, is_return);
+                       ret = parse_probe_arg(arg, &id->orig, is_return);
                        if (ret)
                                kfree(id);
                        else {
@@ -520,7 +515,7 @@ static int create_trace_probe(int argc, char **argv)
 {
        /*
         * Argument syntax:
-        *  - Add kprobe: p[:EVENT] SYMBOL[+OFFS|-OFFS]|ADDRESS [FETCHARGS]
+        *  - Add kprobe: p[:EVENT] SYMBOL[+OFFS]|ADDRESS [FETCHARGS]
         *  - Add kretprobe: r[:EVENT] SYMBOL[+0] [FETCHARGS]
         * Fetch args:
         *  aN  : fetch Nth of function argument. (N:0-)
@@ -539,7 +534,7 @@ static int create_trace_probe(int argc, char **argv)
        int i, ret = 0;
        int is_return = 0;
        char *symbol = NULL, *event = NULL;
-       long offset = 0;
+       unsigned long offset = 0;
        void *addr = NULL;
 
        if (argc < 2)
@@ -580,7 +575,18 @@ static int create_trace_probe(int argc, char **argv)
        argc -= 2; argv += 2;
 
        /* setup a probe */
-       tp = alloc_trace_probe(symbol, event, argc);
+       if (!event) {
+               /* Make a new event name */
+               char buf[MAX_EVENT_NAME_LEN];
+               if (symbol)
+                       snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
+                                is_return ? 'r' : 'p', symbol, offset);
+               else
+                       snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
+                                is_return ? 'r' : 'p', addr);
+               tp = alloc_trace_probe(symbol, buf, argc);
+       } else
+               tp = alloc_trace_probe(symbol, event, argc);
        if (IS_ERR(tp))
                return PTR_ERR(tp);
 
@@ -594,7 +600,7 @@ static int create_trace_probe(int argc, char **argv)
 
        if (tp->symbol) {
                kp->symbol_name = tp->symbol;
-               kp->offset = offset;
+               kp->offset = (unsigned int)offset;
        } else
                kp->addr = addr;
 
@@ -606,7 +612,7 @@ static int create_trace_probe(int argc, char **argv)
                        ret = -ENOSPC;
                        goto error;
                }
-               ret = parse_trace_arg(argv[i], &tp->args[i], is_return);
+               ret = parse_probe_arg(argv[i], &tp->args[i], is_return);
                if (ret)
                        goto error;
        }
@@ -661,16 +667,15 @@ static int probes_seq_show(struct seq_file *m, void *v)
        char buf[MAX_ARGSTR_LEN + 1];
 
        seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
-       if (tp->call.name)
-               seq_printf(m, ":%s", tp->call.name);
+       seq_printf(m, ":%s", tp->call.name);
 
        if (tp->symbol)
-               seq_printf(m, " %s%+ld", probe_symbol(tp), probe_offset(tp));
+               seq_printf(m, " %s+%u", probe_symbol(tp), probe_offset(tp));
        else
                seq_printf(m, " 0x%p", probe_address(tp));
 
        for (i = 0; i < tp->nr_args; i++) {
-               ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
+               ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
                if (ret < 0) {
                        pr_warning("Argument%d decoding error(%d).\n", i, ret);
                        return ret;
@@ -772,25 +777,56 @@ static const struct file_operations kprobe_events_ops = {
        .write          = probes_write,
 };
 
+/* Probes profiling interfaces */
+static int probes_profile_seq_show(struct seq_file *m, void *v)
+{
+       struct trace_probe *tp = v;
+
+       seq_printf(m, "  %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
+                  probe_is_return(tp) ? tp->rp.kp.nmissed : tp->kp.nmissed);
+
+       return 0;
+}
+
+static const struct seq_operations profile_seq_op = {
+       .start  = probes_seq_start,
+       .next   = probes_seq_next,
+       .stop   = probes_seq_stop,
+       .show   = probes_profile_seq_show
+};
+
+static int profile_open(struct inode *inode, struct file *file)
+{
+       return seq_open(file, &profile_seq_op);
+}
+
+static const struct file_operations kprobe_profile_ops = {
+       .owner          = THIS_MODULE,
+       .open           = profile_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = seq_release,
+};
+
 /* Kprobe handler */
 static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
 {
        struct trace_probe *tp = container_of(kp, struct trace_probe, kp);
        struct kprobe_trace_entry *entry;
        struct ring_buffer_event *event;
+       struct ring_buffer *buffer;
        int size, i, pc;
        unsigned long irq_flags;
-       struct ftrace_event_call *call = &event_kprobe;
+       struct ftrace_event_call *call = &tp->call;
 
-       if (&tp->call.name)
-               call = &tp->call;
+       tp->nhit++;
 
        local_save_flags(irq_flags);
        pc = preempt_count();
 
        size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
 
-       event = trace_current_buffer_lock_reserve(TRACE_KPROBE, size,
+       event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
                                                  irq_flags, pc);
        if (!event)
                return 0;
@@ -801,8 +837,8 @@ static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
        for (i = 0; i < tp->nr_args; i++)
                entry->args[i] = call_fetch(&tp->args[i], regs);
 
-       if (!filter_current_check_discard(call, entry, event))
-               trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
+       if (!filter_current_check_discard(buffer, call, entry, event))
+               trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
        return 0;
 }
 
@@ -813,19 +849,17 @@ static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
        struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
        struct kretprobe_trace_entry *entry;
        struct ring_buffer_event *event;
+       struct ring_buffer *buffer;
        int size, i, pc;
        unsigned long irq_flags;
-       struct ftrace_event_call *call = &event_kretprobe;
-
-       if (&tp->call.name)
-               call = &tp->call;
+       struct ftrace_event_call *call = &tp->call;
 
        local_save_flags(irq_flags);
        pc = preempt_count();
 
        size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
 
-       event = trace_current_buffer_lock_reserve(TRACE_KRETPROBE, size,
+       event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
                                                  irq_flags, pc);
        if (!event)
                return 0;
@@ -837,8 +871,8 @@ static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
        for (i = 0; i < tp->nr_args; i++)
                entry->args[i] = call_fetch(&tp->args[i], regs);
 
-       if (!filter_current_check_discard(call, entry, event))
-               trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
+       if (!filter_current_check_discard(buffer, call, entry, event))
+               trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
 
        return 0;
 }
@@ -851,7 +885,7 @@ print_kprobe_event(struct trace_iterator *iter, int flags)
        struct trace_seq *s = &iter->seq;
        int i;
 
-       trace_assign_type(field, iter->ent);
+       field = (struct kprobe_trace_entry *)iter->ent;
 
        if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
                goto partial;
@@ -878,7 +912,7 @@ print_kretprobe_event(struct trace_iterator *iter, int flags)
        struct trace_seq *s = &iter->seq;
        int i;
 
-       trace_assign_type(field, iter->ent);
+       field = (struct kretprobe_trace_entry *)iter->ent;
 
        if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
                goto partial;
@@ -904,16 +938,6 @@ partial:
        return TRACE_TYPE_PARTIAL_LINE;
 }
 
-static struct trace_event kprobe_trace_event = {
-       .type           = TRACE_KPROBE,
-       .trace          = print_kprobe_event,
-};
-
-static struct trace_event kretprobe_trace_event = {
-       .type           = TRACE_KRETPROBE,
-       .trace          = print_kretprobe_event,
-};
-
 static int probe_event_enable(struct ftrace_event_call *call)
 {
        struct trace_probe *tp = (struct trace_probe *)call->data;
@@ -937,7 +961,7 @@ static void probe_event_disable(struct ftrace_event_call *call)
 static int probe_event_raw_init(struct ftrace_event_call *event_call)
 {
        INIT_LIST_HEAD(&event_call->fields);
-       init_preds(event_call);
+
        return 0;
 }
 
@@ -970,7 +994,7 @@ static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
                sprintf(buf, "arg%d", i);
                DEFINE_FIELD(unsigned long, args[i], buf, 0);
                /* Set argument string as an alias field */
-               ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
+               ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
                if (ret < 0)
                        return ret;
                DEFINE_FIELD(unsigned long, args[i], buf, 0);
@@ -997,7 +1021,7 @@ static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
                sprintf(buf, "arg%d", i);
                DEFINE_FIELD(unsigned long, args[i], buf, 0);
                /* Set argument string as an alias field */
-               ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
+               ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
                if (ret < 0)
                        return ret;
                DEFINE_FIELD(unsigned long, args[i], buf, 0);
@@ -1014,7 +1038,7 @@ static int __probe_event_show_format(struct trace_seq *s,
 
        /* Show aliases */
        for (i = 0; i < tp->nr_args; i++) {
-               ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
+               ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
                if (ret < 0)
                        return ret;
                if (!trace_seq_printf(s, "\talias: %s;\toriginal: arg%d;\n",
@@ -1043,7 +1067,7 @@ static int __probe_event_show_format(struct trace_seq *s,
 #define SHOW_FIELD(type, item, name)                                   \
        do {                                                            \
                ret = trace_seq_printf(s, "\tfield: " #type " %s;\t"    \
-                               "offset:%u;tsize:%u;\n", name,          \
+                               "offset:%u;\tsize:%u;\n", name,         \
                                (unsigned int)offsetof(typeof(field), item),\
                                (unsigned int)sizeof(type));            \
                if (!ret)                                               \
@@ -1102,35 +1126,35 @@ static int register_probe_event(struct trace_probe *tp)
        /* Initialize ftrace_event_call */
        call->system = "kprobes";
        if (probe_is_return(tp)) {
-               call->event = &kretprobe_trace_event;
-               call->id = TRACE_KRETPROBE;
+               tp->event.trace = print_kretprobe_event;
                call->raw_init = probe_event_raw_init;
                call->show_format = kretprobe_event_show_format;
                call->define_fields = kretprobe_event_define_fields;
        } else {
-               call->event = &kprobe_trace_event;
-               call->id = TRACE_KPROBE;
+               tp->event.trace = print_kprobe_event;
                call->raw_init = probe_event_raw_init;
                call->show_format = kprobe_event_show_format;
                call->define_fields = kprobe_event_define_fields;
        }
+       call->event = &tp->event;
+       call->id = register_ftrace_event(&tp->event);
+       if (!call->id)
+               return -ENODEV;
        call->enabled = 1;
        call->regfunc = probe_event_enable;
        call->unregfunc = probe_event_disable;
        call->data = tp;
        ret = trace_add_event_call(call);
-       if (ret)
+       if (ret) {
                pr_info("Failed to register kprobe event: %s\n", call->name);
+               unregister_ftrace_event(&tp->event);
+       }
        return ret;
 }
 
 static void unregister_probe_event(struct trace_probe *tp)
 {
-       /*
-        * Prevent to unregister event itself because the event is shared
-        * among other probes.
-        */
-       tp->call.event = NULL;
+       /* tp->event is unregistered in trace_remove_event_call() */
        trace_remove_event_call(&tp->call);
 }
 
@@ -1139,18 +1163,6 @@ static __init int init_kprobe_trace(void)
 {
        struct dentry *d_tracer;
        struct dentry *entry;
-       int ret;
-
-       ret = register_ftrace_event(&kprobe_trace_event);
-       if (!ret) {
-               pr_warning("Could not register kprobe_trace_event type.\n");
-               return 0;
-       }
-       ret = register_ftrace_event(&kretprobe_trace_event);
-       if (!ret) {
-               pr_warning("Could not register kretprobe_trace_event type.\n");
-               return 0;
-       }
 
        d_tracer = tracing_init_dentry();
        if (!d_tracer)
@@ -1159,9 +1171,18 @@ static __init int init_kprobe_trace(void)
        entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
                                    NULL, &kprobe_events_ops);
 
+       /* Event list interface */
        if (!entry)
                pr_warning("Could not create debugfs "
                           "'kprobe_events' entry\n");
+
+       /* Profile interface */
+       entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
+                                   NULL, &kprobe_profile_ops);
+
+       if (!entry)
+               pr_warning("Could not create debugfs "
+                          "'kprobe_profile' entry\n");
        return 0;
 }
 fs_initcall(init_kprobe_trace);