Commit | Line | Data |
---|---|---|
4847e9bb | 1 | /* |
e92f3e28 | 2 | * lttng-context-procname.c |
4847e9bb MD |
3 | * |
4 | * LTTng UST procname context. | |
5 | * | |
009745db | 6 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
e92f3e28 MD |
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 | |
4847e9bb MD |
21 | */ |
22 | ||
3fbec7dc | 23 | #define _LGPL_SOURCE |
4318ae1b MD |
24 | #include <lttng/ust-events.h> |
25 | #include <lttng/ust-tracer.h> | |
26 | #include <lttng/ringbuffer-config.h> | |
8c90a710 | 27 | #include <urcu/tls-compat.h> |
4847e9bb | 28 | #include <assert.h> |
08114193 | 29 | #include "compat.h" |
4847e9bb MD |
30 | |
31 | /* | |
32 | * We cache the result to ensure we don't trigger a system call for | |
33 | * each event. | |
34 | * Upon exec, procname changes, but exec takes care of throwing away | |
35 | * this cached version. | |
009745db MD |
36 | * The procname can also change by calling prctl(). The procname should |
37 | * be set for a thread before the first event is logged within this | |
38 | * thread. | |
4847e9bb | 39 | */ |
8c90a710 MD |
40 | typedef char procname_array[17]; |
41 | static DEFINE_URCU_TLS(procname_array, cached_procname); | |
4847e9bb MD |
42 | |
43 | static inline | |
44 | char *wrapper_getprocname(void) | |
45 | { | |
8c90a710 MD |
46 | if (caa_unlikely(!URCU_TLS(cached_procname)[0])) { |
47 | lttng_ust_getprocname(URCU_TLS(cached_procname)); | |
48 | URCU_TLS(cached_procname)[LTTNG_UST_PROCNAME_LEN - 1] = '\0'; | |
4847e9bb | 49 | } |
8c90a710 | 50 | return URCU_TLS(cached_procname); |
4847e9bb MD |
51 | } |
52 | ||
53 | void lttng_context_procname_reset(void) | |
54 | { | |
8c90a710 | 55 | URCU_TLS(cached_procname)[0] = '\0'; |
4847e9bb MD |
56 | } |
57 | ||
58 | static | |
53569322 | 59 | size_t procname_get_size(struct lttng_ctx_field *field, size_t offset) |
4847e9bb MD |
60 | { |
61 | size_t size = 0; | |
62 | ||
08114193 | 63 | size += LTTNG_UST_PROCNAME_LEN; |
4847e9bb MD |
64 | return size; |
65 | } | |
66 | ||
67 | static | |
68 | void procname_record(struct lttng_ctx_field *field, | |
4cfec15c | 69 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
7dd08bec | 70 | struct lttng_channel *chan) |
4847e9bb MD |
71 | { |
72 | char *procname; | |
73 | ||
74 | procname = wrapper_getprocname(); | |
08114193 | 75 | chan->ops->event_write(ctx, procname, LTTNG_UST_PROCNAME_LEN); |
4847e9bb MD |
76 | } |
77 | ||
77aa5901 MD |
78 | static |
79 | void procname_get_value(struct lttng_ctx_field *field, | |
53569322 | 80 | struct lttng_ctx_value *value) |
77aa5901 MD |
81 | { |
82 | char *procname; | |
83 | ||
84 | procname = wrapper_getprocname(); | |
53569322 | 85 | value->u.str = procname; |
77aa5901 MD |
86 | } |
87 | ||
4847e9bb MD |
88 | int lttng_add_procname_to_ctx(struct lttng_ctx **ctx) |
89 | { | |
90 | struct lttng_ctx_field *field; | |
91 | ||
92 | field = lttng_append_context(ctx); | |
93 | if (!field) | |
94 | return -ENOMEM; | |
95 | if (lttng_find_context(*ctx, "procname")) { | |
96 | lttng_remove_context_field(ctx, field); | |
97 | return -EEXIST; | |
98 | } | |
99 | field->event_field.name = "procname"; | |
100 | field->event_field.type.atype = atype_array; | |
101 | field->event_field.type.u.array.elem_type.atype = atype_integer; | |
102 | field->event_field.type.u.array.elem_type.u.basic.integer.size = sizeof(char) * CHAR_BIT; | |
103 | field->event_field.type.u.array.elem_type.u.basic.integer.alignment = lttng_alignof(char) * CHAR_BIT; | |
104 | field->event_field.type.u.array.elem_type.u.basic.integer.signedness = lttng_is_signed_type(char); | |
105 | field->event_field.type.u.array.elem_type.u.basic.integer.reverse_byte_order = 0; | |
106 | field->event_field.type.u.array.elem_type.u.basic.integer.base = 10; | |
107 | field->event_field.type.u.array.elem_type.u.basic.integer.encoding = lttng_encode_UTF8; | |
08114193 | 108 | field->event_field.type.u.array.length = LTTNG_UST_PROCNAME_LEN; |
4847e9bb MD |
109 | field->get_size = procname_get_size; |
110 | field->record = procname_record; | |
77aa5901 | 111 | field->get_value = procname_get_value; |
b2cc986a | 112 | lttng_context_update(*ctx); |
4847e9bb MD |
113 | return 0; |
114 | } | |
009745db MD |
115 | |
116 | /* | |
117 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
118 | */ | |
119 | void lttng_fixup_procname_tls(void) | |
120 | { | |
8c90a710 | 121 | asm volatile ("" : : "m" (URCU_TLS(cached_procname)[0])); |
009745db | 122 | } |