Cleanup: apply `include-what-you-use` guideline for `size_t`
[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
3fbec7dc 23#define _LGPL_SOURCE
b4051ad8 24#include <stddef.h>
4318ae1b
MD
25#include <lttng/ust-events.h>
26#include <lttng/ust-tracer.h>
27#include <lttng/ringbuffer-config.h>
8c90a710 28#include <urcu/tls-compat.h>
4847e9bb 29#include <assert.h>
08114193 30#include "compat.h"
4847e9bb 31
74011e88
MD
32/* Maximum number of nesting levels for the procname cache. */
33#define PROCNAME_NESTING_MAX 2
34
4847e9bb
MD
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.
009745db
MD
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.
4847e9bb 43 */
74011e88
MD
44typedef char procname_array[PROCNAME_NESTING_MAX][17];
45
16adecf1 46static DEFINE_URCU_TLS(procname_array, cached_procname);
4847e9bb 47
74011e88
MD
48static DEFINE_URCU_TLS(int, procname_nesting);
49
4847e9bb
MD
50static inline
51char *wrapper_getprocname(void)
52{
2c44512a 53 int nesting = CMM_LOAD_SHARED(URCU_TLS(procname_nesting));
74011e88
MD
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);
2c44512a
MD
59 /* Increment nesting before updating cache. */
60 cmm_barrier();
74011e88
MD
61 lttng_ust_getprocname(URCU_TLS(cached_procname)[nesting]);
62 URCU_TLS(cached_procname)[nesting][LTTNG_UST_PROCNAME_LEN - 1] = '\0';
2c44512a
MD
63 /* Decrement nesting after updating cache. */
64 cmm_barrier();
74011e88 65 CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting);
4847e9bb 66 }
74011e88 67 return URCU_TLS(cached_procname)[nesting];
4847e9bb
MD
68}
69
2c44512a 70/* Reset should not be called from a signal handler. */
4847e9bb
MD
71void lttng_context_procname_reset(void)
72{
2c44512a 73 CMM_STORE_SHARED(URCU_TLS(cached_procname)[1][0], '\0');
74011e88 74 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 1);
2c44512a 75 CMM_STORE_SHARED(URCU_TLS(cached_procname)[0][0], '\0');
74011e88 76 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 0);
4847e9bb
MD
77}
78
79static
53569322 80size_t procname_get_size(struct lttng_ctx_field *field, size_t offset)
4847e9bb 81{
6e9ac4ae 82 return LTTNG_UST_PROCNAME_LEN;
4847e9bb
MD
83}
84
85static
86void procname_record(struct lttng_ctx_field *field,
4cfec15c 87 struct lttng_ust_lib_ring_buffer_ctx *ctx,
7dd08bec 88 struct lttng_channel *chan)
4847e9bb
MD
89{
90 char *procname;
91
92 procname = wrapper_getprocname();
08114193 93 chan->ops->event_write(ctx, procname, LTTNG_UST_PROCNAME_LEN);
4847e9bb
MD
94}
95
77aa5901
MD
96static
97void procname_get_value(struct lttng_ctx_field *field,
53569322 98 struct lttng_ctx_value *value)
77aa5901 99{
6e9ac4ae 100 value->u.str = wrapper_getprocname();
77aa5901
MD
101}
102
4847e9bb
MD
103int lttng_add_procname_to_ctx(struct lttng_ctx **ctx)
104{
105 struct lttng_ctx_field *field;
106
107 field = lttng_append_context(ctx);
108 if (!field)
109 return -ENOMEM;
110 if (lttng_find_context(*ctx, "procname")) {
111 lttng_remove_context_field(ctx, field);
112 return -EEXIST;
113 }
114 field->event_field.name = "procname";
115 field->event_field.type.atype = atype_array;
116 field->event_field.type.u.array.elem_type.atype = atype_integer;
117 field->event_field.type.u.array.elem_type.u.basic.integer.size = sizeof(char) * CHAR_BIT;
118 field->event_field.type.u.array.elem_type.u.basic.integer.alignment = lttng_alignof(char) * CHAR_BIT;
119 field->event_field.type.u.array.elem_type.u.basic.integer.signedness = lttng_is_signed_type(char);
120 field->event_field.type.u.array.elem_type.u.basic.integer.reverse_byte_order = 0;
121 field->event_field.type.u.array.elem_type.u.basic.integer.base = 10;
122 field->event_field.type.u.array.elem_type.u.basic.integer.encoding = lttng_encode_UTF8;
08114193 123 field->event_field.type.u.array.length = LTTNG_UST_PROCNAME_LEN;
4847e9bb
MD
124 field->get_size = procname_get_size;
125 field->record = procname_record;
77aa5901 126 field->get_value = procname_get_value;
b2cc986a 127 lttng_context_update(*ctx);
4847e9bb
MD
128 return 0;
129}
009745db
MD
130
131/*
132 * Force a read (imply TLS fixup for dlopen) of TLS variables.
133 */
134void lttng_fixup_procname_tls(void)
135{
8c90a710 136 asm volatile ("" : : "m" (URCU_TLS(cached_procname)[0]));
009745db 137}
This page took 0.040239 seconds and 4 git commands to generate.