Commit | Line | Data |
---|---|---|
4847e9bb | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
4847e9bb | 3 | * |
009745db | 4 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
e92f3e28 | 5 | * |
c0c0989a | 6 | * LTTng UST procname context. |
4847e9bb MD |
7 | */ |
8 | ||
3fbec7dc | 9 | #define _LGPL_SOURCE |
b4051ad8 | 10 | #include <stddef.h> |
4318ae1b MD |
11 | #include <lttng/ust-events.h> |
12 | #include <lttng/ust-tracer.h> | |
0466ac28 | 13 | #include <lttng/ringbuffer-context.h> |
8c90a710 | 14 | #include <urcu/tls-compat.h> |
4847e9bb | 15 | #include <assert.h> |
08114193 | 16 | #include "compat.h" |
4b4a1337 | 17 | #include "lttng-tracer-core.h" |
4847e9bb | 18 | |
fc80554e MJ |
19 | #include "context-internal.h" |
20 | ||
74011e88 MD |
21 | /* Maximum number of nesting levels for the procname cache. */ |
22 | #define PROCNAME_NESTING_MAX 2 | |
23 | ||
4847e9bb MD |
24 | /* |
25 | * We cache the result to ensure we don't trigger a system call for | |
26 | * each event. | |
27 | * Upon exec, procname changes, but exec takes care of throwing away | |
28 | * this cached version. | |
009745db MD |
29 | * The procname can also change by calling prctl(). The procname should |
30 | * be set for a thread before the first event is logged within this | |
31 | * thread. | |
4847e9bb | 32 | */ |
74011e88 MD |
33 | typedef char procname_array[PROCNAME_NESTING_MAX][17]; |
34 | ||
16adecf1 | 35 | static DEFINE_URCU_TLS(procname_array, cached_procname); |
4847e9bb | 36 | |
74011e88 MD |
37 | static DEFINE_URCU_TLS(int, procname_nesting); |
38 | ||
4847e9bb | 39 | static inline |
74c3f8e2 | 40 | const char *wrapper_getprocname(void) |
4847e9bb | 41 | { |
2c44512a | 42 | int nesting = CMM_LOAD_SHARED(URCU_TLS(procname_nesting)); |
74011e88 MD |
43 | |
44 | if (caa_unlikely(nesting >= PROCNAME_NESTING_MAX)) | |
45 | return "<unknown>"; | |
46 | if (caa_unlikely(!URCU_TLS(cached_procname)[nesting][0])) { | |
47 | CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting + 1); | |
2c44512a MD |
48 | /* Increment nesting before updating cache. */ |
49 | cmm_barrier(); | |
0db3d6ee MJ |
50 | lttng_pthread_getname_np(URCU_TLS(cached_procname)[nesting], LTTNG_UST_ABI_PROCNAME_LEN); |
51 | URCU_TLS(cached_procname)[nesting][LTTNG_UST_ABI_PROCNAME_LEN - 1] = '\0'; | |
2c44512a MD |
52 | /* Decrement nesting after updating cache. */ |
53 | cmm_barrier(); | |
74011e88 | 54 | CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting); |
4847e9bb | 55 | } |
74011e88 | 56 | return URCU_TLS(cached_procname)[nesting]; |
4847e9bb MD |
57 | } |
58 | ||
2c44512a | 59 | /* Reset should not be called from a signal handler. */ |
0af0bdb2 | 60 | void lttng_ust_context_procname_reset(void) |
4847e9bb | 61 | { |
2c44512a | 62 | CMM_STORE_SHARED(URCU_TLS(cached_procname)[1][0], '\0'); |
74011e88 | 63 | CMM_STORE_SHARED(URCU_TLS(procname_nesting), 1); |
2c44512a | 64 | CMM_STORE_SHARED(URCU_TLS(cached_procname)[0][0], '\0'); |
74011e88 | 65 | CMM_STORE_SHARED(URCU_TLS(procname_nesting), 0); |
4847e9bb MD |
66 | } |
67 | ||
68 | static | |
2208d8b5 MJ |
69 | size_t procname_get_size(struct lttng_ust_ctx_field *field __attribute__((unused)), |
70 | size_t offset __attribute__((unused))) | |
4847e9bb | 71 | { |
0db3d6ee | 72 | return LTTNG_UST_ABI_PROCNAME_LEN; |
4847e9bb MD |
73 | } |
74 | ||
75 | static | |
2208d8b5 | 76 | void procname_record(struct lttng_ust_ctx_field *field __attribute__((unused)), |
4cfec15c | 77 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
e7bc0ef6 | 78 | struct lttng_ust_channel_buffer *chan) |
4847e9bb | 79 | { |
74c3f8e2 | 80 | const char *procname; |
4847e9bb MD |
81 | |
82 | procname = wrapper_getprocname(); | |
8936b6c0 | 83 | chan->ops->event_write(ctx, procname, LTTNG_UST_ABI_PROCNAME_LEN, 1); |
4847e9bb MD |
84 | } |
85 | ||
77aa5901 | 86 | static |
2208d8b5 | 87 | void procname_get_value(struct lttng_ust_ctx_field *field __attribute__((unused)), |
daacdbfc | 88 | struct lttng_ust_ctx_value *value) |
77aa5901 | 89 | { |
6e9ac4ae | 90 | value->u.str = wrapper_getprocname(); |
77aa5901 MD |
91 | } |
92 | ||
daacdbfc | 93 | int lttng_add_procname_to_ctx(struct lttng_ust_ctx **ctx) |
4847e9bb | 94 | { |
daacdbfc | 95 | struct lttng_ust_ctx_field *field; |
a084756d MD |
96 | struct lttng_ust_type_common *type; |
97 | int ret; | |
4847e9bb | 98 | |
a084756d MD |
99 | type = lttng_ust_create_type_array_text(LTTNG_UST_ABI_PROCNAME_LEN); |
100 | if (!type) | |
4847e9bb | 101 | return -ENOMEM; |
a084756d MD |
102 | field = lttng_append_context(ctx); |
103 | if (!field) { | |
104 | ret = -ENOMEM; | |
105 | goto error_context; | |
106 | } | |
4847e9bb | 107 | if (lttng_find_context(*ctx, "procname")) { |
a084756d MD |
108 | ret = -EEXIST; |
109 | goto error_find_context; | |
110 | } | |
111 | field->event_field->name = strdup("procname"); | |
112 | if (!field->event_field->name) { | |
113 | ret = -ENOMEM; | |
114 | goto error_name; | |
4847e9bb | 115 | } |
a084756d | 116 | field->event_field->type = type; |
4847e9bb MD |
117 | field->get_size = procname_get_size; |
118 | field->record = procname_record; | |
77aa5901 | 119 | field->get_value = procname_get_value; |
b2cc986a | 120 | lttng_context_update(*ctx); |
4847e9bb | 121 | return 0; |
a084756d MD |
122 | |
123 | error_name: | |
124 | error_find_context: | |
125 | lttng_remove_context_field(ctx, field); | |
126 | error_context: | |
127 | lttng_ust_destroy_type(type); | |
128 | return ret; | |
4847e9bb | 129 | } |
009745db MD |
130 | |
131 | /* | |
132 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
133 | */ | |
134 | void lttng_fixup_procname_tls(void) | |
135 | { | |
8c90a710 | 136 | asm volatile ("" : : "m" (URCU_TLS(cached_procname)[0])); |
009745db | 137 | } |