| 1 | /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1) |
| 2 | * |
| 3 | * lttng-context-pid.c |
| 4 | * |
| 5 | * LTTng PID 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 | |
| 18 | static |
| 19 | size_t pid_get_size(size_t offset) |
| 20 | { |
| 21 | size_t size = 0; |
| 22 | |
| 23 | size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); |
| 24 | size += sizeof(pid_t); |
| 25 | return size; |
| 26 | } |
| 27 | |
| 28 | static |
| 29 | void pid_record(struct lttng_ctx_field *field, |
| 30 | struct lib_ring_buffer_ctx *ctx, |
| 31 | struct lttng_channel *chan) |
| 32 | { |
| 33 | pid_t pid; |
| 34 | |
| 35 | pid = task_tgid_nr(current); |
| 36 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(pid)); |
| 37 | chan->ops->event_write(ctx, &pid, sizeof(pid)); |
| 38 | } |
| 39 | |
| 40 | static |
| 41 | void pid_get_value(struct lttng_ctx_field *field, |
| 42 | struct lttng_probe_ctx *lttng_probe_ctx, |
| 43 | union lttng_ctx_value *value) |
| 44 | { |
| 45 | value->s64 = task_tgid_nr(current); |
| 46 | } |
| 47 | |
| 48 | int lttng_add_pid_to_ctx(struct lttng_ctx **ctx) |
| 49 | { |
| 50 | struct lttng_ctx_field *field; |
| 51 | |
| 52 | field = lttng_append_context(ctx); |
| 53 | if (!field) |
| 54 | return -ENOMEM; |
| 55 | if (lttng_find_context(*ctx, "pid")) { |
| 56 | lttng_remove_context_field(ctx, field); |
| 57 | return -EEXIST; |
| 58 | } |
| 59 | field->event_field.name = "pid"; |
| 60 | field->event_field.type.atype = atype_integer; |
| 61 | field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT; |
| 62 | field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT; |
| 63 | field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t); |
| 64 | field->event_field.type.u.basic.integer.reverse_byte_order = 0; |
| 65 | field->event_field.type.u.basic.integer.base = 10; |
| 66 | field->event_field.type.u.basic.integer.encoding = lttng_encode_none; |
| 67 | field->get_size = pid_get_size; |
| 68 | field->record = pid_record; |
| 69 | field->get_value = pid_get_value; |
| 70 | lttng_context_update(*ctx); |
| 71 | wrapper_vmalloc_sync_mappings(); |
| 72 | return 0; |
| 73 | } |
| 74 | EXPORT_SYMBOL_GPL(lttng_add_pid_to_ctx); |