| 1 | /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1) |
| 2 | * |
| 3 | * lttng-context-prio.c |
| 4 | * |
| 5 | * LTTng priority context. |
| 6 | * |
| 7 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <lttng-events.h> |
| 14 | #include <wrapper/ringbuffer/frontend_types.h> |
| 15 | #include <wrapper/vmalloc.h> |
| 16 | #include <wrapper/kallsyms.h> |
| 17 | #include <lttng-tracer.h> |
| 18 | |
| 19 | static |
| 20 | int (*wrapper_task_prio_sym)(struct task_struct *t); |
| 21 | |
| 22 | int wrapper_task_prio_init(void) |
| 23 | { |
| 24 | wrapper_task_prio_sym = (void *) kallsyms_lookup_funcptr("task_prio"); |
| 25 | if (!wrapper_task_prio_sym) { |
| 26 | printk(KERN_WARNING "LTTng: task_prio symbol lookup failed.\n"); |
| 27 | return -EINVAL; |
| 28 | } |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | * Canary function to check for 'task_prio()' at compile time. |
| 34 | * |
| 35 | * From 'include/linux/sched.h': |
| 36 | * |
| 37 | * extern int task_prio(const struct task_struct *p); |
| 38 | */ |
| 39 | __attribute__((unused)) static |
| 40 | int __canary__task_prio(const struct task_struct *p) |
| 41 | { |
| 42 | return task_prio(p); |
| 43 | } |
| 44 | |
| 45 | static |
| 46 | size_t prio_get_size(size_t offset) |
| 47 | { |
| 48 | size_t size = 0; |
| 49 | |
| 50 | size += lib_ring_buffer_align(offset, lttng_alignof(int)); |
| 51 | size += sizeof(int); |
| 52 | return size; |
| 53 | } |
| 54 | |
| 55 | static |
| 56 | void prio_record(struct lttng_ctx_field *field, |
| 57 | struct lib_ring_buffer_ctx *ctx, |
| 58 | struct lttng_channel *chan) |
| 59 | { |
| 60 | int prio; |
| 61 | |
| 62 | prio = wrapper_task_prio_sym(current); |
| 63 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(prio)); |
| 64 | chan->ops->event_write(ctx, &prio, sizeof(prio)); |
| 65 | } |
| 66 | |
| 67 | static |
| 68 | void prio_get_value(struct lttng_ctx_field *field, |
| 69 | struct lttng_probe_ctx *lttng_probe_ctx, |
| 70 | union lttng_ctx_value *value) |
| 71 | { |
| 72 | value->s64 = wrapper_task_prio_sym(current); |
| 73 | } |
| 74 | |
| 75 | int lttng_add_prio_to_ctx(struct lttng_ctx **ctx) |
| 76 | { |
| 77 | struct lttng_ctx_field *field; |
| 78 | int ret; |
| 79 | |
| 80 | if (!wrapper_task_prio_sym) { |
| 81 | ret = wrapper_task_prio_init(); |
| 82 | if (ret) |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | field = lttng_append_context(ctx); |
| 87 | if (!field) |
| 88 | return -ENOMEM; |
| 89 | if (lttng_find_context(*ctx, "prio")) { |
| 90 | lttng_remove_context_field(ctx, field); |
| 91 | return -EEXIST; |
| 92 | } |
| 93 | field->event_field.name = "prio"; |
| 94 | field->event_field.type.atype = atype_integer; |
| 95 | field->event_field.type.u.basic.integer.size = sizeof(int) * CHAR_BIT; |
| 96 | field->event_field.type.u.basic.integer.alignment = lttng_alignof(int) * CHAR_BIT; |
| 97 | field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(int); |
| 98 | field->event_field.type.u.basic.integer.reverse_byte_order = 0; |
| 99 | field->event_field.type.u.basic.integer.base = 10; |
| 100 | field->event_field.type.u.basic.integer.encoding = lttng_encode_none; |
| 101 | field->get_size = prio_get_size; |
| 102 | field->record = prio_record; |
| 103 | field->get_value = prio_get_value; |
| 104 | lttng_context_update(*ctx); |
| 105 | wrapper_vmalloc_sync_mappings(); |
| 106 | return 0; |
| 107 | } |
| 108 | EXPORT_SYMBOL_GPL(lttng_add_prio_to_ctx); |