093b50c7c1444b60d791fee756b883b0c195d365
[lttng-ust.git] / src / lib / lttng-ust / lttng-context-procname.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng UST procname context.
7 */
8
9 #define _LGPL_SOURCE
10 #include <stddef.h>
11 #include <lttng/ust-events.h>
12 #include <lttng/ust-tracer.h>
13 #include <lttng/ust-ringbuffer-context.h>
14 #include <urcu/tls-compat.h>
15 #include <assert.h>
16 #include "common/compat/pthread.h"
17 #include "lttng-tracer-core.h"
18
19 #include "context-internal.h"
20
21 /* Maximum number of nesting levels for the procname cache. */
22 #define PROCNAME_NESTING_MAX 2
23
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.
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.
32 */
33 typedef char procname_array[PROCNAME_NESTING_MAX][17];
34
35 static DEFINE_URCU_TLS(procname_array, cached_procname);
36
37 static DEFINE_URCU_TLS(int, procname_nesting);
38
39 static inline
40 const char *wrapper_getprocname(void)
41 {
42 int nesting = CMM_LOAD_SHARED(URCU_TLS(procname_nesting));
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);
48 /* Increment nesting before updating cache. */
49 cmm_barrier();
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';
52 /* Decrement nesting after updating cache. */
53 cmm_barrier();
54 CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting);
55 }
56 return URCU_TLS(cached_procname)[nesting];
57 }
58
59 /* Reset should not be called from a signal handler. */
60 void lttng_ust_context_procname_reset(void)
61 {
62 CMM_STORE_SHARED(URCU_TLS(cached_procname)[1][0], '\0');
63 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 1);
64 CMM_STORE_SHARED(URCU_TLS(cached_procname)[0][0], '\0');
65 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 0);
66 }
67
68 static
69 size_t procname_get_size(void *priv __attribute__((unused)),
70 size_t offset __attribute__((unused)))
71 {
72 return LTTNG_UST_ABI_PROCNAME_LEN;
73 }
74
75 static
76 void procname_record(void *priv __attribute__((unused)),
77 struct lttng_ust_ring_buffer_ctx *ctx,
78 struct lttng_ust_channel_buffer *chan)
79 {
80 const char *procname;
81
82 procname = wrapper_getprocname();
83 chan->ops->event_write(ctx, procname, LTTNG_UST_ABI_PROCNAME_LEN, 1);
84 }
85
86 static
87 void procname_get_value(void *priv __attribute__((unused)),
88 struct lttng_ust_ctx_value *value)
89 {
90 value->u.str = wrapper_getprocname();
91 }
92
93 static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field(
94 lttng_ust_static_event_field("procname",
95 lttng_ust_static_type_array_text(LTTNG_UST_ABI_PROCNAME_LEN),
96 false, false),
97 procname_get_size,
98 procname_record,
99 procname_get_value,
100 NULL, NULL);
101
102 int lttng_add_procname_to_ctx(struct lttng_ust_ctx **ctx)
103 {
104 int ret;
105
106 if (lttng_find_context(*ctx, ctx_field->event_field->name)) {
107 ret = -EEXIST;
108 goto error_find_context;
109 }
110 ret = lttng_ust_context_append(ctx, ctx_field);
111 if (ret)
112 return ret;
113 return 0;
114
115 error_find_context:
116 return ret;
117 }
118
119 /*
120 * Force a read (imply TLS fixup for dlopen) of TLS variables.
121 */
122 void lttng_fixup_procname_tls(void)
123 {
124 asm volatile ("" : : "m" (URCU_TLS(cached_procname)[0]));
125 }
This page took 0.031516 seconds and 3 git commands to generate.