| 1 | /* |
| 2 | * (C) Copyright 2009-2011 - |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * LTTng UST procname context. |
| 6 | * |
| 7 | * Dual LGPL v2.1/GPL v2 license. |
| 8 | */ |
| 9 | |
| 10 | #include <sys/prctl.h> |
| 11 | #include <lttng/ust-events.h> |
| 12 | #include <lttng/ust-tracer.h> |
| 13 | #include <lttng/ringbuffer-config.h> |
| 14 | #include <assert.h> |
| 15 | |
| 16 | #define PROCNAME_LEN 17 /* includes \0 */ |
| 17 | |
| 18 | /* |
| 19 | * We cache the result to ensure we don't trigger a system call for |
| 20 | * each event. |
| 21 | * Upon exec, procname changes, but exec takes care of throwing away |
| 22 | * this cached version. |
| 23 | */ |
| 24 | static char cached_procname[17]; |
| 25 | |
| 26 | static inline |
| 27 | char *wrapper_getprocname(void) |
| 28 | { |
| 29 | int ret; |
| 30 | |
| 31 | if (caa_unlikely(!cached_procname[0])) { |
| 32 | ret = prctl(PR_GET_NAME, (unsigned long) cached_procname, |
| 33 | 0, 0, 0); |
| 34 | assert(!ret); |
| 35 | } |
| 36 | return cached_procname; |
| 37 | } |
| 38 | |
| 39 | void lttng_context_procname_reset(void) |
| 40 | { |
| 41 | cached_procname[0] = '\0'; |
| 42 | } |
| 43 | |
| 44 | static |
| 45 | size_t procname_get_size(size_t offset) |
| 46 | { |
| 47 | size_t size = 0; |
| 48 | |
| 49 | size += PROCNAME_LEN; |
| 50 | return size; |
| 51 | } |
| 52 | |
| 53 | static |
| 54 | void procname_record(struct lttng_ctx_field *field, |
| 55 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 56 | struct ltt_channel *chan) |
| 57 | { |
| 58 | char *procname; |
| 59 | |
| 60 | procname = wrapper_getprocname(); |
| 61 | chan->ops->event_write(ctx, procname, PROCNAME_LEN); |
| 62 | } |
| 63 | |
| 64 | int lttng_add_procname_to_ctx(struct lttng_ctx **ctx) |
| 65 | { |
| 66 | struct lttng_ctx_field *field; |
| 67 | |
| 68 | field = lttng_append_context(ctx); |
| 69 | if (!field) |
| 70 | return -ENOMEM; |
| 71 | if (lttng_find_context(*ctx, "procname")) { |
| 72 | lttng_remove_context_field(ctx, field); |
| 73 | return -EEXIST; |
| 74 | } |
| 75 | field->event_field.name = "procname"; |
| 76 | field->event_field.type.atype = atype_array; |
| 77 | field->event_field.type.u.array.elem_type.atype = atype_integer; |
| 78 | field->event_field.type.u.array.elem_type.u.basic.integer.size = sizeof(char) * CHAR_BIT; |
| 79 | field->event_field.type.u.array.elem_type.u.basic.integer.alignment = lttng_alignof(char) * CHAR_BIT; |
| 80 | field->event_field.type.u.array.elem_type.u.basic.integer.signedness = lttng_is_signed_type(char); |
| 81 | field->event_field.type.u.array.elem_type.u.basic.integer.reverse_byte_order = 0; |
| 82 | field->event_field.type.u.array.elem_type.u.basic.integer.base = 10; |
| 83 | field->event_field.type.u.array.elem_type.u.basic.integer.encoding = lttng_encode_UTF8; |
| 84 | field->event_field.type.u.array.length = PROCNAME_LEN; |
| 85 | field->get_size = procname_get_size; |
| 86 | field->record = procname_record; |
| 87 | return 0; |
| 88 | } |