Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust / lttng-context-procname.c
CommitLineData
4847e9bb 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
4847e9bb 3 *
009745db 4 * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
e92f3e28 5 *
c0c0989a 6 * LTTng UST procname context.
4847e9bb
MD
7 */
8
3fbec7dc 9#define _LGPL_SOURCE
b4051ad8 10#include <stddef.h>
4318ae1b
MD
11#include <lttng/ust-events.h>
12#include <lttng/ust-tracer.h>
13#include <lttng/ringbuffer-config.h>
8c90a710 14#include <urcu/tls-compat.h>
4847e9bb 15#include <assert.h>
08114193 16#include "compat.h"
4847e9bb 17
74011e88
MD
18/* Maximum number of nesting levels for the procname cache. */
19#define PROCNAME_NESTING_MAX 2
20
4847e9bb
MD
21/*
22 * We cache the result to ensure we don't trigger a system call for
23 * each event.
24 * Upon exec, procname changes, but exec takes care of throwing away
25 * this cached version.
009745db
MD
26 * The procname can also change by calling prctl(). The procname should
27 * be set for a thread before the first event is logged within this
28 * thread.
4847e9bb 29 */
74011e88
MD
30typedef char procname_array[PROCNAME_NESTING_MAX][17];
31
16adecf1 32static DEFINE_URCU_TLS(procname_array, cached_procname);
4847e9bb 33
74011e88
MD
34static DEFINE_URCU_TLS(int, procname_nesting);
35
4847e9bb
MD
36static inline
37char *wrapper_getprocname(void)
38{
2c44512a 39 int nesting = CMM_LOAD_SHARED(URCU_TLS(procname_nesting));
74011e88
MD
40
41 if (caa_unlikely(nesting >= PROCNAME_NESTING_MAX))
42 return "<unknown>";
43 if (caa_unlikely(!URCU_TLS(cached_procname)[nesting][0])) {
44 CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting + 1);
2c44512a
MD
45 /* Increment nesting before updating cache. */
46 cmm_barrier();
0db3d6ee
MJ
47 lttng_pthread_getname_np(URCU_TLS(cached_procname)[nesting], LTTNG_UST_ABI_PROCNAME_LEN);
48 URCU_TLS(cached_procname)[nesting][LTTNG_UST_ABI_PROCNAME_LEN - 1] = '\0';
2c44512a
MD
49 /* Decrement nesting after updating cache. */
50 cmm_barrier();
74011e88 51 CMM_STORE_SHARED(URCU_TLS(procname_nesting), nesting);
4847e9bb 52 }
74011e88 53 return URCU_TLS(cached_procname)[nesting];
4847e9bb
MD
54}
55
2c44512a 56/* Reset should not be called from a signal handler. */
4847e9bb
MD
57void lttng_context_procname_reset(void)
58{
2c44512a 59 CMM_STORE_SHARED(URCU_TLS(cached_procname)[1][0], '\0');
74011e88 60 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 1);
2c44512a 61 CMM_STORE_SHARED(URCU_TLS(cached_procname)[0][0], '\0');
74011e88 62 CMM_STORE_SHARED(URCU_TLS(procname_nesting), 0);
4847e9bb
MD
63}
64
65static
53569322 66size_t procname_get_size(struct lttng_ctx_field *field, size_t offset)
4847e9bb 67{
0db3d6ee 68 return LTTNG_UST_ABI_PROCNAME_LEN;
4847e9bb
MD
69}
70
71static
72void procname_record(struct lttng_ctx_field *field,
4cfec15c 73 struct lttng_ust_lib_ring_buffer_ctx *ctx,
7dd08bec 74 struct lttng_channel *chan)
4847e9bb
MD
75{
76 char *procname;
77
78 procname = wrapper_getprocname();
0db3d6ee 79 chan->ops->event_write(ctx, procname, LTTNG_UST_ABI_PROCNAME_LEN);
4847e9bb
MD
80}
81
77aa5901
MD
82static
83void procname_get_value(struct lttng_ctx_field *field,
53569322 84 struct lttng_ctx_value *value)
77aa5901 85{
6e9ac4ae 86 value->u.str = wrapper_getprocname();
77aa5901
MD
87}
88
218deb69
MD
89static const struct lttng_type procname_array_elem_type =
90 __type_integer(char, BYTE_ORDER, 10, UTF8);
91
4847e9bb
MD
92int lttng_add_procname_to_ctx(struct lttng_ctx **ctx)
93{
94 struct lttng_ctx_field *field;
95
96 field = lttng_append_context(ctx);
97 if (!field)
98 return -ENOMEM;
99 if (lttng_find_context(*ctx, "procname")) {
100 lttng_remove_context_field(ctx, field);
101 return -EEXIST;
102 }
103 field->event_field.name = "procname";
218deb69
MD
104 field->event_field.type.atype = atype_array_nestable;
105 field->event_field.type.u.array_nestable.elem_type =
106 &procname_array_elem_type;
0db3d6ee 107 field->event_field.type.u.array_nestable.length = LTTNG_UST_ABI_PROCNAME_LEN;
4847e9bb
MD
108 field->get_size = procname_get_size;
109 field->record = procname_record;
77aa5901 110 field->get_value = procname_get_value;
b2cc986a 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.036426 seconds and 4 git commands to generate.