8c1520ad51f221b467dd38815cd66868a75b137d
[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 <lttng/ust-events.h>
25 #include <lttng/ust-tracer.h>
26 #include <lttng/ringbuffer-config.h>
27 #include <urcu/tls-compat.h>
28 #include <assert.h>
29 #include "compat.h"
30
31 /* Maximum number of nesting levels for the procname cache. */
32 #define PROCNAME_NESTING_MAX 2
33
34 /*
35 * We cache the result to ensure we don't trigger a system call for
36 * each event.
37 * Upon exec, procname changes, but exec takes care of throwing away
38 * this cached version.
39 * The procname can also change by calling prctl(). The procname should
40 * be set for a thread before the first event is logged within this
41 * thread.
42 */
43 typedef char procname_array[PROCNAME_NESTING_MAX][17];
44
45 static DEFINE_URCU_TLS(procname_array, cached_procname);
46
47 static DEFINE_URCU_TLS(int, procname_nesting);
48
49 static inline
50 char *wrapper_getprocname(void)
51 {
52 int nesting = CMM_LOAD_SHARED(URCU_TLS(procname_nesting));
53
54 if (caa_unlikely(nesting >= PROCNAME_NESTING_MAX))
55 return "<unknown>";
56 if (caa_unlikely(!URCU_TLS(cached_procname)[nesting][0])) {
57 CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting + 1);
58 /* Increment nesting before updating cache. */
59 cmm_barrier();
60 lttng_ust_getprocname(URCU_TLS(cached_procname)[nesting]);
61 URCU_TLS(cached_procname)[nesting][LTTNG_UST_PROCNAME_LEN - 1] = '\0';
62 /* Decrement nesting after updating cache. */
63 cmm_barrier();
64 CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting);
65 }
66 return URCU_TLS(cached_procname)[nesting];
67 }
68
69 /* Reset should not be called from a signal handler. */
70 void lttng_context_procname_reset(void)
71 {
72 CMM_STORE_SHARED(URCU_TLS(cached_procname)[1][0], '\0');
73 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 1);
74 CMM_STORE_SHARED(URCU_TLS(cached_procname)[0][0], '\0');
75 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 0);
76 }
77
78 static
79 size_t procname_get_size(struct lttng_ctx_field *field, size_t offset)
80 {
81 size_t size = 0;
82
83 size += LTTNG_UST_PROCNAME_LEN;
84 return size;
85 }
86
87 static
88 void procname_record(struct lttng_ctx_field *field,
89 struct lttng_ust_lib_ring_buffer_ctx *ctx,
90 struct lttng_channel *chan)
91 {
92 char *procname;
93
94 procname = wrapper_getprocname();
95 chan->ops->event_write(ctx, procname, LTTNG_UST_PROCNAME_LEN);
96 }
97
98 static
99 void procname_get_value(struct lttng_ctx_field *field,
100 struct lttng_ctx_value *value)
101 {
102 char *procname;
103
104 procname = wrapper_getprocname();
105 value->u.str = procname;
106 }
107
108 int lttng_add_procname_to_ctx(struct lttng_ctx **ctx)
109 {
110 struct lttng_ctx_field *field;
111
112 field = lttng_append_context(ctx);
113 if (!field)
114 return -ENOMEM;
115 if (lttng_find_context(*ctx, "procname")) {
116 lttng_remove_context_field(ctx, field);
117 return -EEXIST;
118 }
119 field->event_field.name = "procname";
120 field->event_field.type.atype = atype_array;
121 field->event_field.type.u.array.elem_type.atype = atype_integer;
122 field->event_field.type.u.array.elem_type.u.basic.integer.size = sizeof(char) * CHAR_BIT;
123 field->event_field.type.u.array.elem_type.u.basic.integer.alignment = lttng_alignof(char) * CHAR_BIT;
124 field->event_field.type.u.array.elem_type.u.basic.integer.signedness = lttng_is_signed_type(char);
125 field->event_field.type.u.array.elem_type.u.basic.integer.reverse_byte_order = 0;
126 field->event_field.type.u.array.elem_type.u.basic.integer.base = 10;
127 field->event_field.type.u.array.elem_type.u.basic.integer.encoding = lttng_encode_UTF8;
128 field->event_field.type.u.array.length = LTTNG_UST_PROCNAME_LEN;
129 field->get_size = procname_get_size;
130 field->record = procname_record;
131 field->get_value = procname_get_value;
132 lttng_context_update(*ctx);
133 return 0;
134 }
135
136 /*
137 * Force a read (imply TLS fixup for dlopen) of TLS variables.
138 */
139 void lttng_fixup_procname_tls(void)
140 {
141 asm volatile ("" : : "m" (URCU_TLS(cached_procname)[0]));
142 }
This page took 0.032264 seconds and 3 git commands to generate.