linux/kernel/trace/trace_kprobe.c
<<
>>
Prefs
   1/*
   2 * Kprobes-based tracing events
   3 *
   4 * Created by Masami Hiramatsu <mhiramat@redhat.com>
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program; if not, write to the Free Software
  17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18 */
  19
  20#include <linux/module.h>
  21#include <linux/uaccess.h>
  22
  23#include "trace_probe.h"
  24
  25#define KPROBE_EVENT_SYSTEM "kprobes"
  26
  27/**
  28 * Kprobe event core functions
  29 */
  30struct trace_probe {
  31        struct list_head        list;
  32        struct kretprobe        rp;     /* Use rp.kp for kprobe use */
  33        unsigned long           nhit;
  34        unsigned int            flags;  /* For TP_FLAG_* */
  35        const char              *symbol;        /* symbol name */
  36        struct ftrace_event_class       class;
  37        struct ftrace_event_call        call;
  38        struct ftrace_event_file * __rcu *files;
  39        ssize_t                 size;           /* trace entry size */
  40        unsigned int            nr_args;
  41        struct probe_arg        args[];
  42};
  43
  44#define SIZEOF_TRACE_PROBE(n)                   \
  45        (offsetof(struct trace_probe, args) +   \
  46        (sizeof(struct probe_arg) * (n)))
  47
  48
  49static __kprobes bool trace_probe_is_return(struct trace_probe *tp)
  50{
  51        return tp->rp.handler != NULL;
  52}
  53
  54static __kprobes const char *trace_probe_symbol(struct trace_probe *tp)
  55{
  56        return tp->symbol ? tp->symbol : "unknown";
  57}
  58
  59static __kprobes unsigned long trace_probe_offset(struct trace_probe *tp)
  60{
  61        return tp->rp.kp.offset;
  62}
  63
  64static __kprobes bool trace_probe_is_enabled(struct trace_probe *tp)
  65{
  66        return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE));
  67}
  68
  69static __kprobes bool trace_probe_is_registered(struct trace_probe *tp)
  70{
  71        return !!(tp->flags & TP_FLAG_REGISTERED);
  72}
  73
  74static __kprobes bool trace_probe_has_gone(struct trace_probe *tp)
  75{
  76        return !!(kprobe_gone(&tp->rp.kp));
  77}
  78
  79static __kprobes bool trace_probe_within_module(struct trace_probe *tp,
  80                                                struct module *mod)
  81{
  82        int len = strlen(mod->name);
  83        const char *name = trace_probe_symbol(tp);
  84        return strncmp(mod->name, name, len) == 0 && name[len] == ':';
  85}
  86
  87static __kprobes bool trace_probe_is_on_module(struct trace_probe *tp)
  88{
  89        return !!strchr(trace_probe_symbol(tp), ':');
  90}
  91
  92static int register_probe_event(struct trace_probe *tp);
  93static int unregister_probe_event(struct trace_probe *tp);
  94
  95static DEFINE_MUTEX(probe_lock);
  96static LIST_HEAD(probe_list);
  97
  98static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  99static int kretprobe_dispatcher(struct kretprobe_instance *ri,
 100                                struct pt_regs *regs);
 101
 102/* Memory fetching by symbol */
 103struct symbol_cache {
 104        char            *symbol;
 105        long            offset;
 106        unsigned long   addr;
 107};
 108
 109unsigned long update_symbol_cache(struct symbol_cache *sc)
 110{
 111        sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
 112
 113        if (sc->addr)
 114                sc->addr += sc->offset;
 115
 116        return sc->addr;
 117}
 118
 119void free_symbol_cache(struct symbol_cache *sc)
 120{
 121        kfree(sc->symbol);
 122        kfree(sc);
 123}
 124
 125struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
 126{
 127        struct symbol_cache *sc;
 128
 129        if (!sym || strlen(sym) == 0)
 130                return NULL;
 131
 132        sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
 133        if (!sc)
 134                return NULL;
 135
 136        sc->symbol = kstrdup(sym, GFP_KERNEL);
 137        if (!sc->symbol) {
 138                kfree(sc);
 139                return NULL;
 140        }
 141        sc->offset = offset;
 142        update_symbol_cache(sc);
 143
 144        return sc;
 145}
 146
 147/*
 148 * Kprobes-specific fetch functions
 149 */
 150#define DEFINE_FETCH_stack(type)                                        \
 151static __kprobes void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,\
 152                                          void *offset, void *dest)     \
 153{                                                                       \
 154        *(type *)dest = (type)regs_get_kernel_stack_nth(regs,           \
 155                                (unsigned int)((unsigned long)offset)); \
 156}
 157DEFINE_BASIC_FETCH_FUNCS(stack)
 158/* No string on the stack entry */
 159#define fetch_stack_string      NULL
 160#define fetch_stack_string_size NULL
 161
 162#define DEFINE_FETCH_memory(type)                                       \
 163static __kprobes void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,\
 164                                          void *addr, void *dest)       \
 165{                                                                       \
 166        type retval;                                                    \
 167        if (probe_kernel_address(addr, retval))                         \
 168                *(type *)dest = 0;                                      \
 169        else                                                            \
 170                *(type *)dest = retval;                                 \
 171}
 172DEFINE_BASIC_FETCH_FUNCS(memory)
 173/*
 174 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
 175 * length and relative data location.
 176 */
 177static __kprobes void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
 178                                                      void *addr, void *dest)
 179{
 180        long ret;
 181        int maxlen = get_rloc_len(*(u32 *)dest);
 182        u8 *dst = get_rloc_data(dest);
 183        u8 *src = addr;
 184        mm_segment_t old_fs = get_fs();
 185
 186        if (!maxlen)
 187                return;
 188
 189        /*
 190         * Try to get string again, since the string can be changed while
 191         * probing.
 192         */
 193        set_fs(KERNEL_DS);
 194        pagefault_disable();
 195
 196        do
 197                ret = __copy_from_user_inatomic(dst++, src++, 1);
 198        while (dst[-1] && ret == 0 && src - (u8 *)addr < maxlen);
 199
 200        dst[-1] = '\0';
 201        pagefault_enable();
 202        set_fs(old_fs);
 203
 204        if (ret < 0) {  /* Failed to fetch string */
 205                ((u8 *)get_rloc_data(dest))[0] = '\0';
 206                *(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
 207        } else {
 208                *(u32 *)dest = make_data_rloc(src - (u8 *)addr,
 209                                              get_rloc_offs(*(u32 *)dest));
 210        }
 211}
 212
 213/* Return the length of string -- including null terminal byte */
 214static __kprobes void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
 215                                                        void *addr, void *dest)
 216{
 217        mm_segment_t old_fs;
 218        int ret, len = 0;
 219        u8 c;
 220
 221        old_fs = get_fs();
 222        set_fs(KERNEL_DS);
 223        pagefault_disable();
 224
 225        do {
 226                ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
 227                len++;
 228        } while (c && ret == 0 && len < MAX_STRING_SIZE);
 229
 230        pagefault_enable();
 231        set_fs(old_fs);
 232
 233        if (ret < 0)    /* Failed to check the length */
 234                *(u32 *)dest = 0;
 235        else
 236                *(u32 *)dest = len;
 237}
 238
 239#define DEFINE_FETCH_symbol(type)                                       \
 240__kprobes void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs,      \
 241                                          void *data, void *dest)       \
 242{                                                                       \
 243        struct symbol_cache *sc = data;                                 \
 244        if (sc->addr)                                                   \
 245                fetch_memory_##type(regs, (void *)sc->addr, dest);      \
 246        else                                                            \
 247                *(type *)dest = 0;                                      \
 248}
 249DEFINE_BASIC_FETCH_FUNCS(symbol)
 250DEFINE_FETCH_symbol(string)
 251DEFINE_FETCH_symbol(string_size)
 252
 253/* kprobes don't support file_offset fetch methods */
 254#define fetch_file_offset_u8            NULL
 255#define fetch_file_offset_u16           NULL
 256#define fetch_file_offset_u32           NULL
 257#define fetch_file_offset_u64           NULL
 258#define fetch_file_offset_string        NULL
 259#define fetch_file_offset_string_size   NULL
 260
 261/* Fetch type information table */
 262const struct fetch_type kprobes_fetch_type_table[] = {
 263        /* Special types */
 264        [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
 265                                        sizeof(u32), 1, "__data_loc char[]"),
 266        [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
 267                                        string_size, sizeof(u32), 0, "u32"),
 268        /* Basic types */
 269        ASSIGN_FETCH_TYPE(u8,  u8,  0),
 270        ASSIGN_FETCH_TYPE(u16, u16, 0),
 271        ASSIGN_FETCH_TYPE(u32, u32, 0),
 272        ASSIGN_FETCH_TYPE(u64, u64, 0),
 273        ASSIGN_FETCH_TYPE(s8,  u8,  1),
 274        ASSIGN_FETCH_TYPE(s16, u16, 1),
 275        ASSIGN_FETCH_TYPE(s32, u32, 1),
 276        ASSIGN_FETCH_TYPE(s64, u64, 1),
 277
 278        ASSIGN_FETCH_TYPE_END
 279};
 280
 281/*
 282 * Allocate new trace_probe and initialize it (including kprobes).
 283 */
 284static struct trace_probe *alloc_trace_probe(const char *group,
 285                                             const char *event,
 286                                             void *addr,
 287                                             const char *symbol,
 288                                             unsigned long offs,
 289                                             int nargs, bool is_return)
 290{
 291        struct trace_probe *tp;
 292        int ret = -ENOMEM;
 293
 294        tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
 295        if (!tp)
 296                return ERR_PTR(ret);
 297
 298        if (symbol) {
 299                tp->symbol = kstrdup(symbol, GFP_KERNEL);
 300                if (!tp->symbol)
 301                        goto error;
 302                tp->rp.kp.symbol_name = tp->symbol;
 303                tp->rp.kp.offset = offs;
 304        } else
 305                tp->rp.kp.addr = addr;
 306
 307        if (is_return)
 308                tp->rp.handler = kretprobe_dispatcher;
 309        else
 310                tp->rp.kp.pre_handler = kprobe_dispatcher;
 311
 312        if (!event || !is_good_name(event)) {
 313                ret = -EINVAL;
 314                goto error;
 315        }
 316
 317        tp->call.class = &tp->class;
 318        tp->call.name = kstrdup(event, GFP_KERNEL);
 319        if (!tp->call.name)
 320                goto error;
 321
 322        if (!group || !is_good_name(group)) {
 323                ret = -EINVAL;
 324                goto error;
 325        }
 326
 327        tp->class.system = kstrdup(group, GFP_KERNEL);
 328        if (!tp->class.system)
 329                goto error;
 330
 331        INIT_LIST_HEAD(&tp->list);
 332        return tp;
 333error:
 334        kfree(tp->call.name);
 335        kfree(tp->symbol);
 336        kfree(tp);
 337        return ERR_PTR(ret);
 338}
 339
 340static void free_trace_probe(struct trace_probe *tp)
 341{
 342        int i;
 343
 344        for (i = 0; i < tp->nr_args; i++)
 345                traceprobe_free_probe_arg(&tp->args[i]);
 346
 347        kfree(tp->call.class->system);
 348        kfree(tp->call.name);
 349        kfree(tp->symbol);
 350        kfree(tp);
 351}
 352
 353static struct trace_probe *find_trace_probe(const char *event,
 354                                            const char *group)
 355{
 356        struct trace_probe *tp;
 357
 358        list_for_each_entry(tp, &probe_list, list)
 359                if (strcmp(tp->call.name, event) == 0 &&
 360                    strcmp(tp->call.class->system, group) == 0)
 361                        return tp;
 362        return NULL;
 363}
 364
 365static int trace_probe_nr_files(struct trace_probe *tp)
 366{
 367        struct ftrace_event_file **file;
 368        int ret = 0;
 369
 370        /*
 371         * Since all tp->files updater is protected by probe_enable_lock,
 372         * we don't need to lock an rcu_read_lock.
 373         */
 374        file = rcu_dereference_raw(tp->files);
 375        if (file)
 376                while (*(file++))
 377                        ret++;
 378
 379        return ret;
 380}
 381
 382static DEFINE_MUTEX(probe_enable_lock);
 383
 384/*
 385 * Enable trace_probe
 386 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
 387 */
 388static int
 389enable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
 390{
 391        int ret = 0;
 392
 393        mutex_lock(&probe_enable_lock);
 394
 395        if (file) {
 396                struct ftrace_event_file **new, **old;
 397                int n = trace_probe_nr_files(tp);
 398
 399                old = rcu_dereference_raw(tp->files);
 400                /* 1 is for new one and 1 is for stopper */
 401                new = kzalloc((n + 2) * sizeof(struct ftrace_event_file *),
 402                              GFP_KERNEL);
 403                if (!new) {
 404                        ret = -ENOMEM;
 405                        goto out_unlock;
 406                }
 407                memcpy(new, old, n * sizeof(struct ftrace_event_file *));
 408                new[n] = file;
 409                /* The last one keeps a NULL */
 410
 411                rcu_assign_pointer(tp->files, new);
 412                tp->flags |= TP_FLAG_TRACE;
 413
 414                if (old) {
 415                        /* Make sure the probe is done with old files */
 416                        synchronize_sched();
 417                        kfree(old);
 418                }
 419        } else
 420                tp->flags |= TP_FLAG_PROFILE;
 421
 422        if (trace_probe_is_enabled(tp) && trace_probe_is_registered(tp) &&
 423            !trace_probe_has_gone(tp)) {
 424                if (trace_probe_is_return(tp))
 425                        ret = enable_kretprobe(&tp->rp);
 426                else
 427                        ret = enable_kprobe(&tp->rp.kp);
 428        }
 429
 430 out_unlock:
 431        mutex_unlock(&probe_enable_lock);
 432
 433        return ret;
 434}
 435
 436static int
 437trace_probe_file_index(struct trace_probe *tp, struct ftrace_event_file *file)
 438{
 439        struct ftrace_event_file **files;
 440        int i;
 441
 442        /*
 443         * Since all tp->files updater is protected by probe_enable_lock,
 444         * we don't need to lock an rcu_read_lock.
 445         */
 446        files = rcu_dereference_raw(tp->files);
 447        if (files) {
 448                for (i = 0; files[i]; i++)
 449                        if (files[i] == file)
 450                                return i;
 451        }
 452
 453        return -1;
 454}
 455
 456/*
 457 * Disable trace_probe
 458 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
 459 */
 460static int
 461disable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
 462{
 463        struct ftrace_event_file **old = NULL;
 464        int wait = 0;
 465        int ret = 0;
 466
 467        mutex_lock(&probe_enable_lock);
 468
 469        if (file) {
 470                struct ftrace_event_file **new, **old;
 471                int n = trace_probe_nr_files(tp);
 472                int i, j;
 473
 474                old = rcu_dereference_raw(tp->files);
 475                if (n == 0 || trace_probe_file_index(tp, file) < 0) {
 476                        ret = -EINVAL;
 477                        goto out_unlock;
 478                }
 479
 480                if (n == 1) {   /* Remove the last file */
 481                        tp->flags &= ~TP_FLAG_TRACE;
 482                        new = NULL;
 483                } else {
 484                        new = kzalloc(n * sizeof(struct ftrace_event_file *),
 485                                      GFP_KERNEL);
 486                        if (!new) {
 487                                ret = -ENOMEM;
 488                                goto out_unlock;
 489                        }
 490
 491                        /* This copy & check loop copies the NULL stopper too */
 492                        for (i = 0, j = 0; j < n && i < n + 1; i++)
 493                                if (old[i] != file)
 494                                        new[j++] = old[i];
 495                }
 496
 497                rcu_assign_pointer(tp->files, new);
 498                wait = 1;
 499        } else
 500                tp->flags &= ~TP_FLAG_PROFILE;
 501
 502        if (!trace_probe_is_enabled(tp) && trace_probe_is_registered(tp)) {
 503                if (trace_probe_is_return(tp))
 504                        disable_kretprobe(&tp->rp);
 505                else
 506                        disable_kprobe(&tp->rp.kp);
 507                wait = 1;
 508        }
 509
 510 out_unlock:
 511        mutex_unlock(&probe_enable_lock);
 512
 513        if (wait) {
 514                /*
 515                 * Synchronize with kprobe_trace_func/kretprobe_trace_func
 516                 * to ensure disabled (all running handlers are finished).
 517                 * This is not only for kfree(), but also the caller,
 518                 * trace_remove_event_call() supposes it for releasing
 519                 * event_call related objects, which will be accessed in
 520                 * the kprobe_trace_func/kretprobe_trace_func.
 521                 */
 522                synchronize_sched();
 523                kfree(old);     /* Ignored if link == NULL */
 524        }
 525
 526        return ret;
 527}
 528
 529/* Internal register function - just handle k*probes and flags */
 530static int __register_trace_probe(struct trace_probe *tp)
 531{
 532        int i, ret;
 533
 534        if (trace_probe_is_registered(tp))
 535                return -EINVAL;
 536
 537        for (i = 0; i < tp->nr_args; i++)
 538                traceprobe_update_arg(&tp->args[i]);
 539
 540        /* Set/clear disabled flag according to tp->flag */
 541        if (trace_probe_is_enabled(tp))
 542                tp->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
 543        else
 544                tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
 545
 546        if (trace_probe_is_return(tp))
 547                ret = register_kretprobe(&tp->rp);
 548        else
 549                ret = register_kprobe(&tp->rp.kp);
 550
 551        if (ret == 0)
 552                tp->flags |= TP_FLAG_REGISTERED;
 553        else {
 554                pr_warning("Could not insert probe at %s+%lu: %d\n",
 555                           trace_probe_symbol(tp), trace_probe_offset(tp), ret);
 556                if (ret == -ENOENT && trace_probe_is_on_module(tp)) {
 557                        pr_warning("This probe might be able to register after"
 558                                   "target module is loaded. Continue.\n");
 559                        ret = 0;
 560                } else if (ret == -EILSEQ) {
 561                        pr_warning("Probing address(0x%p) is not an "
 562                                   "instruction boundary.\n",
 563                                   tp->rp.kp.addr);
 564                        ret = -EINVAL;
 565                }
 566        }
 567
 568        return ret;
 569}
 570
 571/* Internal unregister function - just handle k*probes and flags */
 572static void __unregister_trace_probe(struct trace_probe *tp)
 573{
 574        if (trace_probe_is_registered(tp)) {
 575                if (trace_probe_is_return(tp))
 576                        unregister_kretprobe(&tp->rp);
 577                else
 578                        unregister_kprobe(&tp->rp.kp);
 579                tp->flags &= ~TP_FLAG_REGISTERED;
 580                /* Cleanup kprobe for reuse */
 581                if (tp->rp.kp.symbol_name)
 582                        tp->rp.kp.addr = NULL;
 583        }
 584}
 585
 586/* Unregister a trace_probe and probe_event: call with locking probe_lock */
 587static int unregister_trace_probe(struct trace_probe *tp)
 588{
 589        /* Enabled event can not be unregistered */
 590        if (trace_probe_is_enabled(tp))
 591                return -EBUSY;
 592
 593        /* Will fail if probe is being used by ftrace or perf */
 594        if (unregister_probe_event(tp))
 595                return -EBUSY;
 596
 597        __unregister_trace_probe(tp);
 598        list_del(&tp->list);
 599
 600        return 0;
 601}
 602
 603/* Register a trace_probe and probe_event */
 604static int register_trace_probe(struct trace_probe *tp)
 605{
 606        struct trace_probe *old_tp;
 607        int ret;
 608
 609        mutex_lock(&probe_lock);
 610
 611        /* Delete old (same name) event if exist */
 612        old_tp = find_trace_probe(tp->call.name, tp->call.class->system);
 613        if (old_tp) {
 614                ret = unregister_trace_probe(old_tp);
 615                if (ret < 0)
 616                        goto end;
 617                free_trace_probe(old_tp);
 618        }
 619
 620        /* Register new event */
 621        ret = register_probe_event(tp);
 622        if (ret) {
 623                pr_warning("Failed to register probe event(%d)\n", ret);
 624                goto end;
 625        }
 626
 627        /* Register k*probe */
 628        ret = __register_trace_probe(tp);
 629        if (ret < 0)
 630                unregister_probe_event(tp);
 631        else
 632                list_add_tail(&tp->list, &probe_list);
 633
 634end:
 635        mutex_unlock(&probe_lock);
 636        return ret;
 637}
 638
 639/* Module notifier call back, checking event on the module */
 640static int trace_probe_module_callback(struct notifier_block *nb,
 641                                       unsigned long val, void *data)
 642{
 643        struct module *mod = data;
 644        struct trace_probe *tp;
 645        int ret;
 646
 647        if (val != MODULE_STATE_COMING)
 648                return NOTIFY_DONE;
 649
 650        /* Update probes on coming module */
 651        mutex_lock(&probe_lock);
 652        list_for_each_entry(tp, &probe_list, list) {
 653                if (trace_probe_within_module(tp, mod)) {
 654                        /* Don't need to check busy - this should have gone. */
 655                        __unregister_trace_probe(tp);
 656                        ret = __register_trace_probe(tp);
 657                        if (ret)
 658                                pr_warning("Failed to re-register probe %s on"
 659                                           "%s: %d\n",
 660                                           tp->call.name, mod->name, ret);
 661                }
 662        }
 663        mutex_unlock(&probe_lock);
 664
 665        return NOTIFY_DONE;
 666}
 667
 668static struct notifier_block trace_probe_module_nb = {
 669        .notifier_call = trace_probe_module_callback,
 670        .priority = 1   /* Invoked after kprobe module callback */
 671};
 672
 673static int create_trace_probe(int argc, char **argv)
 674{
 675        /*
 676         * Argument syntax:
 677         *  - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
 678         *  - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
 679         * Fetch args:
 680         *  $retval     : fetch return value
 681         *  $stack      : fetch stack address
 682         *  $stackN     : fetch Nth of stack (N:0-)
 683         *  @ADDR       : fetch memory at ADDR (ADDR should be in kernel)
 684         *  @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
 685         *  %REG        : fetch register REG
 686         * Dereferencing memory fetch:
 687         *  +|-offs(ARG) : fetch memory at ARG +|- offs address.
 688         * Alias name of args:
 689         *  NAME=FETCHARG : set NAME as alias of FETCHARG.
 690         * Type of args:
 691         *  FETCHARG:TYPE : use TYPE instead of unsigned long.
 692         */
 693        struct trace_probe *tp;
 694        int i, ret = 0;
 695        bool is_return = false, is_delete = false;
 696        char *symbol = NULL, *event = NULL, *group = NULL;
 697        char *arg;
 698        unsigned long offset = 0;
 699        void *addr = NULL;
 700        char buf[MAX_EVENT_NAME_LEN];
 701
 702        /* argc must be >= 1 */
 703        if (argv[0][0] == 'p')
 704                is_return = false;
 705        else if (argv[0][0] == 'r')
 706                is_return = true;
 707        else if (argv[0][0] == '-')
 708                is_delete = true;
 709        else {
 710                pr_info("Probe definition must be started with 'p', 'r' or"
 711                        " '-'.\n");
 712                return -EINVAL;
 713        }
 714
 715        if (argv[0][1] == ':') {
 716                event = &argv[0][2];
 717                if (strchr(event, '/')) {
 718                        group = event;
 719                        event = strchr(group, '/') + 1;
 720                        event[-1] = '\0';
 721                        if (strlen(group) == 0) {
 722                                pr_info("Group name is not specified\n");
 723                                return -EINVAL;
 724                        }
 725                }
 726                if (strlen(event) == 0) {
 727                        pr_info("Event name is not specified\n");
 728                        return -EINVAL;
 729                }
 730        }
 731        if (!group)
 732                group = KPROBE_EVENT_SYSTEM;
 733
 734        if (is_delete) {
 735                if (!event) {
 736                        pr_info("Delete command needs an event name.\n");
 737                        return -EINVAL;
 738                }
 739                mutex_lock(&probe_lock);
 740                tp = find_trace_probe(event, group);
 741                if (!tp) {
 742                        mutex_unlock(&probe_lock);
 743                        pr_info("Event %s/%s doesn't exist.\n", group, event);
 744                        return -ENOENT;
 745                }
 746                /* delete an event */
 747                ret = unregister_trace_probe(tp);
 748                if (ret == 0)
 749                        free_trace_probe(tp);
 750                mutex_unlock(&probe_lock);
 751                return ret;
 752        }
 753
 754        if (argc < 2) {
 755                pr_info("Probe point is not specified.\n");
 756                return -EINVAL;
 757        }
 758
 759        /* try to parse an address. if that fails, try to read the
 760         * input as a symbol. */
 761        if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
 762                /* a symbol specified */
 763                symbol = argv[1];
 764                /* TODO: support .init module functions */
 765                ret = traceprobe_split_symbol_offset(symbol, &offset);
 766                if (ret) {
 767                        pr_info("Failed to parse either an address or a symbol.\n");
 768                        return ret;
 769                }
 770        }
 771        argc -= 2; argv += 2;
 772
 773        /* setup a probe */
 774        if (!event) {
 775                /* Make a new event name */
 776                if (symbol)
 777                        snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
 778                                 is_return ? 'r' : 'p', symbol, offset);
 779                else
 780                        snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
 781                                 is_return ? 'r' : 'p', addr);
 782                event = buf;
 783        }
 784        tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
 785                               is_return);
 786        if (IS_ERR(tp)) {
 787                pr_info("Failed to allocate trace_probe.(%d)\n",
 788                        (int)PTR_ERR(tp));
 789                return PTR_ERR(tp);
 790        }
 791
 792        /* parse arguments */
 793        ret = 0;
 794        for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
 795                /* Increment count for freeing args in error case */
 796                tp->nr_args++;
 797
 798                /* Parse argument name */
 799                arg = strchr(argv[i], '=');
 800                if (arg) {
 801                        *arg++ = '\0';
 802                        tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
 803                } else {
 804                        arg = argv[i];
 805                        /* If argument name is omitted, set "argN" */
 806                        snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
 807                        tp->args[i].name = kstrdup(buf, GFP_KERNEL);
 808                }
 809
 810                if (!tp->args[i].name) {
 811                        pr_info("Failed to allocate argument[%d] name.\n", i);
 812                        ret = -ENOMEM;
 813                        goto error;
 814                }
 815
 816                if (!is_good_name(tp->args[i].name)) {
 817                        pr_info("Invalid argument[%d] name: %s\n",
 818                                i, tp->args[i].name);
 819                        ret = -EINVAL;
 820                        goto error;
 821                }
 822
 823                if (traceprobe_conflict_field_name(tp->args[i].name,
 824                                                        tp->args, i)) {
 825                        pr_info("Argument[%d] name '%s' conflicts with "
 826                                "another field.\n", i, argv[i]);
 827                        ret = -EINVAL;
 828                        goto error;
 829                }
 830
 831                /* Parse fetch argument */
 832                ret = traceprobe_parse_probe_arg(arg, &tp->size, &tp->args[i],
 833                                                is_return, true);
 834                if (ret) {
 835                        pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
 836                        goto error;
 837                }
 838        }
 839
 840        ret = register_trace_probe(tp);
 841        if (ret)
 842                goto error;
 843        return 0;
 844
 845error:
 846        free_trace_probe(tp);
 847        return ret;
 848}
 849
 850static int release_all_trace_probes(void)
 851{
 852        struct trace_probe *tp;
 853        int ret = 0;
 854
 855        mutex_lock(&probe_lock);
 856        /* Ensure no probe is in use. */
 857        list_for_each_entry(tp, &probe_list, list)
 858                if (trace_probe_is_enabled(tp)) {
 859                        ret = -EBUSY;
 860                        goto end;
 861                }
 862        /* TODO: Use batch unregistration */
 863        while (!list_empty(&probe_list)) {
 864                tp = list_entry(probe_list.next, struct trace_probe, list);
 865                ret = unregister_trace_probe(tp);
 866                if (ret)
 867                        goto end;
 868                free_trace_probe(tp);
 869        }
 870
 871end:
 872        mutex_unlock(&probe_lock);
 873
 874        return ret;
 875}
 876
 877/* Probes listing interfaces */
 878static void *probes_seq_start(struct seq_file *m, loff_t *pos)
 879{
 880        mutex_lock(&probe_lock);
 881        return seq_list_start(&probe_list, *pos);
 882}
 883
 884static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
 885{
 886        return seq_list_next(v, &probe_list, pos);
 887}
 888
 889static void probes_seq_stop(struct seq_file *m, void *v)
 890{
 891        mutex_unlock(&probe_lock);
 892}
 893
 894static int probes_seq_show(struct seq_file *m, void *v)
 895{
 896        struct trace_probe *tp = v;
 897        int i;
 898
 899        seq_printf(m, "%c", trace_probe_is_return(tp) ? 'r' : 'p');
 900        seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
 901
 902        if (!tp->symbol)
 903                seq_printf(m, " 0x%p", tp->rp.kp.addr);
 904        else if (tp->rp.kp.offset)
 905                seq_printf(m, " %s+%u", trace_probe_symbol(tp),
 906                           tp->rp.kp.offset);
 907        else
 908                seq_printf(m, " %s", trace_probe_symbol(tp));
 909
 910        for (i = 0; i < tp->nr_args; i++)
 911                seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
 912        seq_printf(m, "\n");
 913
 914        return 0;
 915}
 916
 917static const struct seq_operations probes_seq_op = {
 918        .start  = probes_seq_start,
 919        .next   = probes_seq_next,
 920        .stop   = probes_seq_stop,
 921        .show   = probes_seq_show
 922};
 923
 924static int probes_open(struct inode *inode, struct file *file)
 925{
 926        int ret;
 927
 928        if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
 929                ret = release_all_trace_probes();
 930                if (ret < 0)
 931                        return ret;
 932        }
 933
 934        return seq_open(file, &probes_seq_op);
 935}
 936
 937static ssize_t probes_write(struct file *file, const char __user *buffer,
 938                            size_t count, loff_t *ppos)
 939{
 940        return traceprobe_probes_write(file, buffer, count, ppos,
 941                        create_trace_probe);
 942}
 943
 944static const struct file_operations kprobe_events_ops = {
 945        .owner          = THIS_MODULE,
 946        .open           = probes_open,
 947        .read           = seq_read,
 948        .llseek         = seq_lseek,
 949        .release        = seq_release,
 950        .write          = probes_write,
 951};
 952
 953/* Probes profiling interfaces */
 954static int probes_profile_seq_show(struct seq_file *m, void *v)
 955{
 956        struct trace_probe *tp = v;
 957
 958        seq_printf(m, "  %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
 959                   tp->rp.kp.nmissed);
 960
 961        return 0;
 962}
 963
 964static const struct seq_operations profile_seq_op = {
 965        .start  = probes_seq_start,
 966        .next   = probes_seq_next,
 967        .stop   = probes_seq_stop,
 968        .show   = probes_profile_seq_show
 969};
 970
 971static int profile_open(struct inode *inode, struct file *file)
 972{
 973        return seq_open(file, &profile_seq_op);
 974}
 975
 976static const struct file_operations kprobe_profile_ops = {
 977        .owner          = THIS_MODULE,
 978        .open           = profile_open,
 979        .read           = seq_read,
 980        .llseek         = seq_lseek,
 981        .release        = seq_release,
 982};
 983
 984/* Sum up total data length for dynamic arraies (strings) */
 985static __kprobes int __get_data_size(struct trace_probe *tp,
 986                                     struct pt_regs *regs)
 987{
 988        int i, ret = 0;
 989        u32 len;
 990
 991        for (i = 0; i < tp->nr_args; i++)
 992                if (unlikely(tp->args[i].fetch_size.fn)) {
 993                        call_fetch(&tp->args[i].fetch_size, regs, &len);
 994                        ret += len;
 995                }
 996
 997        return ret;
 998}
 999
1000/* Store the value of each argument */
1001static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp,
1002                                       struct pt_regs *regs,
1003                                       u8 *data, int maxlen)
1004{
1005        int i;
1006        u32 end = tp->size;
1007        u32 *dl;        /* Data (relative) location */
1008
1009        for (i = 0; i < tp->nr_args; i++) {
1010                if (unlikely(tp->args[i].fetch_size.fn)) {
1011                        /*
1012                         * First, we set the relative location and
1013                         * maximum data length to *dl
1014                         */
1015                        dl = (u32 *)(data + tp->args[i].offset);
1016                        *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
1017                        /* Then try to fetch string or dynamic array data */
1018                        call_fetch(&tp->args[i].fetch, regs, dl);
1019                        /* Reduce maximum length */
1020                        end += get_rloc_len(*dl);
1021                        maxlen -= get_rloc_len(*dl);
1022                        /* Trick here, convert data_rloc to data_loc */
1023                        *dl = convert_rloc_to_loc(*dl,
1024                                 ent_size + tp->args[i].offset);
1025                } else
1026                        /* Just fetching data normally */
1027                        call_fetch(&tp->args[i].fetch, regs,
1028                                   data + tp->args[i].offset);
1029        }
1030}
1031
1032/* Kprobe handler */
1033static __kprobes void
1034__kprobe_trace_func(struct trace_probe *tp, struct pt_regs *regs,
1035                    struct ftrace_event_file *ftrace_file)
1036{
1037        struct kprobe_trace_entry_head *entry;
1038        struct ring_buffer_event *event;
1039        struct ring_buffer *buffer;
1040        int size, dsize, pc;
1041        unsigned long irq_flags;
1042        struct ftrace_event_call *call = &tp->call;
1043
1044        WARN_ON(call != ftrace_file->event_call);
1045
1046        if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &ftrace_file->flags))
1047                return;
1048
1049        local_save_flags(irq_flags);
1050        pc = preempt_count();
1051
1052        dsize = __get_data_size(tp, regs);
1053        size = sizeof(*entry) + tp->size + dsize;
1054
1055        event = trace_event_buffer_lock_reserve(&buffer, ftrace_file,
1056                                                call->event.type,
1057                                                size, irq_flags, pc);
1058        if (!event)
1059                return;
1060
1061        entry = ring_buffer_event_data(event);
1062        entry->ip = (unsigned long)tp->rp.kp.addr;
1063        store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1064
1065        if (!filter_current_check_discard(buffer, call, entry, event))
1066                trace_buffer_unlock_commit_regs(buffer, event,
1067                                                irq_flags, pc, regs);
1068}
1069
1070static __kprobes void
1071kprobe_trace_func(struct trace_probe *tp, struct pt_regs *regs)
1072{
1073        /*
1074         * Note: preempt is already disabled around the kprobe handler.
1075         * However, we still need an smp_read_barrier_depends() corresponding
1076         * to smp_wmb() in rcu_assign_pointer() to access the pointer.
1077         */
1078        struct ftrace_event_file **file = rcu_dereference_raw(tp->files);
1079
1080        if (unlikely(!file))
1081                return;
1082
1083        while (*file) {
1084                __kprobe_trace_func(tp, regs, *file);
1085                file++;
1086        }
1087}
1088
1089/* Kretprobe handler */
1090static __kprobes void
1091__kretprobe_trace_func(struct trace_probe *tp, struct kretprobe_instance *ri,
1092                       struct pt_regs *regs,
1093                       struct ftrace_event_file *ftrace_file)
1094{
1095        struct kretprobe_trace_entry_head *entry;
1096        struct ring_buffer_event *event;
1097        struct ring_buffer *buffer;
1098        int size, pc, dsize;
1099        unsigned long irq_flags;
1100        struct ftrace_event_call *call = &tp->call;
1101
1102        WARN_ON(call != ftrace_file->event_call);
1103
1104        if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &ftrace_file->flags))
1105                return;
1106
1107        local_save_flags(irq_flags);
1108        pc = preempt_count();
1109
1110        dsize = __get_data_size(tp, regs);
1111        size = sizeof(*entry) + tp->size + dsize;
1112
1113        event = trace_event_buffer_lock_reserve(&buffer, ftrace_file,
1114                                                call->event.type,
1115                                                size, irq_flags, pc);
1116        if (!event)
1117                return;
1118
1119        entry = ring_buffer_event_data(event);
1120        entry->func = (unsigned long)tp->rp.kp.addr;
1121        entry->ret_ip = (unsigned long)ri->ret_addr;
1122        store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1123
1124        if (!filter_current_check_discard(buffer, call, entry, event))
1125                trace_buffer_unlock_commit_regs(buffer, event,
1126                                                irq_flags, pc, regs);
1127}
1128
1129static __kprobes void
1130kretprobe_trace_func(struct trace_probe *tp, struct kretprobe_instance *ri,
1131                     struct pt_regs *regs)
1132{
1133        /*
1134         * Note: preempt is already disabled around the kprobe handler.
1135         * However, we still need an smp_read_barrier_depends() corresponding
1136         * to smp_wmb() in rcu_assign_pointer() to access the pointer.
1137         */
1138        struct ftrace_event_file **file = rcu_dereference_raw(tp->files);
1139
1140        if (unlikely(!file))
1141                return;
1142
1143        while (*file) {
1144                __kretprobe_trace_func(tp, ri, regs, *file);
1145                file++;
1146        }
1147}
1148
1149/* Event entry printers */
1150static enum print_line_t
1151print_kprobe_event(struct trace_iterator *iter, int flags,
1152                   struct trace_event *event)
1153{
1154        struct kprobe_trace_entry_head *field;
1155        struct trace_seq *s = &iter->seq;
1156        struct trace_probe *tp;
1157        u8 *data;
1158        int i;
1159
1160        field = (struct kprobe_trace_entry_head *)iter->ent;
1161        tp = container_of(event, struct trace_probe, call.event);
1162
1163        if (!trace_seq_printf(s, "%s: (", tp->call.name))
1164                goto partial;
1165
1166        if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1167                goto partial;
1168
1169        if (!trace_seq_puts(s, ")"))
1170                goto partial;
1171
1172        data = (u8 *)&field[1];
1173        for (i = 0; i < tp->nr_args; i++)
1174                if (!tp->args[i].type->print(s, tp->args[i].name,
1175                                             data + tp->args[i].offset, field))
1176                        goto partial;
1177
1178        if (!trace_seq_puts(s, "\n"))
1179                goto partial;
1180
1181        return TRACE_TYPE_HANDLED;
1182partial:
1183        return TRACE_TYPE_PARTIAL_LINE;
1184}
1185
1186static enum print_line_t
1187print_kretprobe_event(struct trace_iterator *iter, int flags,
1188                      struct trace_event *event)
1189{
1190        struct kretprobe_trace_entry_head *field;
1191        struct trace_seq *s = &iter->seq;
1192        struct trace_probe *tp;
1193        u8 *data;
1194        int i;
1195
1196        field = (struct kretprobe_trace_entry_head *)iter->ent;
1197        tp = container_of(event, struct trace_probe, call.event);
1198
1199        if (!trace_seq_printf(s, "%s: (", tp->call.name))
1200                goto partial;
1201
1202        if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1203                goto partial;
1204
1205        if (!trace_seq_puts(s, " <- "))
1206                goto partial;
1207
1208        if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1209                goto partial;
1210
1211        if (!trace_seq_puts(s, ")"))
1212                goto partial;
1213
1214        data = (u8 *)&field[1];
1215        for (i = 0; i < tp->nr_args; i++)
1216                if (!tp->args[i].type->print(s, tp->args[i].name,
1217                                             data + tp->args[i].offset, field))
1218                        goto partial;
1219
1220        if (!trace_seq_puts(s, "\n"))
1221                goto partial;
1222
1223        return TRACE_TYPE_HANDLED;
1224partial:
1225        return TRACE_TYPE_PARTIAL_LINE;
1226}
1227
1228
1229static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
1230{
1231        int ret, i;
1232        struct kprobe_trace_entry_head field;
1233        struct trace_probe *tp = (struct trace_probe *)event_call->data;
1234
1235        DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1236        /* Set argument names as fields */
1237        for (i = 0; i < tp->nr_args; i++) {
1238                ret = trace_define_field(event_call, tp->args[i].type->fmttype,
1239                                         tp->args[i].name,
1240                                         sizeof(field) + tp->args[i].offset,
1241                                         tp->args[i].type->size,
1242                                         tp->args[i].type->is_signed,
1243                                         FILTER_OTHER);
1244                if (ret)
1245                        return ret;
1246        }
1247        return 0;
1248}
1249
1250static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
1251{
1252        int ret, i;
1253        struct kretprobe_trace_entry_head field;
1254        struct trace_probe *tp = (struct trace_probe *)event_call->data;
1255
1256        DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1257        DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1258        /* Set argument names as fields */
1259        for (i = 0; i < tp->nr_args; i++) {
1260                ret = trace_define_field(event_call, tp->args[i].type->fmttype,
1261                                         tp->args[i].name,
1262                                         sizeof(field) + tp->args[i].offset,
1263                                         tp->args[i].type->size,
1264                                         tp->args[i].type->is_signed,
1265                                         FILTER_OTHER);
1266                if (ret)
1267                        return ret;
1268        }
1269        return 0;
1270}
1271
1272static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
1273{
1274        int i;
1275        int pos = 0;
1276
1277        const char *fmt, *arg;
1278
1279        if (!trace_probe_is_return(tp)) {
1280                fmt = "(%lx)";
1281                arg = "REC->" FIELD_STRING_IP;
1282        } else {
1283                fmt = "(%lx <- %lx)";
1284                arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1285        }
1286
1287        /* When len=0, we just calculate the needed length */
1288#define LEN_OR_ZERO (len ? len - pos : 0)
1289
1290        pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1291
1292        for (i = 0; i < tp->nr_args; i++) {
1293                pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
1294                                tp->args[i].name, tp->args[i].type->fmt);
1295        }
1296
1297        pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
1298
1299        for (i = 0; i < tp->nr_args; i++) {
1300                if (strcmp(tp->args[i].type->name, "string") == 0)
1301                        pos += snprintf(buf + pos, LEN_OR_ZERO,
1302                                        ", __get_str(%s)",
1303                                        tp->args[i].name);
1304                else
1305                        pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
1306                                        tp->args[i].name);
1307        }
1308
1309#undef LEN_OR_ZERO
1310
1311        /* return the length of print_fmt */
1312        return pos;
1313}
1314
1315static int set_print_fmt(struct trace_probe *tp)
1316{
1317        int len;
1318        char *print_fmt;
1319
1320        /* First: called with 0 length to calculate the needed length */
1321        len = __set_print_fmt(tp, NULL, 0);
1322        print_fmt = kmalloc(len + 1, GFP_KERNEL);
1323        if (!print_fmt)
1324                return -ENOMEM;
1325
1326        /* Second: actually write the @print_fmt */
1327        __set_print_fmt(tp, print_fmt, len + 1);
1328        tp->call.print_fmt = print_fmt;
1329
1330        return 0;
1331}
1332
1333#ifdef CONFIG_PERF_EVENTS
1334
1335/* Kprobe profile handler */
1336static __kprobes void
1337kprobe_perf_func(struct trace_probe *tp, struct pt_regs *regs)
1338{
1339        struct ftrace_event_call *call = &tp->call;
1340        struct kprobe_trace_entry_head *entry;
1341        struct hlist_head *head;
1342        int size, __size, dsize;
1343        int rctx;
1344
1345        dsize = __get_data_size(tp, regs);
1346        __size = sizeof(*entry) + tp->size + dsize;
1347        size = ALIGN(__size + sizeof(u32), sizeof(u64));
1348        size -= sizeof(u32);
1349        if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
1350                     "profile buffer not large enough"))
1351                return;
1352
1353        entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
1354        if (!entry)
1355                return;
1356
1357        entry->ip = (unsigned long)tp->rp.kp.addr;
1358        memset(&entry[1], 0, dsize);
1359        store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1360
1361        head = this_cpu_ptr(call->perf_events);
1362        perf_trace_buf_submit(entry, size, rctx,
1363                                        entry->ip, 1, regs, head, NULL);
1364}
1365
1366/* Kretprobe profile handler */
1367static __kprobes void
1368kretprobe_perf_func(struct trace_probe *tp, struct kretprobe_instance *ri,
1369                    struct pt_regs *regs)
1370{
1371        struct ftrace_event_call *call = &tp->call;
1372        struct kretprobe_trace_entry_head *entry;
1373        struct hlist_head *head;
1374        int size, __size, dsize;
1375        int rctx;
1376
1377        dsize = __get_data_size(tp, regs);
1378        __size = sizeof(*entry) + tp->size + dsize;
1379        size = ALIGN(__size + sizeof(u32), sizeof(u64));
1380        size -= sizeof(u32);
1381        if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
1382                     "profile buffer not large enough"))
1383                return;
1384
1385        entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
1386        if (!entry)
1387                return;
1388
1389        entry->func = (unsigned long)tp->rp.kp.addr;
1390        entry->ret_ip = (unsigned long)ri->ret_addr;
1391        store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
1392
1393        head = this_cpu_ptr(call->perf_events);
1394        perf_trace_buf_submit(entry, size, rctx,
1395                                        entry->ret_ip, 1, regs, head, NULL);
1396}
1397#endif  /* CONFIG_PERF_EVENTS */
1398
1399static __kprobes
1400int kprobe_register(struct ftrace_event_call *event,
1401                    enum trace_reg type, void *data)
1402{
1403        struct trace_probe *tp = (struct trace_probe *)event->data;
1404        struct ftrace_event_file *file = data;
1405
1406        switch (type) {
1407        case TRACE_REG_REGISTER:
1408                return enable_trace_probe(tp, file);
1409        case TRACE_REG_UNREGISTER:
1410                return disable_trace_probe(tp, file);
1411
1412#ifdef CONFIG_PERF_EVENTS
1413        case TRACE_REG_PERF_REGISTER:
1414                return enable_trace_probe(tp, NULL);
1415        case TRACE_REG_PERF_UNREGISTER:
1416                return disable_trace_probe(tp, NULL);
1417        case TRACE_REG_PERF_OPEN:
1418        case TRACE_REG_PERF_CLOSE:
1419        case TRACE_REG_PERF_ADD:
1420        case TRACE_REG_PERF_DEL:
1421                return 0;
1422#endif
1423        }
1424        return 0;
1425}
1426
1427static __kprobes
1428int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1429{
1430        struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
1431
1432        tp->nhit++;
1433
1434        if (tp->flags & TP_FLAG_TRACE)
1435                kprobe_trace_func(tp, regs);
1436#ifdef CONFIG_PERF_EVENTS
1437        if (tp->flags & TP_FLAG_PROFILE)
1438                kprobe_perf_func(tp, regs);
1439#endif
1440        return 0;       /* We don't tweek kernel, so just return 0 */
1441}
1442
1443static __kprobes
1444int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1445{
1446        struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
1447
1448        tp->nhit++;
1449
1450        if (tp->flags & TP_FLAG_TRACE)
1451                kretprobe_trace_func(tp, ri, regs);
1452#ifdef CONFIG_PERF_EVENTS
1453        if (tp->flags & TP_FLAG_PROFILE)
1454                kretprobe_perf_func(tp, ri, regs);
1455#endif
1456        return 0;       /* We don't tweek kernel, so just return 0 */
1457}
1458
1459static struct trace_event_functions kretprobe_funcs = {
1460        .trace          = print_kretprobe_event
1461};
1462
1463static struct trace_event_functions kprobe_funcs = {
1464        .trace          = print_kprobe_event
1465};
1466
1467static int register_probe_event(struct trace_probe *tp)
1468{
1469        struct ftrace_event_call *call = &tp->call;
1470        int ret;
1471
1472        /* Initialize ftrace_event_call */
1473        INIT_LIST_HEAD(&call->class->fields);
1474        if (trace_probe_is_return(tp)) {
1475                call->event.funcs = &kretprobe_funcs;
1476                call->class->define_fields = kretprobe_event_define_fields;
1477        } else {
1478                call->event.funcs = &kprobe_funcs;
1479                call->class->define_fields = kprobe_event_define_fields;
1480        }
1481        if (set_print_fmt(tp) < 0)
1482                return -ENOMEM;
1483        ret = register_ftrace_event(&call->event);
1484        if (!ret) {
1485                kfree(call->print_fmt);
1486                return -ENODEV;
1487        }
1488        call->flags = 0;
1489        call->class->reg = kprobe_register;
1490        call->data = tp;
1491        ret = trace_add_event_call(call);
1492        if (ret) {
1493                pr_info("Failed to register kprobe event: %s\n", call->name);
1494                kfree(call->print_fmt);
1495                unregister_ftrace_event(&call->event);
1496        }
1497        return ret;
1498}
1499
1500static int unregister_probe_event(struct trace_probe *tp)
1501{
1502        int ret;
1503
1504        /* tp->event is unregistered in trace_remove_event_call() */
1505        ret = trace_remove_event_call(&tp->call);
1506        if (!ret)
1507                kfree(tp->call.print_fmt);
1508        return ret;
1509}
1510
1511/* Make a debugfs interface for controlling probe points */
1512static __init int init_kprobe_trace(void)
1513{
1514        struct dentry *d_tracer;
1515        struct dentry *entry;
1516
1517        if (register_module_notifier(&trace_probe_module_nb))
1518                return -EINVAL;
1519
1520        d_tracer = tracing_init_dentry();
1521        if (!d_tracer)
1522                return 0;
1523
1524        entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
1525                                    NULL, &kprobe_events_ops);
1526
1527        /* Event list interface */
1528        if (!entry)
1529                pr_warning("Could not create debugfs "
1530                           "'kprobe_events' entry\n");
1531
1532        /* Profile interface */
1533        entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
1534                                    NULL, &kprobe_profile_ops);
1535
1536        if (!entry)
1537                pr_warning("Could not create debugfs "
1538                           "'kprobe_profile' entry\n");
1539        return 0;
1540}
1541fs_initcall(init_kprobe_trace);
1542
1543
1544#ifdef CONFIG_FTRACE_STARTUP_TEST
1545
1546/*
1547 * The "__used" keeps gcc from removing the function symbol
1548 * from the kallsyms table.
1549 */
1550static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
1551                                               int a4, int a5, int a6)
1552{
1553        return a1 + a2 + a3 + a4 + a5 + a6;
1554}
1555
1556static struct ftrace_event_file *
1557find_trace_probe_file(struct trace_probe *tp, struct trace_array *tr)
1558{
1559        struct ftrace_event_file *file;
1560
1561        list_for_each_entry(file, &tr->events, list)
1562                if (file->event_call == &tp->call)
1563                        return file;
1564
1565        return NULL;
1566}
1567
1568static __init int kprobe_trace_self_tests_init(void)
1569{
1570        int ret, warn = 0;
1571        int (*target)(int, int, int, int, int, int);
1572        struct trace_probe *tp;
1573        struct ftrace_event_file *file;
1574
1575        target = kprobe_trace_selftest_target;
1576
1577        pr_info("Testing kprobe tracing: ");
1578
1579        ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
1580                                  "$stack $stack0 +0($stack)",
1581                                  create_trace_probe);
1582        if (WARN_ON_ONCE(ret)) {
1583                pr_warn("error on probing function entry.\n");
1584                warn++;
1585        } else {
1586                /* Enable trace point */
1587                tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
1588                if (WARN_ON_ONCE(tp == NULL)) {
1589                        pr_warn("error on getting new probe.\n");
1590                        warn++;
1591                } else {
1592                        file = find_trace_probe_file(tp, top_trace_array());
1593                        if (WARN_ON_ONCE(file == NULL)) {
1594                                pr_warn("error on getting probe file.\n");
1595                                warn++;
1596                        } else
1597                                enable_trace_probe(tp, file);
1598                }
1599        }
1600
1601        ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
1602                                  "$retval", create_trace_probe);
1603        if (WARN_ON_ONCE(ret)) {
1604                pr_warn("error on probing function return.\n");
1605                warn++;
1606        } else {
1607                /* Enable trace point */
1608                tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
1609                if (WARN_ON_ONCE(tp == NULL)) {
1610                        pr_warn("error on getting 2nd new probe.\n");
1611                        warn++;
1612                } else {
1613                        file = find_trace_probe_file(tp, top_trace_array());
1614                        if (WARN_ON_ONCE(file == NULL)) {
1615                                pr_warn("error on getting probe file.\n");
1616                                warn++;
1617                        } else
1618                                enable_trace_probe(tp, file);
1619                }
1620        }
1621
1622        if (warn)
1623                goto end;
1624
1625        ret = target(1, 2, 3, 4, 5, 6);
1626
1627        /* Disable trace points before removing it */
1628        tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
1629        if (WARN_ON_ONCE(tp == NULL)) {
1630                pr_warn("error on getting test probe.\n");
1631                warn++;
1632        } else {
1633                file = find_trace_probe_file(tp, top_trace_array());
1634                if (WARN_ON_ONCE(file == NULL)) {
1635                        pr_warn("error on getting probe file.\n");
1636                        warn++;
1637                } else
1638                        disable_trace_probe(tp, file);
1639        }
1640
1641        tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
1642        if (WARN_ON_ONCE(tp == NULL)) {
1643                pr_warn("error on getting 2nd test probe.\n");
1644                warn++;
1645        } else {
1646                file = find_trace_probe_file(tp, top_trace_array());
1647                if (WARN_ON_ONCE(file == NULL)) {
1648                        pr_warn("error on getting probe file.\n");
1649                        warn++;
1650                } else
1651                        disable_trace_probe(tp, file);
1652        }
1653
1654        ret = traceprobe_command("-:testprobe", create_trace_probe);
1655        if (WARN_ON_ONCE(ret)) {
1656                pr_warn("error on deleting a probe.\n");
1657                warn++;
1658        }
1659
1660        ret = traceprobe_command("-:testprobe2", create_trace_probe);
1661        if (WARN_ON_ONCE(ret)) {
1662                pr_warn("error on deleting a probe.\n");
1663                warn++;
1664        }
1665
1666end:
1667        release_all_trace_probes();
1668        if (warn)
1669                pr_cont("NG: Some tests are failed. Please check them.\n");
1670        else
1671                pr_cont("OK\n");
1672        return 0;
1673}
1674
1675late_initcall(kprobe_trace_self_tests_init);
1676
1677#endif
1678