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