Refactoring: context structures
[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 #include "context-internal.h"
19
20 /* Maximum number of nesting levels for the procname cache. */
21 #define PROCNAME_NESTING_MAX 2
22
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.
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.
31 */
32 typedef char procname_array[PROCNAME_NESTING_MAX][17];
33
34 static DEFINE_URCU_TLS(procname_array, cached_procname);
35
36 static DEFINE_URCU_TLS(int, procname_nesting);
37
38 static inline
39 char *wrapper_getprocname(void)
40 {
41 int nesting = CMM_LOAD_SHARED(URCU_TLS(procname_nesting));
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);
47 /* Increment nesting before updating cache. */
48 cmm_barrier();
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';
51 /* Decrement nesting after updating cache. */
52 cmm_barrier();
53 CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting);
54 }
55 return URCU_TLS(cached_procname)[nesting];
56 }
57
58 /* Reset should not be called from a signal handler. */
59 void lttng_ust_context_procname_reset(void)
60 {
61 CMM_STORE_SHARED(URCU_TLS(cached_procname)[1][0], '\0');
62 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 1);
63 CMM_STORE_SHARED(URCU_TLS(cached_procname)[0][0], '\0');
64 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 0);
65 }
66
67 static
68 size_t procname_get_size(struct lttng_ust_ctx_field *field, size_t offset)
69 {
70 return LTTNG_UST_ABI_PROCNAME_LEN;
71 }
72
73 static
74 void procname_record(struct lttng_ust_ctx_field *field,
75 struct lttng_ust_lib_ring_buffer_ctx *ctx,
76 struct lttng_channel *chan)
77 {
78 char *procname;
79
80 procname = wrapper_getprocname();
81 chan->ops->event_write(ctx, procname, LTTNG_UST_ABI_PROCNAME_LEN);
82 }
83
84 static
85 void procname_get_value(struct lttng_ust_ctx_field *field,
86 struct lttng_ust_ctx_value *value)
87 {
88 value->u.str = wrapper_getprocname();
89 }
90
91 static const struct lttng_type procname_array_elem_type =
92 __type_integer(char, BYTE_ORDER, 10, UTF8);
93
94 int lttng_add_procname_to_ctx(struct lttng_ust_ctx **ctx)
95 {
96 struct lttng_ust_ctx_field *field;
97
98 field = lttng_append_context(ctx);
99 if (!field)
100 return -ENOMEM;
101 if (lttng_find_context(*ctx, "procname")) {
102 lttng_remove_context_field(ctx, field);
103 return -EEXIST;
104 }
105 field->event_field->name = "procname";
106 field->event_field->type.atype = atype_array_nestable;
107 field->event_field->type.u.array_nestable.elem_type =
108 &procname_array_elem_type;
109 field->event_field->type.u.array_nestable.length = LTTNG_UST_ABI_PROCNAME_LEN;
110 field->get_size = procname_get_size;
111 field->record = procname_record;
112 field->get_value = procname_get_value;
113 lttng_context_update(*ctx);
114 return 0;
115 }
116
117 /*
118 * Force a read (imply TLS fixup for dlopen) of TLS variables.
119 */
120 void lttng_fixup_procname_tls(void)
121 {
122 asm volatile ("" : : "m" (URCU_TLS(cached_procname)[0]));
123 }
This page took 0.032889 seconds and 5 git commands to generate.