238ec10a5ccbf66077e65411f8e81cd32e02b8e3
[lttng-ust.git] / liblttng-ust / lttng-context-procname.c
1 /*
2 * lttng-context-procname.c
3 *
4 * LTTng UST procname context.
5 *
6 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #define _LGPL_SOURCE
24 #include <stddef.h>
25 #include <lttng/ust-events.h>
26 #include <lttng/ust-tracer.h>
27 #include <lttng/ringbuffer-config.h>
28 #include <urcu/tls-compat.h>
29 #include <assert.h>
30 #include "compat.h"
31
32 /* Maximum number of nesting levels for the procname cache. */
33 #define PROCNAME_NESTING_MAX 2
34
35 /*
36 * We cache the result to ensure we don't trigger a system call for
37 * each event.
38 * Upon exec, procname changes, but exec takes care of throwing away
39 * this cached version.
40 * The procname can also change by calling prctl(). The procname should
41 * be set for a thread before the first event is logged within this
42 * thread.
43 */
44 typedef char procname_array[PROCNAME_NESTING_MAX][17];
45
46 static DEFINE_URCU_TLS(procname_array, cached_procname);
47
48 static DEFINE_URCU_TLS(int, procname_nesting);
49
50 static inline
51 char *wrapper_getprocname(void)
52 {
53 int nesting = CMM_LOAD_SHARED(URCU_TLS(procname_nesting));
54
55 if (caa_unlikely(nesting >= PROCNAME_NESTING_MAX))
56 return "<unknown>";
57 if (caa_unlikely(!URCU_TLS(cached_procname)[nesting][0])) {
58 CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting + 1);
59 /* Increment nesting before updating cache. */
60 cmm_barrier();
61 lttng_pthread_getname_np(URCU_TLS(cached_procname)[nesting], LTTNG_UST_ABI_PROCNAME_LEN);
62 URCU_TLS(cached_procname)[nesting][LTTNG_UST_ABI_PROCNAME_LEN - 1] = '\0';
63 /* Decrement nesting after updating cache. */
64 cmm_barrier();
65 CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting);
66 }
67 return URCU_TLS(cached_procname)[nesting];
68 }
69
70 /* Reset should not be called from a signal handler. */
71 void lttng_context_procname_reset(void)
72 {
73 CMM_STORE_SHARED(URCU_TLS(cached_procname)[1][0], '\0');
74 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 1);
75 CMM_STORE_SHARED(URCU_TLS(cached_procname)[0][0], '\0');
76 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 0);
77 }
78
79 static
80 size_t procname_get_size(struct lttng_ctx_field *field, size_t offset)
81 {
82 return LTTNG_UST_ABI_PROCNAME_LEN;
83 }
84
85 static
86 void procname_record(struct lttng_ctx_field *field,
87 struct lttng_ust_lib_ring_buffer_ctx *ctx,
88 struct lttng_channel *chan)
89 {
90 char *procname;
91
92 procname = wrapper_getprocname();
93 chan->ops->event_write(ctx, procname, LTTNG_UST_ABI_PROCNAME_LEN);
94 }
95
96 static
97 void procname_get_value(struct lttng_ctx_field *field,
98 struct lttng_ctx_value *value)
99 {
100 value->u.str = wrapper_getprocname();
101 }
102
103 static const struct lttng_type procname_array_elem_type =
104 __type_integer(char, BYTE_ORDER, 10, UTF8);
105
106 int lttng_add_procname_to_ctx(struct lttng_ctx **ctx)
107 {
108 struct lttng_ctx_field *field;
109
110 field = lttng_append_context(ctx);
111 if (!field)
112 return -ENOMEM;
113 if (lttng_find_context(*ctx, "procname")) {
114 lttng_remove_context_field(ctx, field);
115 return -EEXIST;
116 }
117 field->event_field.name = "procname";
118 field->event_field.type.atype = atype_array_nestable;
119 field->event_field.type.u.array_nestable.elem_type =
120 &procname_array_elem_type;
121 field->event_field.type.u.array_nestable.length = LTTNG_UST_ABI_PROCNAME_LEN;
122 field->get_size = procname_get_size;
123 field->record = procname_record;
124 field->get_value = procname_get_value;
125 lttng_context_update(*ctx);
126 return 0;
127 }
128
129 /*
130 * Force a read (imply TLS fixup for dlopen) of TLS variables.
131 */
132 void lttng_fixup_procname_tls(void)
133 {
134 asm volatile ("" : : "m" (URCU_TLS(cached_procname)[0]));
135 }
This page took 0.030913 seconds and 3 git commands to generate.