| 1 | /* |
| 2 | * (C) Copyright 2009-2011 - |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * LTTng kprobes integration module. |
| 6 | * |
| 7 | * Dual LGPL v2.1/GPL v2 license. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/kprobes.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include "../ltt-events.h" |
| 14 | #include "../wrapper/ringbuffer/frontend_types.h" |
| 15 | #include "../ltt-tracer.h" |
| 16 | |
| 17 | static |
| 18 | int lttng_kprobes_handler_pre(struct kprobe *p, struct pt_regs *regs) |
| 19 | { |
| 20 | struct ltt_event *event = |
| 21 | container_of(p, struct ltt_event, u.kprobe.kp); |
| 22 | struct ltt_channel *chan = event->chan; |
| 23 | struct lib_ring_buffer_ctx ctx; |
| 24 | int ret; |
| 25 | unsigned long data = (unsigned long) p->addr; |
| 26 | |
| 27 | if (!ACCESS_ONCE(chan->session->active)) |
| 28 | return 0; |
| 29 | lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, sizeof(data), |
| 30 | ltt_alignof(data), -1); |
| 31 | ret = chan->ops->event_reserve(&ctx); |
| 32 | if (ret < 0) |
| 33 | return 0; |
| 34 | lib_ring_buffer_align_ctx(&ctx, ltt_alignof(data)); |
| 35 | chan->ops->event_write(&ctx, &data, sizeof(data)); |
| 36 | chan->ops->event_commit(&ctx); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Create event description |
| 42 | */ |
| 43 | static |
| 44 | int lttng_create_kprobe_event(const char *name, struct ltt_event *event) |
| 45 | { |
| 46 | struct lttng_event_field *field; |
| 47 | struct lttng_event_desc *desc; |
| 48 | int ret; |
| 49 | |
| 50 | desc = kzalloc(sizeof(*event->desc), GFP_KERNEL); |
| 51 | if (!desc) |
| 52 | return -ENOMEM; |
| 53 | desc->name = kstrdup(name, GFP_KERNEL); |
| 54 | if (!desc->name) { |
| 55 | ret = -ENOMEM; |
| 56 | goto error_str; |
| 57 | } |
| 58 | desc->nr_fields = 1; |
| 59 | desc->fields = field = |
| 60 | kzalloc(1 * sizeof(struct lttng_event_field), GFP_KERNEL); |
| 61 | field->name = "ip"; |
| 62 | field->type.atype = atype_integer; |
| 63 | field->type.u.basic.integer.size = sizeof(unsigned long); |
| 64 | field->type.u.basic.integer.alignment = ltt_alignof(unsigned long); |
| 65 | field->type.u.basic.integer.signedness = 0; |
| 66 | field->type.u.basic.integer.reverse_byte_order = 0; |
| 67 | field->type.u.basic.integer.base = 16; |
| 68 | field->type.u.basic.integer.encoding = lttng_encode_none; |
| 69 | event->desc = desc; |
| 70 | |
| 71 | return 0; |
| 72 | |
| 73 | error_str: |
| 74 | kfree(desc); |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | int lttng_kprobes_register(const char *name, |
| 79 | const char *symbol_name, |
| 80 | uint64_t offset, |
| 81 | uint64_t addr, |
| 82 | struct ltt_event *event) |
| 83 | { |
| 84 | int ret; |
| 85 | |
| 86 | ret = lttng_create_kprobe_event(name, event); |
| 87 | if (ret) |
| 88 | goto error; |
| 89 | memset(&event->u.kprobe.kp, 0, sizeof(event->u.kprobe.kp)); |
| 90 | event->u.kprobe.kp.pre_handler = lttng_kprobes_handler_pre; |
| 91 | event->u.kprobe.symbol_name = |
| 92 | kzalloc(LTTNG_SYM_NAME_LEN * sizeof(char), |
| 93 | GFP_KERNEL); |
| 94 | if (!event->u.kprobe.symbol_name) { |
| 95 | ret = -ENOMEM; |
| 96 | goto name_error; |
| 97 | } |
| 98 | memcpy(event->u.kprobe.symbol_name, symbol_name, |
| 99 | LTTNG_SYM_NAME_LEN * sizeof(char)); |
| 100 | event->u.kprobe.kp.symbol_name = |
| 101 | event->u.kprobe.symbol_name; |
| 102 | event->u.kprobe.kp.offset = offset; |
| 103 | event->u.kprobe.kp.addr = (void *) addr; |
| 104 | ret = register_kprobe(&event->u.kprobe.kp); |
| 105 | if (ret) |
| 106 | goto register_error; |
| 107 | return 0; |
| 108 | |
| 109 | register_error: |
| 110 | kfree(event->u.kprobe.symbol_name); |
| 111 | name_error: |
| 112 | kfree(event->desc->name); |
| 113 | kfree(event->desc); |
| 114 | error: |
| 115 | return ret; |
| 116 | } |
| 117 | EXPORT_SYMBOL_GPL(lttng_kprobes_register); |
| 118 | |
| 119 | void lttng_kprobes_unregister(struct ltt_event *event) |
| 120 | { |
| 121 | unregister_kprobe(&event->u.kprobe.kp); |
| 122 | kfree(event->u.kprobe.symbol_name); |
| 123 | kfree(event->desc->name); |
| 124 | kfree(event->desc); |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(lttng_kprobes_unregister); |
| 127 | |
| 128 | MODULE_LICENSE("GPL and additional rights"); |
| 129 | MODULE_AUTHOR("Mathieu Desnoyers"); |
| 130 | MODULE_DESCRIPTION("Linux Trace Toolkit Kprobes Support"); |