Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-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/ringbuffer-config.h>
14 #include <urcu/tls-compat.h>
15 #include <assert.h>
16 #include "compat.h"
17
18 /* Maximum number of nesting levels for the procname cache. */
19 #define PROCNAME_NESTING_MAX 2
20
21 /*
22 * We cache the result to ensure we don't trigger a system call for
23 * each event.
24 * Upon exec, procname changes, but exec takes care of throwing away
25 * this cached version.
26 * The procname can also change by calling prctl(). The procname should
27 * be set for a thread before the first event is logged within this
28 * thread.
29 */
30 typedef char procname_array[PROCNAME_NESTING_MAX][17];
31
32 static DEFINE_URCU_TLS(procname_array, cached_procname);
33
34 static DEFINE_URCU_TLS(int, procname_nesting);
35
36 static inline
37 char *wrapper_getprocname(void)
38 {
39 int nesting = CMM_LOAD_SHARED(URCU_TLS(procname_nesting));
40
41 if (caa_unlikely(nesting >= PROCNAME_NESTING_MAX))
42 return "<unknown>";
43 if (caa_unlikely(!URCU_TLS(cached_procname)[nesting][0])) {
44 CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting + 1);
45 /* Increment nesting before updating cache. */
46 cmm_barrier();
47 lttng_pthread_getname_np(URCU_TLS(cached_procname)[nesting], LTTNG_UST_ABI_PROCNAME_LEN);
48 URCU_TLS(cached_procname)[nesting][LTTNG_UST_ABI_PROCNAME_LEN - 1] = '\0';
49 /* Decrement nesting after updating cache. */
50 cmm_barrier();
51 CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting);
52 }
53 return URCU_TLS(cached_procname)[nesting];
54 }
55
56 /* Reset should not be called from a signal handler. */
57 void lttng_context_procname_reset(void)
58 {
59 CMM_STORE_SHARED(URCU_TLS(cached_procname)[1][0], '\0');
60 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 1);
61 CMM_STORE_SHARED(URCU_TLS(cached_procname)[0][0], '\0');
62 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 0);
63 }
64
65 static
66 size_t procname_get_size(struct lttng_ctx_field *field, size_t offset)
67 {
68 return LTTNG_UST_ABI_PROCNAME_LEN;
69 }
70
71 static
72 void procname_record(struct lttng_ctx_field *field,
73 struct lttng_ust_lib_ring_buffer_ctx *ctx,
74 struct lttng_channel *chan)
75 {
76 char *procname;
77
78 procname = wrapper_getprocname();
79 chan->ops->event_write(ctx, procname, LTTNG_UST_ABI_PROCNAME_LEN);
80 }
81
82 static
83 void procname_get_value(struct lttng_ctx_field *field,
84 struct lttng_ctx_value *value)
85 {
86 value->u.str = wrapper_getprocname();
87 }
88
89 static const struct lttng_type procname_array_elem_type =
90 __type_integer(char, BYTE_ORDER, 10, UTF8);
91
92 int lttng_add_procname_to_ctx(struct lttng_ctx **ctx)
93 {
94 struct lttng_ctx_field *field;
95
96 field = lttng_append_context(ctx);
97 if (!field)
98 return -ENOMEM;
99 if (lttng_find_context(*ctx, "procname")) {
100 lttng_remove_context_field(ctx, field);
101 return -EEXIST;
102 }
103 field->event_field.name = "procname";
104 field->event_field.type.atype = atype_array_nestable;
105 field->event_field.type.u.array_nestable.elem_type =
106 &procname_array_elem_type;
107 field->event_field.type.u.array_nestable.length = LTTNG_UST_ABI_PROCNAME_LEN;
108 field->get_size = procname_get_size;
109 field->record = procname_record;
110 field->get_value = procname_get_value;
111 lttng_context_update(*ctx);
112 return 0;
113 }
114
115 /*
116 * Force a read (imply TLS fixup for dlopen) of TLS variables.
117 */
118 void lttng_fixup_procname_tls(void)
119 {
120 asm volatile ("" : : "m" (URCU_TLS(cached_procname)[0]));
121 }
This page took 0.031282 seconds and 4 git commands to generate.