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