| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * lttng-context-ppid.c |
| 4 | * |
| 5 | * LTTng PPID 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 <linux/syscalls.h> |
| 14 | #include <lttng/events.h> |
| 15 | #include <ringbuffer/frontend_types.h> |
| 16 | #include <wrapper/vmalloc.h> |
| 17 | #include <lttng/tracer.h> |
| 18 | |
| 19 | static |
| 20 | size_t ppid_get_size(size_t offset) |
| 21 | { |
| 22 | size_t size = 0; |
| 23 | |
| 24 | size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); |
| 25 | size += sizeof(pid_t); |
| 26 | return size; |
| 27 | } |
| 28 | |
| 29 | static |
| 30 | void ppid_record(struct lttng_ctx_field *field, |
| 31 | struct lib_ring_buffer_ctx *ctx, |
| 32 | struct lttng_channel *chan) |
| 33 | { |
| 34 | pid_t ppid; |
| 35 | |
| 36 | /* |
| 37 | * TODO: when we eventually add RCU subsystem instrumentation, |
| 38 | * taking the rcu read lock here will trigger RCU tracing |
| 39 | * recursively. We should modify the kernel synchronization so |
| 40 | * it synchronizes both for RCU and RCU sched, and rely on |
| 41 | * rcu_read_lock_sched_notrace. |
| 42 | */ |
| 43 | rcu_read_lock(); |
| 44 | ppid = task_tgid_nr(current->real_parent); |
| 45 | rcu_read_unlock(); |
| 46 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(ppid)); |
| 47 | chan->ops->event_write(ctx, &ppid, sizeof(ppid)); |
| 48 | } |
| 49 | |
| 50 | static |
| 51 | void ppid_get_value(struct lttng_ctx_field *field, |
| 52 | struct lttng_probe_ctx *lttng_probe_ctx, |
| 53 | union lttng_ctx_value *value) |
| 54 | { |
| 55 | pid_t ppid; |
| 56 | |
| 57 | /* |
| 58 | * TODO: when we eventually add RCU subsystem instrumentation, |
| 59 | * taking the rcu read lock here will trigger RCU tracing |
| 60 | * recursively. We should modify the kernel synchronization so |
| 61 | * it synchronizes both for RCU and RCU sched, and rely on |
| 62 | * rcu_read_lock_sched_notrace. |
| 63 | */ |
| 64 | rcu_read_lock(); |
| 65 | ppid = task_tgid_nr(current->real_parent); |
| 66 | rcu_read_unlock(); |
| 67 | value->s64 = ppid; |
| 68 | } |
| 69 | |
| 70 | int lttng_add_ppid_to_ctx(struct lttng_ctx **ctx) |
| 71 | { |
| 72 | struct lttng_ctx_field *field; |
| 73 | |
| 74 | field = lttng_append_context(ctx); |
| 75 | if (!field) |
| 76 | return -ENOMEM; |
| 77 | if (lttng_find_context(*ctx, "ppid")) { |
| 78 | lttng_remove_context_field(ctx, field); |
| 79 | return -EEXIST; |
| 80 | } |
| 81 | field->event_field.name = "ppid"; |
| 82 | field->event_field.type.atype = atype_integer; |
| 83 | field->event_field.type.u.integer.size = sizeof(pid_t) * CHAR_BIT; |
| 84 | field->event_field.type.u.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT; |
| 85 | field->event_field.type.u.integer.signedness = lttng_is_signed_type(pid_t); |
| 86 | field->event_field.type.u.integer.reverse_byte_order = 0; |
| 87 | field->event_field.type.u.integer.base = 10; |
| 88 | field->event_field.type.u.integer.encoding = lttng_encode_none; |
| 89 | field->get_size = ppid_get_size; |
| 90 | field->record = ppid_record; |
| 91 | field->get_value = ppid_get_value; |
| 92 | lttng_context_update(*ctx); |
| 93 | wrapper_vmalloc_sync_mappings(); |
| 94 | return 0; |
| 95 | } |
| 96 | EXPORT_SYMBOL_GPL(lttng_add_ppid_to_ctx); |