| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * lttng-context-procname.c |
| 4 | * |
| 5 | * LTTng procname 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 <lttng-tracer.h> |
| 17 | #include <lttng-endian.h> |
| 18 | |
| 19 | static |
| 20 | size_t procname_get_size(size_t offset) |
| 21 | { |
| 22 | size_t size = 0; |
| 23 | |
| 24 | size += sizeof(current->comm); |
| 25 | return size; |
| 26 | } |
| 27 | |
| 28 | /* |
| 29 | * Racy read of procname. We simply copy its whole array size. |
| 30 | * Races with /proc/<task>/procname write only. |
| 31 | * Otherwise having to take a mutex for each event is cumbersome and |
| 32 | * could lead to crash in IRQ context and deadlock of the lockdep tracer. |
| 33 | */ |
| 34 | static |
| 35 | void procname_record(struct lttng_ctx_field *field, |
| 36 | struct lib_ring_buffer_ctx *ctx, |
| 37 | struct lttng_channel *chan) |
| 38 | { |
| 39 | chan->ops->event_write(ctx, current->comm, sizeof(current->comm)); |
| 40 | } |
| 41 | |
| 42 | static |
| 43 | void procname_get_value(struct lttng_ctx_field *field, |
| 44 | struct lttng_probe_ctx *lttng_probe_ctx, |
| 45 | union lttng_ctx_value *value) |
| 46 | { |
| 47 | value->str = current->comm; |
| 48 | } |
| 49 | |
| 50 | static const struct lttng_type procname_array_elem_type = |
| 51 | __type_integer(char, 0, 0, -1, __BYTE_ORDER, 10, UTF8); |
| 52 | |
| 53 | int lttng_add_procname_to_ctx(struct lttng_ctx **ctx) |
| 54 | { |
| 55 | struct lttng_ctx_field *field; |
| 56 | |
| 57 | field = lttng_append_context(ctx); |
| 58 | if (!field) |
| 59 | return -ENOMEM; |
| 60 | if (lttng_find_context(*ctx, "procname")) { |
| 61 | lttng_remove_context_field(ctx, field); |
| 62 | return -EEXIST; |
| 63 | } |
| 64 | field->event_field.name = "procname"; |
| 65 | field->event_field.type.atype = atype_array_nestable; |
| 66 | field->event_field.type.u.array_nestable.elem_type = &procname_array_elem_type; |
| 67 | field->event_field.type.u.array_nestable.length = sizeof(current->comm); |
| 68 | field->event_field.type.u.array_nestable.alignment = 0; |
| 69 | |
| 70 | field->get_size = procname_get_size; |
| 71 | field->record = procname_record; |
| 72 | field->get_value = procname_get_value; |
| 73 | lttng_context_update(*ctx); |
| 74 | wrapper_vmalloc_sync_all(); |
| 75 | return 0; |
| 76 | } |
| 77 | EXPORT_SYMBOL_GPL(lttng_add_procname_to_ctx); |